# Thread: I need a faster way to do this....

1. ## 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. 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

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).

4. 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. 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);