1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
19 #if defined(PLATFORM_MSDOS)
31 /* ========================================================================= */
32 /* exported variables */
33 /* ========================================================================= */
35 struct ProgramInfo program;
36 struct OptionInfo options;
37 struct VideoSystemInfo video;
38 struct AudioSystemInfo audio;
40 struct ArtworkInfo artwork;
41 struct JoystickInfo joystick;
42 struct SetupInfo setup;
44 LevelDirTree *leveldir_first_all = NULL;
45 LevelDirTree *leveldir_first = NULL;
46 LevelDirTree *leveldir_current = NULL;
49 Display *display = NULL;
50 Visual *visual = NULL;
54 DrawWindow *window = NULL;
55 DrawBuffer *backbuffer = NULL;
56 DrawBuffer *drawto = NULL;
58 int button_status = MB_NOT_PRESSED;
59 boolean motion_status = FALSE;
61 int redraw_mask = REDRAW_NONE;
67 /* ========================================================================= */
68 /* init/close functions */
69 /* ========================================================================= */
71 void InitProgramInfo(char *argv0,
72 char *userdata_directory, char *program_title,
73 char *window_title, char *icon_title,
74 char *x11_icon_filename, char *x11_iconmask_filename,
75 char *msdos_cursor_filename,
76 char *cookie_prefix, char *filename_prefix,
79 program.command_basepath = getBasePath(argv0);
80 program.command_basename = getBaseName(argv0);
82 program.userdata_directory = userdata_directory;
83 program.program_title = program_title;
84 program.window_title = window_title;
85 program.icon_title = icon_title;
86 program.x11_icon_filename = x11_icon_filename;
87 program.x11_iconmask_filename = x11_iconmask_filename;
88 program.msdos_cursor_filename = msdos_cursor_filename;
90 program.cookie_prefix = cookie_prefix;
91 program.filename_prefix = filename_prefix;
93 program.version_major = VERSION_MAJOR(program_version);
94 program.version_minor = VERSION_MINOR(program_version);
95 program.version_patch = VERSION_PATCH(program_version);
98 void InitExitFunction(void (*exit_function)(int))
100 program.exit_function = exit_function;
102 /* set signal handlers to custom exit function */
103 signal(SIGINT, exit_function);
104 signal(SIGTERM, exit_function);
106 #if defined(TARGET_SDL)
107 /* set exit function to automatically cleanup SDL stuff after exit() */
112 void InitPlatformDependentStuff(void)
114 #if defined(PLATFORM_MSDOS)
118 #if defined(PLATFORM_WIN32) || defined(PLATFORM_MSDOS)
122 #if defined(TARGET_SDL)
123 if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
124 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
130 void ClosePlatformDependentStuff(void)
132 #if defined(PLATFORM_MSDOS)
137 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
138 int real_sx, int real_sy,
139 int full_sxsize, int full_sysize,
140 Bitmap *field_save_buffer)
146 gfx.real_sx = real_sx;
147 gfx.real_sy = real_sy;
148 gfx.full_sxsize = full_sxsize;
149 gfx.full_sysize = full_sysize;
151 gfx.field_save_buffer = field_save_buffer;
153 gfx.background_bitmap = NULL;
154 gfx.background_bitmap_mask = REDRAW_NONE;
156 SetDrawDeactivationMask(REDRAW_NONE); /* do not deactivate drawing */
157 SetDrawBackgroundMask(REDRAW_NONE); /* deactivate masked drawing */
160 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
168 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
176 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
178 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
179 gfx.scrollbuffer_width = scrollbuffer_width;
180 gfx.scrollbuffer_height = scrollbuffer_height;
183 void SetDrawDeactivationMask(int draw_deactivation_mask)
185 gfx.draw_deactivation_mask = draw_deactivation_mask;
188 void SetDrawBackgroundMask(int draw_background_mask)
190 gfx.draw_background_mask = draw_background_mask;
193 static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
194 int dest_x, int dest_y, int width, int height)
196 int bitmap_xsize = width;
197 int bitmap_ysize = height;
198 int tile_xsize = tile->width;
199 int tile_ysize = tile->height;
200 int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
201 int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
204 for (y = 0; y < tile_ysteps; y++)
206 for (x = 0; x < tile_xsteps; x++)
208 int draw_x = dest_x + x * tile_xsize;
209 int draw_y = dest_y + y * tile_ysize;
210 int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
211 int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
213 BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
218 void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
220 if (background_bitmap_tile != NULL)
221 gfx.background_bitmap_mask |= mask;
223 gfx.background_bitmap_mask &= ~mask;
225 if (gfx.background_bitmap == NULL)
226 gfx.background_bitmap = CreateBitmap(video.width, video.height,
229 if (background_bitmap_tile == NULL) /* empty background requested */
232 if (mask == REDRAW_FIELD)
233 DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
234 gfx.real_sx, gfx.real_sy,
235 gfx.full_sxsize, gfx.full_sysize);
236 else if (mask == REDRAW_DOOR_1)
238 DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
240 gfx.dxsize, gfx.dysize);
244 void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
246 SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
249 void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
251 SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
255 /* ========================================================================= */
256 /* video functions */
257 /* ========================================================================= */
259 inline static int GetRealDepth(int depth)
261 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
264 inline static void sysFillRectangle(Bitmap *bitmap, int x, int y,
265 int width, int height, Pixel color)
267 #if defined(TARGET_SDL)
268 SDLFillRectangle(bitmap, x, y, width, height, color);
270 X11FillRectangle(bitmap, x, y, width, height, color);
274 inline static void sysCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
275 int src_x, int src_y, int width, int height,
276 int dst_x, int dst_y, int mask_mode)
278 #if defined(TARGET_SDL)
279 SDLCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
280 dst_x, dst_y, mask_mode);
282 X11CopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
283 dst_x, dst_y, mask_mode);
287 void InitVideoDisplay(void)
289 #if defined(TARGET_SDL)
290 SDLInitVideoDisplay();
292 X11InitVideoDisplay();
296 void CloseVideoDisplay(void)
298 KeyboardAutoRepeatOn();
300 #if defined(TARGET_SDL)
301 SDL_QuitSubSystem(SDL_INIT_VIDEO);
304 XCloseDisplay(display);
308 void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
309 int width, int height, int depth, boolean fullscreen)
312 video.height = height;
313 video.depth = GetRealDepth(depth);
314 video.fullscreen_available = FULLSCREEN_STATUS;
315 video.fullscreen_enabled = FALSE;
317 #if defined(TARGET_SDL)
318 SDLInitVideoBuffer(backbuffer, window, fullscreen);
320 X11InitVideoBuffer(backbuffer, window);
324 Bitmap *CreateBitmapStruct(void)
326 #if defined(TARGET_SDL)
327 return checked_calloc(sizeof(struct SDLSurfaceInfo));
329 return checked_calloc(sizeof(struct X11DrawableInfo));
333 Bitmap *CreateBitmap(int width, int height, int depth)
335 Bitmap *new_bitmap = CreateBitmapStruct();
336 int real_depth = GetRealDepth(depth);
338 #if defined(TARGET_SDL)
339 SDLCreateBitmapContent(new_bitmap, width, height, real_depth);
341 X11CreateBitmapContent(new_bitmap, width, height, real_depth);
344 new_bitmap->width = width;
345 new_bitmap->height = height;
350 inline static void FreeBitmapPointers(Bitmap *bitmap)
355 #if defined(TARGET_SDL)
356 SDLFreeBitmapPointers(bitmap);
358 X11FreeBitmapPointers(bitmap);
361 checked_free(bitmap->source_filename);
362 bitmap->source_filename = NULL;
365 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
368 if (src_bitmap == NULL || dst_bitmap == NULL)
371 FreeBitmapPointers(dst_bitmap);
373 *dst_bitmap = *src_bitmap;
376 void FreeBitmap(Bitmap *bitmap)
381 FreeBitmapPointers(bitmap);
386 void CloseWindow(DrawWindow *window)
388 #if defined(TARGET_X11)
389 if (window->drawable)
391 XUnmapWindow(display, window->drawable);
392 XDestroyWindow(display, window->drawable);
395 XFreeGC(display, window->gc);
399 inline static boolean CheckDrawingArea(int x, int y, int width, int height,
402 if (draw_mask == REDRAW_NONE)
405 if (draw_mask & REDRAW_ALL)
408 if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
411 if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
414 if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
420 boolean DrawingDeactivated(int x, int y, int width, int height)
422 return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
425 boolean DrawingOnBackground(int x, int y)
427 return (CheckDrawingArea(x, y, 1, 1, gfx.background_bitmap_mask) &&
428 CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
431 void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
432 int src_x, int src_y, int width, int height,
433 int dst_x, int dst_y)
435 if (DrawingDeactivated(dst_x, dst_y, width, height))
438 sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
439 dst_x, dst_y, BLIT_OPAQUE);
442 void FadeScreen(Bitmap *bitmap_cross, int fade_mode, int fade_delay,
445 #if defined(TARGET_SDL)
446 SDLFadeScreen(bitmap_cross, fade_mode, fade_delay, post_delay);
448 X11FadeScreen(bitmap_cross, fade_mode, fade_delay, post_delay);
452 void FillRectangle(Bitmap *bitmap, int x, int y, int width, int height,
455 if (DrawingDeactivated(x, y, width, height))
458 sysFillRectangle(bitmap, x, y, width, height, color);
461 void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
463 FillRectangle(bitmap, x, y, width, height, BLACK_PIXEL);
466 void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
467 int width, int height)
469 if (DrawingOnBackground(x, y))
470 BlitBitmap(gfx.background_bitmap, bitmap, x, y, width, height, x, y);
472 ClearRectangle(bitmap, x, y, width, height);
475 void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
477 #if defined(TARGET_X11)
480 bitmap->clip_gc = clip_gc;
481 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
486 void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
488 #if defined(TARGET_X11)
491 bitmap->clip_gc = clip_gc;
492 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
497 void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
498 int src_x, int src_y, int width, int height,
499 int dst_x, int dst_y)
501 if (DrawingDeactivated(dst_x, dst_y, width, height))
504 sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
505 dst_x, dst_y, BLIT_MASKED);
508 void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
509 int src_x, int src_y, int width, int height,
510 int dst_x, int dst_y)
512 if (DrawingOnBackground(dst_x, dst_y))
514 /* draw background */
515 BlitBitmap(gfx.background_bitmap, dst_bitmap, dst_x, dst_y, width, height,
518 /* draw foreground */
519 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
520 dst_x - src_x, dst_y - src_y);
521 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
525 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
529 void DrawSimpleBlackLine(Bitmap *bitmap, int from_x, int from_y,
532 #if defined(TARGET_SDL)
533 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, BLACK_PIXEL);
535 X11DrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, BLACK_PIXEL);
539 void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
542 #if defined(TARGET_SDL)
543 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
545 X11DrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
549 #if !defined(TARGET_X11_NATIVE)
550 void DrawLine(Bitmap *bitmap, int from_x, int from_y,
551 int to_x, int to_y, Pixel pixel, int line_width)
555 for (x = 0; x < line_width; x++)
557 for (y = 0; y < line_width; y++)
559 int dx = x - line_width / 2;
560 int dy = y - line_width / 2;
562 if ((x == 0 && y == 0) ||
563 (x == 0 && y == line_width - 1) ||
564 (x == line_width - 1 && y == 0) ||
565 (x == line_width - 1 && y == line_width - 1))
568 #if defined(TARGET_SDL)
570 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
571 #elif defined(TARGET_ALLEGRO)
572 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
573 to_x + dx, to_y + dy, pixel);
580 void DrawLines(Bitmap *bitmap, struct XY *points, int num_points, Pixel pixel)
582 #if !defined(TARGET_X11_NATIVE)
586 for (i = 0; i < num_points - 1; i++)
587 DrawLine(bitmap, points[i].x, points[i].y,
588 points[i + 1].x, points[i + 1].y, pixel, line_width);
591 SDLDrawLines(bitmap->surface, points, num_points, pixel);
594 XSetForeground(display, bitmap->line_gc[1], pixel);
595 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
596 (XPoint *)points, num_points, CoordModeOrigin);
600 Pixel GetPixel(Bitmap *bitmap, int x, int y)
602 if (x < 0 || x >= bitmap->width ||
603 y < 0 || y >= bitmap->height)
606 #if defined(TARGET_SDL)
607 return SDLGetPixel(bitmap, x, y);
608 #elif defined(TARGET_ALLEGRO)
609 return AllegroGetPixel(bitmap->drawable, x, y);
611 return X11GetPixel(bitmap, x, y);
615 Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
616 unsigned int color_g, unsigned int color_b)
618 #if defined(TARGET_SDL)
619 return SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
620 #elif defined(TARGET_ALLEGRO)
621 return AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
623 return X11GetPixelFromRGB(color_r, color_g, color_b);
627 Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
629 unsigned int color_r = (color >> 16) & 0xff;
630 unsigned int color_g = (color >> 8) & 0xff;
631 unsigned int color_b = (color >> 0) & 0xff;
633 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
636 /* execute all pending screen drawing operations */
637 void FlushDisplay(void)
644 /* execute and wait for all pending screen drawing operations */
645 void SyncDisplay(void)
648 XSync(display, FALSE);
652 void KeyboardAutoRepeatOn(void)
654 #if defined(TARGET_SDL)
655 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
656 SDL_DEFAULT_REPEAT_INTERVAL / 2);
657 SDL_EnableUNICODE(1);
660 XAutoRepeatOn(display);
664 void KeyboardAutoRepeatOff(void)
666 #if defined(TARGET_SDL)
667 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
668 SDL_EnableUNICODE(0);
671 XAutoRepeatOff(display);
675 boolean PointerInWindow(DrawWindow *window)
677 #if defined(TARGET_SDL)
685 /* if XQueryPointer() returns False, the pointer
686 is not on the same screen as the specified window */
687 return XQueryPointer(display, window->drawable, &root, &child,
688 &root_x, &root_y, &win_x, &win_y, &mask);
692 boolean SetVideoMode(boolean fullscreen)
694 #if defined(TARGET_SDL)
695 return SDLSetVideoMode(&backbuffer, fullscreen);
697 boolean success = TRUE;
699 if (fullscreen && video.fullscreen_available)
701 Error(ERR_WARN, "fullscreen not available in X11 version");
703 /* display error message only once */
704 video.fullscreen_available = FALSE;
713 boolean ChangeVideoModeIfNeeded(boolean fullscreen)
715 #if defined(TARGET_SDL)
716 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
717 (!fullscreen && video.fullscreen_enabled))
718 fullscreen = SetVideoMode(fullscreen);
724 Bitmap *LoadImage(char *filename)
728 #if defined(TARGET_SDL)
729 new_bitmap = SDLLoadImage(filename);
731 new_bitmap = X11LoadImage(filename);
735 new_bitmap->source_filename = getStringCopy(filename);
740 Bitmap *LoadCustomImage(char *basename)
742 char *filename = getCustomImageFilename(basename);
745 if (filename == NULL)
746 Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
748 if ((new_bitmap = LoadImage(filename)) == NULL)
749 Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
754 void ReloadCustomImage(Bitmap *bitmap, char *basename)
756 char *filename = getCustomImageFilename(basename);
759 if (filename == NULL) /* (should never happen) */
761 Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
765 if (strEqual(filename, bitmap->source_filename))
767 /* The old and new image are the same (have the same filename and path).
768 This usually means that this image does not exist in this graphic set
769 and a fallback to the existing image is done. */
774 if ((new_bitmap = LoadImage(filename)) == NULL)
776 Error(ERR_WARN, "LoadImage() failed: %s", GetError());
780 if (bitmap->width != new_bitmap->width ||
781 bitmap->height != new_bitmap->height)
783 Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
785 FreeBitmap(new_bitmap);
789 TransferBitmapPointers(new_bitmap, bitmap);
793 Bitmap *ZoomBitmap(Bitmap *src_bitmap, int zoom_width, int zoom_height)
795 Bitmap *dst_bitmap = CreateBitmap(zoom_width, zoom_height, DEFAULT_DEPTH);
797 #if defined(TARGET_SDL)
798 SDLZoomBitmap(src_bitmap, dst_bitmap);
800 X11ZoomBitmap(src_bitmap, dst_bitmap);
806 static void CreateScaledBitmaps(Bitmap *old_bitmap, int zoom_factor,
807 boolean create_small_bitmaps)
810 Bitmap *new_bitmap, *tmp_bitmap_1, *tmp_bitmap_2, *tmp_bitmap_8;
811 int width_1, height_1, width_2, height_2, width_8, height_8;
812 int new_width, new_height;
814 /* calculate new image dimensions for normal sized image */
815 width_1 = old_bitmap->width * zoom_factor;
816 height_1 = old_bitmap->height * zoom_factor;
818 /* get image with normal size (this might require scaling up) */
819 if (zoom_factor != 1)
820 tmp_bitmap_1 = ZoomBitmap(old_bitmap, width_1, height_1);
822 tmp_bitmap_1 = old_bitmap;
824 /* this is only needed to make compilers happy */
825 tmp_bitmap_2 = tmp_bitmap_8 = NULL;
827 if (create_small_bitmaps)
829 /* calculate new image dimensions for small images */
830 width_2 = width_1 / 2;
831 height_2 = height_1 / 2;
832 width_8 = width_1 / 8;
833 height_8 = height_1 / 8;
835 /* get image with 1/2 of normal size (for use in the level editor) */
836 if (zoom_factor != 2)
837 tmp_bitmap_2 = ZoomBitmap(tmp_bitmap_1, width_1 / 2, height_1 / 2);
839 tmp_bitmap_2 = old_bitmap;
841 /* get image with 1/8 of normal size (for use on the preview screen) */
842 if (zoom_factor != 8)
843 tmp_bitmap_8 = ZoomBitmap(tmp_bitmap_1, width_1 / 8, height_1 / 8);
845 tmp_bitmap_8 = old_bitmap;
848 /* if image was scaled up, create new clipmask for normal size image */
849 if (zoom_factor != 1)
851 #if defined(TARGET_X11)
852 if (old_bitmap->clip_mask)
853 XFreePixmap(display, old_bitmap->clip_mask);
855 old_bitmap->clip_mask =
856 Pixmap_to_Mask(tmp_bitmap_1->drawable, width_1, height_1);
858 XSetClipMask(display, old_bitmap->stored_clip_gc, old_bitmap->clip_mask);
860 SDL_Surface *tmp_surface_1 = tmp_bitmap_1->surface;
862 if (old_bitmap->surface_masked)
863 SDL_FreeSurface(old_bitmap->surface_masked);
865 SDL_SetColorKey(tmp_surface_1, SDL_SRCCOLORKEY,
866 SDL_MapRGB(tmp_surface_1->format, 0x00, 0x00, 0x00));
867 if ((old_bitmap->surface_masked = SDL_DisplayFormat(tmp_surface_1)) ==NULL)
868 Error(ERR_EXIT, "SDL_DisplayFormat() failed");
869 SDL_SetColorKey(tmp_surface_1, 0, 0); /* reset transparent pixel */
873 if (create_small_bitmaps)
876 new_height = height_1 + (height_1 + 1) / 2; /* prevent odd height */
878 new_bitmap = CreateBitmap(new_width, new_height, DEFAULT_DEPTH);
880 BlitBitmap(tmp_bitmap_1, new_bitmap, 0, 0, width_1, height_1, 0, 0);
881 BlitBitmap(tmp_bitmap_2, new_bitmap, 0, 0, width_1 / 2, height_1 / 2,
883 BlitBitmap(tmp_bitmap_8, new_bitmap, 0, 0, width_1 / 8, height_1 / 8,
884 3 * width_1 / 4, height_1);
889 new_height = height_1;
891 new_bitmap = tmp_bitmap_1; /* directly use tmp_bitmap_1 as new bitmap */
894 if (create_small_bitmaps)
896 /* if no small bitmaps created, tmp_bitmap_1 is used as new bitmap now */
897 if (zoom_factor != 1)
898 FreeBitmap(tmp_bitmap_1);
900 if (zoom_factor != 2)
901 FreeBitmap(tmp_bitmap_2);
903 if (zoom_factor != 8)
904 FreeBitmap(tmp_bitmap_8);
907 /* replace image with extended image (containing normal, 1/2 and 1/8 size) */
908 #if defined(TARGET_SDL)
909 swap_bitmap.surface = old_bitmap->surface;
910 old_bitmap->surface = new_bitmap->surface;
911 new_bitmap->surface = swap_bitmap.surface;
913 swap_bitmap.drawable = old_bitmap->drawable;
914 old_bitmap->drawable = new_bitmap->drawable;
915 new_bitmap->drawable = swap_bitmap.drawable;
918 old_bitmap->width = new_bitmap->width;
919 old_bitmap->height = new_bitmap->height;
921 FreeBitmap(new_bitmap); /* this actually frees the _old_ bitmap now */
924 void CreateBitmapWithSmallBitmaps(Bitmap *old_bitmap, int zoom_factor)
926 CreateScaledBitmaps(old_bitmap, zoom_factor, TRUE);
929 void ScaleBitmap(Bitmap *old_bitmap, int zoom_factor)
931 CreateScaledBitmaps(old_bitmap, zoom_factor, FALSE);
935 /* ------------------------------------------------------------------------- */
936 /* mouse pointer functions */
937 /* ------------------------------------------------------------------------- */
939 #if !defined(PLATFORM_MSDOS)
941 static const char *cursor_image_playfield[] =
943 /* width height num_colors chars_per_pixel */
952 /* some people complained about a "white dot" on the screen and thought it
953 was a graphical error... OK, let's just remove the whole pointer :-) */
1001 #if defined(TARGET_SDL)
1002 static const int cursor_bit_order = BIT_ORDER_MSB;
1003 #elif defined(TARGET_X11_NATIVE)
1004 static const int cursor_bit_order = BIT_ORDER_LSB;
1007 static struct MouseCursorInfo *get_cursor_from_image(const char **image)
1009 struct MouseCursorInfo *cursor;
1010 boolean bit_order_msb = (cursor_bit_order == BIT_ORDER_MSB);
1011 int header_lines = 4;
1014 cursor = checked_calloc(sizeof(struct MouseCursorInfo));
1016 sscanf(image[0], " %d %d ", &cursor->width, &cursor->height);
1019 for (y = 0; y < cursor->width; y++)
1021 for (x = 0; x < cursor->height; x++)
1024 int bit_mask = 0x01 << (bit_order_msb ? 7 - bit_nr : bit_nr );
1029 cursor->data[i] = cursor->mask[i] = 0;
1032 switch (image[header_lines + y][x])
1035 cursor->data[i] |= bit_mask;
1036 cursor->mask[i] |= bit_mask;
1040 cursor->mask[i] |= bit_mask;
1049 sscanf(image[header_lines + y], "%d,%d", &cursor->hot_x, &cursor->hot_y);
1053 #endif /* !PLATFORM_MSDOS */
1055 void SetMouseCursor(int mode)
1057 #if !defined(PLATFORM_MSDOS)
1058 static struct MouseCursorInfo *cursor_playfield = NULL;
1060 if (cursor_playfield == NULL)
1061 cursor_playfield = get_cursor_from_image(cursor_image_playfield);
1063 #if defined(TARGET_SDL)
1064 SDLSetMouseCursor(mode == CURSOR_PLAYFIELD ? cursor_playfield : NULL);
1065 #elif defined(TARGET_X11_NATIVE)
1066 X11SetMouseCursor(mode == CURSOR_PLAYFIELD ? cursor_playfield : NULL);
1072 /* ========================================================================= */
1073 /* audio functions */
1074 /* ========================================================================= */
1076 void OpenAudio(void)
1078 /* always start with reliable default values */
1079 audio.sound_available = FALSE;
1080 audio.music_available = FALSE;
1081 audio.loops_available = FALSE;
1083 audio.sound_enabled = FALSE;
1084 audio.sound_deactivated = FALSE;
1086 audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
1087 audio.mixer_pid = 0;
1088 audio.device_name = NULL;
1089 audio.device_fd = -1;
1091 audio.num_channels = 0;
1092 audio.music_channel = 0;
1093 audio.first_sound_channel = 0;
1095 #if defined(TARGET_SDL)
1097 #elif defined(PLATFORM_UNIX)
1099 #elif defined(PLATFORM_MSDOS)
1104 void CloseAudio(void)
1106 #if defined(TARGET_SDL)
1108 #elif defined(PLATFORM_UNIX)
1110 #elif defined(PLATFORM_MSDOS)
1114 audio.sound_enabled = FALSE;
1117 void SetAudioMode(boolean enabled)
1119 if (!audio.sound_available)
1122 audio.sound_enabled = enabled;
1126 /* ========================================================================= */
1127 /* event functions */
1128 /* ========================================================================= */
1130 void InitEventFilter(EventFilter filter_function)
1132 #if defined(TARGET_SDL)
1133 /* set event filter to filter out certain events */
1134 SDL_SetEventFilter(filter_function);
1138 boolean PendingEvent(void)
1140 #if defined(TARGET_SDL)
1141 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
1143 return (XPending(display) ? TRUE : FALSE);
1147 void NextEvent(Event *event)
1149 #if defined(TARGET_SDL)
1150 SDLNextEvent(event);
1152 XNextEvent(display, event);
1156 void PeekEvent(Event *event)
1158 #if defined(TARGET_SDL)
1159 SDL_PeepEvents(event, 1, SDL_PEEKEVENT, SDL_ALLEVENTS);
1161 XPeekEvent(display, event);
1165 Key GetEventKey(KeyEvent *event, boolean with_modifiers)
1167 #if defined(TARGET_SDL)
1170 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
1171 (int)event->keysym.unicode,
1172 (int)event->keysym.sym,
1173 (int)SDL_GetModState());
1176 if (with_modifiers &&
1177 event->keysym.unicode > 0x0000 &&
1178 event->keysym.unicode < 0x2000)
1179 return event->keysym.unicode;
1181 return event->keysym.sym;
1186 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
1187 (int)XLookupKeysym(event, event->state),
1188 (int)XLookupKeysym(event, 0));
1192 return XLookupKeysym(event, event->state);
1194 return XLookupKeysym(event, 0);
1198 KeyMod HandleKeyModState(Key key, int key_status)
1200 static KeyMod current_modifiers = KMOD_None;
1202 #if !defined(TARGET_SDL)
1203 if (key != KSYM_UNDEFINED) /* new key => check for modifier key change */
1205 KeyMod new_modifier = KMOD_None;
1210 new_modifier = KMOD_Shift_L;
1213 new_modifier = KMOD_Shift_R;
1215 case KSYM_Control_L:
1216 new_modifier = KMOD_Control_L;
1218 case KSYM_Control_R:
1219 new_modifier = KMOD_Control_R;
1222 new_modifier = KMOD_Meta_L;
1225 new_modifier = KMOD_Meta_R;
1228 new_modifier = KMOD_Alt_L;
1231 new_modifier = KMOD_Alt_R;
1237 if (key_status == KEY_PRESSED)
1238 current_modifiers |= new_modifier;
1240 current_modifiers &= ~new_modifier;
1244 return current_modifiers;
1247 KeyMod GetKeyModState()
1249 #if defined(TARGET_SDL)
1250 return (KeyMod)SDL_GetModState();
1252 return HandleKeyModState(KSYM_UNDEFINED, 0);
1256 boolean CheckCloseWindowEvent(ClientMessageEvent *event)
1258 if (event->type != EVENT_CLIENTMESSAGE)
1261 #if defined(TARGET_SDL)
1262 return TRUE; /* the only possible message here is SDL_QUIT */
1263 #elif defined(PLATFORM_UNIX)
1264 if ((event->window == window->drawable) &&
1265 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
1273 /* ========================================================================= */
1274 /* joystick functions */
1275 /* ========================================================================= */
1277 void InitJoysticks()
1281 #if defined(NO_JOYSTICK)
1282 return; /* joysticks generally deactivated by compile-time directive */
1285 /* always start with reliable default values */
1286 joystick.status = JOYSTICK_NOT_AVAILABLE;
1287 for (i = 0; i < MAX_PLAYERS; i++)
1288 joystick.fd[i] = -1; /* joystick device closed */
1290 #if defined(TARGET_SDL)
1292 #elif defined(PLATFORM_UNIX)
1293 UnixInitJoysticks();
1294 #elif defined(PLATFORM_MSDOS)
1295 MSDOSInitJoysticks();
1299 for (i = 0; i < MAX_PLAYERS; i++)
1300 printf("::: Joystick for player %d: %d\n", i, joystick.fd[i]);
1304 boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1306 #if defined(TARGET_SDL)
1307 return SDLReadJoystick(nr, x, y, b1, b2);
1308 #elif defined(PLATFORM_UNIX)
1309 return UnixReadJoystick(nr, x, y, b1, b2);
1310 #elif defined(PLATFORM_MSDOS)
1311 return MSDOSReadJoystick(nr, x, y, b1, b2);