19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Test program to check the resolution of the SDL timer on the current
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   platform
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h>
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_RESOLUTION	1
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ticks = 0;
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint32 SDLCALL ticktock(Uint32 interval)
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++ticks;
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(interval);
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint32 SDLCALL callback(Uint32 interval, void *param)
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  printf("Timer %d : param = %d\n", interval, (int)(uintptr_t)param);
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return interval;
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char *argv[])
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int desired;
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_TimerID t1, t2, t3;
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(1);
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Start the timer */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	desired = 0;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( argv[1] ) {
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		desired = atoi(argv[1]);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( desired == 0 ) {
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		desired = DEFAULT_RESOLUTION;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetTimer(desired, ticktock);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait 10 seconds */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Waiting 10 seconds\n");
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Delay(10*1000);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Stop the timer */
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetTimer(0, NULL);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Print the results */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ticks ) {
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr,
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		"Timer resolution: desired = %d ms, actual = %f ms\n",
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					desired, (double)(10*1000)/ticks);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Test multiple timers */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Testing multiple timers...\n");
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	t1 = SDL_AddTimer(100, callback, (void*)1);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(!t1)
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  fprintf(stderr,"Could not create timer 1: %s\n", SDL_GetError());
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	t2 = SDL_AddTimer(50, callback, (void*)2);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(!t2)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  fprintf(stderr,"Could not create timer 2: %s\n", SDL_GetError());
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	t3 = SDL_AddTimer(233, callback, (void*)3);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(!t3)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  fprintf(stderr,"Could not create timer 3: %s\n", SDL_GetError());
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait 10 seconds */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Waiting 10 seconds\n");
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Delay(10*1000);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	printf("Removing timer 1 and waiting 5 more seconds\n");
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RemoveTimer(t1);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Delay(5*1000);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RemoveTimer(t2);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RemoveTimer(t3);
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
88