rnd-20040816-2-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 ArtworkInfo      artwork;
40 struct JoystickInfo     joystick;
41 struct SetupInfo        setup;
42
43 LevelDirTree           *leveldir_first = NULL;
44 LevelDirTree           *leveldir_current = NULL;
45 int                     level_nr;
46
47 Display                *display = NULL;
48 Visual                 *visual = NULL;
49 int                     screen = 0;
50 Colormap                cmap = None;
51
52 DrawWindow             *window = NULL;
53 DrawBuffer             *backbuffer = NULL;
54 DrawBuffer             *drawto = NULL;
55
56 int                     button_status = MB_NOT_PRESSED;
57 boolean                 motion_status = FALSE;
58
59 int                     redraw_mask = REDRAW_NONE;
60 int                     redraw_tiles = 0;
61
62 int                     FrameCounter = 0;
63
64
65 /* ========================================================================= */
66 /* init/close functions                                                      */
67 /* ========================================================================= */
68
69 void InitProgramInfo(char *argv0,
70                      char *userdata_directory, char *program_title,
71                      char *window_title, char *icon_title,
72                      char *x11_icon_filename, char *x11_iconmask_filename,
73                      char *msdos_cursor_filename,
74                      char *cookie_prefix, char *filename_prefix,
75                      int program_version)
76 {
77   program.command_basename =
78     (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
79
80   program.userdata_directory = userdata_directory;
81   program.program_title = program_title;
82   program.window_title = window_title;
83   program.icon_title = icon_title;
84   program.x11_icon_filename = x11_icon_filename;
85   program.x11_iconmask_filename = x11_iconmask_filename;
86   program.msdos_cursor_filename = msdos_cursor_filename;
87
88   program.cookie_prefix = cookie_prefix;
89   program.filename_prefix = filename_prefix;
90
91   program.version_major = VERSION_MAJOR(program_version);
92   program.version_minor = VERSION_MINOR(program_version);
93   program.version_patch = VERSION_PATCH(program_version);
94 }
95
96 void InitExitFunction(void (*exit_function)(int))
97 {
98   program.exit_function = exit_function;
99
100   /* set signal handlers to custom exit function */
101   signal(SIGINT, exit_function);
102   signal(SIGTERM, exit_function);
103
104 #if defined(TARGET_SDL)
105   /* set exit function to automatically cleanup SDL stuff after exit() */
106   atexit(SDL_Quit);
107 #endif
108 }
109
110 void InitPlatformDependentStuff(void)
111 {
112 #if defined(PLATFORM_MSDOS)
113   _fmode = O_BINARY;
114   initErrorFile();
115 #endif
116
117 #if defined(TARGET_SDL)
118   if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
119     Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
120
121   SDLNet_Init();
122 #endif
123 }
124
125 void ClosePlatformDependentStuff(void)
126 {
127 #if defined(PLATFORM_MSDOS)
128   dumpErrorFile();
129 #endif
130 }
131
132 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
133                       int real_sx, int real_sy,
134                       int full_sxsize, int full_sysize,
135                       Bitmap *field_save_buffer)
136 {
137   gfx.sx = sx;
138   gfx.sy = sy;
139   gfx.sxsize = sxsize;
140   gfx.sysize = sysize;
141   gfx.real_sx = real_sx;
142   gfx.real_sy = real_sy;
143   gfx.full_sxsize = full_sxsize;
144   gfx.full_sysize = full_sysize;
145
146   gfx.field_save_buffer = field_save_buffer;
147
148   gfx.background_bitmap = NULL;
149   gfx.background_bitmap_mask = REDRAW_NONE;
150
151   SetDrawDeactivationMask(REDRAW_NONE);         /* do not deactivate drawing */
152   SetDrawBackgroundMask(REDRAW_NONE);           /* deactivate masked drawing */
153 }
154
155 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
156 {
157   gfx.dx = dx;
158   gfx.dy = dy;
159   gfx.dxsize = dxsize;
160   gfx.dysize = dysize;
161 }
162
163 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
164 {
165   gfx.vx = vx;
166   gfx.vy = vy;
167   gfx.vxsize = vxsize;
168   gfx.vysize = vysize;
169 }
170
171 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
172 {
173   /* currently only used by MSDOS code to alloc VRAM buffer, if available */
174   gfx.scrollbuffer_width = scrollbuffer_width;
175   gfx.scrollbuffer_height = scrollbuffer_height;
176 }
177
178 void SetDrawDeactivationMask(int draw_deactivation_mask)
179 {
180   gfx.draw_deactivation_mask = draw_deactivation_mask;
181 }
182
183 void SetDrawBackgroundMask(int draw_background_mask)
184 {
185   gfx.draw_background_mask = draw_background_mask;
186 }
187
188 static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
189                                int dest_x, int dest_y, int width, int height)
190 {
191   int bitmap_xsize = width;
192   int bitmap_ysize = height;
193   int tile_xsize = tile->width;
194   int tile_ysize = tile->height;
195   int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
196   int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
197   int x, y;
198
199   for (y = 0; y < tile_ysteps; y++)
200   {
201     for (x = 0; x < tile_xsteps; x++)
202     {
203       int draw_x = dest_x + x * tile_xsize;
204       int draw_y = dest_y + y * tile_ysize;
205       int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
206       int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
207
208       BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
209     }
210   }
211 }
212
213 void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
214 {
215   static Bitmap *main_bitmap_tile = NULL;
216   static Bitmap *door_bitmap_tile = NULL;
217
218   if (mask == REDRAW_FIELD)
219   {
220     if (background_bitmap_tile == main_bitmap_tile)
221       return;           /* main background tile has not changed */
222
223     main_bitmap_tile = background_bitmap_tile;
224   }
225   else if (mask == REDRAW_DOOR_1)
226   {
227     if (background_bitmap_tile == door_bitmap_tile)
228       return;   /* main background tile has not changed */
229
230     door_bitmap_tile = background_bitmap_tile;
231   }
232   else          /* should not happen */
233     return;
234
235   if (background_bitmap_tile)
236     gfx.background_bitmap_mask |= mask;
237   else
238     gfx.background_bitmap_mask &= ~mask;
239
240   if (gfx.background_bitmap == NULL)
241     gfx.background_bitmap = CreateBitmap(video.width, video.height,
242                                          DEFAULT_DEPTH);
243
244   if (background_bitmap_tile == NULL)   /* empty background requested */
245     return;
246
247   if (mask == REDRAW_FIELD)
248     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
249                        gfx.real_sx, gfx.real_sy,
250                        gfx.full_sxsize, gfx.full_sysize);
251   else
252     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
253                        gfx.dx, gfx.dy,
254                        gfx.dxsize, gfx.dysize);
255 }
256
257 void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
258 {
259   SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
260 }
261
262 void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
263 {
264   SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
265 }
266
267
268 /* ========================================================================= */
269 /* video functions                                                           */
270 /* ========================================================================= */
271
272 inline static int GetRealDepth(int depth)
273 {
274   return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
275 }
276
277 inline static void sysFillRectangle(Bitmap *bitmap, int x, int y,
278                                int width, int height, Pixel color)
279 {
280 #if defined(TARGET_SDL)
281   SDLFillRectangle(bitmap, x, y, width, height, color);
282 #else
283   X11FillRectangle(bitmap, x, y, width, height, color);
284 #endif
285 }
286
287 inline static void sysCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
288                                int src_x, int src_y, int width, int height,
289                                int dst_x, int dst_y, int mask_mode)
290 {
291 #if defined(TARGET_SDL)
292   SDLCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
293               dst_x, dst_y, mask_mode);
294 #else
295   X11CopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
296               dst_x, dst_y, mask_mode);
297 #endif
298 }
299
300 inline void InitVideoDisplay(void)
301 {
302 #if defined(TARGET_SDL)
303   SDLInitVideoDisplay();
304 #else
305   X11InitVideoDisplay();
306 #endif
307 }
308
309 inline void CloseVideoDisplay(void)
310 {
311   KeyboardAutoRepeatOn();
312
313 #if defined(TARGET_SDL)
314   SDL_QuitSubSystem(SDL_INIT_VIDEO);
315 #else
316   if (display)
317     XCloseDisplay(display);
318 #endif
319 }
320
321 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
322                             int width, int height, int depth,
323                             boolean fullscreen)
324 {
325   video.width = width;
326   video.height = height;
327   video.depth = GetRealDepth(depth);
328   video.fullscreen_available = FULLSCREEN_STATUS;
329   video.fullscreen_enabled = FALSE;
330
331 #if defined(TARGET_SDL)
332   SDLInitVideoBuffer(backbuffer, window, fullscreen);
333 #else
334   X11InitVideoBuffer(backbuffer, window);
335 #endif
336 }
337
338 inline Bitmap *CreateBitmapStruct(void)
339 {
340 #if defined(TARGET_SDL)
341   return checked_calloc(sizeof(struct SDLSurfaceInfo));
342 #else
343   return checked_calloc(sizeof(struct X11DrawableInfo));
344 #endif
345 }
346
347 inline Bitmap *CreateBitmap(int width, int height, int depth)
348 {
349   Bitmap *new_bitmap = CreateBitmapStruct();
350   int real_depth = GetRealDepth(depth);
351
352 #if defined(TARGET_SDL)
353   SDLCreateBitmapContent(new_bitmap, width, height, real_depth);
354 #else
355   X11CreateBitmapContent(new_bitmap, width, height, real_depth);
356 #endif
357
358   new_bitmap->width = width;
359   new_bitmap->height = height;
360
361   return new_bitmap;
362 }
363
364 inline static void FreeBitmapPointers(Bitmap *bitmap)
365 {
366   if (bitmap == NULL)
367     return;
368
369 #if defined(TARGET_SDL)
370   SDLFreeBitmapPointers(bitmap);
371 #else
372   X11FreeBitmapPointers(bitmap);
373 #endif
374
375   checked_free(bitmap->source_filename);
376   bitmap->source_filename = NULL;
377 }
378
379 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
380                                           Bitmap *dst_bitmap)
381 {
382   if (src_bitmap == NULL || dst_bitmap == NULL)
383     return;
384
385   FreeBitmapPointers(dst_bitmap);
386
387   *dst_bitmap = *src_bitmap;
388 }
389
390 inline void FreeBitmap(Bitmap *bitmap)
391 {
392   if (bitmap == NULL)
393     return;
394
395   FreeBitmapPointers(bitmap);
396
397   free(bitmap);
398 }
399
400 inline void CloseWindow(DrawWindow *window)
401 {
402 #if defined(TARGET_X11)
403   if (window->drawable)
404   {
405     XUnmapWindow(display, window->drawable);
406     XDestroyWindow(display, window->drawable);
407   }
408   if (window->gc)
409     XFreeGC(display, window->gc);
410 #endif
411 }
412
413 static inline boolean CheckDrawingArea(int x, int y, int width, int height,
414                                        int draw_mask)
415 {
416   if (draw_mask == REDRAW_NONE)
417     return FALSE;
418
419   if (draw_mask & REDRAW_ALL)
420     return TRUE;
421
422   if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
423     return TRUE;
424
425   if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
426     return TRUE;
427
428   if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
429     return TRUE;
430
431   return FALSE;
432 }
433
434 inline boolean DrawingDeactivated(int x, int y, int width, int height)
435 {
436   return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
437 }
438
439 inline boolean DrawingOnBackground(int x, int y)
440 {
441   return ((gfx.draw_background_mask & gfx.background_bitmap_mask) &&
442           CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
443 }
444
445 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
446                        int src_x, int src_y, int width, int height,
447                        int dst_x, int dst_y)
448 {
449   if (DrawingDeactivated(dst_x, dst_y, width, height))
450     return;
451
452   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
453               dst_x, dst_y, BLIT_OPAQUE);
454 }
455
456 inline void FillRectangle(Bitmap *bitmap, int x, int y, int width, int height,
457                           Pixel color)
458 {
459   if (DrawingDeactivated(x, y, width, height))
460     return;
461
462   sysFillRectangle(bitmap, x, y, width, height, color);
463 }
464
465 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
466 {
467   FillRectangle(bitmap, x, y, width, height, BLACK_PIXEL);
468 }
469
470 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
471                                        int width, int height)
472 {
473   if (DrawingOnBackground(x, y))
474     BlitBitmap(gfx.background_bitmap, bitmap, x, y, width, height, x, y);
475   else
476     ClearRectangle(bitmap, x, y, width, height);
477 }
478
479 #if 0
480 #ifndef TARGET_SDL
481 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
482 #endif
483 #endif
484
485 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
486 {
487 #if defined(TARGET_X11)
488   if (clip_gc)
489   {
490     bitmap->clip_gc = clip_gc;
491     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
492   }
493 #if 0
494   last_clip_gc = clip_gc;
495 #endif
496 #endif
497 }
498
499 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
500 {
501 #if defined(TARGET_X11)
502   if (clip_gc)
503   {
504     bitmap->clip_gc = clip_gc;
505     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
506   }
507 #if 0
508   last_clip_gc = clip_gc;
509 #endif
510 #endif
511 }
512
513 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
514                              int src_x, int src_y,
515                              int width, int height,
516                              int dst_x, int dst_y)
517 {
518   if (DrawingDeactivated(dst_x, dst_y, width, height))
519     return;
520
521   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
522               dst_x, dst_y, BLIT_MASKED);
523 }
524
525 inline void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
526                                    int src_x, int src_y,
527                                    int width, int height,
528                                    int dst_x, int dst_y)
529 {
530   if (DrawingOnBackground(dst_x, dst_y))
531   {
532     /* draw background */
533     BlitBitmap(gfx.background_bitmap, dst_bitmap, dst_x, dst_y, width, height,
534                dst_x, dst_y);
535
536     /* draw foreground */
537     SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
538                   dst_x - src_x, dst_y - src_y);
539     BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
540                      dst_x, dst_y);
541   }
542   else
543     BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
544                dst_x, dst_y);
545 }
546
547 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
548                                 int to_x, int to_y)
549 {
550 #if defined(TARGET_SDL)
551   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
552 #else
553   X11DrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
554 #endif
555 }
556
557 #if !defined(TARGET_X11_NATIVE)
558 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
559                      int to_x, int to_y, Pixel pixel, int line_width)
560 {
561   int x, y;
562
563   for (x = 0; x < line_width; x++)
564   {
565     for (y = 0; y < line_width; y++)
566     {
567       int dx = x - line_width / 2;
568       int dy = y - line_width / 2;
569
570       if ((x == 0 && y == 0) ||
571           (x == 0 && y == line_width - 1) ||
572           (x == line_width - 1 && y == 0) ||
573           (x == line_width - 1 && y == line_width - 1))
574         continue;
575
576 #if defined(TARGET_SDL)
577       SDLDrawLine(bitmap,
578                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
579 #elif defined(TARGET_ALLEGRO)
580       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
581                       to_x + dx, to_y + dy, pixel);
582 #endif
583     }
584   }
585 }
586 #endif
587
588 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
589                       Pixel pixel)
590 {
591 #if !defined(TARGET_X11_NATIVE)
592   int line_width = 4;
593   int i;
594
595   for (i = 0; i < num_points - 1; i++)
596     DrawLine(bitmap, points[i].x, points[i].y,
597              points[i + 1].x, points[i + 1].y, pixel, line_width);
598
599   /*
600   SDLDrawLines(bitmap->surface, points, num_points, pixel);
601   */
602 #else
603   XSetForeground(display, bitmap->line_gc[1], pixel);
604   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
605              (XPoint *)points, num_points, CoordModeOrigin);
606 #endif
607 }
608
609 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
610 {
611   if (x < 0 || x >= bitmap->width ||
612       y < 0 || y >= bitmap->height)
613     return BLACK_PIXEL;
614
615 #if defined(TARGET_SDL)
616   return SDLGetPixel(bitmap, x, y);
617 #elif defined(TARGET_ALLEGRO)
618   return AllegroGetPixel(bitmap->drawable, x, y);
619 #else
620   return X11GetPixel(bitmap, x, y);
621 #endif
622 }
623
624 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
625                              unsigned int color_g, unsigned int color_b)
626 {
627 #if defined(TARGET_SDL)
628   return SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
629 #elif defined(TARGET_ALLEGRO)
630   return AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
631 #else
632   return X11GetPixelFromRGB(color_r, color_g, color_b);
633 #endif
634 }
635
636 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
637 {
638   unsigned int color_r = (color >> 16) & 0xff;
639   unsigned int color_g = (color >>  8) & 0xff;
640   unsigned int color_b = (color >>  0) & 0xff;
641
642   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
643 }
644
645 /* execute all pending screen drawing operations */
646 inline void FlushDisplay(void)
647 {
648 #ifndef TARGET_SDL
649   XFlush(display);
650 #endif
651 }
652
653 /* execute and wait for all pending screen drawing operations */
654 inline void SyncDisplay(void)
655 {
656 #ifndef TARGET_SDL
657   XSync(display, FALSE);
658 #endif
659 }
660
661 inline void KeyboardAutoRepeatOn(void)
662 {
663 #if defined(TARGET_SDL)
664   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
665                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
666   SDL_EnableUNICODE(1);
667 #else
668   if (display)
669     XAutoRepeatOn(display);
670 #endif
671 }
672
673 inline void KeyboardAutoRepeatOff(void)
674 {
675 #if defined(TARGET_SDL)
676   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
677   SDL_EnableUNICODE(0);
678 #else
679   if (display)
680     XAutoRepeatOff(display);
681 #endif
682 }
683
684 inline boolean PointerInWindow(DrawWindow *window)
685 {
686 #if defined(TARGET_SDL)
687   return TRUE;
688 #else
689   Window root, child;
690   int root_x, root_y;
691   unsigned int mask;
692   int win_x, win_y;
693
694   /* if XQueryPointer() returns False, the pointer
695      is not on the same screen as the specified window */
696   return XQueryPointer(display, window->drawable, &root, &child,
697                        &root_x, &root_y, &win_x, &win_y, &mask);
698 #endif
699 }
700
701 inline boolean SetVideoMode(boolean fullscreen)
702 {
703 #if defined(TARGET_SDL)
704   return SDLSetVideoMode(&backbuffer, fullscreen);
705 #else
706   boolean success = TRUE;
707
708   if (fullscreen && video.fullscreen_available)
709   {
710     Error(ERR_WARN, "fullscreen not available in X11 version");
711
712     /* display error message only once */
713     video.fullscreen_available = FALSE;
714
715     success = FALSE;
716   }
717
718   return success;
719 #endif
720 }
721
722 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
723 {
724 #if defined(TARGET_SDL)
725   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
726       (!fullscreen && video.fullscreen_enabled))
727     fullscreen = SetVideoMode(fullscreen);
728 #endif
729
730   return fullscreen;
731 }
732
733 Bitmap *LoadImage(char *filename)
734 {
735   Bitmap *new_bitmap;
736
737 #if defined(TARGET_SDL)
738   new_bitmap = SDLLoadImage(filename);
739 #else
740   new_bitmap = X11LoadImage(filename);
741 #endif
742
743   if (new_bitmap)
744     new_bitmap->source_filename = getStringCopy(filename);
745
746   return new_bitmap;
747 }
748
749 Bitmap *LoadCustomImage(char *basename)
750 {
751   char *filename = getCustomImageFilename(basename);
752   Bitmap *new_bitmap;
753
754   if (filename == NULL)
755     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
756
757   if ((new_bitmap = LoadImage(filename)) == NULL)
758     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
759
760   return new_bitmap;
761 }
762
763 void ReloadCustomImage(Bitmap *bitmap, char *basename)
764 {
765   char *filename = getCustomImageFilename(basename);
766   Bitmap *new_bitmap;
767
768   if (filename == NULL)         /* (should never happen) */
769   {
770     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
771     return;
772   }
773
774   if (strcmp(filename, bitmap->source_filename) == 0)
775   {
776     /* The old and new image are the same (have the same filename and path).
777        This usually means that this image does not exist in this graphic set
778        and a fallback to the existing image is done. */
779
780     return;
781   }
782
783   if ((new_bitmap = LoadImage(filename)) == NULL)
784   {
785     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
786     return;
787   }
788
789   if (bitmap->width != new_bitmap->width ||
790       bitmap->height != new_bitmap->height)
791   {
792     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
793           filename);
794     FreeBitmap(new_bitmap);
795     return;
796   }
797
798   TransferBitmapPointers(new_bitmap, bitmap);
799   free(new_bitmap);
800 }
801
802 Bitmap *ZoomBitmap(Bitmap *src_bitmap, int zoom_width, int zoom_height)
803 {
804   Bitmap *dst_bitmap = CreateBitmap(zoom_width, zoom_height, DEFAULT_DEPTH);
805
806 #if defined(TARGET_SDL)
807   SDLZoomBitmap(src_bitmap, dst_bitmap);
808 #else
809   X11ZoomBitmap(src_bitmap, dst_bitmap);
810 #endif
811
812   return dst_bitmap;
813 }
814
815 void CreateBitmapWithSmallBitmaps(Bitmap *src_bitmap)
816 {
817   Bitmap swap_bitmap;
818   Bitmap *tmp_bitmap, *tmp_bitmap_2, *tmp_bitmap_8;
819   int src_width, src_height;
820   int tmp_width, tmp_height;
821
822   src_width  = src_bitmap->width;
823   src_height = src_bitmap->height;
824
825   tmp_width  = src_width;
826   tmp_height = src_height + (src_height + 1) / 2;     /* prevent odd height */
827
828   tmp_bitmap = CreateBitmap(tmp_width, tmp_height, DEFAULT_DEPTH);
829
830   tmp_bitmap_2 = ZoomBitmap(src_bitmap, src_width / 2, src_height / 2);
831   tmp_bitmap_8 = ZoomBitmap(src_bitmap, src_width / 8, src_height / 8);
832
833   BlitBitmap(src_bitmap, tmp_bitmap, 0, 0, src_width, src_height, 0, 0);
834   BlitBitmap(tmp_bitmap_2, tmp_bitmap, 0, 0, src_width / 2, src_height / 2,
835              0, src_height);
836   BlitBitmap(tmp_bitmap_8, tmp_bitmap, 0, 0, src_width / 8, src_height / 8,
837              3 * src_width / 4, src_height);
838
839   FreeBitmap(tmp_bitmap_2);
840   FreeBitmap(tmp_bitmap_8);
841
842 #if 0
843
844 #if defined(TARGET_SDL)
845   /* !!! what about the old src_bitmap->surface ??? FIX ME !!! */
846   src_bitmap->surface = tmp_bitmap->surface;
847   tmp_bitmap->surface = NULL;
848 #else
849   /* !!! see above !!! */
850   src_bitmap->drawable = tmp_bitmap->drawable;
851   tmp_bitmap->drawable = None;
852 #endif
853
854 #else
855
856 #if defined(TARGET_SDL)
857   swap_bitmap.surface = src_bitmap->surface;
858   src_bitmap->surface = tmp_bitmap->surface;
859   tmp_bitmap->surface = swap_bitmap.surface;
860 #else
861   swap_bitmap.drawable = src_bitmap->drawable;
862   src_bitmap->drawable = tmp_bitmap->drawable;
863   tmp_bitmap->drawable = swap_bitmap.drawable;
864 #endif
865
866 #endif
867
868   src_bitmap->height = tmp_bitmap->height;
869
870   FreeBitmap(tmp_bitmap);
871 }
872
873
874 /* ------------------------------------------------------------------------- */
875 /* mouse pointer functions                                                   */
876 /* ------------------------------------------------------------------------- */
877
878 #if !defined(PLATFORM_MSDOS)
879 /* XPM */
880 static const char *cursor_image_playfield[] =
881 {
882   /* width height num_colors chars_per_pixel */
883   "    16    16        3            1",
884
885   /* colors */
886   "X c #000000",
887   ". c #ffffff",
888   "  c None",
889
890 #if 1
891   /* some people complained about a "white dot" on the screen and thought it
892      was a graphical error... OK, let's just remove the whole pointer :-) */
893
894   /* pixels */
895   "                ",
896   "                ",
897   "                ",
898   "                ",
899   "                ",
900   "                ",
901   "                ",
902   "                ",
903   "                ",
904   "                ",
905   "                ",
906   "                ",
907   "                ",
908   "                ",
909   "                ",
910   "                ",
911
912   /* hot spot */
913   "0,0"
914 #else
915   /* pixels */
916   " X              ",
917   "X.X             ",
918   " X              ",
919   "                ",
920   "                ",
921   "                ",
922   "                ",
923   "                ",
924   "                ",
925   "                ",
926   "                ",
927   "                ",
928   "                ",
929   "                ",
930   "                ",
931   "                ",
932
933   /* hot spot */
934   "1,1"
935 #endif
936 };
937
938 #if defined(TARGET_SDL)
939 static const int cursor_bit_order = BIT_ORDER_MSB;
940 #elif defined(TARGET_X11_NATIVE)
941 static const int cursor_bit_order = BIT_ORDER_LSB;
942 #endif
943
944 static struct MouseCursorInfo *get_cursor_from_image(const char **image)
945 {
946   struct MouseCursorInfo *cursor;
947   boolean bit_order_msb = (cursor_bit_order == BIT_ORDER_MSB);
948   int header_lines = 4;
949   int x, y, i;
950
951   cursor = checked_calloc(sizeof(struct MouseCursorInfo));
952
953   sscanf(image[0], " %d %d ", &cursor->width, &cursor->height);
954
955   i = -1;
956   for (y = 0; y < cursor->width; y++)
957   {
958     for (x = 0; x < cursor->height; x++)
959     {
960       int bit_nr = x % 8;
961       int bit_mask = 0x01 << (bit_order_msb ? 7 - bit_nr : bit_nr );
962
963       if (bit_nr == 0)
964       {
965         i++;
966         cursor->data[i] = cursor->mask[i] = 0;
967       }
968
969       switch (image[header_lines + y][x])
970       {
971         case 'X':
972           cursor->data[i] |= bit_mask;
973           cursor->mask[i] |= bit_mask;
974           break;
975
976         case '.':
977           cursor->mask[i] |= bit_mask;
978           break;
979
980         case ' ':
981           break;
982       }
983     }
984   }
985
986   sscanf(image[header_lines + y], "%d,%d", &cursor->hot_x, &cursor->hot_y);
987
988   return cursor;
989 }
990 #endif  /* !PLATFORM_MSDOS */
991
992 void SetMouseCursor(int mode)
993 {
994 #if !defined(PLATFORM_MSDOS)
995   static struct MouseCursorInfo *cursor_playfield = NULL;
996
997   if (cursor_playfield == NULL)
998     cursor_playfield = get_cursor_from_image(cursor_image_playfield);
999
1000 #if defined(TARGET_SDL)
1001   SDLSetMouseCursor(mode == CURSOR_PLAYFIELD ? cursor_playfield : NULL);
1002 #elif defined(TARGET_X11_NATIVE)
1003   X11SetMouseCursor(mode == CURSOR_PLAYFIELD ? cursor_playfield : NULL);
1004 #endif
1005 #endif
1006 }
1007
1008
1009 /* ========================================================================= */
1010 /* audio functions                                                           */
1011 /* ========================================================================= */
1012
1013 inline void OpenAudio(void)
1014 {
1015   /* always start with reliable default values */
1016   audio.sound_available = FALSE;
1017   audio.music_available = FALSE;
1018   audio.loops_available = FALSE;
1019
1020   audio.sound_enabled = FALSE;
1021   audio.sound_deactivated = FALSE;
1022
1023   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
1024   audio.mixer_pid = 0;
1025   audio.device_name = NULL;
1026   audio.device_fd = -1;
1027
1028   audio.num_channels = 0;
1029   audio.music_channel = 0;
1030   audio.first_sound_channel = 0;
1031
1032 #if defined(TARGET_SDL)
1033   SDLOpenAudio();
1034 #elif defined(PLATFORM_UNIX)
1035   UnixOpenAudio();
1036 #elif defined(PLATFORM_MSDOS)
1037   MSDOSOpenAudio();
1038 #endif
1039 }
1040
1041 inline void CloseAudio(void)
1042 {
1043 #if defined(TARGET_SDL)
1044   SDLCloseAudio();
1045 #elif defined(PLATFORM_UNIX)
1046   UnixCloseAudio();
1047 #elif defined(PLATFORM_MSDOS)
1048   MSDOSCloseAudio();
1049 #endif
1050
1051   audio.sound_enabled = FALSE;
1052 }
1053
1054 inline void SetAudioMode(boolean enabled)
1055 {
1056   if (!audio.sound_available)
1057     return;
1058
1059   audio.sound_enabled = enabled;
1060 }
1061
1062
1063 /* ========================================================================= */
1064 /* event functions                                                           */
1065 /* ========================================================================= */
1066
1067 inline void InitEventFilter(EventFilter filter_function)
1068 {
1069 #if defined(TARGET_SDL)
1070   /* set event filter to filter out certain events */
1071   SDL_SetEventFilter(filter_function);
1072 #endif
1073 }
1074
1075 inline boolean PendingEvent(void)
1076 {
1077 #if defined(TARGET_SDL)
1078   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
1079 #else
1080   return (XPending(display) ? TRUE : FALSE);
1081 #endif
1082 }
1083
1084 inline void NextEvent(Event *event)
1085 {
1086 #if defined(TARGET_SDL)
1087   SDLNextEvent(event);
1088 #else
1089   XNextEvent(display, event);
1090 #endif
1091 }
1092
1093 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
1094 {
1095 #if defined(TARGET_SDL)
1096 #if 0
1097   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
1098          (int)event->keysym.unicode,
1099          (int)event->keysym.sym,
1100          (int)SDL_GetModState());
1101 #endif
1102
1103   if (with_modifiers &&
1104       event->keysym.unicode > 0x0000 &&
1105       event->keysym.unicode < 0x2000)
1106     return event->keysym.unicode;
1107   else
1108     return event->keysym.sym;
1109 #else
1110 #if 0
1111   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
1112          (int)XLookupKeysym(event, event->state),
1113          (int)XLookupKeysym(event, 0));
1114 #endif
1115
1116   if (with_modifiers)
1117     return XLookupKeysym(event, event->state);
1118   else
1119     return XLookupKeysym(event, 0);
1120 #endif
1121 }
1122
1123 inline KeyMod HandleKeyModState(Key key, int key_status)
1124 {
1125   static KeyMod current_modifiers = KMOD_None;
1126
1127 #if !defined(TARGET_SDL)
1128   if (key != KSYM_UNDEFINED)    /* new key => check for modifier key change */
1129   {
1130     KeyMod new_modifier = KMOD_None;
1131
1132     switch(key)
1133     {
1134       case KSYM_Shift_L:
1135         new_modifier = KMOD_Shift_L;
1136         break;
1137       case KSYM_Shift_R:
1138         new_modifier = KMOD_Shift_R;
1139         break;
1140       case KSYM_Control_L:
1141         new_modifier = KMOD_Control_L;
1142         break;
1143       case KSYM_Control_R:
1144         new_modifier = KMOD_Control_R;
1145         break;
1146       case KSYM_Meta_L:
1147         new_modifier = KMOD_Meta_L;
1148         break;
1149       case KSYM_Meta_R:
1150         new_modifier = KMOD_Meta_R;
1151         break;
1152       case KSYM_Alt_L:
1153         new_modifier = KMOD_Alt_L;
1154         break;
1155       case KSYM_Alt_R:
1156         new_modifier = KMOD_Alt_R;
1157         break;
1158       default:
1159         break;
1160     }
1161
1162     if (key_status == KEY_PRESSED)
1163       current_modifiers |= new_modifier;
1164     else
1165       current_modifiers &= ~new_modifier;
1166   }
1167 #endif
1168
1169   return current_modifiers;
1170 }
1171
1172 inline KeyMod GetKeyModState()
1173 {
1174 #if defined(TARGET_SDL)
1175   return (KeyMod)SDL_GetModState();
1176 #else
1177   return HandleKeyModState(KSYM_UNDEFINED, 0);
1178 #endif
1179 }
1180
1181 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
1182 {
1183   if (event->type != EVENT_CLIENTMESSAGE)
1184     return FALSE;
1185
1186 #if defined(TARGET_SDL)
1187   return TRUE;          /* the only possible message here is SDL_QUIT */
1188 #elif defined(PLATFORM_UNIX)
1189   if ((event->window == window->drawable) &&
1190       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
1191     return TRUE;
1192 #endif
1193
1194   return FALSE;
1195 }
1196
1197
1198 /* ========================================================================= */
1199 /* joystick functions                                                        */
1200 /* ========================================================================= */
1201
1202 inline void InitJoysticks()
1203 {
1204   int i;
1205
1206 #if defined(NO_JOYSTICK)
1207   return;       /* joysticks generally deactivated by compile-time directive */
1208 #endif
1209
1210   /* always start with reliable default values */
1211   joystick.status = JOYSTICK_NOT_AVAILABLE;
1212   for (i = 0; i < MAX_PLAYERS; i++)
1213     joystick.fd[i] = -1;                /* joystick device closed */
1214
1215 #if defined(TARGET_SDL)
1216   SDLInitJoysticks();
1217 #elif defined(PLATFORM_UNIX)
1218   UnixInitJoysticks();
1219 #elif defined(PLATFORM_MSDOS)
1220   MSDOSInitJoysticks();
1221 #endif
1222 }
1223
1224 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1225 {
1226 #if defined(TARGET_SDL)
1227   return SDLReadJoystick(nr, x, y, b1, b2);
1228 #elif defined(PLATFORM_UNIX)
1229   return UnixReadJoystick(nr, x, y, b1, b2);
1230 #elif defined(PLATFORM_MSDOS)
1231   return MSDOSReadJoystick(nr, x, y, b1, b2);
1232 #endif
1233 }