Jump to content
  • Advertisement
Sign in to follow this  
yoni0505

Lag problem

This topic is 2839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there.

I started to learn game programming using C++ and SDL few days ago.
I built a simple program which loads a map of tiles and a simple character that moves around.

The problem is that the program is really laggy. It is caused by the updating process of the map on the screen. Once every loop the program display each tile of the map, 300 of them (20x15).

Does it caused by the image format?
There is any other way to do display the tile map without lag? Any SDL function I dont know, perhaps a method to display?

I'm displaying the map using the next functions:

//update the screen
void updateDisplay() {
loadMap(level.map);
loadBMP(player.image, player.x * 32, player.y * 32);
SDL_Flip(screen);
}






//load a map to the screen
void loadMap(int map[15][20]) {
for(int y=0; y < 15; y++) {
for(int x=0; x < 20; x++) {
drawTile(map[y][x],x,y);
}
}
}






//check which image to draw for a tile
void drawTile(int tile,int x, int y) {
switch(tile) {
case 0:
loadBMP("tile0.bmp",x*32,y*32);
break;
case 1:
loadBMP("tile1.bmp",x*32,y*32);
break;
case 2:
loadBMP("tile2.bmp",x*32,y*32);
break;
}
}






//load a .BMP file to the screen
void loadBMP(char* file_name, int x, int y) {
SDL_Surface *image = NULL;

image = SDL_LoadBMP(file_name);
if(image == NULL) {
return;
}

image = SDL_DisplayFormat(image);

SDL_Rect src, dest;
src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;

dest.x = x;
dest.y = y;
dest.w = image->w;
dest.h = image->h;

SDL_BlitSurface(image, &src, screen, &dest);

SDL_FreeSurface(image);
}





also I use "SDL_Delay(5);" at the end of the main loop.

What can I do?

[Edited by - yoni0505 on December 12, 2010 11:53:51 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hello.

I read through the source code you posted and noticed that you are loading from disk every image to be drawn right before you draw it to the backbuffer. You could increase the speed of the program if you would load your bitmaps only once at the beginning of the program instead of every time the screen is redrawn.

Share this post


Link to post
Share on other sites
Quote:
Original post by JasonR
Hello.

I read through the source code you posted and noticed that you are loading from disk every image to be drawn right before you draw it to the backbuffer. You could increase the speed of the program if you would load your bitmaps only once at the beginning of the program instead of every time the screen is redrawn.


Thank you, I'll do it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!