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)
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";
99 #if defined(PLATFORM_MSDOS)
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());
109 void ClosePlatformDependantStuff(void)
111 #if defined(PLATFORM_MSDOS)
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,
123 #if defined(PLATFORM_UNIX)
124 program.userdata_directory = unix_userdata_directory;
126 program.userdata_directory = "userdata";
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;
136 program.cookie_prefix = cookie_prefix;
137 program.filename_prefix = filename_prefix;
139 program.version_major = VERSION_MAJOR(program_version);
140 program.version_minor = VERSION_MINOR(program_version);
141 program.version_patch = VERSION_PATCH(program_version);
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)
152 gfx.real_sx = real_sx;
153 gfx.real_sy = real_sy;
154 gfx.full_sxsize = full_sxsize;
155 gfx.full_sysize = full_sysize;
157 SetDrawDeactivationMask(REDRAW_NONE); /* do not deactivate 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;
189 /* ========================================================================= */
190 /* video functions */
191 /* ========================================================================= */
193 inline static int GetRealDepth(int depth)
195 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
198 inline void InitVideoDisplay(void)
200 #if defined(TARGET_SDL)
201 SDLInitVideoDisplay();
203 X11InitVideoDisplay();
207 inline void CloseVideoDisplay(void)
209 KeyboardAutoRepeatOn();
211 #if defined(TARGET_SDL)
212 SDL_QuitSubSystem(SDL_INIT_VIDEO);
216 XCloseDisplay(display);
220 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
221 int width, int height, int depth,
225 video.height = height;
226 video.depth = GetRealDepth(depth);
227 video.fullscreen_available = FULLSCREEN_STATUS;
228 video.fullscreen_enabled = FALSE;
231 SDLInitVideoBuffer(backbuffer, window, fullscreen);
233 X11InitVideoBuffer(backbuffer, window);
237 inline Bitmap *CreateBitmapStruct(void)
240 return checked_calloc(sizeof(struct SDLSurfaceInfo));
242 return checked_calloc(sizeof(struct X11DrawableInfo));
246 inline Bitmap *CreateBitmap(int width, int height, int depth)
248 Bitmap *new_bitmap = CreateBitmapStruct();
249 int real_depth = GetRealDepth(depth);
252 SDL_Surface *surface_tmp, *surface_native;
254 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
255 real_depth, 0, 0, 0, 0))
257 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
259 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
260 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
262 SDL_FreeSurface(surface_tmp);
264 new_bitmap->surface = surface_native;
268 if ((pixmap = XCreatePixmap(display, window->drawable,
269 width, height, real_depth))
271 Error(ERR_EXIT, "cannot create pixmap");
273 new_bitmap->drawable = pixmap;
276 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
278 new_bitmap->gc = window->gc;
280 new_bitmap->line_gc[0] = window->line_gc[0];
281 new_bitmap->line_gc[1] = window->line_gc[1];
287 inline static void FreeBitmapPointers(Bitmap *bitmap)
294 SDL_FreeSurface(bitmap->surface);
295 if (bitmap->surface_masked)
296 SDL_FreeSurface(bitmap->surface_masked);
297 bitmap->surface = NULL;
298 bitmap->surface_masked = NULL;
300 /* The X11 version seems to have a memory leak here -- although
301 "XFreePixmap()" is called, the correspondig memory seems not
302 to be freed (according to "ps"). The SDL version apparently
303 does not have this problem. */
305 if (bitmap->drawable)
306 XFreePixmap(display, bitmap->drawable);
307 if (bitmap->clip_mask)
308 XFreePixmap(display, bitmap->clip_mask);
309 if (bitmap->stored_clip_gc)
310 XFreeGC(display, bitmap->stored_clip_gc);
311 /* the other GCs are only pointers to GCs used elsewhere */
312 bitmap->drawable = None;
313 bitmap->clip_mask = None;
314 bitmap->stored_clip_gc = None;
317 if (bitmap->source_filename)
318 free(bitmap->source_filename);
319 bitmap->source_filename = NULL;
322 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
325 if (src_bitmap == NULL || dst_bitmap == NULL)
328 FreeBitmapPointers(dst_bitmap);
330 *dst_bitmap = *src_bitmap;
333 inline void FreeBitmap(Bitmap *bitmap)
338 FreeBitmapPointers(bitmap);
343 inline void CloseWindow(DrawWindow *window)
346 if (window->drawable)
348 XUnmapWindow(display, window->drawable);
349 XDestroyWindow(display, window->drawable);
352 XFreeGC(display, window->gc);
356 inline boolean DrawingDeactivated(int x, int y, int width, int height)
358 if (gfx.draw_deactivation_mask != REDRAW_NONE)
360 if ((gfx.draw_deactivation_mask & REDRAW_FIELD) &&
361 x < gfx.sx + gfx.sxsize)
363 else if ((gfx.draw_deactivation_mask & REDRAW_DOORS) &&
366 if ((gfx.draw_deactivation_mask & REDRAW_DOOR_1) &&
367 y < gfx.dy + gfx.dysize)
369 else if ((gfx.draw_deactivation_mask & REDRAW_DOOR_2) &&
378 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
379 int src_x, int src_y,
380 int width, int height,
381 int dst_x, int dst_y)
383 if (DrawingDeactivated(dst_x, dst_y, width, height))
387 SDLCopyArea(src_bitmap, dst_bitmap,
388 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
390 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
391 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
395 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
397 if (DrawingDeactivated(x, y, width, height))
401 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
403 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
409 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
413 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
418 bitmap->clip_gc = clip_gc;
419 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
422 last_clip_gc = clip_gc;
427 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
432 bitmap->clip_gc = clip_gc;
433 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
436 last_clip_gc = clip_gc;
441 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
442 int src_x, int src_y,
443 int width, int height,
444 int dst_x, int dst_y)
446 if (DrawingDeactivated(dst_x, dst_y, width, height))
450 SDLCopyArea(src_bitmap, dst_bitmap,
451 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
453 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
454 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
458 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
462 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
464 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
465 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
466 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
470 #if !defined(TARGET_X11_NATIVE)
471 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
472 int to_x, int to_y, Pixel pixel, int line_width)
476 for (x=0; x<line_width; x++)
478 for (y=0; y<line_width; y++)
480 int dx = x - line_width / 2;
481 int dy = y - line_width / 2;
483 if ((x == 0 && y == 0) ||
484 (x == 0 && y == line_width - 1) ||
485 (x == line_width - 1 && y == 0) ||
486 (x == line_width - 1 && y == line_width - 1))
489 #if defined(TARGET_SDL)
491 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
492 #elif defined(TARGET_ALLEGRO)
493 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
494 to_x + dx, to_y + dy, pixel);
501 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
504 #if !defined(TARGET_X11_NATIVE)
508 for (i=0; i<num_points - 1; i++)
509 DrawLine(bitmap, points[i].x, points[i].y,
510 points[i + 1].x, points[i + 1].y, pixel, line_width);
513 SDLDrawLines(bitmap->surface, points, num_points, pixel);
516 XSetForeground(display, bitmap->line_gc[1], pixel);
517 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
518 (XPoint *)points, num_points, CoordModeOrigin);
520 XSetForeground(display, gc, BlackPixel(display, screen));
525 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
527 #if defined(TARGET_SDL)
528 return SDLGetPixel(bitmap, x, y);
529 #elif defined(TARGET_ALLEGRO)
530 return AllegroGetPixel(bitmap->drawable, x, y);
532 unsigned long pixel_value;
535 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
537 pixel_value = XGetPixel(pixel_image, 0, 0);
539 XDestroyImage(pixel_image);
545 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
546 unsigned int color_g, unsigned int color_b)
550 #if defined(TARGET_SDL)
551 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
552 #elif defined(TARGET_ALLEGRO)
553 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
554 #elif defined(TARGET_X11_NATIVE)
557 xcolor.flags = DoRed | DoGreen | DoBlue;
558 xcolor.red = (color_r << 8);
559 xcolor.green = (color_g << 8);
560 xcolor.blue = (color_b << 8);
561 XAllocColor(display, cmap, &xcolor);
562 pixel = xcolor.pixel;
568 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
570 unsigned int color_r = (color >> 16) & 0xff;
571 unsigned int color_g = (color >> 8) & 0xff;
572 unsigned int color_b = (color >> 0) & 0xff;
574 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
577 /* execute all pending screen drawing operations */
578 inline void FlushDisplay(void)
585 /* execute and wait for all pending screen drawing operations */
586 inline void SyncDisplay(void)
589 XSync(display, FALSE);
593 inline void KeyboardAutoRepeatOn(void)
596 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
597 SDL_DEFAULT_REPEAT_INTERVAL / 2);
598 SDL_EnableUNICODE(1);
601 XAutoRepeatOn(display);
605 inline void KeyboardAutoRepeatOff(void)
608 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
609 SDL_EnableUNICODE(0);
612 XAutoRepeatOff(display);
616 inline boolean PointerInWindow(DrawWindow *window)
626 /* if XQueryPointer() returns False, the pointer
627 is not on the same screen as the specified window */
628 return XQueryPointer(display, window->drawable, &root, &child,
629 &root_x, &root_y, &win_x, &win_y, &mask);
633 inline boolean SetVideoMode(boolean fullscreen)
636 return SDLSetVideoMode(&backbuffer, fullscreen);
638 boolean success = TRUE;
640 if (fullscreen && video.fullscreen_available)
642 Error(ERR_WARN, "fullscreen not available in X11 version");
644 /* display error message only once */
645 video.fullscreen_available = FALSE;
654 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
657 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
658 (!fullscreen && video.fullscreen_enabled))
659 fullscreen = SetVideoMode(fullscreen);
665 Bitmap *LoadImage(char *filename)
669 #if defined(TARGET_SDL)
670 new_bitmap = SDLLoadImage(filename);
672 new_bitmap = X11LoadImage(filename);
676 new_bitmap->source_filename = getStringCopy(filename);
681 Bitmap *LoadCustomImage(char *basename)
683 char *filename = getCustomImageFilename(basename);
686 if (filename == NULL)
687 Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
689 if ((new_bitmap = LoadImage(filename)) == NULL)
690 Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
695 void ReloadCustomImage(Bitmap *bitmap, char *basename)
697 char *filename = getCustomImageFilename(basename);
700 if (filename == NULL) /* (should never happen) */
702 Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
706 if (strcmp(filename, bitmap->source_filename) == 0)
708 /* The old and new image are the same (have the same filename and path).
709 This usually means that this image does not exist in this graphic set
710 and a fallback to the existing image is done. */
715 if ((new_bitmap = LoadImage(filename)) == NULL)
717 Error(ERR_WARN, "LoadImage() failed: %s", GetError());
721 if (bitmap->width != new_bitmap->width ||
722 bitmap->height != new_bitmap->height)
724 Error(ERR_WARN, "ReloadCustomImage: new image has wrong dimensions");
725 FreeBitmap(new_bitmap);
729 TransferBitmapPointers(new_bitmap, bitmap);
734 /* ========================================================================= */
735 /* audio functions */
736 /* ========================================================================= */
738 inline void OpenAudio(void)
740 /* always start with reliable default values */
741 audio.sound_available = FALSE;
742 audio.music_available = FALSE;
743 audio.loops_available = FALSE;
745 audio.sound_enabled = FALSE;
746 audio.sound_deactivated = FALSE;
748 audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
749 audio.mixer_pid = -1;
750 audio.device_name = NULL;
751 audio.device_fd = -1;
753 audio.num_channels = 0;
754 audio.music_channel = 0;
755 audio.first_sound_channel = 0;
757 #if defined(TARGET_SDL)
759 #elif defined(PLATFORM_UNIX)
761 #elif defined(PLATFORM_MSDOS)
766 inline void CloseAudio(void)
768 #if defined(TARGET_SDL)
770 #elif defined(PLATFORM_UNIX)
772 #elif defined(PLATFORM_MSDOS)
776 audio.sound_enabled = FALSE;
779 inline void SetAudioMode(boolean enabled)
781 if (!audio.sound_available)
784 audio.sound_enabled = enabled;
788 /* ========================================================================= */
789 /* event functions */
790 /* ========================================================================= */
792 inline void InitEventFilter(EventFilter filter_function)
795 /* set event filter to filter out certain events */
796 SDL_SetEventFilter(filter_function);
800 inline boolean PendingEvent(void)
803 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
805 return (XPending(display) ? TRUE : FALSE);
809 inline void NextEvent(Event *event)
814 XNextEvent(display, event);
818 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
822 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
823 (int)event->keysym.unicode,
824 (int)event->keysym.sym,
825 (int)SDL_GetModState());
828 if (with_modifiers &&
829 event->keysym.unicode > 0x0000 &&
830 event->keysym.unicode < 0x2000)
831 return event->keysym.unicode;
833 return event->keysym.sym;
836 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
837 (int)XLookupKeysym(event, event->state),
838 (int)XLookupKeysym(event, 0));
842 return XLookupKeysym(event, event->state);
844 return XLookupKeysym(event, 0);
848 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
850 if (event->type != EVENT_CLIENTMESSAGE)
853 #if defined(TARGET_SDL)
854 return TRUE; /* the only possible message here is SDL_QUIT */
855 #elif defined(PLATFORM_UNIX)
856 if ((event->window == window->drawable) &&
857 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
865 /* ========================================================================= */
866 /* joystick functions */
867 /* ========================================================================= */
869 inline void InitJoysticks()
874 return; /* joysticks generally deactivated by compile-time directive */
877 /* always start with reliable default values */
878 joystick.status = JOYSTICK_NOT_AVAILABLE;
879 for (i=0; i<MAX_PLAYERS; i++)
880 joystick.fd[i] = -1; /* joystick device closed */
882 #if defined(TARGET_SDL)
884 #elif defined(PLATFORM_UNIX)
886 #elif defined(PLATFORM_MSDOS)
887 MSDOSInitJoysticks();
891 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
893 #if defined(TARGET_SDL)
894 return SDLReadJoystick(nr, x, y, b1, b2);
895 #elif defined(PLATFORM_UNIX)
896 return UnixReadJoystick(nr, x, y, b1, b2);
897 #elif defined(PLATFORM_MSDOS)
898 return MSDOSReadJoystick(nr, x, y, b1, b2);