Sign in to follow this  
yoni0505

Lag problem

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

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