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;
40 struct ArtworkInfo artwork;
41 struct JoystickInfo joystick;
42 struct SetupInfo setup;
44 LevelDirTree *leveldir_first = NULL;
45 LevelDirTree *leveldir_current = NULL;
48 Display *display = NULL;
49 Visual *visual = NULL;
53 DrawWindow *window = NULL;
54 DrawBuffer *backbuffer = NULL;
55 DrawBuffer *drawto = NULL;
57 int button_status = MB_NOT_PRESSED;
58 boolean motion_status = FALSE;
60 int redraw_mask = REDRAW_NONE;
66 /* ========================================================================= */
67 /* init/close functions */
68 /* ========================================================================= */
70 void InitCommandName(char *argv0)
72 program.command_basename =
73 (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
76 void InitExitFunction(void (*exit_function)(int))
78 program.exit_function = exit_function;
80 /* set signal handlers to custom exit function */
81 signal(SIGINT, exit_function);
82 signal(SIGTERM, exit_function);
84 #if defined(TARGET_SDL)
85 /* set exit function to automatically cleanup SDL stuff after exit() */
90 void InitPlatformDependantStuff(void)
92 #if defined(PLATFORM_MSDOS)
96 #if !defined(PLATFORM_UNIX)
97 program.userdata_directory = "userdata";
100 #if defined(PLATFORM_MSDOS)
104 #if defined(TARGET_SDL)
105 if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
106 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
110 void ClosePlatformDependantStuff(void)
112 #if defined(PLATFORM_MSDOS)
117 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
118 char *window_title, char *icon_title,
119 char *x11_icon_filename, char *x11_iconmask_filename,
120 char *msdos_pointer_filename,
121 char *cookie_prefix, char *filename_prefix,
124 #if defined(PLATFORM_UNIX)
125 program.userdata_directory = unix_userdata_directory;
127 program.userdata_directory = "userdata";
130 program.program_title = program_title;
131 program.window_title = window_title;
132 program.icon_title = icon_title;
133 program.x11_icon_filename = x11_icon_filename;
134 program.x11_iconmask_filename = x11_iconmask_filename;
135 program.msdos_pointer_filename = msdos_pointer_filename;
137 program.cookie_prefix = cookie_prefix;
138 program.filename_prefix = filename_prefix;
140 program.version_major = VERSION_MAJOR(program_version);
141 program.version_minor = VERSION_MINOR(program_version);
142 program.version_patch = VERSION_PATCH(program_version);
145 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
146 int real_sx, int real_sy,
147 int full_sxsize, int full_sysize)
153 gfx.real_sx = real_sx;
154 gfx.real_sy = real_sy;
155 gfx.full_sxsize = full_sxsize;
156 gfx.full_sysize = full_sysize;
158 SetDrawDeactivationMask(REDRAW_NONE); /* do not deactivate drawing */
161 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
169 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
177 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
179 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
180 gfx.scrollbuffer_width = scrollbuffer_width;
181 gfx.scrollbuffer_height = scrollbuffer_height;
184 void SetDrawDeactivationMask(int draw_deactivation_mask)
186 gfx.draw_deactivation_mask = draw_deactivation_mask;
189 void SetBackgroundBitmap(Bitmap *background_bitmap)
191 gfx.background_bitmap = background_bitmap;
195 /* ========================================================================= */
196 /* video functions */
197 /* ========================================================================= */
199 inline static int GetRealDepth(int depth)
201 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
204 inline void InitVideoDisplay(void)
206 #if defined(TARGET_SDL)
207 SDLInitVideoDisplay();
209 X11InitVideoDisplay();
213 inline void CloseVideoDisplay(void)
215 KeyboardAutoRepeatOn();
217 #if defined(TARGET_SDL)
218 SDL_QuitSubSystem(SDL_INIT_VIDEO);
222 XCloseDisplay(display);
226 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
227 int width, int height, int depth,
231 video.height = height;
232 video.depth = GetRealDepth(depth);
233 video.fullscreen_available = FULLSCREEN_STATUS;
234 video.fullscreen_enabled = FALSE;
237 SDLInitVideoBuffer(backbuffer, window, fullscreen);
239 X11InitVideoBuffer(backbuffer, window);
243 inline Bitmap *CreateBitmapStruct(void)
246 return checked_calloc(sizeof(struct SDLSurfaceInfo));
248 return checked_calloc(sizeof(struct X11DrawableInfo));
252 inline Bitmap *CreateBitmap(int width, int height, int depth)
254 Bitmap *new_bitmap = CreateBitmapStruct();
255 int real_depth = GetRealDepth(depth);
258 SDL_Surface *surface_tmp, *surface_native;
260 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
261 real_depth, 0, 0, 0, 0))
263 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
265 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
266 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
268 SDL_FreeSurface(surface_tmp);
270 new_bitmap->surface = surface_native;
274 if ((pixmap = XCreatePixmap(display, window->drawable,
275 width, height, real_depth))
277 Error(ERR_EXIT, "cannot create pixmap");
279 new_bitmap->drawable = pixmap;
282 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
284 new_bitmap->gc = window->gc;
286 new_bitmap->line_gc[0] = window->line_gc[0];
287 new_bitmap->line_gc[1] = window->line_gc[1];
293 inline static void FreeBitmapPointers(Bitmap *bitmap)
300 SDL_FreeSurface(bitmap->surface);
301 if (bitmap->surface_masked)
302 SDL_FreeSurface(bitmap->surface_masked);
303 bitmap->surface = NULL;
304 bitmap->surface_masked = NULL;
306 /* The X11 version seems to have a memory leak here -- although
307 "XFreePixmap()" is called, the correspondig memory seems not
308 to be freed (according to "ps"). The SDL version apparently
309 does not have this problem. */
311 if (bitmap->drawable)
312 XFreePixmap(display, bitmap->drawable);
313 if (bitmap->clip_mask)
314 XFreePixmap(display, bitmap->clip_mask);
315 if (bitmap->stored_clip_gc)
316 XFreeGC(display, bitmap->stored_clip_gc);
317 /* the other GCs are only pointers to GCs used elsewhere */
318 bitmap->drawable = None;
319 bitmap->clip_mask = None;
320 bitmap->stored_clip_gc = None;
323 if (bitmap->source_filename)
324 free(bitmap->source_filename);
325 bitmap->source_filename = NULL;
328 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
331 if (src_bitmap == NULL || dst_bitmap == NULL)
334 FreeBitmapPointers(dst_bitmap);
336 *dst_bitmap = *src_bitmap;
339 inline void FreeBitmap(Bitmap *bitmap)
344 FreeBitmapPointers(bitmap);
349 inline void CloseWindow(DrawWindow *window)
352 if (window->drawable)
354 XUnmapWindow(display, window->drawable);
355 XDestroyWindow(display, window->drawable);
358 XFreeGC(display, window->gc);
362 inline boolean DrawingDeactivated(int x, int y, int width, int height)
364 if (gfx.draw_deactivation_mask != REDRAW_NONE)
366 if (gfx.draw_deactivation_mask & REDRAW_ALL)
368 else if ((gfx.draw_deactivation_mask & REDRAW_FIELD) &&
369 x < gfx.sx + gfx.sxsize)
371 else if ((gfx.draw_deactivation_mask & REDRAW_DOORS) &&
374 if ((gfx.draw_deactivation_mask & REDRAW_DOOR_1) &&
375 y < gfx.dy + gfx.dysize)
377 else if ((gfx.draw_deactivation_mask & REDRAW_DOOR_2) &&
386 inline boolean DrawingOnBackground(int x, int y)
388 return (gfx.background_bitmap != NULL && x < gfx.sx + gfx.sxsize);
391 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
392 int src_x, int src_y,
393 int width, int height,
394 int dst_x, int dst_y)
396 if (DrawingDeactivated(dst_x, dst_y, width, height))
400 SDLCopyArea(src_bitmap, dst_bitmap,
401 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
403 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
404 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
408 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
410 if (DrawingDeactivated(x, y, width, height))
414 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
416 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
420 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
421 int width, int height)
423 if (!DrawingOnBackground(x, y))
424 ClearRectangle(bitmap, x, y, width, height);
426 BlitBitmap(gfx.background_bitmap, bitmap,
427 x - gfx.real_sx, y - gfx.real_sy, width, height, x, y);
432 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
436 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
441 bitmap->clip_gc = clip_gc;
442 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
445 last_clip_gc = clip_gc;
450 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
455 bitmap->clip_gc = clip_gc;
456 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
459 last_clip_gc = clip_gc;
464 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
465 int src_x, int src_y,
466 int width, int height,
467 int dst_x, int dst_y)
469 if (DrawingDeactivated(dst_x, dst_y, width, height))
473 SDLCopyArea(src_bitmap, dst_bitmap,
474 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
476 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
477 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
481 inline void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
482 int src_x, int src_y,
483 int width, int height,
484 int dst_x, int dst_y)
486 if (!DrawingOnBackground(src_x, src_y))
487 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
491 /* draw background */
492 BlitBitmap(gfx.background_bitmap, dst_bitmap,
493 dst_x - gfx.real_sx, dst_y - gfx.real_sy, width, height,
496 /* draw foreground */
497 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
498 dst_x - src_x, dst_y - src_y);
499 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
504 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
508 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
510 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
511 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
512 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
516 #if !defined(TARGET_X11_NATIVE)
517 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
518 int to_x, int to_y, Pixel pixel, int line_width)
522 for (x=0; x<line_width; x++)
524 for (y=0; y<line_width; y++)
526 int dx = x - line_width / 2;
527 int dy = y - line_width / 2;
529 if ((x == 0 && y == 0) ||
530 (x == 0 && y == line_width - 1) ||
531 (x == line_width - 1 && y == 0) ||
532 (x == line_width - 1 && y == line_width - 1))
535 #if defined(TARGET_SDL)
537 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
538 #elif defined(TARGET_ALLEGRO)
539 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
540 to_x + dx, to_y + dy, pixel);
547 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
550 #if !defined(TARGET_X11_NATIVE)
554 for (i=0; i<num_points - 1; i++)
555 DrawLine(bitmap, points[i].x, points[i].y,
556 points[i + 1].x, points[i + 1].y, pixel, line_width);
559 SDLDrawLines(bitmap->surface, points, num_points, pixel);
562 XSetForeground(display, bitmap->line_gc[1], pixel);
563 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
564 (XPoint *)points, num_points, CoordModeOrigin);
566 XSetForeground(display, gc, BlackPixel(display, screen));
571 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
573 #if defined(TARGET_SDL)
574 return SDLGetPixel(bitmap, x, y);
575 #elif defined(TARGET_ALLEGRO)
576 return AllegroGetPixel(bitmap->drawable, x, y);
578 unsigned long pixel_value;
581 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
583 pixel_value = XGetPixel(pixel_image, 0, 0);
585 XDestroyImage(pixel_image);
591 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
592 unsigned int color_g, unsigned int color_b)
596 #if defined(TARGET_SDL)
597 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
598 #elif defined(TARGET_ALLEGRO)
599 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
600 #elif defined(TARGET_X11_NATIVE)
603 xcolor.flags = DoRed | DoGreen | DoBlue;
604 xcolor.red = (color_r << 8);
605 xcolor.green = (color_g << 8);
606 xcolor.blue = (color_b << 8);
607 XAllocColor(display, cmap, &xcolor);
608 pixel = xcolor.pixel;
614 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
616 unsigned int color_r = (color >> 16) & 0xff;
617 unsigned int color_g = (color >> 8) & 0xff;
618 unsigned int color_b = (color >> 0) & 0xff;
620 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
623 /* execute all pending screen drawing operations */
624 inline void FlushDisplay(void)
631 /* execute and wait for all pending screen drawing operations */
632 inline void SyncDisplay(void)
635 XSync(display, FALSE);
639 inline void KeyboardAutoRepeatOn(void)
642 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
643 SDL_DEFAULT_REPEAT_INTERVAL / 2);
644 SDL_EnableUNICODE(1);
647 XAutoRepeatOn(display);
651 inline void KeyboardAutoRepeatOff(void)
654 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
655 SDL_EnableUNICODE(0);
658 XAutoRepeatOff(display);
662 inline boolean PointerInWindow(DrawWindow *window)
672 /* if XQueryPointer() returns False, the pointer
673 is not on the same screen as the specified window */
674 return XQueryPointer(display, window->drawable, &root, &child,
675 &root_x, &root_y, &win_x, &win_y, &mask);
679 inline boolean SetVideoMode(boolean fullscreen)
682 return SDLSetVideoMode(&backbuffer, fullscreen);
684 boolean success = TRUE;
686 if (fullscreen && video.fullscreen_available)
688 Error(ERR_WARN, "fullscreen not available in X11 version");
690 /* display error message only once */
691 video.fullscreen_available = FALSE;
700 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
703 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
704 (!fullscreen && video.fullscreen_enabled))
705 fullscreen = SetVideoMode(fullscreen);
711 Bitmap *LoadImage(char *filename)
715 #if defined(TARGET_SDL)
716 new_bitmap = SDLLoadImage(filename);
718 new_bitmap = X11LoadImage(filename);
722 new_bitmap->source_filename = getStringCopy(filename);
727 Bitmap *LoadCustomImage(char *basename)
729 char *filename = getCustomImageFilename(basename);
732 if (filename == NULL)
733 Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
735 if ((new_bitmap = LoadImage(filename)) == NULL)
736 Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
741 void ReloadCustomImage(Bitmap *bitmap, char *basename)
743 char *filename = getCustomImageFilename(basename);
746 if (filename == NULL) /* (should never happen) */
748 Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
752 if (strcmp(filename, bitmap->source_filename) == 0)
754 /* The old and new image are the same (have the same filename and path).
755 This usually means that this image does not exist in this graphic set
756 and a fallback to the existing image is done. */
761 if ((new_bitmap = LoadImage(filename)) == NULL)
763 Error(ERR_WARN, "LoadImage() failed: %s", GetError());
767 if (bitmap->width != new_bitmap->width ||
768 bitmap->height != new_bitmap->height)
770 Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
772 FreeBitmap(new_bitmap);
776 TransferBitmapPointers(new_bitmap, bitmap);
781 /* ========================================================================= */
782 /* audio functions */
783 /* ========================================================================= */
785 inline void OpenAudio(void)
787 /* always start with reliable default values */
788 audio.sound_available = FALSE;
789 audio.music_available = FALSE;
790 audio.loops_available = FALSE;
792 audio.sound_enabled = FALSE;
793 audio.sound_deactivated = FALSE;
795 audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
796 audio.mixer_pid = -1;
797 audio.device_name = NULL;
798 audio.device_fd = -1;
800 audio.num_channels = 0;
801 audio.music_channel = 0;
802 audio.first_sound_channel = 0;
804 #if defined(TARGET_SDL)
806 #elif defined(PLATFORM_UNIX)
808 #elif defined(PLATFORM_MSDOS)
813 inline void CloseAudio(void)
815 #if defined(TARGET_SDL)
817 #elif defined(PLATFORM_UNIX)
819 #elif defined(PLATFORM_MSDOS)
823 audio.sound_enabled = FALSE;
826 inline void SetAudioMode(boolean enabled)
828 if (!audio.sound_available)
831 audio.sound_enabled = enabled;
835 /* ========================================================================= */
836 /* event functions */
837 /* ========================================================================= */
839 inline void InitEventFilter(EventFilter filter_function)
842 /* set event filter to filter out certain events */
843 SDL_SetEventFilter(filter_function);
847 inline boolean PendingEvent(void)
850 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
852 return (XPending(display) ? TRUE : FALSE);
856 inline void NextEvent(Event *event)
861 XNextEvent(display, event);
865 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
869 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
870 (int)event->keysym.unicode,
871 (int)event->keysym.sym,
872 (int)SDL_GetModState());
875 if (with_modifiers &&
876 event->keysym.unicode > 0x0000 &&
877 event->keysym.unicode < 0x2000)
878 return event->keysym.unicode;
880 return event->keysym.sym;
883 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
884 (int)XLookupKeysym(event, event->state),
885 (int)XLookupKeysym(event, 0));
889 return XLookupKeysym(event, event->state);
891 return XLookupKeysym(event, 0);
895 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
897 if (event->type != EVENT_CLIENTMESSAGE)
900 #if defined(TARGET_SDL)
901 return TRUE; /* the only possible message here is SDL_QUIT */
902 #elif defined(PLATFORM_UNIX)
903 if ((event->window == window->drawable) &&
904 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
912 /* ========================================================================= */
913 /* joystick functions */
914 /* ========================================================================= */
916 inline void InitJoysticks()
921 return; /* joysticks generally deactivated by compile-time directive */
924 /* always start with reliable default values */
925 joystick.status = JOYSTICK_NOT_AVAILABLE;
926 for (i=0; i<MAX_PLAYERS; i++)
927 joystick.fd[i] = -1; /* joystick device closed */
929 #if defined(TARGET_SDL)
931 #elif defined(PLATFORM_UNIX)
933 #elif defined(PLATFORM_MSDOS)
934 MSDOSInitJoysticks();
938 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
940 #if defined(TARGET_SDL)
941 return SDLReadJoystick(nr, x, y, b1, b2);
942 #elif defined(PLATFORM_UNIX)
943 return UnixReadJoystick(nr, x, y, b1, b2);
944 #elif defined(PLATFORM_MSDOS)
945 return MSDOSReadJoystick(nr, x, y, b1, b2);