test/threadwin.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 562 cb40b26523a5
permissions -rw-r--r--
Initial revision
slouken@0
     1
slouken@0
     2
/* Test out the multi-threaded event handling functions */
slouken@0
     3
slouken@0
     4
#include <stdlib.h>
slouken@0
     5
#include <stdio.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
#include "SDL_thread.h"
slouken@0
    10
slouken@0
    11
/* Are we done yet? */
slouken@0
    12
static int done = 0;
slouken@0
    13
slouken@0
    14
/* Is the cursor visible? */
slouken@0
    15
static int visible = 1;
slouken@0
    16
slouken@0
    17
SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
slouken@0
    18
{
slouken@0
    19
	SDL_Surface *icon;
slouken@0
    20
	Uint8       *pixels;
slouken@0
    21
	Uint8       *mask;
slouken@0
    22
	int          mlen, i;
slouken@0
    23
slouken@0
    24
	*maskp = NULL;
slouken@0
    25
slouken@0
    26
	/* Load the icon surface */
slouken@0
    27
	icon = SDL_LoadBMP(file);
slouken@0
    28
	if ( icon == NULL ) {
slouken@0
    29
		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@0
    30
		return(NULL);
slouken@0
    31
	}
slouken@0
    32
slouken@0
    33
	/* Check width and height */
slouken@0
    34
	if ( (icon->w%8) != 0 ) {
slouken@0
    35
		fprintf(stderr, "Icon width must be a multiple of 8!\n");
slouken@0
    36
		SDL_FreeSurface(icon);
slouken@0
    37
		return(NULL);
slouken@0
    38
	}
slouken@0
    39
	if ( icon->format->palette == NULL ) {
slouken@0
    40
		fprintf(stderr, "Icon must have a palette!\n");
slouken@0
    41
		SDL_FreeSurface(icon);
slouken@0
    42
		return(NULL);
slouken@0
    43
	}
slouken@0
    44
slouken@0
    45
	/* Set the colorkey */
slouken@0
    46
	SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));
slouken@0
    47
slouken@0
    48
	/* Create the mask */
slouken@0
    49
	pixels = (Uint8 *)icon->pixels;
slouken@0
    50
	printf("Transparent pixel: (%d,%d,%d)\n",
slouken@0
    51
				icon->format->palette->colors[*pixels].r,
slouken@0
    52
				icon->format->palette->colors[*pixels].g,
slouken@0
    53
				icon->format->palette->colors[*pixels].b);
slouken@0
    54
	mlen = icon->w*icon->h;
slouken@0
    55
	mask = (Uint8 *)malloc(mlen/8);
slouken@0
    56
	if ( mask == NULL ) {
slouken@0
    57
		fprintf(stderr, "Out of memory!\n");
slouken@0
    58
		SDL_FreeSurface(icon);
slouken@0
    59
		return(NULL);
slouken@0
    60
	}
slouken@0
    61
	memset(mask, 0, mlen/8);
slouken@0
    62
	for ( i=0; i<mlen; ) {
slouken@0
    63
		if ( pixels[i] != *pixels )
slouken@0
    64
			mask[i/8] |= 0x01;
slouken@0
    65
		++i;
slouken@0
    66
		if ( (i%8) != 0 )
slouken@0
    67
			mask[i/8] <<= 1;
slouken@0
    68
	}
slouken@0
    69
	*maskp = mask;
slouken@0
    70
	return(icon);
slouken@0
    71
}
slouken@0
    72
slouken@0
    73
