I need a faster way to do this....

This is a discussion on I need a faster way to do this.... within the C++ Programming forums, part of the General Programming Boards category; Take a look at this extremely sloppy code and try to improve it plz. It draws a map from an ...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    I need a faster way to do this....

    Take a look at this extremely sloppy code and try to improve it plz. It draws a map from an array. The theme sets different tilesets, becuase the whole thing screws with a two digit number, so I have to use 0 through 9 and -1 through -9, but don't worry about that, I just don't want it to draw so slow. It's not bad until you try to juggle 3 map layers, and even one layer draws slowly, and you can see it drawing from left to right, I'm on a 500 MHz AMD K6-2 for crying out loud, and this is a 13h program! It's not the drawing functions, it's this sloppy map drawing function I made in like ten minutes.

    Code:
    int DrawMapLayer1(int mapnumber)
    {
    switch (mapnumber)
     {
     case 1:
     switch (mapone[0][0])
     {
     case 1:
     theme=1;
     break;
     case 2:
     theme=2;
     break;
     }
       maponeloop:
       if (down!=21)
       if (right!=12)
       switch (theme)
       {
       case 1:
       switch (mapone[right][down])
        {
         case 1:
         Draw_T_grass(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 0:
         Draw_T_hole(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 2:
         Draw_T_wall(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 3:
         Draw_T_path(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 4:
         Draw_T_woodfloor(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 5:
         Draw_T_water(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         maperrorlevel=0;
         goto maponeloop;
         break;
         case 6:
         Draw_T_door(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
        }
        break;
        case 2:
        switch (mapone[right][down])
        {
         case 0:
         Draw_T_hole(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 1:
         Draw_T_castlefloor(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 2:
         Draw_T_castlewall(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 3:
         Draw_T_door(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 4:
         Draw_T_castlefloor(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 5:
         Draw_T_castledoorwest(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 6:
         Draw_T_castledooreast(drawpositiondown,drawpositionright,'n');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 8:
         Draw_T_castlefloor(drawpositiondown,drawpositionright,'y');
         Draw_T_candleleft(drawpositiondown,drawpositionright,'y');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         case 7:
         Draw_T_castlefloor(drawpositiondown,drawpositionright,'y');
         Draw_T_candleright(drawpositiondown,drawpositionright,'y');
         right++;
         drawpositionright=drawpositionright+16;
         goto maponeloop;
         break;
         
        }
    
        }
        }
        if (down!=21)
        if (right==12)
        {
         right=1;
         down++;
         drawpositiondown=drawpositiondown+16;
         drawpositionright=1;
         goto maponeloop;
        }
        right=1;   
        down=1;
        drawpositionright=1;
        drawpositiondown=1;
    
    
       }

  2. #2
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    also BTW, it looks screwed up because it is indented from the monospace thing, and it goes to the next line too fast, it isn't supposed to look like that

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    Instead of:

    mapone[x][y];
    maptwo[x][y];
    ...etc

    You could use:

    map[number][x][y];


    And what screws up with two digit numbers? What's the data type? Even a char can store up to 255 values (three digits).

    Advice: Don't use goto, try to make your loops with for/while instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this. I moved the switch() statement for theme, and added += for the addition.
    Code:
    int DrawMapLayer1(int mapnumber)
    {
    switch (mapnumber)
    {
    case 1:
       switch (mapone[0][0])
       {
       case 1:
          theme=1;
          break;
       case 2:
          theme=2;
          break;
       }
       switch (theme)
       {
       case 1:
          maponelooptheme1:
          if (down!=21)
          if (right!=12)
                switch (mapone[right][down])
                {
                case 1:
                   Draw_T_grass(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme1;
                   break;
                case 0:
                   Draw_T_hole(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme1;
                   break;
                case 2:
                   Draw_T_wall(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme1;
                   break;
                case 3:
                   Draw_T_path(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme1;
                   break;
                case 4:
                   Draw_T_woodfloor(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme1;
                   break;
                case 5:
                   Draw_T_water(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   maperrorlevel=0;
                   goto maponelooptheme1;
                   break;
                case 6:
                   Draw_T_door(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme1;
                   break;
                }
          if (down!=21)
          if (right==12)
          {
             right=1;
             down++;
             drawpositiondown +=16;
             drawpositionright=1;
             goto maponelooptheme1;
          }
    
       case 2:
          maponelooptheme2:
          if (down!=21)
          if (right!=12)
                switch (mapone[right][down])
                {
                case 0:
                   Draw_T_hole(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 1:
                   Draw_T_castlefloor(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 2:
                   Draw_T_castlewall(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 3:
                   Draw_T_door(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 4:
                   Draw_T_castlefloor(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 5:
                   Draw_T_castledoorwest(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 6:
                   Draw_T_castledooreast(drawpositiondown,drawpositionright,'n');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 8:
                   Draw_T_castlefloor(drawpositiondown,drawpositionright,'y');
                   Draw_T_candleleft(drawpositiondown,drawpositionright,'y');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
                case 7:
                   Draw_T_castlefloor(drawpositiondown,drawpositionright,'y');
                   Draw_T_candleright(drawpositiondown,drawpositionright,'y');
                   right++;
                   drawpositionright +=16;
                   goto maponelooptheme2;
                   break;
         
                }
          if (down!=21)
          if (right==12)
          {
             right=1;
             down++;
             drawpositiondown +=16;
             drawpositionright=1;
             goto maponelooptheme2;
          }
       }
    }
    right=1;
    down=1;
    drawpositionright=1;
    drawpositiondown=1;
    }

  5. #5
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    I'd recommend scrapping the whole thing...

    save the tiles as prerendered images in memory and have a unified drawTile() function. keep the tiles in an array and specify which tile to draw by its index in the array.

    So instead of a length switch ... case bitty, have something simpler like

    drawTile(mapone[right][down], drawpositiondown, drawpositionright);
    .sect signature

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 02:56 PM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Which Operation is Faster "=" or "+=" ?
    By thetinman in forum C++ Programming
    Replies: 37
    Last Post: 06-06-2007, 08:29 PM
  4. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 10:03 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21