test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 May 2006 08:18:28 +0000
branchSDL-1.3
changeset 1659 14717b52abc0
parent 1615 d5298e8f22b3
child 1662 782fd950bd46
child 1895 c121d94672cb
permissions -rw-r--r--
Merge trunk-1.3-3
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@1659
    15
static Uint32 SDLCALL ticktock(Uint32 interval)
slouken@0
    16
{
slouken@0
    17
	++ticks;
slouken@0
    18
	return(interval);
slouken@0
    19
}
slouken@0
    20
slouken@1659
    21
static Uint32 SDLCALL callback(Uint32 interval, void *param)
slouken@0
    22
{
slouken@1615
    23
  printf("Timer %d : param = %d\n", interval, (int)(uintptr_t)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@1484
    33
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@1151
    34
		return(1);
slouken@0
    35
	}
slouken@0
    36
slouken@0
    37
	/* Start the timer */
slouken@0
    38
	desired = 0;
slouken@0
    39
	if ( argv[1] ) {
slouken@0
    40
		desired = atoi(argv[1]);
slouken@0
    41
	}
slouken@0
    42
	if ( desired == 0 ) {
slouken@0
    43
		desired = DEFAULT_RESOLUTION;
slouken@0
    44
	}
slouken@0
    45
	SDL_SetTimer(desired, ticktock);
slouken@0
    46
slouken@0
    47
	/* Wait 10 seconds */
slouken@0
    48
	printf("Waiting 10 seconds\n");
slouken@0
    49
	SDL_Delay(10*1000);
slouken@0
    50
slouken@0
    51
	/* Stop the timer */
slouken@0
    52
	SDL_SetTimer(0, NULL);
slouken@0
    53
slouken@0
    54
	/* Print the results */
slouken@0
    55
	if ( ticks ) {
slouken@0
    56
		fprintf(stderr,
slouken@0
    57
		"Timer resolution: desired = %d ms, actual = %f ms\n",
slouken@0
    58
					desired, (double)(10*1000)/ticks);
slouken@0
    59
	}
slouken@0
    60
	
slouken@0
    61
	/* Test multiple timers */
slouken@0
    62
	printf("Testing multiple timers...\n");
slouken@0
    63
	t1 = SDL_AddTimer(100, callback, (void*)1);
slouken@0
    64
	if(!t1)
slouken@766
    65
	  fprintf(stderr,"Could not create timer 1: %s\n", SDL_GetError());
slouken@0
    66
	t2 = SDL_AddTimer(50, callback, (void*)2);
slouken@0
    67
	if(!t2)
slouken@766
    68
	  fprintf(stderr,"Could not create timer 2: %s\n", SDL_GetError());
slouken@0
    69
	t3 = SDL_AddTimer(233, callback, (void*)3);
slouken@0
    70
	if(!t3)
slouken@766
    71
	  fprintf(stderr,"Could not create timer 3: %s\n", SDL_GetError());
slouken@0
    72
	
slouken@0
    73
	/* Wait 10 seconds */
slouken@0
    74
	printf("Waiting 10 seconds\n");
slouken@0
    75
	SDL_Delay(10*1000);
slouken@0
    76
slouken@0
    77
	printf("Removing timer 1 and waiting 5 more seconds\n");
slouken@0
    78
	SDL_RemoveTimer(t1);
slouken@0
    79
slouken@0
    80
	SDL_Delay(5*1000);
slouken@0
    81
slouken@0
    82
	SDL_RemoveTimer(t2);
slouken@0
    83
	SDL_RemoveTimer(t3);
slouken@0
    84
icculus@1151
    85
	SDL_Quit();
slouken@0
    86
	return(0);
slouken@0
    87
}