test/torturethread.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 1154 d93862a3d821
child 1495 1dd8bf30a109
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
/* Simple test of the SDL threading code */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <signal.h>
slouken@0
     7
#include <string.h>
slouken@0
     8
slouken@0
     9
#include "SDL.h"
slouken@0
    10
#include "SDL_thread.h"
slouken@0
    11
slouken@0
    12
#define NUMTHREADS 10
slouken@0
    13
slouken@0
    14
static char volatile time_for_threads_to_die[NUMTHREADS];
slouken@0
    15
icculus@1151
    16
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    17
static void quit(int rc)
icculus@1151
    18
{
icculus@1151
    19
	SDL_Quit();
icculus@1151
    20
	exit(rc);
icculus@1151
    21
}
icculus@1151
    22
slouken@0
    23
int SubThreadFunc(void *data) {
slouken@0
    24
	while(! *(int volatile *)data) {
icculus@1154
    25
		; /*SDL_Delay(10);*/  /* do nothing */
slouken@0
    26
	}
slouken@0
    27
	return 0;
slouken@0
    28
}
slouken@0
    29
slouken@0
    30
int ThreadFunc(void *data) {
slouken@0
    31
	SDL_Thread *sub_threads[NUMTHREADS];
slouken@0
    32
	int flags[NUMTHREADS];
slouken@0
    33
	int i;
slouken@0
    34
	int tid = (int ) data;
slouken@0
    35
slouken@0
    36
	fprintf(stderr, "Creating Thread %d\n", tid);
slouken@0
    37
slouken@0
    38
	for(i = 0; i < NUMTHREADS; i++) {
slouken@0
    39
		flags[i] = 0;
slouken@0
    40
		sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
slouken@0
    41
	}
slouken@0
    42
slouken@0
    43
	printf("Thread '%d' waiting for signal\n", tid);
slouken@0
    44
	while(time_for_threads_to_die[tid] != 1) {
slouken@0
    45
		; /* do nothing */
slouken@0
    46
	}
slouken@0
    47
slouken@0
    48
	printf("Thread '%d' sending signals to subthreads\n", tid);
slouken@0
    49
	for(i = 0; i <  NUMTHREADS; i++) {
slouken@0
    50
		flags[i] = 1;
slouken@0
    51
		SDL_WaitThread(sub_threads[i], NULL);
slouken@0
    52
	}
slouken@0
    53
slouken@0
    54
	printf("Thread '%d' exiting!\n", tid);
slouken@0
    55
slouken@0
    56
	return 0;
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
int main(int argc, char *argv[])
slouken@0
    60
{
slouken@0
    61
	SDL_Thread *threads[NUMTHREADS];
slouken@0
    62
	int i;
slouken@0
    63
slouken@0
    64
	/* Load the SDL library */
slouken@0
    65
	if ( SDL_Init(0) < 0 ) {
slouken@0
    66
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    67
		return(1);
slouken@0
    68
	}
slouken@0
    69
slouken@0
    70
	signal(SIGSEGV, SIG_DFL);
slouken@0
    71
	for(i = 0; i < NUMTHREADS; i++) {
slouken@0
    72
		time_for_threads_to_die[i] = 0;
slouken@0
    73
		threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
slouken@0
    74
	
slouken@0
    75
		if ( threads[i] == NULL ) {
slouken@0
    76
			fprintf(stderr,
slouken@0
    77
			"Couldn't create thread: %s\n", SDL_GetError());
icculus@1151
    78
			quit(1);
slouken@0
    79
		}
slouken@0
    80
	}
slouken@0
    81
slouken@0
    82
	for(i = 0; i < NUMTHREADS; i++) {
slouken@0
    83
		time_for_threads_to_die[i] = 1;
slouken@0
    84
	}
slouken@0
    85
slouken@0
    86
	for(i = NUMTHREADS-1; i >= 0; --i) {
slouken@0
    87
		SDL_WaitThread(threads[i], NULL);
slouken@0
    88
	}
icculus@1151
    89
	SDL_Quit();
icculus@1151
    90
	return(0);
slouken@0
    91
}