rnd-20030404-3-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_pointer_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_pointer_filename = msdos_pointer_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 InitPlatformDependantStuff(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 #endif
121 }
122
123 void ClosePlatformDependantStuff(void)
124 {
125 #if defined(PLATFORM_MSDOS)
126   dumpErrorFile();
127 #endif
128 }
129
130 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
131                       int real_sx, int real_sy,
132                       int full_sxsize, int full_sysize,
133                       Bitmap *field_save_buffer)
134 {
135   gfx.sx = sx;
136   gfx.sy = sy;
137   gfx.sxsize = sxsize;
138   gfx.sysize = sysize;
139   gfx.real_sx = real_sx;
140   gfx.real_sy = real_sy;
141   gfx.full_sxsize = full_sxsize;
142   gfx.full_sysize = full_sysize;
143
144   gfx.field_save_buffer = field_save_buffer;
145
146   gfx.background_bitmap = NULL;
147   gfx.background_bitmap_mask = REDRAW_NONE;
148
149   SetDrawDeactivationMask(REDRAW_NONE);         /* do not deactivate drawing */
150   SetDrawBackgroundMask(REDRAW_NONE);           /* deactivate masked drawing */
151 }
152
153 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
154 {
155   gfx.dx = dx;
156   gfx.dy = dy;
157   gfx.dxsize = dxsize;
158   gfx.dysize = dysize;
159 }
160
161 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
162 {
163   gfx.vx = vx;
164   gfx.vy = vy;
165   gfx.vxsize = vxsize;
166   gfx.vysize = vysize;
167 }
168
169 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
170 {
171   /* currently only used by MSDOS code to alloc VRAM buffer, if available */
172   gfx.scrollbuffer_width = scrollbuffer_width;
173   gfx.scrollbuffer_height = scrollbuffer_height;
174 }
175
176 void SetDrawDeactivationMask(int draw_deactivation_mask)
177 {
178   gfx.draw_deactivation_mask = draw_deactivation_mask;
179 }
180
181 void SetDrawBackgroundMask(int draw_background_mask)
182 {
183   gfx.draw_background_mask = draw_background_mask;
184 }
185
186 static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
187                                int dest_x, int dest_y, int width, int height)
188 {
189   int bitmap_xsize = width;
190   int bitmap_ysize = height;
191   int tile_xsize = tile->width;
192   int tile_ysize = tile->height;
193   int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
194   int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
195   int x, y;
196
197   for (y=0; y < tile_ysteps; y++)
198   {
199     for (x=0; x < tile_xsteps; x++)
200     {
201       int draw_x = dest_x + x * tile_xsize;
202       int draw_y = dest_y + y * tile_ysize;
203       int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
204       int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
205
206       BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
207     }
208   }
209 }
210
211 void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
212 {
213   static Bitmap *main_bitmap_tile = NULL;
214   static Bitmap *door_bitmap_tile = NULL;
215
216   if (mask == REDRAW_FIELD)
217   {
218     if (background_bitmap_tile == main_bitmap_tile)
219       return;           /* main background tile has not changed */
220
221     main_bitmap_tile = background_bitmap_tile;
222   }
223   else if (mask == REDRAW_DOOR_1)
224   {
225     if (background_bitmap_tile == door_bitmap_tile)
226       return;   /* main background tile has not changed */
227
228     door_bitmap_tile = background_bitmap_tile;
229   }
230   else          /* should not happen */
231     return;
232
233   if (background_bitmap_tile)
234     gfx.background_bitmap_mask |= mask;
235   else
236     gfx.background_bitmap_mask &= ~mask;
237
238   if (gfx.background_bitmap == NULL)
239     gfx.background_bitmap = CreateBitmap(video.width, video.height,
240                                          DEFAULT_DEPTH);
241
242   if (background_bitmap_tile == NULL)   /* empty background requested */
243     return;
244
245   if (mask == REDRAW_FIELD)
246     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
247                        gfx.real_sx, gfx.real_sy,
248                        gfx.full_sxsize, gfx.full_sysize);
249   else
250     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
251                        gfx.dx, gfx.dy,
252                        gfx.dxsize, gfx.dysize);
253 }
254
255 void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
256 {
257   SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
258 }
259
260 void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
261 {
262   SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
263 }
264
265
266 /* ========================================================================= */
267 /* video functions                                                           */
268 /* ========================================================================= */
269
270 inline static int GetRealDepth(int depth)
271 {
272   return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
273 }
274
275 inline static void sysFillRectangle(Bitmap *bitmap, int x, int y,
276                                int width, int height, Pixel color)
277 {
278 #if defined(TARGET_SDL)
279   SDLFillRectangle(bitmap, x, y, width, height, color);
280 #else
281   X11FillRectangle(bitmap, x, y, width, height, color);
282 #endif
283 }
284
285 inline static void sysCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
286                                int src_x, int src_y, int width, int height,
287                                int dst_x, int dst_y, int mask_mode)
288 {
289 #if defined(TARGET_SDL)
290   SDLCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
291               dst_x, dst_y, mask_mode);
292 #else
293   X11CopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
294               dst_x, dst_y, mask_mode);
295 #endif
296 }
297
298 inline void InitVideoDisplay(void)
299 {
300 #if defined(TARGET_SDL)
301   SDLInitVideoDisplay();
302 #else
303   X11InitVideoDisplay();
304 #endif
305 }
306
307 inline void CloseVideoDisplay(void)
308 {
309   KeyboardAutoRepeatOn();
310
311 #if defined(TARGET_SDL)
312   SDL_QuitSubSystem(SDL_INIT_VIDEO);
313 #else
314   if (display)
315     XCloseDisplay(display);
316 #endif
317 }
318
319 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
320                             int width, int height, int depth,
321                             boolean fullscreen)
322 {
323   video.width = width;
324   video.height = height;
325   video.depth = GetRealDepth(depth);
326   video.fullscreen_available = FULLSCREEN_STATUS;
327   video.fullscreen_enabled = FALSE;
328
329 #if defined(TARGET_SDL)
330   SDLInitVideoBuffer(backbuffer, window, fullscreen);
331 #else
332   X11InitVideoBuffer(backbuffer, window);
333 #endif
334 }
335
336 inline Bitmap *CreateBitmapStruct(void)
337 {
338 #if defined(TARGET_SDL)
339   return checked_calloc(sizeof(struct SDLSurfaceInfo));
340 #else
341   return checked_calloc(sizeof(struct X11DrawableInfo));
342 #endif
343 }
344
345 inline Bitmap *CreateBitmap(int width, int height, int depth)
346 {
347   Bitmap *new_bitmap = CreateBitmapStruct();
348   int real_depth = GetRealDepth(depth);
349
350 #if defined(TARGET_SDL)
351   SDLCreateBitmapContent(new_bitmap, width, height, real_depth);
352 #else
353   X11CreateBitmapContent(new_bitmap, width, height, real_depth);
354 #endif
355
356   new_bitmap->width = width;
357   new_bitmap->height = height;
358
359   return new_bitmap;
360 }
361
362 inline static void FreeBitmapPointers(Bitmap *bitmap)
363 {
364   if (bitmap == NULL)
365     return;
366
367 #if defined(TARGET_SDL)
368   SDLFreeBitmapPointers(bitmap);
369 #else
370   X11FreeBitmapPointers(bitmap);
371 #endif
372
373   if (bitmap->source_filename)
374     free(bitmap->source_filename);
375   bitmap->source_filename = NULL;
376 }
377
378 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
379                                           Bitmap *dst_bitmap)
380 {
381   if (src_bitmap == NULL || dst_bitmap == NULL)
382     return;
383
384   FreeBitmapPointers(dst_bitmap);
385
386   *dst_bitmap = *src_bitmap;
387 }
388
389 inline void FreeBitmap(Bitmap *bitmap)
390 {
391   if (bitmap == NULL)
392     return;
393
394   FreeBitmapPointers(bitmap);
395
396   free(bitmap);
397 }
398
399 inline void CloseWindow(DrawWindow *window)
400 {
401 #if defined(TARGET_X11)
402   if (window->drawable)
403   {
404     XUnmapWindow(display, window->drawable);
405     XDestroyWindow(display, window->drawable);
406   }
407   if (window->gc)
408     XFreeGC(display, window->gc);
409 #endif
410 }
411
412 static inline boolean CheckDrawingArea(int x, int y, int width, int height,
413                                        int draw_mask)
414 {
415   if (draw_mask == REDRAW_NONE)
416     return FALSE;
417
418   if (draw_mask & REDRAW_ALL)
419     return TRUE;
420
421   if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
422     return TRUE;
423
424   if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
425     return TRUE;
426
427   if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
428     return TRUE;
429
430   return FALSE;
431 }
432
433 inline boolean DrawingDeactivated(int x, int y, int width, int height)
434 {
435   return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
436 }
437
438 inline boolean DrawingOnBackground(int x, int y)
439 {
440   return ((gfx.draw_background_mask & gfx.background_bitmap_mask) &&
441           CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
442 }
443
444 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
445                        int src_x, int src_y, int width, int height,
446                        int dst_x, int dst_y)
447 {
448   if (DrawingDeactivated(dst_x, dst_y, width, height))
449     return;
450
451   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
452               dst_x, dst_y, BLIT_OPAQUE);
453 }
454
455 inline void FillRectangle(Bitmap *bitmap, int x, int y, int width, int height,
456                           Pixel color)
457 {
458   if (DrawingDeactivated(x, y, width, height))
459     return;
460
461   sysFillRectangle(bitmap, x, y, width, height, color);
462 }
463
464 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
465 {
466   FillRectangle(bitmap, x, y, width, height, BLACK_PIXEL);
467 }
468
469 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
470                                        int width, int height)
471 {
472   if (DrawingOnBackground(x, y))
473     BlitBitmap(gfx.background_bitmap, bitmap, x, y, width, height, x, y);
474   else
475     ClearRectangle(bitmap, x, y, width, height);
476 }
477
478 #if 0
479 #ifndef TARGET_SDL
480 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
481 #endif
482 #endif
483
484 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
485 {
486 #if defined(TARGET_X11)
487   if (clip_gc)
488   {
489     bitmap->clip_gc = clip_gc;
490     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
491   }
492 #if 0
493   last_clip_gc = clip_gc;
494 #endif
495 #endif
496 }
497
498 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
499 {
500 #if defined(TARGET_X11)
501   if (clip_gc)
502   {
503     bitmap->clip_gc = clip_gc;
504     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
505   }
506 #if 0
507   last_clip_gc = clip_gc;
508 #endif
509 #endif
510 }
511
512 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
513                              int src_x, int src_y,
514                              int width, int height,
515                              int dst_x, int dst_y)
516 {
517   if (DrawingDeactivated(dst_x, dst_y, width, height))
518     return;
519
520   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
521               dst_x, dst_y, BLIT_MASKED);
522 }
523
524 inline void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
525                                    int src_x, int src_y,
526                                    int width, int height,
527                                    int dst_x, int dst_y)
528 {
529   if (DrawingOnBackground(dst_x, dst_y))
530   {
531     /* draw background */
532     BlitBitmap(gfx.background_bitmap, dst_bitmap, dst_x, dst_y, width, height,
533                dst_x, dst_y);
534
535     /* draw foreground */
536     SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
537                   dst_x - src_x, dst_y - src_y);
538     BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
539                      dst_x, dst_y);
540   }
541   else
542     BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
543                dst_x, dst_y);
544 }
545
546 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
547                                 int to_x, int to_y)
548 {
549 #if defined(TARGET_SDL)
550   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
551 #else
552   X11DrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
553 #endif
554 }
555
556 #if !defined(TARGET_X11_NATIVE)
557 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
558                      int to_x, int to_y, Pixel pixel, int line_width)
559 {
560   int x, y;
561
562   for (x=0; x<line_width; x++)
563   {
564     for (y=0; y<line_width; y++)
565     {
566       int dx = x - line_width / 2;
567       int dy = y - line_width / 2;
568
569       if ((x == 0 && y == 0) ||
570           (x == 0 && y == line_width - 1) ||
571           (x == line_width - 1 && y == 0) ||
572           (x == line_width - 1 && y == line_width - 1))
573         continue;
574
575 #if defined(TARGET_SDL)
576       SDLDrawLine(bitmap,
577                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
578 #elif defined(TARGET_ALLEGRO)
579       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
580                       to_x + dx, to_y + dy, pixel);
581 #endif
582     }
583   }
584 }
585 #endif
586
587 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
588                       Pixel pixel)
589 {
590 #if !defined(TARGET_X11_NATIVE)
591   int line_width = 4;
592   int i;
593
594   for (i=0; i<num_points - 1; i++)
595     DrawLine(bitmap, points[i].x, points[i].y,
596              points[i + 1].x, points[i + 1].y, pixel, line_width);
597
598   /*
599   SDLDrawLines(bitmap->surface, points, num_points, pixel);
600   */
601 #else
602   XSetForeground(display, bitmap->line_gc[1], pixel);
603   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
604              (XPoint *)points, num_points, CoordModeOrigin);
605 #endif
606 }
607
608 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
609 {
610 #if defined(TARGET_SDL)
611   return SDLGetPixel(bitmap, x, y);
612 #elif defined(TARGET_ALLEGRO)
613   return AllegroGetPixel(bitmap->drawable, x, y);
614 #else
615   return X11GetPixel(bitmap, x, y);
616 #endif
617 }
618
619 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
620                              unsigned int color_g, unsigned int color_b)
621 {
622 #if defined(TARGET_SDL)
623   return SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
624 #elif defined(TARGET_ALLEGRO)
625   return AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
626 #else
627   return X11GetPixelFromRGB(color_r, color_g, color_b);
628 #endif
629 }
630
631 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
632 {
633   unsigned int color_r = (color >> 16) & 0xff;
634   unsigned int color_g = (color >>  8) & 0xff;
635   unsigned int color_b = (color >>  0) & 0xff;
636
637   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
638 }
639
640 /* execute all pending screen drawing operations */
641 inline void FlushDisplay(void)
642 {
643 #ifndef TARGET_SDL
644   XFlush(display);
645 #endif
646 }
647
648 /* execute and wait for all pending screen drawing operations */
649 inline void SyncDisplay(void)
650 {
651 #ifndef TARGET_SDL
652   XSync(display, FALSE);
653 #endif
654 }
655
656 inline void KeyboardAutoRepeatOn(void)
657 {
658 #if defined(TARGET_SDL)
659   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
660                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
661   SDL_EnableUNICODE(1);
662 #else
663   if (display)
664     XAutoRepeatOn(display);
665 #endif
666 }
667
668 inline void KeyboardAutoRepeatOff(void)
669 {
670 #if defined(TARGET_SDL)
671   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
672   SDL_EnableUNICODE(0);
673 #else
674   if (display)
675     XAutoRepeatOff(display);
676 #endif
677 }
678
679 inline boolean PointerInWindow(DrawWindow *window)
680 {
681 #if defined(TARGET_SDL)
682   return TRUE;
683 #else
684   Window root, child;
685   int root_x, root_y;
686   unsigned int mask;
687   int win_x, win_y;
688
689   /* if XQueryPointer() returns False, the pointer
690      is not on the same screen as the specified window */
691   return XQueryPointer(display, window->drawable, &root, &child,
692                        &root_x, &root_y, &win_x, &win_y, &mask);
693 #endif
694 }
695
696 inline boolean SetVideoMode(boolean fullscreen)
697 {
698 #if defined(TARGET_SDL)
699   return SDLSetVideoMode(&backbuffer, fullscreen);
700 #else
701   boolean success = TRUE;
702
703   if (fullscreen && video.fullscreen_available)
704   {
705     Error(ERR_WARN, "fullscreen not available in X11 version");
706
707     /* display error message only once */
708     video.fullscreen_available = FALSE;
709
710     success = FALSE;
711   }
712
713   return success;
714 #endif
715 }
716
717 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
718 {
719 #if defined(TARGET_SDL)
720   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
721       (!fullscreen && video.fullscreen_enabled))
722     fullscreen = SetVideoMode(fullscreen);
723 #endif
724
725   return fullscreen;
726 }
727
728 Bitmap *LoadImage(char *filename)
729 {
730   Bitmap *new_bitmap;
731
732 #if defined(TARGET_SDL)
733   new_bitmap = SDLLoadImage(filename);
734 #else
735   new_bitmap = X11LoadImage(filename);
736 #endif
737
738   if (new_bitmap)
739     new_bitmap->source_filename = getStringCopy(filename);
740
741   return new_bitmap;
742 }
743
744 Bitmap *LoadCustomImage(char *basename)
745 {
746   char *filename = getCustomImageFilename(basename);
747   Bitmap *new_bitmap;
748
749   if (filename == NULL)
750     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
751
752   if ((new_bitmap = LoadImage(filename)) == NULL)
753     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
754
755   return new_bitmap;
756 }
757
758 void ReloadCustomImage(Bitmap *bitmap, char *basename)
759 {
760   char *filename = getCustomImageFilename(basename);
761   Bitmap *new_bitmap;
762
763   if (filename == NULL)         /* (should never happen) */
764   {
765     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
766     return;
767   }
768
769   if (strcmp(filename, bitmap->source_filename) == 0)
770   {
771     /* The old and new image are the same (have the same filename and path).
772        This usually means that this image does not exist in this graphic set
773        and a fallback to the existing image is done. */
774
775     return;
776   }
777
778   if ((new_bitmap = LoadImage(filename)) == NULL)
779   {
780     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
781     return;
782   }
783
784   if (bitmap->width != new_bitmap->width ||
785       bitmap->height != new_bitmap->height)
786   {
787     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
788           filename);
789     FreeBitmap(new_bitmap);
790     return;
791   }
792
793   TransferBitmapPointers(new_bitmap, bitmap);
794   free(new_bitmap);
795 }
796
797 Bitmap *ZoomBitmap(Bitmap *src_bitmap, int zoom_width, int zoom_height)
798 {
799   Bitmap *dst_bitmap = CreateBitmap(zoom_width, zoom_height, DEFAULT_DEPTH);
800
801 #if defined(TARGET_SDL)
802   SDLZoomBitmap(src_bitmap, dst_bitmap);
803 #else
804   X11ZoomBitmap(src_bitmap, dst_bitmap);
805 #endif
806
807   return dst_bitmap;
808 }
809
810 void CreateBitmapWithSmallBitmaps(Bitmap *src_bitmap)
811 {
812   Bitmap *tmp_bitmap, *tmp_bitmap_2, *tmp_bitmap_8;
813   int src_width, src_height;
814   int tmp_width, tmp_height;
815
816   src_width  = src_bitmap->width;
817   src_height = src_bitmap->height;
818
819   tmp_width  = src_width;
820   tmp_height = src_height + src_height / 2;
821
822   tmp_bitmap = CreateBitmap(tmp_width, tmp_height, DEFAULT_DEPTH);
823
824   tmp_bitmap_2 = ZoomBitmap(src_bitmap, src_width / 2, src_height / 2);
825   tmp_bitmap_8 = ZoomBitmap(src_bitmap, src_width / 8, src_height / 8);
826
827   BlitBitmap(src_bitmap, tmp_bitmap, 0, 0, src_width, src_height, 0, 0);
828   BlitBitmap(tmp_bitmap_2, tmp_bitmap, 0, 0, src_width / 2, src_height / 2,
829              0, src_height);
830   BlitBitmap(tmp_bitmap_8, tmp_bitmap, 0, 0, src_width / 8, src_height / 8,
831              3 * src_width / 4, src_height);
832
833   FreeBitmap(tmp_bitmap_2);
834   FreeBitmap(tmp_bitmap_8);
835
836 #if defined(TARGET_SDL)
837   src_bitmap->surface = tmp_bitmap->surface;
838   tmp_bitmap->surface = NULL;
839 #else
840   src_bitmap->drawable = tmp_bitmap->drawable;
841   tmp_bitmap->drawable = None;
842 #endif
843
844   src_bitmap->height = tmp_bitmap->height;
845
846   FreeBitmap(tmp_bitmap);
847 }
848
849
850 /* ========================================================================= */
851 /* audio functions                                                           */
852 /* ========================================================================= */
853
854 inline void OpenAudio(void)
855 {
856   /* always start with reliable default values */
857   audio.sound_available = FALSE;
858   audio.music_available = FALSE;
859   audio.loops_available = FALSE;
860
861   audio.sound_enabled = FALSE;
862   audio.sound_deactivated = FALSE;
863
864   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
865   audio.mixer_pid = 0;
866   audio.device_name = NULL;
867   audio.device_fd = -1;
868
869   audio.num_channels = 0;
870   audio.music_channel = 0;
871   audio.first_sound_channel = 0;
872
873 #if defined(TARGET_SDL)
874   SDLOpenAudio();
875 #elif defined(PLATFORM_UNIX)
876   UnixOpenAudio();
877 #elif defined(PLATFORM_MSDOS)
878   MSDOSOpenAudio();
879 #endif
880 }
881
882 inline void CloseAudio(void)
883 {
884 #if defined(TARGET_SDL)
885   SDLCloseAudio();
886 #elif defined(PLATFORM_UNIX)
887   UnixCloseAudio();
888 #elif defined(PLATFORM_MSDOS)
889   MSDOSCloseAudio();
890 #endif
891
892   audio.sound_enabled = FALSE;
893 }
894
895 inline void SetAudioMode(boolean enabled)
896 {
897   if (!audio.sound_available)
898     return;
899
900   audio.sound_enabled = enabled;
901 }
902
903
904 /* ========================================================================= */
905 /* event functions                                                           */
906 /* ========================================================================= */
907
908 inline void InitEventFilter(EventFilter filter_function)
909 {
910 #if defined(TARGET_SDL)
911   /* set event filter to filter out certain events */
912   SDL_SetEventFilter(filter_function);
913 #endif
914 }
915
916 inline boolean PendingEvent(void)
917 {
918 #if defined(TARGET_SDL)
919   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
920 #else
921   return (XPending(display) ? TRUE : FALSE);
922 #endif
923 }
924
925 inline void NextEvent(Event *event)
926 {
927 #if defined(TARGET_SDL)
928   SDLNextEvent(event);
929 #else
930   XNextEvent(display, event);
931 #endif
932 }
933
934 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
935 {
936 #if defined(TARGET_SDL)
937 #if 0
938   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
939          (int)event->keysym.unicode,
940          (int)event->keysym.sym,
941          (int)SDL_GetModState());
942 #endif
943
944   if (with_modifiers &&
945       event->keysym.unicode > 0x0000 &&
946       event->keysym.unicode < 0x2000)
947     return event->keysym.unicode;
948   else
949     return event->keysym.sym;
950 #else
951 #if 0
952   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
953          (int)XLookupKeysym(event, event->state),
954          (int)XLookupKeysym(event, 0));
955 #endif
956
957   if (with_modifiers)
958     return XLookupKeysym(event, event->state);
959   else
960     return XLookupKeysym(event, 0);
961 #endif
962 }
963
964 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
965 {
966   if (event->type != EVENT_CLIENTMESSAGE)
967     return FALSE;
968
969 #if defined(TARGET_SDL)
970   return TRUE;          /* the only possible message here is SDL_QUIT */
971 #elif defined(PLATFORM_UNIX)
972   if ((event->window == window->drawable) &&
973       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
974     return TRUE;
975 #endif
976
977   return FALSE;
978 }
979
980
981 /* ========================================================================= */
982 /* joystick functions                                                        */
983 /* ========================================================================= */
984
985 inline void InitJoysticks()
986 {
987   int i;
988
989 #if defined(NO_JOYSTICK)
990   return;       /* joysticks generally deactivated by compile-time directive */
991 #endif
992
993   /* always start with reliable default values */
994   joystick.status = JOYSTICK_NOT_AVAILABLE;
995   for (i=0; i<MAX_PLAYERS; i++)
996     joystick.fd[i] = -1;                /* joystick device closed */
997
998 #if defined(TARGET_SDL)
999   SDLInitJoysticks();
1000 #elif defined(PLATFORM_UNIX)
1001   UnixInitJoysticks();
1002 #elif defined(PLATFORM_MSDOS)
1003   MSDOSInitJoysticks();
1004 #endif
1005 }
1006
1007 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1008 {
1009 #if defined(TARGET_SDL)
1010   return SDLReadJoystick(nr, x, y, b1, b2);
1011 #elif defined(PLATFORM_UNIX)
1012   return UnixReadJoystick(nr, x, y, b1, b2);
1013 #elif defined(PLATFORM_MSDOS)
1014   return MSDOSReadJoystick(nr, x, y, b1, b2);
1015 #endif
1016 }