Look Over My Code Please?


Vlad

Still Fresh
Joined
Jan 27, 2006
Messages
35
I'm new to coding in C, and for handhelds, but I've been picking it up (at least I think) fairly well. I tossed up a little demo, where the player is a car that has to be kept inside the track (though that part isn't even done yet.) Now I'm ready to start adding some nifty features, but for some reason, the game performs woefully. Even when the fps is capped, my PC performs eons better than the GP2X. Now I know that this is because the GP2X runs at 133~200Mhz, but I HAVE seen it do much more intensive processes than what I'm doing. If someone could point out any disgustingly huge errors I'm making, I'd appreciate it.

-Vlad

Screenshot:


Code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>

#ifdef GP2X
#include <unistd.h>
#endif

/* The screen surface */
SDL_Surface *screen = NULL;

/* The image surface */
SDL_Surface *bground;
SDL_Surface *mini;
SDL_Surface *line;
SDL_Surface *road;
SDL_Rect street[320];
int beforetime;
int aftertime;
int sleeptime;
int px;
int py;
int pdy;
int w;

int dist;

void Terminate(void)
{
	SDL_Quit();
#ifdef GP2X
	chdir("/usr/gp2x");
	execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
#endif
}

/* Image display function */
void DrawImage(SDL_Surface *screen,SDL_Surface *sprite, int x, int y)
{
	SDL_Rect dst, src;

	src.x = 0;
	src.y = 0;
	src.w = 320;
	src.h = 240;

	dst.x = x;
	dst.y = y;

 	SDL_BlitSurface(sprite, &src, screen, &dst);
}

int main (int argc, char *argv[])
{
	int done;

	/* Initialize SDL */
	if (SDL_Init (SDL_INIT_VIDEO) < 0) {
		fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
		exit (1);
	}
	atexit (Terminate);
	
	SDL_Init(SDL_INIT_JOYSTICK);
	SDL_JoystickOpen(0);

	SDL_ShowCursor(SDL_DISABLE);

	/* Set 320x240 16-bits video mode */
	screen = SDL_SetVideoMode (320, 240, 16, SDL_SWSURFACE);
	if (screen == NULL) {
		fprintf (stderr, "Couldn't set 320x240x16 video mode: %s\n", SDL_GetError ());
		exit (2);
	}
	
	int i=0; //setting up the initial road
	while(i<320)
	{	
		street[i].w=1;
		street[i].h=100;
		street[i].x=i;
		street[i].y=120;
		i++;
	}
	
	px = 105;
	py = 160;
	pdy = 0;
	w = 100;
	dist = 0;
	
	bground = SDL_LoadBMP("bground.bmp");
	mini = SDL_LoadBMP("mini.bmp");
	SDL_SetColorKey(mini, SDL_SRCCOLORKEY, SDL_MapRGB(mini->format, 255, 0, 255));
	line = SDL_LoadBMP("line.bmp");
	road = SDL_LoadBMP("road.bmp");

	done = 0;
	beforetime=SDL_GetTicks();
	while (!done)
	{
		dist++; //Updating phase
	
		py+=pdy;
		
		int newval;
		int randomz;
		if( (rand() % 10) > 7)
		{
	   		if(rand()%2)
		 	{  randomz = -1;
			}else{
			  randomz = 1;
			}
		}else{
			randomz=0;
		}			  
		
		newval = street[319].y+randomz;	//make new starting point for road
		if(newval < 105)
				newval = newval+2;
		if(newval+w > 240)
				newval = newval-2;			
			 
		i=0;   
		while(i<319)
		{
				street[i].y=street[i+1].y;
				street[i].h=street[i+1].h;
				i++;
		}
		street[319].y=newval;
		
		SDL_Event event;
	
		/* Check for events */
		while (SDL_PollEvent (&event))
		{
			switch (event.type)
			{
				case SDL_JOYBUTTONDOWN:
					 switch(event.jbutton.button)
					{
						case 0:
							pdy = -2;
						break;
						case 4:
							pdy = 2;
						break;
						case 8:
							Terminate();
						break;
					 }
					break;
				case SDL_JOYBUTTONUP:
					 pdy=0;
					 break;
				case SDL_QUIT:
					done = 1;
					break;
				default:
					break;
			}
		}
		
		if(dist == 1000)
		{
			street[319].h=(street[319].h-5);
			dist=0;
		}
		
		 DrawImage(screen,bground, 0, 0); //Rendering phase
		 int i = 0;
		 while(i<320) //drawing the road
		 {	
			  DrawImage(screen, line, i, street);		
		 SDL_FillRect(screen,&street[i],SDL_MapRGB(screen->format,128,128,128));
			i++;
		 }
		 DrawImage(screen, mini, px, py);
		 SDL_Flip (screen);
		
		 aftertime=SDL_GetTicks(); //Sleep phase
		 sleeptime = 25-(aftertime-beforetime);
		 if( sleeptime > 0)
		 {
			   SDL_Delay(sleeptime);
		 }else{
			   SDL_Delay(1);
		 }
	}
	return 0;
}

Thanks
 
Are you using the peayrn's HW SDL libraries or GPH's SDK?

Also after you load an image, you need to convert it to the same format as the screen otherwise SDL tries to convert everytime it blits the screen which is slow.
Code:
NewConvertedSurface = SDL_DisplayFormat(LoadedSurface);
SDL_FreeSurface(LoadedSurface);
 
plus if your using paeryn's SDL libs you'll probably want to make a HWSURFACE rather than a SWSURFACE.

Code:
screen = SDL_SetVideoMode (320, 240, 16, SDL_HWSURFACE);

and you may want to look into the joys of double-buffering:

Code:
screen = SDL_SetVideoMode (320, 240, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
 
Is this correct?
Code:
	/* Initialize SDL */
	if (SDL_Init (SDL_INIT_VIDEO) < 0) {
		fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
		exit (1);
	}
	atexit (Terminate);
	
	SDL_Init(SDL_INIT_JOYSTICK);
I usualy do:
Code:
	if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
		fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
		exit (1);
	}
	atexit (Terminate);
 
Back
Top