SDL Timer problems?

Started by
0 comments, last by rip-off 17 years, 8 months ago
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.
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() );}

This topic is closed to new replies.

Advertisement