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