rnd-20030128-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 AnimInfo         anim;
41 struct ArtworkInfo      artwork;
42 struct JoystickInfo     joystick;
43 struct SetupInfo        setup;
44
45 LevelDirTree           *leveldir_first = NULL;
46 LevelDirTree           *leveldir_current = NULL;
47 int                     level_nr;
48
49 Display                *display = NULL;
50 Visual                 *visual = NULL;
51 int                     screen = 0;
52 Colormap                cmap = None;
53
54 DrawWindow             *window = NULL;
55 DrawBuffer             *backbuffer = NULL;
56 DrawBuffer             *drawto = NULL;
57
58 int                     button_status = MB_NOT_PRESSED;
59 boolean                 motion_status = FALSE;
60
61 int                     redraw_mask = REDRAW_NONE;
62 int                     redraw_tiles = 0;
63
64 int                     FrameCounter = 0;
65
66
67 /* ========================================================================= */
68 /* init/close functions                                                      */
69 /* ========================================================================= */
70
71 void InitCommandName(char *argv0)
72 {
73   program.command_basename =
74     (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
75 }
76
77 void InitExitFunction(void (*exit_function)(int))
78 {
79   program.exit_function = exit_function;
80
81   /* set signal handlers to custom exit function */
82   signal(SIGINT, exit_function);
83   signal(SIGTERM, exit_function);
84
85 #if defined(TARGET_SDL)
86   /* set exit function to automatically cleanup SDL stuff after exit() */
87   atexit(SDL_Quit);
88 #endif
89 }
90
91 void InitPlatformDependantStuff(void)
92 {
93 #if defined(PLATFORM_MSDOS)
94   _fmode = O_BINARY;
95 #endif
96
97 #if !defined(PLATFORM_UNIX)
98   program.userdata_directory = "userdata";
99 #endif
100
101 #if defined(PLATFORM_MSDOS)
102   initErrorFile();
103 #endif
104
105 #if defined(TARGET_SDL)
106   if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
107     Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
108 #endif
109 }
110
111 void ClosePlatformDependantStuff(void)
112 {
113 #if defined(PLATFORM_MSDOS)
114   dumpErrorFile();
115 #endif
116 }
117
118 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
119                      char *window_title, char *icon_title,
120                      char *x11_icon_filename, char *x11_iconmask_filename,
121                      char *msdos_pointer_filename,
122                      char *cookie_prefix, char *filename_prefix,
123                      int program_version)
124 {
125 #if defined(PLATFORM_UNIX)
126   program.userdata_directory = unix_userdata_directory;
127 #else
128   program.userdata_directory = "userdata";
129 #endif
130
131   program.program_title = program_title;
132   program.window_title = window_title;
133   program.icon_title = icon_title;
134   program.x11_icon_filename = x11_icon_filename;
135   program.x11_iconmask_filename = x11_iconmask_filename;
136   program.msdos_pointer_filename = msdos_pointer_filename;
137
138   program.cookie_prefix = cookie_prefix;
139   program.filename_prefix = filename_prefix;
140
141   program.version_major = VERSION_MAJOR(program_version);
142   program.version_minor = VERSION_MINOR(program_version);
143   program.version_patch = VERSION_PATCH(program_version);
144 }
145
146 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
147                       int real_sx, int real_sy,
148                       int full_sxsize, int full_sysize)
149 {
150   gfx.sx = sx;
151   gfx.sy = sy;
152   gfx.sxsize = sxsize;
153   gfx.sysize = sysize;
154   gfx.real_sx = real_sx;
155   gfx.real_sy = real_sy;
156   gfx.full_sxsize = full_sxsize;
157   gfx.full_sysize = full_sysize;
158
159   gfx.background_bitmap = NULL;
160   gfx.background_bitmap_mask = REDRAW_NONE;
161
162   SetDrawDeactivationMask(REDRAW_NONE);         /* do not deactivate drawing */
163   SetDrawBackgroundMask(REDRAW_NONE);           /* deactivate masked drawing */
164 }
165
166 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
167 {
168   gfx.dx = dx;
169   gfx.dy = dy;
170   gfx.dxsize = dxsize;
171   gfx.dysize = dysize;
172 }
173
174 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
175 {
176   gfx.vx = vx;
177   gfx.vy = vy;
178   gfx.vxsize = vxsize;
179   gfx.vysize = vysize;
180 }
181
182 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
183 {
184   /* currently only used by MSDOS code to alloc VRAM buffer, if available */
185   gfx.scrollbuffer_width = scrollbuffer_width;
186   gfx.scrollbuffer_height = scrollbuffer_height;
187 }
188
189 void SetDrawDeactivationMask(int draw_deactivation_mask)
190 {
191   gfx.draw_deactivation_mask = draw_deactivation_mask;
192 }
193
194 void SetDrawBackgroundMask(int draw_background_mask)
195 {
196   gfx.draw_background_mask = draw_background_mask;
197 }
198
199 static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
200                                int dest_x, int dest_y, int width, int height)
201 {
202   int bitmap_xsize = width;
203   int bitmap_ysize = height;
204   int tile_xsize = tile->width;
205   int tile_ysize = tile->height;
206   int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
207   int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
208   int x, y;
209
210   for (y=0; y < tile_ysteps; y++)
211   {
212     for (x=0; x < tile_xsteps; x++)
213     {
214       int draw_x = dest_x + x * tile_xsize;
215       int draw_y = dest_y + y * tile_ysize;
216       int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
217       int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
218
219       BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
220     }
221   }
222 }
223
224 void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
225 {
226   static Bitmap *main_bitmap_tile = NULL;
227   static Bitmap *door_bitmap_tile = NULL;
228
229   if (mask == REDRAW_FIELD)
230   {
231     if (background_bitmap_tile == main_bitmap_tile)
232       return;           /* main background tile has not changed */
233
234     main_bitmap_tile = background_bitmap_tile;
235   }
236   else if (mask == REDRAW_DOOR_1)
237   {
238     if (background_bitmap_tile == door_bitmap_tile)
239       return;   /* main background tile has not changed */
240
241     door_bitmap_tile = background_bitmap_tile;
242   }
243   else          /* should not happen */
244     return;
245
246   if (background_bitmap_tile)
247     gfx.background_bitmap_mask |= mask;
248   else
249     gfx.background_bitmap_mask &= ~mask;
250
251   if (gfx.background_bitmap == NULL)
252     gfx.background_bitmap = CreateBitmap(video.width, video.height,
253                                          DEFAULT_DEPTH);
254
255   if (background_bitmap_tile == NULL)   /* empty background requested */
256     return;
257
258   if (mask == REDRAW_FIELD)
259     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
260                        gfx.real_sx, gfx.real_sy,
261                        gfx.full_sxsize, gfx.full_sysize);
262   else
263     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
264                        gfx.dx, gfx.dy,
265                        gfx.dxsize, gfx.dysize);
266 }
267
268 void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
269 {
270   SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
271 }
272
273 void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
274 {
275   SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
276 }
277
278
279 /* ========================================================================= */
280 /* video functions                                                           */
281 /* ========================================================================= */
282
283 inline static int GetRealDepth(int depth)
284 {
285   return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
286 }
287
288 inline void InitVideoDisplay(void)
289 {
290 #if defined(TARGET_SDL)
291   SDLInitVideoDisplay();
292 #else
293   X11InitVideoDisplay();
294 #endif
295 }
296
297 inline void CloseVideoDisplay(void)
298 {
299   KeyboardAutoRepeatOn();
300
301 #if defined(TARGET_SDL)
302   SDL_QuitSubSystem(SDL_INIT_VIDEO);
303 #else
304
305   if (display)
306     XCloseDisplay(display);
307 #endif
308 }
309
310 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
311                             int width, int height, int depth,
312                             boolean fullscreen)
313 {
314   video.width = width;
315   video.height = height;
316   video.depth = GetRealDepth(depth);
317   video.fullscreen_available = FULLSCREEN_STATUS;
318   video.fullscreen_enabled = FALSE;
319
320 #ifdef TARGET_SDL
321   SDLInitVideoBuffer(backbuffer, window, fullscreen);
322 #else
323   X11InitVideoBuffer(backbuffer, window);
324 #endif
325 }
326
327 inline Bitmap *CreateBitmapStruct(void)
328 {
329 #ifdef TARGET_SDL
330   return checked_calloc(sizeof(struct SDLSurfaceInfo));
331 #else
332   return checked_calloc(sizeof(struct X11DrawableInfo));
333 #endif
334 }
335
336 inline Bitmap *CreateBitmap(int width, int height, int depth)
337 {
338   Bitmap *new_bitmap = CreateBitmapStruct();
339   int real_depth = GetRealDepth(depth);
340
341 #ifdef TARGET_SDL
342   SDL_Surface *surface_tmp, *surface_native;
343
344   if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
345                                           real_depth, 0, 0, 0, 0))
346       == NULL)
347     Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
348
349   if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
350     Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
351
352   SDL_FreeSurface(surface_tmp);
353
354   new_bitmap->surface = surface_native;
355 #else
356   Pixmap pixmap;
357
358   if ((pixmap = XCreatePixmap(display, window->drawable,
359                               width, height, real_depth))
360       == None)
361     Error(ERR_EXIT, "cannot create pixmap");
362
363   new_bitmap->drawable = pixmap;
364
365   if (window == NULL)
366     Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
367
368   new_bitmap->gc = window->gc;
369
370   new_bitmap->line_gc[0] = window->line_gc[0];
371   new_bitmap->line_gc[1] = window->line_gc[1];
372 #endif
373
374   new_bitmap->width = width;
375   new_bitmap->height = height;
376
377   return new_bitmap;
378 }
379
380 inline static void FreeBitmapPointers(Bitmap *bitmap)
381 {
382   if (bitmap == NULL)
383     return;
384
385 #ifdef TARGET_SDL
386   if (bitmap->surface)
387     SDL_FreeSurface(bitmap->surface);
388   if (bitmap->surface_masked)
389     SDL_FreeSurface(bitmap->surface_masked);
390   bitmap->surface = NULL;
391   bitmap->surface_masked = NULL;
392 #else
393   /* The X11 version seems to have a memory leak here -- although
394      "XFreePixmap()" is called, the corresponding memory seems not
395      to be freed (according to "ps"). The SDL version apparently
396      does not have this problem. */
397
398   if (bitmap->drawable)
399     XFreePixmap(display, bitmap->drawable);
400   if (bitmap->clip_mask)
401     XFreePixmap(display, bitmap->clip_mask);
402   if (bitmap->stored_clip_gc)
403     XFreeGC(display, bitmap->stored_clip_gc);
404   /* the other GCs are only pointers to GCs used elsewhere */
405   bitmap->drawable = None;
406   bitmap->clip_mask = None;
407   bitmap->stored_clip_gc = None;
408 #endif
409
410   if (bitmap->source_filename)
411     free(bitmap->source_filename);
412   bitmap->source_filename = NULL;
413 }
414
415 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
416                                           Bitmap *dst_bitmap)
417 {
418   if (src_bitmap == NULL || dst_bitmap == NULL)
419     return;
420
421   FreeBitmapPointers(dst_bitmap);
422
423   *dst_bitmap = *src_bitmap;
424 }
425
426 inline void FreeBitmap(Bitmap *bitmap)
427 {
428   if (bitmap == NULL)
429     return;
430
431   FreeBitmapPointers(bitmap);
432
433   free(bitmap);
434 }
435
436 inline void CloseWindow(DrawWindow *window)
437 {
438 #ifdef TARGET_X11
439   if (window->drawable)
440   {
441     XUnmapWindow(display, window->drawable);
442     XDestroyWindow(display, window->drawable);
443   }
444   if (window->gc)
445     XFreeGC(display, window->gc);
446 #endif
447 }
448
449 static inline boolean CheckDrawingArea(int x, int y, int width, int height,
450                                        int draw_mask)
451 {
452   if (draw_mask == REDRAW_NONE)
453     return FALSE;
454
455   if (draw_mask & REDRAW_ALL)
456     return TRUE;
457
458   if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
459     return TRUE;
460
461   if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
462     return TRUE;
463
464   if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
465     return TRUE;
466
467   return FALSE;
468 }
469
470 inline boolean DrawingDeactivated(int x, int y, int width, int height)
471 {
472   return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
473 }
474
475 inline boolean DrawingOnBackground(int x, int y)
476 {
477   return ((gfx.draw_background_mask & gfx.background_bitmap_mask) &&
478           CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
479 }
480
481 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
482                        int src_x, int src_y,
483                        int width, int height,
484                        int dst_x, int dst_y)
485 {
486   if (DrawingDeactivated(dst_x, dst_y, width, height))
487     return;
488
489 #ifdef TARGET_SDL
490   SDLCopyArea(src_bitmap, dst_bitmap,
491               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
492 #else
493   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
494             dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
495 #endif
496 }
497
498 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
499 {
500   if (DrawingDeactivated(x, y, width, height))
501     return;
502
503 #ifdef TARGET_SDL
504   SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
505 #else
506   XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
507 #endif
508 }
509
510 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
511                                        int width, int height)
512 {
513   if (DrawingOnBackground(x, y))
514     BlitBitmap(gfx.background_bitmap, bitmap, x, y, 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 Bitmap *ZoomBitmap(Bitmap *src_bitmap, int zoom_width, int zoom_height)
869 {
870   Bitmap *dst_bitmap = CreateBitmap(zoom_width, zoom_height, DEFAULT_DEPTH);
871
872 #if defined(TARGET_SDL)
873   SDLZoomBitmap(src_bitmap, dst_bitmap);
874 #else
875   X11ZoomBitmap(src_bitmap, dst_bitmap);
876 #endif
877
878   return dst_bitmap;
879 }
880
881 void CreateBitmapWithSmallBitmaps(Bitmap *src_bitmap)
882 {
883   Bitmap *tmp_bitmap, *tmp_bitmap_2, *tmp_bitmap_4, *tmp_bitmap_8;
884   int src_width, src_height;
885   int tmp_width, tmp_height;
886
887   src_width  = src_bitmap->width;
888   src_height = src_bitmap->height;
889
890   tmp_width  = src_width;
891   tmp_height = src_height + src_height / 2;
892
893   tmp_bitmap = CreateBitmap(tmp_width, tmp_height, DEFAULT_DEPTH);
894
895   tmp_bitmap_2 = ZoomBitmap(src_bitmap, src_width / 2, src_height / 2);
896   tmp_bitmap_4 = ZoomBitmap(src_bitmap, src_width / 4, src_height / 4);
897   tmp_bitmap_8 = ZoomBitmap(src_bitmap, src_width / 8, src_height / 8);
898
899   BlitBitmap(src_bitmap, tmp_bitmap, 0, 0, src_width, src_height, 0, 0);
900   BlitBitmap(tmp_bitmap_2, tmp_bitmap, 0, 0, src_width / 2, src_height / 2,
901              0, src_height);
902   BlitBitmap(tmp_bitmap_4, tmp_bitmap, 0, 0, src_width / 4, src_height / 4,
903              src_width / 2, src_height);
904   BlitBitmap(tmp_bitmap_8, tmp_bitmap, 0, 0, src_width / 8, src_height / 8,
905              3 * src_width / 4, src_height);
906
907   FreeBitmap(tmp_bitmap_2);
908   FreeBitmap(tmp_bitmap_4);
909   FreeBitmap(tmp_bitmap_8);
910
911 #ifdef TARGET_SDL
912   src_bitmap->surface = tmp_bitmap->surface;
913   tmp_bitmap->surface = NULL;
914 #else
915   src_bitmap->drawable = tmp_bitmap->drawable;
916   tmp_bitmap->drawable = None;
917 #endif
918
919   src_bitmap->height = tmp_bitmap->height;
920
921   FreeBitmap(tmp_bitmap);
922 }
923
924
925 /* ========================================================================= */
926 /* audio functions                                                           */
927 /* ========================================================================= */
928
929 inline void OpenAudio(void)
930 {
931   /* always start with reliable default values */
932   audio.sound_available = FALSE;
933   audio.music_available = FALSE;
934   audio.loops_available = FALSE;
935
936   audio.sound_enabled = FALSE;
937   audio.sound_deactivated = FALSE;
938
939   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
940   audio.mixer_pid = 0;
941   audio.device_name = NULL;
942   audio.device_fd = -1;
943
944   audio.num_channels = 0;
945   audio.music_channel = 0;
946   audio.first_sound_channel = 0;
947
948 #if defined(TARGET_SDL)
949   SDLOpenAudio();
950 #elif defined(PLATFORM_UNIX)
951   UnixOpenAudio();
952 #elif defined(PLATFORM_MSDOS)
953   MSDOSOpenAudio();
954 #endif
955 }
956
957 inline void CloseAudio(void)
958 {
959 #if defined(TARGET_SDL)
960   SDLCloseAudio();
961 #elif defined(PLATFORM_UNIX)
962   UnixCloseAudio();
963 #elif defined(PLATFORM_MSDOS)
964   MSDOSCloseAudio();
965 #endif
966
967   audio.sound_enabled = FALSE;
968 }
969
970 inline void SetAudioMode(boolean enabled)
971 {
972   if (!audio.sound_available)
973     return;
974
975   audio.sound_enabled = enabled;
976 }
977
978
979 /* ========================================================================= */
980 /* event functions                                                           */
981 /* ========================================================================= */
982
983 inline void InitEventFilter(EventFilter filter_function)
984 {
985 #ifdef TARGET_SDL
986   /* set event filter to filter out certain events */
987   SDL_SetEventFilter(filter_function);
988 #endif
989 }
990
991 inline boolean PendingEvent(void)
992 {
993 #ifdef TARGET_SDL
994   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
995 #else
996   return (XPending(display) ? TRUE : FALSE);
997 #endif
998 }
999
1000 inline void NextEvent(Event *event)
1001 {
1002 #ifdef TARGET_SDL
1003   SDLNextEvent(event);
1004 #else
1005   XNextEvent(display, event);
1006 #endif
1007 }
1008
1009 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
1010 {
1011 #ifdef TARGET_SDL
1012 #if 0
1013   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
1014          (int)event->keysym.unicode,
1015          (int)event->keysym.sym,
1016          (int)SDL_GetModState());
1017 #endif
1018
1019   if (with_modifiers &&
1020       event->keysym.unicode > 0x0000 &&
1021       event->keysym.unicode < 0x2000)
1022     return event->keysym.unicode;
1023   else
1024     return event->keysym.sym;
1025 #else
1026 #if 0
1027   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
1028          (int)XLookupKeysym(event, event->state),
1029          (int)XLookupKeysym(event, 0));
1030 #endif
1031
1032   if (with_modifiers)
1033     return XLookupKeysym(event, event->state);
1034   else
1035     return XLookupKeysym(event, 0);
1036 #endif
1037 }
1038
1039 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
1040 {
1041   if (event->type != EVENT_CLIENTMESSAGE)
1042     return FALSE;
1043
1044 #if defined(TARGET_SDL)
1045   return TRUE;          /* the only possible message here is SDL_QUIT */
1046 #elif defined(PLATFORM_UNIX)
1047   if ((event->window == window->drawable) &&
1048       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
1049     return TRUE;
1050 #endif
1051
1052   return FALSE;
1053 }
1054
1055
1056 /* ========================================================================= */
1057 /* joystick functions                                                        */
1058 /* ========================================================================= */
1059
1060 inline void InitJoysticks()
1061 {
1062   int i;
1063
1064 #ifdef NO_JOYSTICK
1065   return;       /* joysticks generally deactivated by compile-time directive */
1066 #endif
1067
1068   /* always start with reliable default values */
1069   joystick.status = JOYSTICK_NOT_AVAILABLE;
1070   for (i=0; i<MAX_PLAYERS; i++)
1071     joystick.fd[i] = -1;                /* joystick device closed */
1072
1073 #if defined(TARGET_SDL)
1074   SDLInitJoysticks();
1075 #elif defined(PLATFORM_UNIX)
1076   UnixInitJoysticks();
1077 #elif defined(PLATFORM_MSDOS)
1078   MSDOSInitJoysticks();
1079 #endif
1080 }
1081
1082 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1083 {
1084 #if defined(TARGET_SDL)
1085   return SDLReadJoystick(nr, x, y, b1, b2);
1086 #elif defined(PLATFORM_UNIX)
1087   return UnixReadJoystick(nr, x, y, b1, b2);
1088 #elif defined(PLATFORM_MSDOS)
1089   return MSDOSReadJoystick(nr, x, y, b1, b2);
1090 #endif
1091 }