int FilterEvents(const SDL_Event *event)
slouken@0
    74
{
slouken@0
    75
	static int reallyquit = 0;
slouken@0
    76
slouken@0
    77
	switch (event->type) {
slouken@0
    78
slouken@0
    79
		case SDL_ACTIVEEVENT:
slouken@0
    80
			/* See what happened */
slouken@0
    81
			printf("App %s ",
slouken@0
    82
				event->active.gain ? "gained" : "lost");
slouken@0
    83
			if ( event->active.state & SDL_APPACTIVE )
slouken@0
    84
				printf("active ");
slouken@0
    85
			if ( event->active.state & SDL_APPMOUSEFOCUS )
slouken@0
    86
				printf("mouse ");
slouken@0
    87
			if ( event->active.state & SDL_APPINPUTFOCUS )
slouken@0
    88
				printf("input ");
slouken@0
    89
			printf("focus\n");
slouken@0
    90
slouken@0
    91
			/* See if we are iconified or restored */
slouken@0
    92
			if ( event->active.state & SDL_APPACTIVE ) {
slouken@0
    93
				printf("App has been %s\n",
slouken@0
    94
					event->active.gain ?
slouken@0
    95
						 "restored" : "iconified");
slouken@0
    96
			}
slouken@0
    97
			return(0);
slouken@0
    98
slouken@0
    99
		/* This is important!  Queue it if we want to quit. */
slouken@0
   100
		case SDL_QUIT:
slouken@0
   101
			if ( ! reallyquit ) {
slouken@0
   102
				reallyquit = 1;
slouken@0
   103
				printf("Quit requested\n");
slouken@0
   104
				return(0);
slouken@0
   105
			}
slouken@0
   106
			printf("Quit demanded\n");
slouken@0
   107
			return(1);
slouken@0
   108
slouken@0
   109
		/* Mouse and keyboard events go to threads */
slouken@0
   110
		case SDL_MOUSEMOTION:
slouken@0
   111
		case SDL_MOUSEBUTTONDOWN:
slouken@0
   112
		case SDL_MOUSEBUTTONUP:
slouken@0
   113
		case SDL_KEYDOWN:
slouken@0
   114
		case SDL_KEYUP:
slouken@0
   115
			return(1);
slouken@0
   116
slouken@0
   117
		/* Drop all other events */
slouken@0
   118
		default:
slouken@0
   119
			return(0);
slouken@0
   120
	}
slouken@0
   121
}
slouken@0
   122
slouken@0
   123
