Lag problem

Started by
1 comment, last by yoni0505 13 years, 4 months ago
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 screenvoid updateDisplay() {	loadMap(level.map);	loadBMP(player.image, player.x * 32, player.y * 32);	SDL_Flip(screen);}


//load a map to the screenvoid 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 tilevoid 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 screenvoid 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]
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.
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.

This topic is closed to new replies.

Advertisement