rnd-20030106-1-src
[rocksndiamonds.git] / src / libgame / system.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * system.c                                                 *
12 ***********************************************************/
13
14 #include <string.h>
15 #include <signal.h>
16
17 #include "platform.h"
18
19 #if defined(PLATFORM_MSDOS)
20 #include <fcntl.h>
21 #endif
22
23 #include "system.h"
24 #include "sound.h"
25 #include "setup.h"
26 #include "joystick.h"
27 #include "misc.h"
28
29
30 /* ========================================================================= */
31 /* exported variables                                                        */
32 /* ========================================================================= */
33
34 struct ProgramInfo      program;
35 struct OptionInfo       options;
36 struct VideoSystemInfo  video;
37 struct AudioSystemInfo  audio;
38 struct GfxInfo          gfx;
39 struct FontInfo         font;
40 struct ArtworkInfo      artwork;
41 struct JoystickInfo     joystick;
42 struct SetupInfo        setup;
43
44 LevelDirTree           *leveldir_first = NULL;
45 LevelDirTree           *leveldir_current = NULL;
46 int                     level_nr;
47
48 Display                *display = NULL;
49 Visual                 *visual = NULL;
50 int                     screen = 0;
51 Colormap                cmap = None;
52
53 DrawWindow             *window = NULL;
54 DrawBuffer             *backbuffer = NULL;
55 DrawBuffer             *drawto = NULL;
56
57 int                     button_status = MB_NOT_PRESSED;
58 boolean                 motion_status = FALSE;
59
60 int                     redraw_mask = REDRAW_NONE;
61 int                     redraw_tiles = 0;
62
63 int                     FrameCounter = 0;
64
65
66 /* ========================================================================= */
67 /* init/close functions                                                      */
68 /* ========================================================================= */
69
70 void InitCommandName(char *argv0)
71 {
72   program.command_basename =
73     (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
74 }
75
76 void InitExitFunction(void (*exit_function)(int))
77 {
78   program.exit_function = exit_function;
79
80   /* set signal handlers to custom exit function */
81   signal(SIGINT, exit_function);
82   signal(SIGTERM, exit_function);
83
84 #if defined(TARGET_SDL)
85   /* set exit function to automatically cleanup SDL stuff after exit() */
86   atexit(SDL_Quit);
87 #endif
88 }
89
90 void InitPlatformDependantStuff(void)
91 {
92 #if defined(PLATFORM_MSDOS)
93   _fmode = O_BINARY;
94 #endif
95
96 #if !defined(PLATFORM_UNIX)
97   program.userdata_directory = "userdata";
98 #endif
99
100 #if defined(PLATFORM_MSDOS)
101   initErrorFile();
102 #endif
103
104 #if defined(TARGET_SDL)
105   if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
106     Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
107 #endif
108 }
109
110 void ClosePlatformDependantStuff(void)
111 {
112 #if defined(PLATFORM_MSDOS)
113   dumpErrorFile();
114 #endif
115 }
116
117 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
118                      char *window_title, char *icon_title,
119                      char *x11_icon_filename, char *x11_iconmask_filename,
120                      char *msdos_pointer_filename,
121                      char *cookie_prefix, char *filename_prefix,
122                      int program_version)
123 {
124 #if defined(PLATFORM_UNIX)
125   program.userdata_directory = unix_userdata_directory;
126 #else
127   program.userdata_directory = "userdata";
128 #endif
129
130   program.program_title = program_title;
131   program.window_title = window_title;
132   program.icon_title = icon_title;
133   program.x11_icon_filename = x11_icon_filename;
134   program.x11_iconmask_filename = x11_iconmask_filename;
135   program.msdos_pointer_filename = msdos_pointer_filename;
136
137   program.cookie_prefix = cookie_prefix;
138   program.filename_prefix = filename_prefix;
139
140   program.version_major = VERSION_MAJOR(program_version);
141   program.version_minor = VERSION_MINOR(program_version);
142   program.version_patch = VERSION_PATCH(program_version);
143 }
144
145 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
146                       int real_sx, int real_sy,
147                       int full_sxsize, int full_sysize)
148 {
149   gfx.sx = sx;
150   gfx.sy = sy;
151   gfx.sxsize = sxsize;
152   gfx.sysize = sysize;
153   gfx.real_sx = real_sx;
154   gfx.real_sy = real_sy;
155   gfx.full_sxsize = full_sxsize;
156   gfx.full_sysize = full_sysize;
157
158   gfx.background_bitmap = NULL;
159   gfx.background_bitmap_mask = REDRAW_NONE;
160
161   SetDrawDeactivationMask(REDRAW_NONE);         /* do not deactivate drawing */
162   SetDrawBackgroundMask(REDRAW_NONE);           /* deactivate masked drawing */
163 }
164
165 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
166 {
167   gfx.dx = dx;
168   gfx.dy = dy;
169   gfx.dxsize = dxsize;
170   gfx.dysize = dysize;
171 }
172
173 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
174 {
175   gfx.vx = vx;
176   gfx.vy = vy;
177   gfx.vxsize = vxsize;
178   gfx.vysize = vysize;
179 }
180
181 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
182 {
183   /* currently only used by MSDOS code to alloc VRAM buffer, if available */
184   gfx.scrollbuffer_width = scrollbuffer_width;
185   gfx.scrollbuffer_height = scrollbuffer_height;
186 }
187
188 void SetDrawDeactivationMask(int draw_deactivation_mask)
189 {
190   gfx.draw_deactivation_mask = draw_deactivation_mask;
191 }
192
193 void SetDrawBackgroundMask(int draw_background_mask)
194 {
195   gfx.draw_background_mask = draw_background_mask;
196 }
197
198 static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
199                                int dest_x, int dest_y, int width, int height)
200 {
201   int bitmap_xsize = width;
202   int bitmap_ysize = height;
203   int tile_xsize = tile->width;
204   int tile_ysize = tile->height;
205   int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
206   int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
207   int x, y;
208
209   for (y=0; y < tile_ysteps; y++)
210   {
211     for (x=0; x < tile_xsteps; x++)
212     {
213       int draw_x = dest_x + x * tile_xsize;
214       int draw_y = dest_y + y * tile_ysize;
215       int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
216       int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
217
218       BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
219     }
220   }
221 }
222
223 void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
224 {
225   static Bitmap *main_bitmap_tile = NULL;
226   static Bitmap *door_bitmap_tile = NULL;
227
228   if (mask == REDRAW_FIELD)
229   {
230     if (background_bitmap_tile == main_bitmap_tile)
231       return;           /* main background tile has not changed */
232
233     main_bitmap_tile = background_bitmap_tile;
234   }
235   else if (mask == REDRAW_DOOR_1)
236   {
237     if (background_bitmap_tile == door_bitmap_tile)
238       return;   /* main background tile has not changed */
239
240     door_bitmap_tile = background_bitmap_tile;
241   }
242   else          /* should not happen */
243     return;
244
245   if (background_bitmap_tile)
246     gfx.background_bitmap_mask |= mask;
247   else
248     gfx.background_bitmap_mask &= ~mask;
249
250   if (gfx.background_bitmap == NULL)
251     gfx.background_bitmap = CreateBitmap(video.width, video.height,
252                                          DEFAULT_DEPTH);
253
254   if (background_bitmap_tile == NULL)   /* empty background requested */
255     return;
256
257   if (mask == REDRAW_FIELD)
258     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
259                        gfx.real_sx, gfx.real_sy,
260                        gfx.full_sxsize, gfx.full_sysize);
261   else
262     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
263                        gfx.dx, gfx.dy,
264                        gfx.dxsize, gfx.dysize);
265 }
266
267 void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
268 {
269   SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
270 }
271
272 void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
273 {
274   SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
275 }
276
277
278 /* ========================================================================= */
279 /* video functions                                                           */
280 /* ========================================================================= */
281
282 inline static int GetRealDepth(int depth)
283 {
284   return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
285 }
286
287 inline void InitVideoDisplay(void)
288 {
289 #if defined(TARGET_SDL)
290   SDLInitVideoDisplay();
291 #else
292   X11InitVideoDisplay();
293 #endif
294 }
295
296 inline void CloseVideoDisplay(void)
297 {
298   KeyboardAutoRepeatOn();
299
300 #if defined(TARGET_SDL)
301   SDL_QuitSubSystem(SDL_INIT_VIDEO);
302 #else
303
304   if (display)
305     XCloseDisplay(display);
306 #endif
307 }
308
309 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
310                             int width, int height, int depth,
311                             boolean fullscreen)
312 {
313   video.width = width;
314   video.height = height;
315   video.depth = GetRealDepth(depth);
316   video.fullscreen_available = FULLSCREEN_STATUS;
317   video.fullscreen_enabled = FALSE;
318
319 #ifdef TARGET_SDL
320   SDLInitVideoBuffer(backbuffer, window, fullscreen);
321 #else
322   X11InitVideoBuffer(backbuffer, window);
323 #endif
324 }
325
326 inline Bitmap *CreateBitmapStruct(void)
327 {
328 #ifdef TARGET_SDL
329   return checked_calloc(sizeof(struct SDLSurfaceInfo));
330 #else
331   return checked_calloc(sizeof(struct X11DrawableInfo));
332 #endif
333 }
334
335 inline Bitmap *CreateBitmap(int width, int height, int depth)
336 {
337   Bitmap *new_bitmap = CreateBitmapStruct();
338   int real_depth = GetRealDepth(depth);
339
340 #ifdef TARGET_SDL
341   SDL_Surface *surface_tmp, *surface_native;
342
343   if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
344                                           real_depth, 0, 0, 0, 0))
345       == NULL)
346     Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
347
348   if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
349     Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
350
351   SDL_FreeSurface(surface_tmp);
352
353   new_bitmap->surface = surface_native;
354 #else
355   Pixmap pixmap;
356
357   if ((pixmap = XCreatePixmap(display, window->drawable,
358                               width, height, real_depth))
359       == None)
360     Error(ERR_EXIT, "cannot create pixmap");
361
362   new_bitmap->drawable = pixmap;
363
364   if (window == NULL)
365     Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
366
367   new_bitmap->gc = window->gc;
368
369   new_bitmap->line_gc[0] = window->line_gc[0];
370   new_bitmap->line_gc[1] = window->line_gc[1];
371 #endif
372
373   new_bitmap->width = width;
374   new_bitmap->height = height;
375
376   return new_bitmap;
377 }
378
379 inline static void FreeBitmapPointers(Bitmap *bitmap)
380 {
381   if (bitmap == NULL)
382     return;
383
384 #ifdef TARGET_SDL
385   if (bitmap->surface)
386     SDL_FreeSurface(bitmap->surface);
387   if (bitmap->surface_masked)
388     SDL_FreeSurface(bitmap->surface_masked);
389   bitmap->surface = NULL;
390   bitmap->surface_masked = NULL;
391 #else
392   /* The X11 version seems to have a memory leak here -- although
393      "XFreePixmap()" is called, the correspondig memory seems not
394      to be freed (according to "ps"). The SDL version apparently
395      does not have this problem. */
396
397   if (bitmap->drawable)
398     XFreePixmap(display, bitmap->drawable);
399   if (bitmap->clip_mask)
400     XFreePixmap(display, bitmap->clip_mask);
401   if (bitmap->stored_clip_gc)
402     XFreeGC(display, bitmap->stored_clip_gc);
403   /* the other GCs are only pointers to GCs used elsewhere */
404   bitmap->drawable = None;
405   bitmap->clip_mask = None;
406   bitmap->stored_clip_gc = None;
407 #endif
408
409   if (bitmap->source_filename)
410     free(bitmap->source_filename);
411   bitmap->source_filename = NULL;
412 }
413
414 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
415                                           Bitmap *dst_bitmap)
416 {
417   if (src_bitmap == NULL || dst_bitmap == NULL)
418     return;
419
420   FreeBitmapPointers(dst_bitmap);
421
422   *dst_bitmap = *src_bitmap;
423 }
424
425 inline void FreeBitmap(Bitmap *bitmap)
426 {
427   if (bitmap == NULL)
428     return;
429
430   FreeBitmapPointers(bitmap);
431
432   free(bitmap);
433 }
434
435 inline void CloseWindow(DrawWindow *window)
436 {
437 #ifdef TARGET_X11
438   if (window->drawable)
439   {
440     XUnmapWindow(display, window->drawable);
441     XDestroyWindow(display, window->drawable);
442   }
443   if (window->gc)
444     XFreeGC(display, window->gc);
445 #endif
446 }
447
448 static inline boolean CheckDrawingArea(int x, int y, int width, int height,
449                                        int draw_mask)
450 {
451   if (draw_mask == REDRAW_NONE)
452     return FALSE;
453
454   if (draw_mask & REDRAW_ALL)
455     return TRUE;
456
457   if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
458     return TRUE;
459
460   if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
461     return TRUE;
462
463   if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
464     return TRUE;
465
466   return FALSE;
467 }
468
469 inline boolean DrawingDeactivated(int x, int y, int width, int height)
470 {
471   return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
472 }
473
474 inline boolean DrawingOnBackground(int x, int y)
475 {
476   return ((gfx.draw_background_mask & gfx.background_bitmap_mask) &&
477           CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
478 }
479
480 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
481                        int src_x, int src_y,
482                        int width, int height,
483                        int dst_x, int dst_y)
484 {
485   if (DrawingDeactivated(dst_x, dst_y, width, height))
486     return;
487
488 #ifdef TARGET_SDL
489   SDLCopyArea(src_bitmap, dst_bitmap,
490               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
491 #else
492   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
493             dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
494 #endif
495 }
496
497 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
498 {
499   if (DrawingDeactivated(x, y, width, height))
500     return;
501
502 #ifdef TARGET_SDL
503   SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
504 #else
505   XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
506 #endif
507 }
508
509 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
510                                        int width, int height)
511 {
512   if (DrawingOnBackground(x, y))
513     BlitBitmap(gfx.background_bitmap, bitmap,
514                x - gfx.real_sx, y - gfx.real_sy, width, height, x, y);
515   else
516     ClearRectangle(bitmap, x, y, width, height);
517 }
518
519 #if 0
520 #ifndef TARGET_SDL
521 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
522 #endif
523 #endif
524
525 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
526 {
527 #ifdef TARGET_X11
528   if (clip_gc)
529   {
530     bitmap->clip_gc = clip_gc;
531     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
532   }
533 #if 0
534   last_clip_gc = clip_gc;
535 #endif
536 #endif
537 }
538
539 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
540 {
541 #ifdef TARGET_X11
542   if (clip_gc)
543   {
544     bitmap->clip_gc = clip_gc;
545     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
546   }
547 #if 0
548   last_clip_gc = clip_gc;
549 #endif
550 #endif
551 }
552
553 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
554                              int src_x, int src_y,
555                              int width, int height,
556                              int dst_x, int dst_y)
557 {
558   if (DrawingDeactivated(dst_x, dst_y, width, height))
559     return;
560
561 #ifdef TARGET_SDL
562   SDLCopyArea(src_bitmap, dst_bitmap,
563               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
564 #else
565   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
566             src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
567 #endif
568 }
569
570 inline void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
571                                    int src_x, int src_y,
572                                    int width, int height,
573                                    int dst_x, int dst_y)
574 {
575   if (DrawingOnBackground(src_x, src_y))
576   {
577     /* draw background */
578     BlitBitmap(gfx.background_bitmap, dst_bitmap, dst_x, dst_y, width, height,
579                dst_x, dst_y);
580
581     /* draw foreground */
582     SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
583                   dst_x - src_x, dst_y - src_y);
584     BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
585                      dst_x, dst_y);
586   }
587   else
588     BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
589                dst_x, dst_y);
590 }
591
592 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
593                                 int to_x, int to_y)
594 {
595 #ifdef TARGET_SDL
596   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
597 #else
598   XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
599   XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
600   XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
601 #endif
602 }
603
604 #if !defined(TARGET_X11_NATIVE)
605 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
606                      int to_x, int to_y, Pixel pixel, int line_width)
607 {
608   int x, y;
609
610   for (x=0; x<line_width; x++)
611   {
612     for (y=0; y<line_width; y++)
613     {
614       int dx = x - line_width / 2;
615       int dy = y - line_width / 2;
616
617       if ((x == 0 && y == 0) ||
618           (x == 0 && y == line_width - 1) ||
619           (x == line_width - 1 && y == 0) ||
620           (x == line_width - 1 && y == line_width - 1))
621         continue;
622
623 #if defined(TARGET_SDL)
624       SDLDrawLine(bitmap,
625                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
626 #elif defined(TARGET_ALLEGRO)
627       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
628                       to_x + dx, to_y + dy, pixel);
629 #endif
630     }
631   }
632 }
633 #endif
634
635 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
636                       Pixel pixel)
637 {
638 #if !defined(TARGET_X11_NATIVE)
639   int line_width = 4;
640   int i;
641
642   for (i=0; i<num_points - 1; i++)
643     DrawLine(bitmap, points[i].x, points[i].y,
644              points[i + 1].x, points[i + 1].y, pixel, line_width);
645
646   /*
647   SDLDrawLines(bitmap->surface, points, num_points, pixel);
648   */
649 #else
650   XSetForeground(display, bitmap->line_gc[1], pixel);
651   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
652              (XPoint *)points, num_points, CoordModeOrigin);
653   /*
654   XSetForeground(display, gc, BlackPixel(display, screen));
655   */
656 #endif
657 }
658
659 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
660 {
661 #if defined(TARGET_SDL)
662   return SDLGetPixel(bitmap, x, y);
663 #elif defined(TARGET_ALLEGRO)
664   return AllegroGetPixel(bitmap->drawable, x, y);
665 #else
666   unsigned long pixel_value;
667   XImage *pixel_image;
668
669   pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
670                           AllPlanes, ZPixmap);
671   pixel_value = XGetPixel(pixel_image, 0, 0);
672
673   XDestroyImage(pixel_image);
674
675   return pixel_value;
676 #endif
677 }
678
679 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
680                              unsigned int color_g, unsigned int color_b)
681 {
682   Pixel pixel;
683
684 #if defined(TARGET_SDL)
685   pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
686 #elif defined(TARGET_ALLEGRO)
687   pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
688 #elif defined(TARGET_X11_NATIVE)
689   XColor xcolor;
690
691   xcolor.flags = DoRed | DoGreen | DoBlue;
692   xcolor.red = (color_r << 8);
693   xcolor.green = (color_g << 8);
694   xcolor.blue = (color_b << 8);
695   XAllocColor(display, cmap, &xcolor);
696   pixel = xcolor.pixel;
697 #endif
698
699   return pixel;
700 }
701
702 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
703 {
704   unsigned int color_r = (color >> 16) & 0xff;
705   unsigned int color_g = (color >>  8) & 0xff;
706   unsigned int color_b = (color >>  0) & 0xff;
707
708   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
709 }
710
711 /* execute all pending screen drawing operations */
712 inline void FlushDisplay(void)
713 {
714 #ifndef TARGET_SDL
715   XFlush(display);
716 #endif
717 }
718
719 /* execute and wait for all pending screen drawing operations */
720 inline void SyncDisplay(void)
721 {
722 #ifndef TARGET_SDL
723   XSync(display, FALSE);
724 #endif
725 }
726
727 inline void KeyboardAutoRepeatOn(void)
728 {
729 #ifdef TARGET_SDL
730   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
731                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
732   SDL_EnableUNICODE(1);
733 #else
734   if (display)
735     XAutoRepeatOn(display);
736 #endif
737 }
738
739 inline void KeyboardAutoRepeatOff(void)
740 {
741 #ifdef TARGET_SDL
742   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
743   SDL_EnableUNICODE(0);
744 #else
745   if (display)
746     XAutoRepeatOff(display);
747 #endif
748 }
749
750 inline boolean PointerInWindow(DrawWindow *window)
751 {
752 #ifdef TARGET_SDL
753   return TRUE;
754 #else
755   Window root, child;
756   int root_x, root_y;
757   unsigned int mask;
758   int win_x, win_y;
759
760   /* if XQueryPointer() returns False, the pointer
761      is not on the same screen as the specified window */
762   return XQueryPointer(display, window->drawable, &root, &child,
763                        &root_x, &root_y, &win_x, &win_y, &mask);
764 #endif
765 }
766
767 inline boolean SetVideoMode(boolean fullscreen)
768 {
769 #ifdef TARGET_SDL
770   return SDLSetVideoMode(&backbuffer, fullscreen);
771 #else
772   boolean success = TRUE;
773
774   if (fullscreen && video.fullscreen_available)
775   {
776     Error(ERR_WARN, "fullscreen not available in X11 version");
777
778     /* display error message only once */
779     video.fullscreen_available = FALSE;
780
781     success = FALSE;
782   }
783
784   return success;
785 #endif
786 }
787
788 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
789 {
790 #ifdef TARGET_SDL
791   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
792       (!fullscreen && video.fullscreen_enabled))
793     fullscreen = SetVideoMode(fullscreen);
794 #endif
795
796   return fullscreen;
797 }
798
799 Bitmap *LoadImage(char *filename)
800 {
801   Bitmap *new_bitmap;
802
803 #if defined(TARGET_SDL)
804   new_bitmap = SDLLoadImage(filename);
805 #else
806   new_bitmap = X11LoadImage(filename);
807 #endif
808
809   if (new_bitmap)
810     new_bitmap->source_filename = getStringCopy(filename);
811
812   return new_bitmap;
813 }
814
815 Bitmap *LoadCustomImage(char *basename)
816 {
817   char *filename = getCustomImageFilename(basename);
818   Bitmap *new_bitmap;
819
820   if (filename == NULL)
821     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
822
823   if ((new_bitmap = LoadImage(filename)) == NULL)
824     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
825
826   return new_bitmap;
827 }
828
829 void ReloadCustomImage(Bitmap *bitmap, char *basename)
830 {
831   char *filename = getCustomImageFilename(basename);
832   Bitmap *new_bitmap;
833
834   if (filename == NULL)         /* (should never happen) */
835   {
836     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
837     return;
838   }
839
840   if (strcmp(filename, bitmap->source_filename) == 0)
841   {
842     /* The old and new image are the same (have the same filename and path).
843        This usually means that this image does not exist in this graphic set
844        and a fallback to the existing image is done. */
845
846     return;
847   }
848
849   if ((new_bitmap = LoadImage(filename)) == NULL)
850   {
851     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
852     return;
853   }
854
855   if (bitmap->width != new_bitmap->width ||
856       bitmap->height != new_bitmap->height)
857   {
858     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
859           filename);
860     FreeBitmap(new_bitmap);
861     return;
862   }
863
864   TransferBitmapPointers(new_bitmap, bitmap);
865   free(new_bitmap);
866 }
867
868
869 /* ========================================================================= */
870 /* audio functions                                                           */
871 /* ========================================================================= */
872
873 inline void OpenAudio(void)
874 {
875   /* always start with reliable default values */
876   audio.sound_available = FALSE;
877   audio.music_available = FALSE;
878   audio.loops_available = FALSE;
879
880   audio.sound_enabled = FALSE;
881   audio.sound_deactivated = FALSE;
882
883   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
884   audio.mixer_pid = -1;
885   audio.device_name = NULL;
886   audio.device_fd = -1;
887
888   audio.num_channels = 0;
889   audio.music_channel = 0;
890   audio.first_sound_channel = 0;
891
892 #if defined(TARGET_SDL)
893   SDLOpenAudio();
894 #elif defined(PLATFORM_UNIX)
895   UnixOpenAudio();
896 #elif defined(PLATFORM_MSDOS)
897   MSDOSOpenAudio();
898 #endif
899 }
900
901 inline void CloseAudio(void)
902 {
903 #if defined(TARGET_SDL)
904   SDLCloseAudio();
905 #elif defined(PLATFORM_UNIX)
906   UnixCloseAudio();
907 #elif defined(PLATFORM_MSDOS)
908   MSDOSCloseAudio();
909 #endif
910
911   audio.sound_enabled = FALSE;
912 }
913
914 inline void SetAudioMode(boolean enabled)
915 {
916   if (!audio.sound_available)
917     return;
918
919   audio.sound_enabled = enabled;
920 }
921
922
923 /* ========================================================================= */
924 /* event functions                                                           */
925 /* ========================================================================= */
926
927 inline void InitEventFilter(EventFilter filter_function)
928 {
929 #ifdef TARGET_SDL
930   /* set event filter to filter out certain events */
931   SDL_SetEventFilter(filter_function);
932 #endif
933 }
934
935 inline boolean PendingEvent(void)
936 {
937 #ifdef TARGET_SDL
938   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
939 #else
940   return (XPending(display) ? TRUE : FALSE);
941 #endif
942 }
943
944 inline void NextEvent(Event *event)
945 {
946 #ifdef TARGET_SDL
947   SDLNextEvent(event);
948 #else
949   XNextEvent(display, event);
950 #endif
951 }
952
953 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
954 {
955 #ifdef TARGET_SDL
956 #if 0
957   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
958          (int)event->keysym.unicode,
959          (int)event->keysym.sym,
960          (int)SDL_GetModState());
961 #endif
962
963   if (with_modifiers &&
964       event->keysym.unicode > 0x0000 &&
965       event->keysym.unicode < 0x2000)
966     return event->keysym.unicode;
967   else
968     return event->keysym.sym;
969 #else
970 #if 0
971   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
972          (int)XLookupKeysym(event, event->state),
973          (int)XLookupKeysym(event, 0));
974 #endif
975
976   if (with_modifiers)
977     return XLookupKeysym(event, event->state);
978   else
979     return XLookupKeysym(event, 0);
980 #endif
981 }
982
983 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
984 {
985   if (event->type != EVENT_CLIENTMESSAGE)
986     return FALSE;
987
988 #if defined(TARGET_SDL)
989   return TRUE;          /* the only possible message here is SDL_QUIT */
990 #elif defined(PLATFORM_UNIX)
991   if ((event->window == window->drawable) &&
992       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
993     return TRUE;
994 #endif
995
996   return FALSE;
997 }
998
999
1000 /* ========================================================================= */
1001 /* joystick functions                                                        */
1002 /* ========================================================================= */
1003
1004 inline void InitJoysticks()
1005 {
1006   int i;
1007
1008 #ifdef NO_JOYSTICK
1009   return;       /* joysticks generally deactivated by compile-time directive */
1010 #endif
1011
1012   /* always start with reliable default values */
1013   joystick.status = JOYSTICK_NOT_AVAILABLE;
1014   for (i=0; i<MAX_PLAYERS; i++)
1015     joystick.fd[i] = -1;                /* joystick device closed */
1016
1017 #if defined(TARGET_SDL)
1018   SDLInitJoysticks();
1019 #elif defined(PLATFORM_UNIX)
1020   UnixInitJoysticks();
1021 #elif defined(PLATFORM_MSDOS)
1022   MSDOSInitJoysticks();
1023 #endif
1024 }
1025
1026 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1027 {
1028 #if defined(TARGET_SDL)
1029   return SDLReadJoystick(nr, x, y, b1, b2);
1030 #elif defined(PLATFORM_UNIX)
1031   return UnixReadJoystick(nr, x, y, b1, b2);
1032 #elif defined(PLATFORM_MSDOS)
1033   return MSDOSReadJoystick(nr, x, y, b1, b2);
1034 #endif
1035 }