Sign in to follow this  
ooleziloo

[SDL] slow down the code?

Recommended Posts

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


[img]http://s4.postimage.org/codu218mw/fail.jpg[/img]

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1313267249' post='4848735']
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?


[/quote]



correct

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1313273500' post='4848768']
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.
[/quote]
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);

Share this post


Link to post
Share on other sites
I assume that [b]newMap[/b] 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 [b]player[/b] sometimes be "Marrow.bmp" and other times be "artemis.bmp", depending on the value of the enigmatic [b]menu[/b] 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.

[code]

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;
} [/code]
Also, again as rip-off indicated, your use of [b]mapX[/b] and [b]mapY[/b] 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:

[code]

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

Obviously, if the for block has exited, then [b]levelX[/b] is no longer less than 20, so testing for [b]levelX>=20[/b] is redundant and further serves to obfuscate the code. And the fact that you are incrementing [b]levelY[/b], when [b]levelY[/b] 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this