author | Radek Brich <radek.brich@devl.cz> |
Sat, 15 Mar 2014 15:28:20 +0100 | |
changeset 85 | 6828c5b16087 |
parent 72 | 6e0656600754 |
permissions | -rw-r--r-- |
72
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
#include <SDL/SDL.h> |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
Uint32 timeout_callback(Uint32 interval, void *param) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
SDL_Event event; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
event.type = SDL_USEREVENT; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
event.user.code = 1; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
SDL_PushEvent(&event); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
return 0; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
bool handle_events(Uint32 timeout) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
SDL_Event event; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
SDL_TimerID timer_id = NULL; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
bool done = false; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
if (timeout) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
timer_id = SDL_AddTimer(timeout, timeout_callback, NULL); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
// wait for events and handle them |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
if (SDL_WaitEvent(&event)) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
do |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
switch (event.type) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
case SDL_USEREVENT: |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
// timeout |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
if (event.user.code == 1) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
SDL_RemoveTimer(timer_id); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
printf("timeout\n"); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
break; // continue loop |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
case SDL_QUIT: |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
printf("quit\n"); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
done = true; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
break; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
case SDL_KEYUP: |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
printf("keyup\n"); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
if (event.key.keysym.sym == SDLK_ESCAPE) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
done = true; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
break; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
case SDL_MOUSEBUTTONDOWN: |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
printf("mouse button down\n"); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
break; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
case SDL_MOUSEBUTTONUP: |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
printf("mouse button up\n"); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
break; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
while (SDL_PollEvent(&event)); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
// remove timer when other event came before timeout |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
if (timeout) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
SDL_RemoveTimer(timer_id); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
return done; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
int main(int argc, char **argv) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
SDL_Surface* screen = NULL; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
fprintf(stderr, "SDL_Init: %s\n", SDL_GetError()); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
exit(1); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
atexit(SDL_Quit); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
screen = SDL_SetVideoMode(800, 480, 16, SDL_SWSURFACE); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
if (screen == NULL) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
exit(1); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
bool done = false; |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
while (!done) |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
{ |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
done = handle_events(1000); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
} |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
|
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
return (0); |
6e0656600754
DriverSDL: Fix event dispatching. Add SDL events test.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
} |