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