test/testtimer.c
changeset 0 74212992fb08
child 766 ed57c876700d
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
       
     1 
       
     2 /* Test program to check the resolution of the SDL timer on the current
       
     3    platform
       
     4 */
       
     5 
       
     6 #include <stdlib.h>
       
     7 #include <stdio.h>
       
     8 
       
     9 #include "SDL.h"
       
    10 
       
    11 #define DEFAULT_RESOLUTION	1
       
    12 
       
    13 static int ticks = 0;
       
    14 
       
    15 static Uint32 ticktock(Uint32 interval)
       
    16 {
       
    17 	++ticks;
       
    18 	return(interval);
       
    19 }
       
    20 
       
    21 static Uint32 callback(Uint32 interval, void *param)
       
    22 {
       
    23   printf("Timer %d : param = %d\n", interval, (int) param);
       
    24   return interval;
       
    25 }
       
    26 
       
    27 int main(int argc, char *argv[])
       
    28 {
       
    29 	int desired;
       
    30 	SDL_TimerID t1, t2, t3;
       
    31 
       
    32 	if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
       
    33 		fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
       
    34 		exit(1);
       
    35 	}
       
    36 	atexit(SDL_Quit);
       
    37 
       
    38 	/* Start the timer */
       
    39 	desired = 0;
       
    40 	if ( argv[1] ) {
       
    41 		desired = atoi(argv[1]);
       
    42 	}
       
    43 	if ( desired == 0 ) {
       
    44 		desired = DEFAULT_RESOLUTION;
       
    45 	}
       
    46 	SDL_SetTimer(desired, ticktock);
       
    47 
       
    48 	/* Wait 10 seconds */
       
    49 	printf("Waiting 10 seconds\n");
       
    50 	SDL_Delay(10*1000);
       
    51 
       
    52 	/* Stop the timer */
       
    53 	SDL_SetTimer(0, NULL);
       
    54 
       
    55 	/* Print the results */
       
    56 	if ( ticks ) {
       
    57 		fprintf(stderr,
       
    58 		"Timer resolution: desired = %d ms, actual = %f ms\n",
       
    59 					desired, (double)(10*1000)/ticks);
       
    60 	}
       
    61 	
       
    62 	/* Test multiple timers */
       
    63 	printf("Testing multiple timers...\n");
       
    64 	t1 = SDL_AddTimer(100, callback, (void*)1);
       
    65 	if(!t1)
       
    66 	  fprintf(stderr,"Could not create timer 1\n");
       
    67 	t2 = SDL_AddTimer(50, callback, (void*)2);
       
    68 	if(!t2)
       
    69 	  fprintf(stderr,"Could not create timer 2\n");
       
    70 	t3 = SDL_AddTimer(233, callback, (void*)3);
       
    71 	if(!t3)
       
    72 	  fprintf(stderr,"Could not create timer 3\n");
       
    73 	
       
    74 	/* Wait 10 seconds */
       
    75 	printf("Waiting 10 seconds\n");
       
    76 	SDL_Delay(10*1000);
       
    77 
       
    78 	printf("Removing timer 1 and waiting 5 more seconds\n");
       
    79 	SDL_RemoveTimer(t1);
       
    80 
       
    81 	SDL_Delay(5*1000);
       
    82 
       
    83 	SDL_RemoveTimer(t2);
       
    84 	SDL_RemoveTimer(t3);
       
    85 
       
    86 	return(0);
       
    87 }