int HandleMouse(void *unused)
slouken@0
   124
{
slouken@0
   125
	SDL_Event events[10];
slouken@0
   126
	int i, found;
slouken@0
   127
	Uint32 mask;
slouken@0
   128
slouken@0
   129
	/* Handle mouse events here */
slouken@0
   130
	mask = (SDL_MOUSEMOTIONMASK|SDL_MOUSEBUTTONDOWNMASK|SDL_MOUSEBUTTONUPMASK);
slouken@0
   131
	while ( ! done ) {
slouken@0
   132
		found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
slouken@0
   133
		for ( i=0; i<found; ++i ) {
slouken@0
   134
			switch(events[i].type) {
slouken@0
   135
				/* We want to toggle visibility on buttonpress */
slouken@0
   136
				case SDL_MOUSEBUTTONDOWN:
slouken@0
   137
				case SDL_MOUSEBUTTONUP:
slouken@0
   138
					if ( events[i].button.state == SDL_PRESSED ) {
slouken@0
   139
						visible = !visible;
slouken@0
   140
						SDL_ShowCursor(visible);
slouken@0
   141
					}
slouken@0
   142
					printf("Mouse button %d has been %s\n",
slouken@0
   143
						events[i].button.button,
slouken@0
   144
						(events[i].button.state == SDL_PRESSED) ?
slouken@0
   145
						"pressed" : "released");
slouken@0
   146
					break;
slouken@0
   147
				/* Show relative mouse motion */
slouken@0
   148
				case SDL_MOUSEMOTION:
slouken@0
   149
					printf("Mouse relative motion: {%d,%d}\n",
slouken@0
   150
							events[i].motion.xrel, events[i].motion.yrel);
slouken@0
   151
					break;
slouken@0
   152
			}
slouken@0
   153
		}
slouken@0
   154
		/* Give up some CPU to allow events to arrive */
slouken@0
   155
		SDL_Delay(20);
slouken@0
   156
	}
slouken@0
   157
	return(0);
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
int HandleKeyboard(void *unused)
slouken@0
   161
{
slouken@0
   162
	SDL_Event events[10];
slouken@0
   163
	int i, found;
slouken@0
   164
	Uint32 mask;
slouken@0
   165
slouken@0
   166
	/* Handle mouse events here */
slouken@0
   167
	mask = (SDL_KEYDOWNMASK|SDL_KEYUPMASK);
slouken@0
   168
	while ( ! done ) {
slouken@0
   169
		found = SDL_PeepEvents(events, 10, SDL_GETEVENT, mask);
slouken@0
   170
		for ( i=0; i<found; ++i ) {
slouken@0
   171
			switch(events[i].type) {
slouken@0
   172
			    /* We want to toggle visibility on buttonpress */
slouken@0
   173
			    case SDL_KEYDOWN:
slouken@0
   174
			    case SDL_KEYUP:
slouken@0
   175
			    	/* Allow hitting <ESC> to quit the app */
slouken@0
   176
			    	if ( events[i].key.keysym.sym == SDLK_ESCAPE ) {
slouken@0
   177
			    		done = 1;
slouken@0
   178
			    	}
slouken@0
   179
			    	printf("Key '%c' has been %s\n",
slouken@0
   180
						events[i].key.keysym.unicode,
slouken@0
   181
					(events[i].key.state == SDL_PRESSED) ?
slouken@0
   182
						"pressed" : "released");
slouken@0
   183
			    	break;
slouken@0
   184
			}
slouken@0
   185
		}
slouken@0
   186
		/* Give up some CPU to allow events to arrive */
slouken@0
   187
		SDL_Delay(20);
slouken@0
   188
	}
slouken@0
   189
	return(0);
slouken@0
   190
}
slouken@0
   191
slouken@0
   192
int main(int argc, char *argv[])
slouken@0
   193
{
slouken@0
   194
	SDL_Surface *screen;
slouken@0
   195
	SDL_Surface *icon;
slouken@0
   196
	Uint8 *icon_mask;
slouken@0
   197
	int i, parsed;
slouken@0
   198
	Uint8 *buffer;
slouken@0
   199
	SDL_Color palette[256];
slouken@0
   200
	Uint32 init_flags;
slouken@0
   201
	Uint8  video_bpp;
slouken@0
   202
	Uint32 video_flags;
slouken@0
   203
	SDL_Thread *mouse_thread;
slouken@0
   204
	SDL_Thread *keybd_thread;
slouken@0
   205
slouken@0
   206
	/* Set the options, based on command line arguments */
slouken@0
   207
	init_flags = SDL_INIT_VIDEO;
slouken@0
   208
	video_bpp = 8;
slouken@0
   209
	video_flags = SDL_SWSURFACE;
slouken@0
   210
	parsed = 1;
slouken@0
   211
	while ( parsed ) {
slouken@0
   212
		/* If the threaded option is enabled, and the SDL library hasn't
slouken@0
   213
		   been compiled with threaded events enabled, then the mouse and
slouken@0
   214
		   keyboard won't respond.
slouken@0
   215
		 */
slouken@0
   216
		if ( (argc >= 2) && (strcmp(argv[1], "-threaded") == 0) ) {
slouken@0
   217
			init_flags |= SDL_INIT_EVENTTHREAD;
slouken@0
   218
			argc -= 1;
slouken@0
   219
			argv += 1;
slouken@0
   220
			printf("Running with threaded events\n");
slouken@0
   221
		} else
slouken@0
   222
		if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) {
slouken@0
   223
			video_flags |= SDL_FULLSCREEN;
slouken@0
   224
			argc -= 1;
slouken@0
   225
			argv += 1;
slouken@0
   226
		} else
slouken@0
   227
		if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) {
slouken@0
   228
			video_bpp = atoi(argv[2]);
slouken@0
   229
			argc -= 2;
slouken@0
   230
			argv += 2;
slouken@0
   231
		} else {
slouken@0
   232
			parsed = 0;
slouken@0
   233
		}
slouken@0
   234
	}
slouken@0
   235
