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