1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * ©1995 Artsoft Development *
6 * 33659 Bielefeld-Senne *
7 * Telefon: (0521) 493245 *
8 * eMail: aeglos@valinor.owl.de *
9 * aeglos@uni-paderborn.de *
10 * q99492@pbhrzx.uni-paderborn.de *
11 *----------------------------------------------------------*
13 ***********************************************************/
18 /* ========================================================================= */
19 /* exported variables */
20 /* ========================================================================= */
22 struct ProgramInfo program;
23 struct OptionInfo options;
24 struct VideoSystemInfo video;
25 struct AudioSystemInfo audio;
28 Display *display = NULL;
29 Visual *visual = NULL;
33 DrawWindow window = NULL;
34 DrawBuffer backbuffer = NULL;
35 DrawBuffer drawto = NULL;
37 int button_status = MB_NOT_PRESSED;
38 boolean motion_status = FALSE;
40 int redraw_mask = REDRAW_NONE;
46 /* ========================================================================= */
48 /* ========================================================================= */
50 inline void InitProgramInfo(char *command_name, char *program_title,
51 char *window_title, char *icon_title,
52 char *x11_icon_filename,
53 char *x11_iconmask_filename,
54 char *msdos_pointer_filename)
56 program.command_name = command_name;
57 program.program_title = program_title;
58 program.window_title = window_title;
59 program.icon_title = icon_title;
60 program.x11_icon_filename = x11_icon_filename;
61 program.x11_iconmask_filename = x11_iconmask_filename;
62 program.msdos_pointer_filename = msdos_pointer_filename;
65 inline void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
66 int real_sx, int real_sy,
67 int full_sxsize, int full_sysize)
73 gfx.real_sx = real_sx;
74 gfx.real_sy = real_sy;
75 gfx.full_sxsize = full_sxsize;
76 gfx.full_sysize = full_sysize;
79 inline void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
87 inline void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
95 inline void InitGfxScrollbufferInfo(int scrollbuffer_width,
96 int scrollbuffer_height)
98 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
99 gfx.scrollbuffer_width = scrollbuffer_width;
100 gfx.scrollbuffer_height = scrollbuffer_height;
103 inline static int GetRealDepth(int depth)
105 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
108 inline void InitVideoDisplay(void)
111 SDLInitVideoDisplay();
113 X11InitVideoDisplay();
117 inline void InitVideoBuffer(DrawBuffer *backbuffer, DrawWindow *window,
118 int width, int height, int depth,
122 video.height = height;
123 video.depth = GetRealDepth(depth);
124 video.fullscreen_available = FULLSCREEN_STATUS;
125 video.fullscreen_enabled = FALSE;
128 SDLInitVideoBuffer(backbuffer, window, fullscreen);
130 X11InitVideoBuffer(backbuffer, window);
134 inline Bitmap CreateBitmapStruct(void)
137 return checked_calloc(sizeof(struct SDLSurfaceInfo));
139 return checked_calloc(sizeof(struct X11DrawableInfo));
143 inline Bitmap CreateBitmap(int width, int height, int depth)
145 Bitmap new_bitmap = CreateBitmapStruct();
146 int real_depth = GetRealDepth(depth);
149 SDL_Surface *surface_tmp, *surface_native;
151 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
152 real_depth, 0, 0, 0, 0))
154 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
156 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
157 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
159 SDL_FreeSurface(surface_tmp);
161 new_bitmap->surface = surface_native;
165 if (!(pixmap = XCreatePixmap(display, window->drawable,
166 width, height, real_depth)))
167 Error(ERR_EXIT, "cannot create pixmap");
168 new_bitmap->drawable = pixmap;
171 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
172 new_bitmap->gc = window->gc;
179 inline void FreeBitmap(Bitmap bitmap)
186 SDL_FreeSurface(bitmap->surface);
187 if (bitmap->surface_masked)
188 SDL_FreeSurface(bitmap->surface_masked);
190 if (bitmap->drawable)
191 XFreePixmap(display, bitmap->drawable);
192 if (bitmap->clip_mask)
193 XFreePixmap(display, bitmap->clip_mask);
194 if (bitmap->stored_clip_gc)
195 XFreeGC(display, bitmap->stored_clip_gc);
201 inline void CloseWindow(DrawWindow window)
204 if (window->drawable)
206 XUnmapWindow(display, window->drawable);
207 XDestroyWindow(display, window->drawable);
210 XFreeGC(display, window->gc);
214 inline void BlitBitmap(Bitmap src_bitmap, Bitmap dst_bitmap,
215 int src_x, int src_y,
216 int width, int height,
217 int dst_x, int dst_y)
220 SDLCopyArea(src_bitmap, dst_bitmap,
221 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
223 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
224 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
228 inline void ClearRectangle(Bitmap bitmap, int x, int y, int width, int height)
231 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
233 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
239 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
243 inline void SetClipMask(Bitmap bitmap, GC clip_gc, Pixmap clip_pixmap)
248 bitmap->clip_gc = clip_gc;
249 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
252 last_clip_gc = clip_gc;
257 inline void SetClipOrigin(Bitmap bitmap, GC clip_gc, int clip_x, int clip_y)
262 bitmap->clip_gc = clip_gc;
263 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
266 last_clip_gc = clip_gc;
271 inline void BlitBitmapMasked(Bitmap src_bitmap, Bitmap dst_bitmap,
272 int src_x, int src_y,
273 int width, int height,
274 int dst_x, int dst_y)
277 SDLCopyArea(src_bitmap, dst_bitmap,
278 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
280 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
281 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
285 inline void DrawSimpleWhiteLine(Bitmap bitmap, int from_x, int from_y,
289 SDLDrawSimpleLine(bitmap->surface, from_x, from_y, to_x, to_y, 0xffffff);
291 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
292 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
293 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
297 /* execute all pending screen drawing operations */
298 inline void FlushDisplay(void)
305 /* execute and wait for all pending screen drawing operations */
306 inline void SyncDisplay(void)
309 XSync(display, FALSE);
313 inline void KeyboardAutoRepeatOn(void)
316 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
317 SDL_DEFAULT_REPEAT_INTERVAL / 2);
318 SDL_EnableUNICODE(1);
320 XAutoRepeatOn(display);
324 inline void KeyboardAutoRepeatOff(void)
327 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
328 SDL_EnableUNICODE(0);
330 XAutoRepeatOff(display);
334 inline boolean PointerInWindow(DrawWindow window)
344 /* if XQueryPointer() returns False, the pointer
345 is not on the same screen as the specified window */
346 return XQueryPointer(display, window->drawable, &root, &child,
347 &root_x, &root_y, &win_x, &win_y, &mask);
351 inline boolean SetVideoMode(boolean fullscreen)
354 return SDLSetVideoMode(&backbuffer, fullscreen);
356 boolean success = TRUE;
358 if (fullscreen && video.fullscreen_available)
360 Error(ERR_WARN, "fullscreen not available in X11 version");
362 /* display error message only once */
363 video.fullscreen_available = FALSE;
372 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
375 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
376 (!fullscreen && video.fullscreen_enabled))
377 fullscreen = SetVideoMode(fullscreen);
384 /* ========================================================================= */
385 /* audio functions */
386 /* ========================================================================= */
388 inline boolean OpenAudio(struct AudioSystemInfo *audio)
390 audio->sound_available = FALSE;
391 audio->loops_available = FALSE;
392 audio->soundserver_pipe[0] = audio->soundserver_pipe[1] = 0;
393 audio->soundserver_pid = 0;
394 audio->device_name = NULL;
395 audio->device_fd = 0;
397 #if defined(TARGET_SDL)
400 audio->sound_available = TRUE;
401 audio->loops_available = TRUE;
403 #elif defined(PLATFORM_MSDOS)
404 if (MSDOSOpenAudio())
406 audio->sound_available = TRUE;
407 audio->loops_available = TRUE;
409 #elif defined(PLATFORM_UNIX)
410 UnixOpenAudio(audio);
413 return audio->sound_available;
416 inline void CloseAudio(struct AudioSystemInfo *audio)
418 #if defined(TARGET_SDL)
420 #elif defined(PLATFORM_MSDOS)
422 #elif defined(PLATFORM_UNIX)
423 UnixCloseAudio(audio);
426 audio->sound_available = FALSE;
427 audio->loops_available = FALSE;
431 /* ========================================================================= */
432 /* event functions */
433 /* ========================================================================= */
435 inline void InitEventFilter(EventFilter filter_function)
438 /* set event filter to filter out certain events */
439 SDL_SetEventFilter(filter_function);
443 inline boolean PendingEvent(void)
446 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
448 return (XPending(display) ? TRUE : FALSE);
452 inline void NextEvent(Event *event)
455 SDL_WaitEvent(event);
457 XNextEvent(display, event);
461 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
465 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
466 (int)event->keysym.unicode,
467 (int)event->keysym.sym,
468 (int)SDL_GetModState());
471 if (with_modifiers && event->keysym.unicode != 0)
472 return event->keysym.unicode;
474 return event->keysym.sym;
477 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
478 (int)XLookupKeysym(event, event->state),
479 (int)XLookupKeysym(event, 0));
483 return XLookupKeysym(event, event->state);
485 return XLookupKeysym(event, 0);
489 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
491 if (event->type != EVENT_CLIENTMESSAGE)
494 #if defined(TARGET_SDL)
495 return TRUE; /* the only possible message here is SDL_QUIT */
496 #elif defined(PLATFORM_UNIX)
497 if ((event->window == window->drawable) &&
498 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
506 inline void dummy(void)