Jump to content
  • Advertisement
Sign in to follow this  
spiroth10

SDL Timer problems?

This topic is 4354 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

Ok, I'm trying to set up a timer so that NPC sprites can move themselves after x amount of milliseconds. But for some reason, just trying to use the SDL_AddTimer() function is giving me linker errors. I'll post my initialization code, and the code relating to the timer, the rest would be useless:
SDL_TimerID etimer;

Uint32 etcall(Uint32 interval, void *param);

int sdlinit(){
	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1){
		printf("For some reason, SDL could not initialize\n");
	}
	MainSurface = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT/*SDL_HWSURFACE | SDL_DOUBLEBUF*/);
	SDL_WM_SetCaption("Psychotic Universe", "");
	etimer=SDL_AddTimer(1000,etcall,NULL);
	atexit(SDL_Quit);
	return 0;
}


the error is:
Linking...
C:\MinGWStudio\Projects\psu\Debug\main.o: In function `sdlinit':
C:\MinGWStudio\Projects\psu\main.c:21: undefined reference to `etcall'
collect2: ld returned 1 exit status
sdlinit() is called from main(). There *might* be some inconsistencies in this code snippit -- I also have a header file with some code in it, and some other functions as well -- but i think this code should be able to stand on it's own -- providing you add a main() function and call sdlinit() from within it. any idea what is causing this? if I change SDL_AddTimer to SDL_AddTimer(1000,etcall(),NULL); It errors due to not enough arguments to etcall, and SDL_AddTimer(1000,etcall(1000,NULL),NULL) also causes problems.

Share this post


Link to post
Share on other sites
Advertisement
Its complaining because it cannot find the code associated with etcall.

"undefined reference to" as a linker error means that the function etcall hasnt been compiled, possibly because it hasnt been written or maybe it is located in a file that is not included for compilation.

This should work:

Uint32 etcall( Uint32 interval, void *param )
{
return interval;
}

int sdlinit(){

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1){

printf("For some reason, SDL could not initialize\n");

}

MainSurface = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT/*SDL_HWSURFACE | SDL_DOUBLEBUF*/);

SDL_WM_SetCaption("Psychotic Universe", "");

etimer=SDL_AddTimer(1000,etcall,NULL);

atexit(SDL_Quit);

return 0;

}



And finally, in case your SDL_Init call ever does fail, use SDL_GetError() to tell you why:

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1){

printf("SDL could not initialize: %s \n",SDL_GetError() );
}

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!