Mercurial > sdl-ios-xcode
annotate test/testwm.c @ 582:16cc97387d52
Worked around objective C brokenness in Cygwin's automake
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 01 Feb 2003 18:58:42 +0000 |
parents | 3ef4bc90c388 |
children | 9c9598e4b904 |
rev | line source |
---|---|
0 | 1 |
2 /* Test out the window manager interaction functions */ | |
3 | |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 | |
8 #include "SDL.h" | |
9 | |
10 /* Is the cursor visible? */ | |
11 static int visible = 1; | |
12 | |
13 SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp) | |
14 { | |
15 SDL_Surface *icon; | |
16 Uint8 *pixels; | |
17 Uint8 *mask; | |
18 int mlen, i; | |
19 | |
20 *maskp = NULL; | |
21 | |
22 /* Load the icon surface */ | |
23 icon = SDL_LoadBMP(file); | |
24 if ( icon == NULL ) { | |
25 fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError()); | |
26 return(NULL); | |
27 } | |
28 | |
29 /* Check width and height */ | |
30 if ( (icon->w%8) != 0 ) { | |
31 fprintf(stderr, "Icon width must be a multiple of 8!\n"); | |
32 SDL_FreeSurface(icon); | |
33 return(NULL); | |
34 } | |
35 if ( icon->format->palette == NULL ) { | |
36 fprintf(stderr, "Icon must have a palette!\n"); | |
37 SDL_FreeSurface(icon); | |
38 return(NULL); | |
39 } | |
40 | |
41 /* Set the colorkey */ | |
42 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels)); | |
43 | |
44 /* Create the mask */ | |
45 pixels = (Uint8 *)icon->pixels; | |
46 printf("Transparent pixel: (%d,%d,%d)\n", | |
47 icon->format->palette->colors[*pixels].r, | |
48 icon->format->palette->colors[*pixels].g, | |
49 icon->format->palette->colors[*pixels].b); | |
50 mlen = icon->w*icon->h; | |
51 mask = (Uint8 *)malloc(mlen/8); | |
52 if ( mask == NULL ) { | |
53 fprintf(stderr, "Out of memory!\n"); | |
54 SDL_FreeSurface(icon); | |
55 return(NULL); | |
56 } | |
57 memset(mask, 0, mlen/8); | |
58 for ( i=0; i<mlen; ) { | |
59 if ( pixels[i] != *pixels ) | |
60 mask[i/8] |= 0x01; | |
61 ++i; | |
62 if ( (i%8) != 0 ) | |
63 mask[i/8] <<= 1; | |
64 } | |
65 *maskp = mask; | |
66 return(icon); | |
67 } | |
68 | |
69 void HotKey_ToggleFullScreen(void) | |
70 { | |
71 SDL_Surface *screen; | |
72 | |
73 screen = SDL_GetVideoSurface(); | |
74 if ( SDL_WM_ToggleFullScreen(screen) ) { | |
75 printf("Toggled fullscreen mode - now %s\n", | |
76 (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed"); | |
77 } else { | |
78 printf("Unable to toggle fullscreen mode\n"); | |
79 } | |
80 } | |
81 | |
82 void HotKey_ToggleGrab(void) | |
83 { | |
84 SDL_GrabMode mode; | |
85 | |
86 printf("Ctrl-G: toggling input grab!\n"); | |
87 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY); | |
88 if ( mode == SDL_GRAB_ON ) { | |
89 printf("Grab was on\n"); | |
90 } else { | |
91 printf("Grab was off\n"); | |
92 } | |
93 mode = SDL_WM_GrabInput(!mode); | |
94 if ( mode == SDL_GRAB_ON ) { | |
95 printf("Grab is now on\n"); | |
96 } else { | |
97 printf("Grab is now off\n"); | |
98 } | |
99 } | |
100 | |
101 void HotKey_Iconify(void) | |
102 { | |
103 printf("Ctrl-Z: iconifying window!\n"); | |
104 SDL_WM_IconifyWindow(); | |
105 } | |
106 | |
107 void HotKey_Quit(void) | |
108 { | |
109 SDL_Event event; | |
110 | |
111 printf("Posting internal quit request\n"); | |
112 event.type = SDL_USEREVENT; | |
113 SDL_PushEvent(&event); | |
114 } | |
115 | |
116 int FilterEvents(const SDL_Event *event) | |
117 { | |
118 static int reallyquit = 0; | |
119 | |
120 switch (event->type) { | |
121 | |
122 case SDL_ACTIVEEVENT: | |
123 /* See what happened */ | |
124 printf("App %s ", | |
125 event->active.gain ? "gained" : "lost"); | |
126 if ( event->active.state & SDL_APPACTIVE ) | |
127 printf("active "); | |
128 if ( event->active.state & SDL_APPMOUSEFOCUS ) | |
129 printf("mouse "); | |
130 if ( event->active.state & SDL_APPINPUTFOCUS ) | |
131 printf("input "); | |
132 printf("focus\n"); | |
133 | |
134 /* See if we are iconified or restored */ | |
135 if ( event->active.state & SDL_APPACTIVE ) { | |
136 printf("App has been %s\n", | |
137 event->active.gain ? | |
138 "restored" : "iconified"); | |
139 } | |
140 return(0); | |
141 | |
142 /* We want to toggle visibility on buttonpress */ | |
143 case SDL_MOUSEBUTTONDOWN: | |
144 case SDL_MOUSEBUTTONUP: | |
145 if ( event->button.state == SDL_PRESSED ) { | |
146 visible = !visible; | |
147 SDL_ShowCursor(visible); | |
148 } | |
149 printf("Mouse button %d has been %s\n", | |
150 event->button.button, | |
151 (event->button.state == SDL_PRESSED) ? | |
152 "pressed" : "released"); | |
153 return(0); | |
154 | |
155 /* Show relative mouse motion */ | |
156 case SDL_MOUSEMOTION: | |
157 #if 0 | |
158 printf("Mouse relative motion: {%d,%d}\n", | |
159 event->motion.xrel, event->motion.yrel); | |
160 #endif | |
161 return(0); | |
162 | |
163 case SDL_KEYDOWN: | |
164 if ( event->key.keysym.sym == SDLK_ESCAPE ) { | |
165 HotKey_Quit(); | |
166 } | |
167 if ( (event->key.keysym.sym == SDLK_g) && | |
168 (event->key.keysym.mod & KMOD_CTRL) ) { | |
169 HotKey_ToggleGrab(); | |
170 } | |
171 if ( (event->key.keysym.sym == SDLK_z) && | |
172 (event->key.keysym.mod & KMOD_CTRL) ) { | |
173 HotKey_Iconify(); | |
174 } | |
175 if ( (event->key.keysym.sym == SDLK_RETURN) && | |
176 (event->key.keysym.mod & KMOD_ALT) ) { | |
177 HotKey_ToggleFullScreen(); | |
178 } | |
179 return(0); | |
180 | |
181 /* Pass the video resize event through .. */ | |
182 case SDL_VIDEORESIZE: | |
183 return(1); | |
184 | |
185 /* This is important! Queue it if we want to quit. */ | |
186 case SDL_QUIT: | |
187 if ( ! reallyquit ) { | |
188 reallyquit = 1; | |
189 printf("Quit requested\n"); | |
190 return(0); | |
191 } | |
192 printf("Quit demanded\n"); | |
193 return(1); | |
194 | |
195 /* This will never happen because events queued directly | |
196 to the event queue are not filtered. | |
197 */ | |
198 case SDL_USEREVENT: | |
199 return(1); | |
200 | |
201 /* Drop all other events */ | |
202 default: | |
203 return(0); | |
204 } | |
205 } | |
206 | |
207 static Uint8 video_bpp; | |
208 static Uint32 video_flags; | |
209 | |
210 int SetVideoMode(int w, int h) | |
211 { | |
212 SDL_Surface *screen; | |
213 int i; | |
214 Uint8 *buffer; | |
215 SDL_Color palette[256]; | |
216 | |
217 screen = SDL_SetVideoMode(w, h, video_bpp, video_flags); | |
218 if ( screen == NULL ) { | |
219 fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", | |
220 w, h, video_bpp, SDL_GetError()); | |
221 return(-1); | |
222 } | |
223 printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ? | |
224 "fullscreen" : "windowed"); | |
225 | |
226 /* Set the surface pixels and refresh! */ | |
227 for ( i=0; i<256; ++i ) { | |
228 palette[i].r = 255-i; | |
229 palette[i].g = 255-i; | |
230 palette[i].b = 255-i; | |
231 } | |
232 SDL_SetColors(screen, palette, 0, 256); | |
233 if ( SDL_LockSurface(screen) < 0 ) { | |
234 fprintf(stderr, "Couldn't lock display surface: %s\n", | |
235 SDL_GetError()); | |
236 return(-1); | |
237 } | |
238 buffer = (Uint8 *)screen->pixels; | |
239 for ( i=0; i<screen->h; ++i ) { | |
240 memset(buffer,(i*255)/screen->h, | |
241 screen->w*screen->format->BytesPerPixel); | |
242 buffer += screen->pitch; | |
243 } | |
244 SDL_UnlockSurface(screen); | |
245 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
246 | |
247 return(0); | |
248 } | |
249 | |
250 int main(int argc, char *argv[]) | |
251 { | |
252 SDL_Event event; | |
253 char *title; | |
254 SDL_Surface *icon; | |
255 Uint8 *icon_mask; | |
256 int parsed; | |
87
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
257 int w, h; |
0 | 258 |
259 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { | |
260 fprintf(stderr, | |
261 "Couldn't initialize SDL: %s\n", SDL_GetError()); | |
262 exit(1); | |
263 } | |
264 atexit(SDL_Quit); | |
265 | |
266 /* Check command line arguments */ | |
87
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
267 w = 640; |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
268 h = 480; |
0 | 269 video_bpp = 8; |
270 video_flags = SDL_SWSURFACE; | |
271 parsed = 1; | |
272 while ( parsed ) { | |
273 if ( (argc >= 2) && (strcmp(argv[1], "-fullscreen") == 0) ) { | |
274 video_flags |= SDL_FULLSCREEN; | |
275 argc -= 1; | |
276 argv += 1; | |
277 } else | |
278 if ( (argc >= 2) && (strcmp(argv[1], "-resize") == 0) ) { | |
279 video_flags |= SDL_RESIZABLE; | |
280 argc -= 1; | |
281 argv += 1; | |
282 } else | |
283 if ( (argc >= 2) && (strcmp(argv[1], "-noframe") == 0) ) { | |
284 video_flags |= SDL_NOFRAME; | |
285 argc -= 1; | |
286 argv += 1; | |
287 } else | |
87
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
288 if ( (argc >= 3) && (strcmp(argv[1], "-width") == 0) ) { |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
289 w = atoi(argv[2]); |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
290 argc -= 2; |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
291 argv += 2; |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
292 } else |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
293 if ( (argc >= 3) && (strcmp(argv[1], "-height") == 0) ) { |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
294 h = atoi(argv[2]); |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
295 argc -= 2; |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
296 argv += 2; |
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
297 } else |
0 | 298 if ( (argc >= 3) && (strcmp(argv[1], "-bpp") == 0) ) { |
299 video_bpp = atoi(argv[2]); | |
300 argc -= 2; | |
301 argv += 2; | |
302 } else { | |
303 parsed = 0; | |
304 } | |
305 } | |
306 | |
307 /* Set the icon -- this must be done before the first mode set */ | |
308 icon = LoadIconSurface("icon.bmp", &icon_mask); | |
309 if ( icon != NULL ) { | |
310 SDL_WM_SetIcon(icon, icon_mask); | |
311 } | |
312 if ( icon_mask != NULL ) | |
313 free(icon_mask); | |
314 | |
315 /* Set the title bar */ | |
316 if ( argv[1] == NULL ) | |
317 title = "Testing 1.. 2.. 3..."; | |
318 else | |
319 title = argv[1]; | |
320 SDL_WM_SetCaption(title, "testwm"); | |
321 | |
322 /* See if it's really set */ | |
323 SDL_WM_GetCaption(&title, NULL); | |
324 if ( title ) | |
325 printf("Title was set to: %s\n", title); | |
326 else | |
327 printf("No window title was set!\n"); | |
328 | |
329 /* Initialize the display */ | |
87
3ef4bc90c388
Added -width and -height command line options
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
330 if ( SetVideoMode(w, h) < 0 ) { |
0 | 331 return(1); |
332 } | |
333 | |
334 /* Set an event filter that discards everything but QUIT */ | |
335 SDL_SetEventFilter(FilterEvents); | |
336 | |
337 /* Ignore key up events, they don't even get filtered */ | |
338 SDL_EventState(SDL_KEYUP, SDL_IGNORE); | |
339 | |
340 /* Loop, waiting for QUIT */ | |
341 while ( SDL_WaitEvent(&event) ) { | |
342 switch (event.type) { | |
343 case SDL_VIDEORESIZE: | |
344 printf("Got a resize event: %dx%d\n", | |
345 event.resize.w, event.resize.h); | |
346 SetVideoMode(event.resize.w, event.resize.h); | |
347 break; | |
348 case SDL_USEREVENT: | |
349 printf("Handling internal quit request\n"); | |
350 /* Fall through to the quit handler */ | |
351 case SDL_QUIT: | |
352 printf("Bye bye..\n"); | |
353 return(0); | |
354 default: | |
355 /* This should never happen */ | |
356 printf("Warning: Event %d wasn't filtered\n", | |
357 event.type); | |
358 break; | |
359 } | |
360 } | |
361 printf("SDL_WaitEvent() error: %s\n", SDL_GetError()); | |
362 return(255); | |
363 } |