Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
341 lines (303 loc) · 9.79 KB

threadwin.c

File metadata and controls

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