[SDL] timing problem

VnzlaDun dundunj at hotmail.com
Sun Dec 19 16:41:21 PST 2010


maybe the problem isnt in that part of the code here is the whole code


Code:
#include<SDL.h>
#include<SDL_ttf.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
	TTF_Init();
	SDL_Surface *screen=NULL;
	SDL_Surface *speedtextsurf=NULL;
	SDL_Surface *timetextsurf=NULL;
	SDL_Surface *tickstextsurf=NULL;
	SDL_Event action;
	SDL_Rect back,cuadro,offset1,offset2;
	SDL_Color white={255,255,255};
	TTF_Font *font;
	bool run=true;
	bool right=false;
	char speedtext[11];
	char timetext[11];
	char ticks[11];
	Sint16 speed=0;
	Uint32 time=0;
	Uint32 newtime=0;
	cuadro.x=100;
	cuadro.y=100;
	cuadro.h=10;
	cuadro.w=10;
	back.x=0;
	back.y=0;
	back.h=480;
	back.w=640;
	offset1.x=0;
	offset1.y=14;
	offset2.x=0;
	offset2.y=28;
	screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE);
	font=TTF_OpenFont("impact.ttf",14);
	Uint32 blanco=SDL_MapRGB(screen->format,255,255,255);
	Uint32 rojo=SDL_MapRGB(screen->format,255,0,0);
	//SDL_FillRect(screen,&cuadro,blanco);
	//SDL_Flip(screen);
	while(run)
	{
		if(SDL_PollEvent(&action))
		{
			switch (action.type)
			{
			case SDL_KEYDOWN:
				switch (action.key.keysym.sym)
				{
				case SDLK_RIGHT:
					right=true;
					break;
				case SDLK_ESCAPE:
					run=false;
					break;
				}
				break;
			case SDL_KEYUP:
				switch (action.key.keysym.sym)
				{
				case SDLK_RIGHT:
					right=false;
					break;
				}
				break;
			}
			if(action.type==SDL_QUIT) run=false;
		}
		bool moving=false;
		if(right)
		{
			cuadro.x=200;
			if(!moving)
			{
				time=SDL_GetTicks();

				moving=true;
			}
		
			newtime=SDL_GetTicks()-time;
			speed=newtime;
			
		}
		else
		{
			cuadro.x=100;
			moving=false;
			speed=0;	
		}
		

		itoa(SDL_GetTicks(), ticks, 10);
		itoa(newtime, timetext, 10);
		itoa(speed, speedtext, 10);

		timetextsurf=TTF_RenderText_Solid(font,timetext,white);
		speedtextsurf=TTF_RenderText_Solid(font,speedtext,white);
		tickstextsurf=TTF_RenderText_Solid(font,ticks,white);

		SDL_FillRect(screen,&back,0);

		SDL_BlitSurface(tickstextsurf,NULL,screen,NULL);
		SDL_BlitSurface(speedtextsurf,NULL,screen,&offset1);
		SDL_BlitSurface(timetextsurf,NULL,screen,&offset2);
		//cuadro.x += speed;

		SDL_FillRect(screen,&cuadro,blanco);
		SDL_Flip(screen);
	}
	SDL_FreeSurface(speedtextsurf);
	SDL_FreeSurface(timetextsurf);
	SDL_FreeSurface(tickstextsurf);
	TTF_CloseFont( font );
	SDL_Quit();
	TTF_Quit();
	return 0;
}








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20101219/4c69dc67/attachment-0008.htm>


More information about the SDL mailing list