test/testtimer.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 1151 be9c9c8f6d53
child 1484 b2b476a4a73c
permissions -rw-r--r--
[PATCH] SDL_GetVideoMode() does not find best mode, part 2

Following commit 1.51, I come accross a problem when SDL must choose between
several video modes that could suit the one asked.

If I ask 320x240 with this list:
768x480 768x240 640x400 640x200 384x480 384x240 320x400 320x200

The smallest selectables modes are 384x240 and 320x400. And SDL choose the later
in this list, but 384x240 is more suitable. So I added a check to compare
the pixel count (surface) of modes, and select the one which has the smallest
pixel count.

In my example, 384x240 has 92160 pixels, and 320x400 has 128000 pixels. So now
SDL will choose 384x240 for the asked 320x240 mode.
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@0
    30
  printf("Timer %d : param = %d\n", interval, (int) 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@0
    40
		fprintf(stderr, "Couldn't load 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
}