test/testtimer.c
changeset 0 74212992fb08
child 766 ed57c876700d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testtimer.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,87 @@
     1.4 +
     1.5 +/* Test program to check the resolution of the SDL timer on the current
     1.6 +   platform
     1.7 +*/
     1.8 +
     1.9 +#include <stdlib.h>
    1.10 +#include <stdio.h>
    1.11 +
    1.12 +#include "SDL.h"
    1.13 +
    1.14 +#define DEFAULT_RESOLUTION	1
    1.15 +
    1.16 +static int ticks = 0;
    1.17 +
    1.18 +static Uint32 ticktock(Uint32 interval)
    1.19 +{
    1.20 +	++ticks;
    1.21 +	return(interval);
    1.22 +}
    1.23 +
    1.24 +static Uint32 callback(Uint32 interval, void *param)
    1.25 +{
    1.26 +  printf("Timer %d : param = %d\n", interval, (int) param);
    1.27 +  return interval;
    1.28 +}
    1.29 +
    1.30 +int main(int argc, char *argv[])
    1.31 +{
    1.32 +	int desired;
    1.33 +	SDL_TimerID t1, t2, t3;
    1.34 +
    1.35 +	if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
    1.36 +		fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
    1.37 +		exit(1);
    1.38 +	}
    1.39 +	atexit(SDL_Quit);
    1.40 +
    1.41 +	/* Start the timer */
    1.42 +	desired = 0;
    1.43 +	if ( argv[1] ) {
    1.44 +		desired = atoi(argv[1]);
    1.45 +	}
    1.46 +	if ( desired == 0 ) {
    1.47 +		desired = DEFAULT_RESOLUTION;
    1.48 +	}
    1.49 +	SDL_SetTimer(desired, ticktock);
    1.50 +
    1.51 +	/* Wait 10 seconds */
    1.52 +	printf("Waiting 10 seconds\n");
    1.53 +	SDL_Delay(10*1000);
    1.54 +
    1.55 +	/* Stop the timer */
    1.56 +	SDL_SetTimer(0, NULL);
    1.57 +
    1.58 +	/* Print the results */
    1.59 +	if ( ticks ) {
    1.60 +		fprintf(stderr,
    1.61 +		"Timer resolution: desired = %d ms, actual = %f ms\n",
    1.62 +					desired, (double)(10*1000)/ticks);
    1.63 +	}
    1.64 +	
    1.65 +	/* Test multiple timers */
    1.66 +	printf("Testing multiple timers...\n");
    1.67 +	t1 = SDL_AddTimer(100, callback, (void*)1);
    1.68 +	if(!t1)
    1.69 +	  fprintf(stderr,"Could not create timer 1\n");
    1.70 +	t2 = SDL_AddTimer(50, callback, (void*)2);
    1.71 +	if(!t2)
    1.72 +	  fprintf(stderr,"Could not create timer 2\n");
    1.73 +	t3 = SDL_AddTimer(233, callback, (void*)3);
    1.74 +	if(!t3)
    1.75 +	  fprintf(stderr,"Could not create timer 3\n");
    1.76 +	
    1.77 +	/* Wait 10 seconds */
    1.78 +	printf("Waiting 10 seconds\n");
    1.79 +	SDL_Delay(10*1000);
    1.80 +
    1.81 +	printf("Removing timer 1 and waiting 5 more seconds\n");
    1.82 +	SDL_RemoveTimer(t1);
    1.83 +
    1.84 +	SDL_Delay(5*1000);
    1.85 +
    1.86 +	SDL_RemoveTimer(t2);
    1.87 +	SDL_RemoveTimer(t3);
    1.88 +
    1.89 +	return(0);
    1.90 +}