1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2001 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
19 #if defined(PLATFORM_MSDOS)
30 /* ========================================================================= */
31 /* exported variables */
32 /* ========================================================================= */
34 struct ProgramInfo program;
35 struct OptionInfo options;
36 struct VideoSystemInfo video;
37 struct AudioSystemInfo audio;
39 struct ArtworkInfo artwork;
40 struct JoystickInfo joystick;
41 struct SetupInfo setup;
43 LevelDirTree *leveldir_first = NULL;
44 LevelDirTree *leveldir_current = NULL;
47 Display *display = NULL;
48 Visual *visual = NULL;
52 DrawWindow *window = NULL;
53 DrawBuffer *backbuffer = NULL;
54 DrawBuffer *drawto = NULL;
56 int button_status = MB_NOT_PRESSED;
57 boolean motion_status = FALSE;
59 int redraw_mask = REDRAW_NONE;
65 /* ========================================================================= */
66 /* init/close functions */
67 /* ========================================================================= */
69 void InitCommandName(char *argv0)
71 program.command_basename =
72 (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
75 void InitExitFunction(void (*exit_function)(int))
77 program.exit_function = exit_function;
79 /* set signal handlers to custom exit function */
80 signal(SIGINT, exit_function);
81 signal(SIGTERM, exit_function);
83 #if defined(TARGET_SDL)
84 /* set exit function to automatically cleanup SDL stuff after exit() */
89 void InitPlatformDependantStuff(void)
91 #if defined(PLATFORM_MSDOS)
95 #if !defined(PLATFORM_UNIX)
96 program.userdata_directory = "userdata";
100 #if defined(TARGET_SDL)
101 if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
102 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
106 void ClosePlatformDependantStuff(void)
108 #if !defined(PLATFORM_UNIX)
113 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
114 char *window_title, char *icon_title,
115 char *x11_icon_filename, char *x11_iconmask_filename,
116 char *msdos_pointer_filename,
117 char *cookie_prefix, char *filename_prefix,
120 #if defined(PLATFORM_UNIX)
121 program.userdata_directory = unix_userdata_directory;
123 program.userdata_directory = "userdata";
126 program.program_title = program_title;
127 program.window_title = window_title;
128 program.icon_title = icon_title;
129 program.x11_icon_filename = x11_icon_filename;
130 program.x11_iconmask_filename = x11_iconmask_filename;
131 program.msdos_pointer_filename = msdos_pointer_filename;
133 program.cookie_prefix = cookie_prefix;
134 program.filename_prefix = filename_prefix;
136 program.version_major = VERSION_MAJOR(program_version);
137 program.version_minor = VERSION_MINOR(program_version);
138 program.version_patch = VERSION_PATCH(program_version);
141 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
142 int real_sx, int real_sy,
143 int full_sxsize, int full_sysize)
149 gfx.real_sx = real_sx;
150 gfx.real_sy = real_sy;
151 gfx.full_sxsize = full_sxsize;
152 gfx.full_sysize = full_sysize;
154 SetDrawDeactivationMask(REDRAW_NONE); /* do not deactivate drawing */
157 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
165 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
173 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
175 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
176 gfx.scrollbuffer_width = scrollbuffer_width;
177 gfx.scrollbuffer_height = scrollbuffer_height;
180 void SetDrawDeactivationMask(int draw_deactivation_mask)
182 gfx.draw_deactivation_mask = draw_deactivation_mask;
186 /* ========================================================================= */
187 /* video functions */
188 /* ========================================================================= */
190 inline static int GetRealDepth(int depth)
192 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
195 inline void InitVideoDisplay(void)
197 #if defined(TARGET_SDL)
198 SDLInitVideoDisplay();
200 X11InitVideoDisplay();
204 inline void CloseVideoDisplay(void)
206 KeyboardAutoRepeatOn();
208 #if defined(TARGET_SDL)
209 SDL_QuitSubSystem(SDL_INIT_VIDEO);
213 XCloseDisplay(display);
217 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
218 int width, int height, int depth,
222 video.height = height;
223 video.depth = GetRealDepth(depth);
224 video.fullscreen_available = FULLSCREEN_STATUS;
225 video.fullscreen_enabled = FALSE;
228 SDLInitVideoBuffer(backbuffer, window, fullscreen);
230 X11InitVideoBuffer(backbuffer, window);
234 inline Bitmap *CreateBitmapStruct(void)
237 return checked_calloc(sizeof(struct SDLSurfaceInfo));
239 return checked_calloc(sizeof(struct X11DrawableInfo));
243 inline Bitmap *CreateBitmap(int width, int height, int depth)
245 Bitmap *new_bitmap = CreateBitmapStruct();
246 int real_depth = GetRealDepth(depth);
249 SDL_Surface *surface_tmp, *surface_native;
251 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
252 real_depth, 0, 0, 0, 0))
254 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
256 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
257 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
259 SDL_FreeSurface(surface_tmp);
261 new_bitmap->surface = surface_native;
265 if ((pixmap = XCreatePixmap(display, window->drawable,
266 width, height, real_depth))
268 Error(ERR_EXIT, "cannot create pixmap");
270 new_bitmap->drawable = pixmap;
273 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
275 new_bitmap->gc = window->gc;
277 new_bitmap->line_gc[0] = window->line_gc[0];
278 new_bitmap->line_gc[1] = window->line_gc[1];
284 inline static void FreeBitmapPointers(Bitmap *bitmap)
291 SDL_FreeSurface(bitmap->surface);
292 if (bitmap->surface_masked)
293 SDL_FreeSurface(bitmap->surface_masked);
294 bitmap->surface = NULL;
295 bitmap->surface_masked = NULL;
297 /* The X11 version seems to have a memory leak here -- although
298 "XFreePixmap()" is called, the correspondig memory seems not
299 to be freed (according to "ps"). The SDL version apparently
300 does not have this problem. */
302 if (bitmap->drawable)
303 XFreePixmap(display, bitmap->drawable);
304 if (bitmap->clip_mask)
305 XFreePixmap(display, bitmap->clip_mask);
306 if (bitmap->stored_clip_gc)
307 XFreeGC(display, bitmap->stored_clip_gc);
308 /* the other GCs are only pointers to GCs used elsewhere */
309 bitmap->drawable = None;
310 bitmap->clip_mask = None;
311 bitmap->stored_clip_gc = None;
314 if (bitmap->source_filename)
315 free(bitmap->source_filename);
316 bitmap->source_filename = NULL;
319 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
322 if (src_bitmap == NULL || dst_bitmap == NULL)
325 FreeBitmapPointers(dst_bitmap);
327 *dst_bitmap = *src_bitmap;
330 inline void FreeBitmap(Bitmap *bitmap)
335 FreeBitmapPointers(bitmap);
340 inline void CloseWindow(DrawWindow *window)
343 if (window->drawable)
345 XUnmapWindow(display, window->drawable);
346 XDestroyWindow(display, window->drawable);
349 XFreeGC(display, window->gc);
353 inline boolean DrawingDeactivated(int x, int y, int width, int height)
355 if (gfx.draw_deactivation_mask != REDRAW_NONE)
357 if ((gfx.draw_deactivation_mask & REDRAW_FIELD) &&
358 x < gfx.sx + gfx.sxsize)
360 else if ((gfx.draw_deactivation_mask & REDRAW_DOORS) &&
363 if ((gfx.draw_deactivation_mask & REDRAW_DOOR_1) &&
364 y < gfx.dy + gfx.dysize)
366 else if ((gfx.draw_deactivation_mask & REDRAW_DOOR_2) &&
375 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
376 int src_x, int src_y,
377 int width, int height,
378 int dst_x, int dst_y)
380 if (DrawingDeactivated(dst_x, dst_y, width, height))
384 SDLCopyArea(src_bitmap, dst_bitmap,
385 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
387 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
388 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
392 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
394 if (DrawingDeactivated(x, y, width, height))
398 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
400 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
406 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
410 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
415 bitmap->clip_gc = clip_gc;
416 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
419 last_clip_gc = clip_gc;
424 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
429 bitmap->clip_gc = clip_gc;
430 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
433 last_clip_gc = clip_gc;
438 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
439 int src_x, int src_y,
440 int width, int height,
441 int dst_x, int dst_y)
443 if (DrawingDeactivated(dst_x, dst_y, width, height))
447 SDLCopyArea(src_bitmap, dst_bitmap,
448 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
450 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
451 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
455 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
459 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
461 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
462 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
463 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
467 #if !defined(TARGET_X11_NATIVE)
468 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
469 int to_x, int to_y, Pixel pixel, int line_width)
473 for (x=0; x<line_width; x++)
475 for (y=0; y<line_width; y++)
477 int dx = x - line_width / 2;
478 int dy = y - line_width / 2;
480 if ((x == 0 && y == 0) ||
481 (x == 0 && y == line_width - 1) ||
482 (x == line_width - 1 && y == 0) ||
483 (x == line_width - 1 && y == line_width - 1))
486 #if defined(TARGET_SDL)
488 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
489 #elif defined(TARGET_ALLEGRO)
490 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
491 to_x + dx, to_y + dy, pixel);
498 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
501 #if !defined(TARGET_X11_NATIVE)
505 for (i=0; i<num_points - 1; i++)
506 DrawLine(bitmap, points[i].x, points[i].y,
507 points[i + 1].x, points[i + 1].y, pixel, line_width);
510 SDLDrawLines(bitmap->surface, points, num_points, pixel);
513 XSetForeground(display, bitmap->line_gc[1], pixel);
514 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
515 (XPoint *)points, num_points, CoordModeOrigin);
517 XSetForeground(display, gc, BlackPixel(display, screen));
522 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
524 #if defined(TARGET_SDL)
525 return SDLGetPixel(bitmap, x, y);
526 #elif defined(TARGET_ALLEGRO)
527 return AllegroGetPixel(bitmap->drawable, x, y);
529 unsigned long pixel_value;
532 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
534 pixel_value = XGetPixel(pixel_image, 0, 0);
536 XDestroyImage(pixel_image);
542 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
543 unsigned int color_g, unsigned int color_b)
547 #if defined(TARGET_SDL)
548 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
549 #elif defined(TARGET_ALLEGRO)
550 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
551 #elif defined(TARGET_X11_NATIVE)
554 xcolor.flags = DoRed | DoGreen | DoBlue;
555 xcolor.red = (color_r << 8);
556 xcolor.green = (color_g << 8);
557 xcolor.blue = (color_b << 8);
558 XAllocColor(display, cmap, &xcolor);
559 pixel = xcolor.pixel;
565 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
567 unsigned int color_r = (color >> 16) & 0xff;
568 unsigned int color_g = (color >> 8) & 0xff;
569 unsigned int color_b = (color >> 0) & 0xff;
571 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
574 /* execute all pending screen drawing operations */
575 inline void FlushDisplay(void)
582 /* execute and wait for all pending screen drawing operations */
583 inline void SyncDisplay(void)
586 XSync(display, FALSE);
590 inline void KeyboardAutoRepeatOn(void)
593 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
594 SDL_DEFAULT_REPEAT_INTERVAL / 2);
595 SDL_EnableUNICODE(1);
598 XAutoRepeatOn(display);
602 inline void KeyboardAutoRepeatOff(void)
605 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
606 SDL_EnableUNICODE(0);
609 XAutoRepeatOff(display);
613 inline boolean PointerInWindow(DrawWindow *window)
623 /* if XQueryPointer() returns False, the pointer
624 is not on the same screen as the specified window */
625 return XQueryPointer(display, window->drawable, &root, &child,
626 &root_x, &root_y, &win_x, &win_y, &mask);
630 inline boolean SetVideoMode(boolean fullscreen)
633 return SDLSetVideoMode(&backbuffer, fullscreen);
635 boolean success = TRUE;
637 if (fullscreen && video.fullscreen_available)
639 Error(ERR_WARN, "fullscreen not available in X11 version");
641 /* display error message only once */
642 video.fullscreen_available = FALSE;
651 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
654 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
655 (!fullscreen && video.fullscreen_enabled))
656 fullscreen = SetVideoMode(fullscreen);
662 Bitmap *LoadImage(char *filename)
666 #if defined(TARGET_SDL)
667 new_bitmap = SDLLoadImage(filename);
669 new_bitmap = X11LoadImage(filename);
673 new_bitmap->source_filename = getStringCopy(filename);
678 Bitmap *LoadCustomImage(char *basename)
680 char *filename = getCustomImageFilename(basename);
683 if (filename == NULL)
684 Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
686 if ((new_bitmap = LoadImage(filename)) == NULL)
687 Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
692 void ReloadCustomImage(Bitmap *bitmap, char *basename)
694 char *filename = getCustomImageFilename(basename);
697 if (filename == NULL) /* (should never happen) */
699 Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
703 if (strcmp(filename, bitmap->source_filename) == 0)
705 /* The old and new image are the same (have the same filename and path).
706 This usually means that this image does not exist in this graphic set
707 and a fallback to the existing image is done. */
712 if ((new_bitmap = LoadImage(filename)) == NULL)
714 Error(ERR_WARN, "LoadImage() failed: %s", GetError());
718 if (bitmap->width != new_bitmap->width ||
719 bitmap->height != new_bitmap->height)
721 Error(ERR_WARN, "ReloadCustomImage: new image has wrong dimensions");
722 FreeBitmap(new_bitmap);
726 TransferBitmapPointers(new_bitmap, bitmap);
731 /* ========================================================================= */
732 /* audio functions */
733 /* ========================================================================= */
735 inline void OpenAudio(void)
737 /* always start with reliable default values */
738 audio.sound_available = FALSE;
739 audio.music_available = FALSE;
740 audio.loops_available = FALSE;
742 audio.sound_enabled = FALSE;
743 audio.sound_deactivated = FALSE;
745 audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0;
746 audio.soundserver_pid = -1;
747 audio.device_name = NULL;
751 audio.music_channel = 0;
753 #if defined(TARGET_SDL)
755 #elif defined(PLATFORM_UNIX)
757 #elif defined(PLATFORM_MSDOS)
762 inline void CloseAudio(void)
764 #if defined(TARGET_SDL)
766 #elif defined(PLATFORM_UNIX)
768 #elif defined(PLATFORM_MSDOS)
772 audio.sound_enabled = FALSE;
775 inline void SetAudioMode(boolean enabled)
777 if (!audio.sound_available)
780 audio.sound_enabled = enabled;
784 /* ========================================================================= */
785 /* event functions */
786 /* ========================================================================= */
788 inline void InitEventFilter(EventFilter filter_function)
791 /* set event filter to filter out certain events */
792 SDL_SetEventFilter(filter_function);
796 inline boolean PendingEvent(void)
799 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
801 return (XPending(display) ? TRUE : FALSE);
805 inline void NextEvent(Event *event)
810 XNextEvent(display, event);
814 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
818 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
819 (int)event->keysym.unicode,
820 (int)event->keysym.sym,
821 (int)SDL_GetModState());
824 if (with_modifiers && event->keysym.unicode != 0)
825 return event->keysym.unicode;
827 return event->keysym.sym;
830 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
831 (int)XLookupKeysym(event, event->state),
832 (int)XLookupKeysym(event, 0));
836 return XLookupKeysym(event, event->state);
838 return XLookupKeysym(event, 0);
842 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
844 if (event->type != EVENT_CLIENTMESSAGE)
847 #if defined(TARGET_SDL)
848 return TRUE; /* the only possible message here is SDL_QUIT */
849 #elif defined(PLATFORM_UNIX)
850 if ((event->window == window->drawable) &&
851 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
859 /* ========================================================================= */
860 /* joystick functions */
861 /* ========================================================================= */
863 inline void InitJoysticks()
868 return; /* joysticks generally deactivated by compile-time directive */
871 /* always start with reliable default values */
872 joystick.status = JOYSTICK_NOT_AVAILABLE;
873 for (i=0; i<MAX_PLAYERS; i++)
874 joystick.fd[i] = -1; /* joystick device closed */
876 #if defined(TARGET_SDL)
878 #elif defined(PLATFORM_UNIX)
880 #elif defined(PLATFORM_MSDOS)
881 MSDOSInitJoysticks();
885 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
887 #if defined(TARGET_SDL)
888 return SDLReadJoystick(nr, x, y, b1, b2);
889 #elif defined(PLATFORM_UNIX)
890 return UnixReadJoystick(nr, x, y, b1, b2);
891 #elif defined(PLATFORM_MSDOS)
892 return MSDOSReadJoystick(nr, x, y, b1, b2);