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