[SDL] slow down the code?

Started by
6 comments, last by JTippetts 12 years, 8 months ago
hey guys, well i guess that my program is updating my images too fast because you can watch them overlap each other, im trying to draw tiles on a stationary background and all i see is the background flash on top of the tiles then tie tiles flash on the background but neither ever stop flashing, ne ideas?:

UPDATE - this is what i get if i dont blip the background every loop, you can see the the tiles, but they are popping up completely in the wrong order and they dont match the file at all(which could happen if its putting them on the screen wrong) excuse the 5 second programmer graphics lol


fail.jpg
Advertisement
You need to ensure all your game visuals are drawn in a consistent order frame to frame. I assume you're only calling SDL_Flip() once per frame too?

You need to ensure all your game visuals are drawn in a consistent order frame to frame. I assume you're only calling SDL_Flip() once per frame too?






correct
Post your drawing code. We can tell very little without seeing what you are doing.
background flashes and theres a bug somewhere in the for loop or the input because its not putting the bricks where they are suppose to go the txt file looks like this
map01.dat
010101010101
010101010101

15x20

then the code like this,

void gameEvents::gameUpdate()
{
Map map;
if(menu)
{
player = SDL_LoadBMP("Marrow.bmp");
}

if(newMap)
{
background = SDL_LoadBMP("data/maps/map01.bmp");//adds a basic background
player = SDL_LoadBMP("artemis.bmp");// change from menu arrow to artemis, the players character
item = SDL_LoadBMP("brick.bmp");//layer for background
ifstream ifs ( "data/maps/map01.dat" , ifstream::in );

while (ifs.good())
{

map.level[levelY][levelX] << ifs.get();
levelX += 1;
if(levelX >= 20)
{

levelX = 1;
levelY += 1;
}
}
ifs.close();
newMap = false;
}
SDL_BlitSurface( background, NULL, screen, NULL );
for (levelY=0;levelY<15;levelY++)
{
for (levelX=0;levelX<20;levelX++)
{
if (map.level[levelY][levelX] == 1)
{
gameDraw(srcmapX,srcmapY,mapX,mapY,playerWidth,playerHeight,item,screen); //i know item is the same in both places, just using that to test
mapX += 64;
}
if(map.level[levelY][levelX] == 0)
{
gameDraw(srcmapX,srcmapY,mapX,mapY,playerWidth,playerHeight,item,screen); //i know item is the same in both places, just using that to test
mapX += 64;
}
}
if(levelX >= 20)
{
levelX = 0;
levelY += 1;
mapY += 64;
}
}

SDL_SetColorKey(player,SDL_SRCCOLORKEY,SDL_MapRGB(player->format, 200,0,200));
gameDraw(srcX,srcY,desX,desY,playerWidth,playerHeight,player,screen);
SDL_Flip(screen);
gameRunning = true;
}
Load your images at startup. Check for errors. Setting the colour key should be included in this.

Load your map level into an array at startup. Ensure that your loading is correct (e.g. output the values to the console) before trying to debug your draw routine. For example, you're setting "levelX" to 1, not zero, between each row of tiles. The values of levelX and levelY should probably be local to this function, right now I don't know if you can guarantee they are set correctly.

I'm suspicious of your use of "mapX" and "mapY" inside your drawing loop.

Load your images at startup. Check for errors. Setting the colour key should be included in this.

Load your map level into an array at startup. Ensure that your loading is correct (e.g. output the values to the console) before trying to debug your draw routine. For example, you're setting "levelX" to 1, not zero, between each row of tiles. The values of levelX and levelY should probably be local to this function, right now I don't know if you can guarantee they are set correctly.

I'm suspicious of your use of "mapX" and "mapY" inside your drawing loop.

void gameEvents::gameDraw(int srcX, int srcY, int desX,int desY,int width, int height, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect src;
src.x = srcX;
src.y = srcY;
src.w = width;
src.h = height;

SDL_Rect des;
des.x = desX;
des.y = desY;
des.w = width;
des.h = height;

SDL_BlitSurface(source, &src, destination, &des);
I assume that newMap is a global that you set to ensure the sprites and map are loaded only once (when Update is first called), correct? If this is the case, it's very bad form and serves only to hide the initialization in a place that is non-intuitive for it to be (Update implies an update, not a map and graphic load). Move it out of there, clean up your Update function a bit. In fact, there is quite a bit of highly dodgy behavior going on in there. Why would player sometimes be "Marrow.bmp" and other times be "artemis.bmp", depending on the value of the enigmatic menu variable? Also, you can use [ source ] [ / source ] tags to enclose blocks of code and preserve the formatting and indentation to make it a lot easier for us to read.



void gameEvents::gameUpdate()
{
Map map;
if(menu)
{
player = SDL_LoadBMP("Marrow.bmp");
}

if(newMap)
{
background = SDL_LoadBMP("data/maps/map01.bmp");//adds a basic background
player = SDL_LoadBMP("artemis.bmp");// change from menu arrow to artemis, the players character
item = SDL_LoadBMP("brick.bmp");//layer for background
ifstream ifs ( "data/maps/map01.dat" , ifstream::in );

while (ifs.good())
{

map.level[levelY][levelX] << ifs.get();
levelX += 1;
if(levelX >= 20)
{

levelX = 1;
levelY += 1;
}
}
ifs.close();
newMap = false;
}
SDL_BlitSurface( background, NULL, screen, NULL );
for (levelY=0;levelY<15;levelY++)
{
for (levelX=0;levelX<20;levelX++)
{
if (map.level[levelY][levelX] == 1)
{
gameDraw(srcmapX,srcmapY,mapX,mapY,playerWidth,playerHeight,item,screen); //i know item is the same in both places, just using that to test
mapX += 64;
}
if(map.level[levelY][levelX] == 0)
{
gameDraw(srcmapX,srcmapY,mapX,mapY,playerWidth,playerHeight,item,screen); //i know item is the same in both places, just using that to test
mapX += 64;
}
}
if(levelX >= 20)
{
levelX = 0;
levelY += 1;
mapY += 64;
}
}

SDL_SetColorKey(player,SDL_SRCCOLORKEY,SDL_MapRGB(player->format, 200,0,200));
gameDraw(srcX,srcY,desX,desY,playerWidth,playerHeight,player,screen);
SDL_Flip(screen);
gameRunning = true;
}

Also, again as rip-off indicated, your use of mapX and mapY is fishy. Where are they initialized? Why aren't they defined and initialized inside of Update where the map drawing is taking place?

Another thing: Why do you have this:



for (levelY=0;levelY<15;levelY++)
{
for (levelX=0;levelX<20;levelX++)
{
// snip some stuff
}
if(levelX >= 20)
{
levelX = 0;
levelY += 1;
mapY += 64;
}
}


Obviously, if the for block has exited, then levelX is no longer less than 20, so testing for levelX>=20 is redundant and further serves to obfuscate the code. And the fact that you are incrementing levelY, when levelY is already being incremented by the for loop, means that each time through it is going to be incremented by 2, which is likely not what you want.

This topic is closed to new replies.

Advertisement