test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Mar 2006 01:47:23 +0000
changeset 1500 f58c88a4dff5
parent 1484 b2b476a4a73c
child 1615 d5298e8f22b3
permissions -rw-r--r--
Blah
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
icculus@1151
    15
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    16
static void quit(int rc)
icculus@1151
    17
{
icculus@1151
    18
	SDL_Quit();
icculus@1151
    19
	exit(rc);
icculus@1151
    20
}
icculus@1151
    21
slouken@0
    22
static Uint32 ticktock(Uint32 interval)
slouken@0
    23
{
slouken@0
    24
	++ticks;
slouken@0
    25
	return(interval);
slouken@0
    26
}
slouken@0
    27
slouken@0
    28
static Uint32 callback(Uint32 interval, void *param)
slouken@0
    29
{
slouken@1500
    30
  printf("Timer %d : param = %d\n", interval, (uintptr_t)param);
slouken@0
    31
  return interval;
slouken@0
    32
}
slouken@0
    33
slouken@0
    34
int main(int argc, char *argv[])
slouken@0
    35
{
slouken@0
    36
	int desired;
slouken@0
    37
	SDL_TimerID t1, t2, t3;
slouken@0
    38
slouken@0
    39
	if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
slouken@1484
    40
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@1151
    41
		return(1);
slouken@0
    42
	}
slouken@0
    43
slouken@0
    44
	/* Start the timer */
slouken@0
    45
	desired = 0;
slouken@0
    46
	if ( argv[1] ) {
slouken@0
    47
		desired = atoi(argv[1]);
slouken@0
    48
	}
slouken@0
    49
	if ( desired == 0 ) {
slouken@0
    50
		desired = DEFAULT_RESOLUTION;
slouken@0
    51
	}
slouken@0
    52
	SDL_SetTimer(desired, ticktock);
slouken@0
    53
slouken@0
    54
	/* Wait 10 seconds */
slouken@0
    55
	printf("Waiting 10 seconds\n");
slouken@0
    56
	SDL_Delay(10*1000);
slouken@0
    57
slouken@0
    58
	/* Stop the timer */
slouken@0
    59
	SDL_SetTimer(0, NULL);
slouken@0
    60
slouken@0
    61
	/* Print the results */
slouken@0
    62
	if ( ticks ) {
slouken@0
    63
		fprintf(stderr,
slouken@0
    64
		"Timer resolution: desired = %d ms, actual = %f ms\n",
slouken@0
    65
					desired, (double)(10*1000)/ticks);
slouken@0
    66
	}
slouken@0
    67
	
slouken@0
    68
	/* Test multiple timers */
slouken@0
    69
	printf("Testing multiple timers...\n");
slouken@0
    70
	t1 = SDL_AddTimer(100, callback, (void*)1);
slouken@0
    71
	if(!t1)
slouken@766
    72
	  fprintf(stderr,"Could not create timer 1: %s\n", SDL_GetError());
slouken@0
    73
	t2 = SDL_AddTimer(50, callback, (void*)2);
slouken@0
    74
	if(!t2)
slouken@766
    75
	  fprintf(stderr,"Could not create timer 2: %s\n", SDL_GetError());
slouken@0
    76
	t3 = SDL_AddTimer(233, callback, (void*)3);
slouken@0
    77
	if(!t3)
slouken@766
    78
	  fprintf(stderr,"Could not create timer 3: %s\n", SDL_GetError());
slouken@0
    79
	
slouken@0
    80
	/* Wait 10 seconds */
slouken@0
    81
	printf("Waiting 10 seconds\n");
slouken@0
    82
	SDL_Delay(10*1000);
slouken@0
    83
slouken@0
    84
	printf("Removing timer 1 and waiting 5 more seconds\n");
slouken@0
    85
	SDL_RemoveTimer(t1);
slouken@0
    86
slouken@0
    87
	SDL_Delay(5*1000);
slouken@0
    88
slouken@0
    89
	SDL_RemoveTimer(t2);
slouken@0
    90
	SDL_RemoveTimer(t3);
slouken@0
    91
icculus@1151
    92
	SDL_Quit();
slouken@0
    93
	return(0);
slouken@0
    94
}