slouken@0
   236
	/* Initialize SDL with the requested flags */
slouken@0
   237
	if ( SDL_Init(init_flags) < 0 ) {
slouken@0
   238
		fprintf(stderr,
slouken@0
   239
			"Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@0
   240
		exit(1);
slouken@0
   241
	}
slouken@0
   242
	atexit(SDL_Quit);
slouken@0
   243
slouken@0
   244
	/* Set the icon -- this must be done before the first mode set */
slouken@0
   245
	icon = LoadIconSurface("icon.bmp", &icon_mask);
slouken@0
   246
	if ( icon != NULL ) {
slouken@0
   247
		SDL_WM_SetIcon(icon, icon_mask);
slouken@0
   248
	}
slouken@0
   249
	if ( icon_mask != NULL )
slouken@0
   250
		free(icon_mask);
slouken@0
   251
slouken@0
   252
	/* Initialize the display */
slouken@0
   253
	screen = SDL_SetVideoMode(640, 480, video_bpp, video_flags);
slouken@0
   254
	if (  screen == NULL ) {
slouken@0
   255
		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
slouken@0
   256
						video_bpp, SDL_GetError());
slouken@0
   257
		exit(1);
slouken@0
   258
	}
slouken@0
   259
	printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
slouken@0
   260
						"fullscreen" : "windowed");
slouken@0
   261
slouken@0
   262
	/* Enable printable characters */
slouken@0
   263
	SDL_EnableUNICODE(1);
slouken@0
   264
slouken@0
   265
	/* Set an event filter that discards everything but QUIT */
slouken@0
   266
	SDL_SetEventFilter(FilterEvents);
slouken@0
   267
slouken@0
   268
	/* Create the event handling threads */
slouken@0
   269
	mouse_thread = SDL_CreateThread(HandleMouse, NULL);
slouken@0
   270
	keybd_thread = SDL_CreateThread(HandleKeyboard, NULL);
slouken@0
   271
slouken@0
   272
	/* Set the surface pixels and refresh! */
slouken@0
   273
	for ( i=0; i<256; ++i ) {
slouken@0
   274
		palette[i].r = 255-i;
slouken@0
   275
		palette[i].g = 255-i;
slouken@0
   276
		palette[i].b = 255-i;
slouken@0
   277
	}
slouken@0
   278
	SDL_SetColors(screen, palette, 0, 256);
slouken@0
   279
	if ( SDL_LockSurface(screen) < 0 ) {
slouken@0
   280
		fprintf(stderr, "Couldn't lock display surface: %s\n",
slouken@0
   281
							SDL_GetError());
slouken@0
   282
		exit(2);
slouken@0
   283
	}
slouken@0
   284
	buffer = (Uint8 *)screen->pixels;
slouken@0
   285
	for ( i=0; i<screen->h; ++i ) {
slouken@0
   286
		memset(buffer,(i*255)/screen->h,
slouken@0
   287
				screen->w*screen->format->BytesPerPixel);
slouken@0
   288
		buffer += screen->pitch;
slouken@0
   289
	}
slouken@0
   290
	SDL_UnlockSurface(screen);
slouken@0
   291
	SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@0
   292
slouken@0
   293
	/* Loop, waiting for QUIT */
slouken@0
   294
	while ( ! done ) {
slouken@0
   295
		if ( ! (init_flags & SDL_INIT_EVENTTHREAD) ) {
slouken@0
   296
			SDL_PumpEvents(); /* Needed when event thread is off */
slouken@0
   297
		}
slouken@0
   298
		if ( SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_QUITMASK) ) {
slouken@0
   299
			done = 1;
slouken@0
   300
		}
slouken@0
   301
		/* Give up some CPU so the events can accumulate */
slouken@0
   302
		SDL_Delay(20);
slouken@0
   303
	}
slouken@0
   304
	SDL_WaitThread(mouse_thread, NULL);
slouken@0
   305
	SDL_WaitThread(keybd_thread, NULL);
slouken@0
   306
	return(0);
slouken@0
   307
}