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 struct LevelDirInfo *leveldir_first = NULL;
44 struct LevelDirInfo *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_basename, char *x11_iconmask_basename,
116 char *msdos_pointer_basename,
117 char *cookie_prefix, char *filename_prefix,
120 char *x11_icon_filename =
121 getPath2(options.graphics_directory, x11_icon_basename);
122 char *x11_iconmask_filename =
123 getPath2(options.graphics_directory, x11_iconmask_basename);
124 char *msdos_pointer_filename =
125 getPath2(options.graphics_directory, msdos_pointer_basename);
127 #if defined(PLATFORM_UNIX)
128 program.userdata_directory = unix_userdata_directory;
130 program.userdata_directory = "userdata";
133 program.program_title = program_title;
134 program.window_title = window_title;
135 program.icon_title = icon_title;
136 program.x11_icon_filename = x11_icon_filename;
137 program.x11_iconmask_filename = x11_iconmask_filename;
138 program.msdos_pointer_filename = msdos_pointer_filename;
140 program.cookie_prefix = cookie_prefix;
141 program.filename_prefix = filename_prefix;
143 program.version_major = VERSION_MAJOR(program_version);
144 program.version_minor = VERSION_MINOR(program_version);
145 program.version_patch = VERSION_PATCH(program_version);
148 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
149 int real_sx, int real_sy,
150 int full_sxsize, int full_sysize)
156 gfx.real_sx = real_sx;
157 gfx.real_sy = real_sy;
158 gfx.full_sxsize = full_sxsize;
159 gfx.full_sysize = full_sysize;
161 SetDrawDeactivationMask(REDRAW_NONE); /* do not deactivate drawing */
164 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
172 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
180 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
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;
187 void SetDrawDeactivationMask(int draw_deactivation_mask)
189 gfx.draw_deactivation_mask = draw_deactivation_mask;
193 /* ========================================================================= */
194 /* video functions */
195 /* ========================================================================= */
197 inline static int GetRealDepth(int depth)
199 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
202 inline void InitVideoDisplay(void)
204 #if defined(TARGET_SDL)
205 SDLInitVideoDisplay();
207 X11InitVideoDisplay();
211 inline void CloseVideoDisplay(void)
213 KeyboardAutoRepeatOn();
215 #if defined(TARGET_SDL)
216 SDL_QuitSubSystem(SDL_INIT_VIDEO);
219 XCloseDisplay(display);
223 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
224 int width, int height, int depth,
228 video.height = height;
229 video.depth = GetRealDepth(depth);
230 video.fullscreen_available = FULLSCREEN_STATUS;
231 video.fullscreen_enabled = FALSE;
234 SDLInitVideoBuffer(backbuffer, window, fullscreen);
236 X11InitVideoBuffer(backbuffer, window);
240 inline Bitmap *CreateBitmapStruct(void)
243 return checked_calloc(sizeof(struct SDLSurfaceInfo));
245 return checked_calloc(sizeof(struct X11DrawableInfo));
249 inline Bitmap *CreateBitmap(int width, int height, int depth)
251 Bitmap *new_bitmap = CreateBitmapStruct();
252 int real_depth = GetRealDepth(depth);
255 SDL_Surface *surface_tmp, *surface_native;
257 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
258 real_depth, 0, 0, 0, 0))
260 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
262 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
263 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
265 SDL_FreeSurface(surface_tmp);
267 new_bitmap->surface = surface_native;
271 if ((pixmap = XCreatePixmap(display, window->drawable,
272 width, height, real_depth))
274 Error(ERR_EXIT, "cannot create pixmap");
276 new_bitmap->drawable = pixmap;
279 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
281 new_bitmap->gc = window->gc;
283 new_bitmap->line_gc[0] = window->line_gc[0];
284 new_bitmap->line_gc[1] = window->line_gc[1];
290 inline void FreeBitmap(Bitmap *bitmap)
297 SDL_FreeSurface(bitmap->surface);
298 if (bitmap->surface_masked)
299 SDL_FreeSurface(bitmap->surface_masked);
301 if (bitmap->drawable)
302 XFreePixmap(display, bitmap->drawable);
303 if (bitmap->clip_mask)
304 XFreePixmap(display, bitmap->clip_mask);
305 if (bitmap->stored_clip_gc)
306 XFreeGC(display, bitmap->stored_clip_gc);
309 if (bitmap->source_filename)
310 free(bitmap->source_filename);
315 inline void CloseWindow(DrawWindow *window)
318 if (window->drawable)
320 XUnmapWindow(display, window->drawable);
321 XDestroyWindow(display, window->drawable);
324 XFreeGC(display, window->gc);
328 inline boolean DrawingDeactivated(int x, int y, int width, int height)
330 if (gfx.draw_deactivation_mask != REDRAW_NONE)
332 if ((gfx.draw_deactivation_mask & REDRAW_FIELD) &&
333 x < gfx.sx + gfx.sxsize)
340 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
341 int src_x, int src_y,
342 int width, int height,
343 int dst_x, int dst_y)
345 if (DrawingDeactivated(dst_x, dst_y, width, height))
349 SDLCopyArea(src_bitmap, dst_bitmap,
350 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
352 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
353 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
357 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
359 if (DrawingDeactivated(x, y, width, height))
363 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
365 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
371 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
375 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
380 bitmap->clip_gc = clip_gc;
381 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
384 last_clip_gc = clip_gc;
389 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
394 bitmap->clip_gc = clip_gc;
395 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
398 last_clip_gc = clip_gc;
403 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
404 int src_x, int src_y,
405 int width, int height,
406 int dst_x, int dst_y)
408 if (DrawingDeactivated(dst_x, dst_y, width, height))
412 SDLCopyArea(src_bitmap, dst_bitmap,
413 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
415 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
416 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
420 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
424 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
426 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
427 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
428 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
432 #if !defined(TARGET_X11_NATIVE)
433 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
434 int to_x, int to_y, Pixel pixel, int line_width)
438 for (x=0; x<line_width; x++)
440 for (y=0; y<line_width; y++)
442 int dx = x - line_width / 2;
443 int dy = y - line_width / 2;
445 if ((x == 0 && y == 0) ||
446 (x == 0 && y == line_width - 1) ||
447 (x == line_width - 1 && y == 0) ||
448 (x == line_width - 1 && y == line_width - 1))
451 #if defined(TARGET_SDL)
453 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
454 #elif defined(TARGET_ALLEGRO)
455 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
456 to_x + dx, to_y + dy, pixel);
463 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
466 #if !defined(TARGET_X11_NATIVE)
470 for (i=0; i<num_points - 1; i++)
471 DrawLine(bitmap, points[i].x, points[i].y,
472 points[i + 1].x, points[i + 1].y, pixel, line_width);
475 SDLDrawLines(bitmap->surface, points, num_points, pixel);
478 XSetForeground(display, bitmap->line_gc[1], pixel);
479 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
480 (XPoint *)points, num_points, CoordModeOrigin);
482 XSetForeground(display, gc, BlackPixel(display, screen));
487 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
489 #if defined(TARGET_SDL)
490 return SDLGetPixel(bitmap, x, y);
491 #elif defined(TARGET_ALLEGRO)
492 return AllegroGetPixel(bitmap->drawable, x, y);
494 unsigned long pixel_value;
497 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
499 pixel_value = XGetPixel(pixel_image, 0, 0);
501 XDestroyImage(pixel_image);
507 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
508 unsigned int color_g, unsigned int color_b)
512 #if defined(TARGET_SDL)
513 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
514 #elif defined(TARGET_ALLEGRO)
515 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
516 #elif defined(TARGET_X11_NATIVE)
519 xcolor.flags = DoRed | DoGreen | DoBlue;
520 xcolor.red = (color_r << 8);
521 xcolor.green = (color_g << 8);
522 xcolor.blue = (color_b << 8);
523 XAllocColor(display, cmap, &xcolor);
524 pixel = xcolor.pixel;
530 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
532 unsigned int color_r = (color >> 16) & 0xff;
533 unsigned int color_g = (color >> 8) & 0xff;
534 unsigned int color_b = (color >> 0) & 0xff;
536 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
539 /* execute all pending screen drawing operations */
540 inline void FlushDisplay(void)
547 /* execute and wait for all pending screen drawing operations */
548 inline void SyncDisplay(void)
551 XSync(display, FALSE);
555 inline void KeyboardAutoRepeatOn(void)
558 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
559 SDL_DEFAULT_REPEAT_INTERVAL / 2);
560 SDL_EnableUNICODE(1);
563 XAutoRepeatOn(display);
567 inline void KeyboardAutoRepeatOff(void)
570 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
571 SDL_EnableUNICODE(0);
574 XAutoRepeatOff(display);
578 inline boolean PointerInWindow(DrawWindow *window)
588 /* if XQueryPointer() returns False, the pointer
589 is not on the same screen as the specified window */
590 return XQueryPointer(display, window->drawable, &root, &child,
591 &root_x, &root_y, &win_x, &win_y, &mask);
595 inline boolean SetVideoMode(boolean fullscreen)
598 return SDLSetVideoMode(&backbuffer, fullscreen);
600 boolean success = TRUE;
602 if (fullscreen && video.fullscreen_available)
604 Error(ERR_WARN, "fullscreen not available in X11 version");
606 /* display error message only once */
607 video.fullscreen_available = FALSE;
616 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
619 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
620 (!fullscreen && video.fullscreen_enabled))
621 fullscreen = SetVideoMode(fullscreen);
627 Bitmap *LoadImage(char *filename)
631 #if defined(TARGET_SDL)
632 new_bitmap = SDLLoadImage(filename);
634 new_bitmap = X11LoadImage(filename);
640 Bitmap *LoadCustomImage(char *basename)
642 char *filename = getStringCopy(getCustomImageFilename(basename));
645 if ((new_bitmap = LoadImage(filename)) == NULL)
646 Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
648 new_bitmap->source_filename = filename;
654 /* ========================================================================= */
655 /* audio functions */
656 /* ========================================================================= */
658 inline void OpenAudio(void)
660 /* always start with reliable default values */
661 audio.sound_available = FALSE;
662 audio.music_available = FALSE;
663 audio.loops_available = FALSE;
664 audio.mods_available = FALSE;
665 audio.sound_enabled = FALSE;
667 audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0;
668 audio.soundserver_pid = 0;
669 audio.device_name = NULL;
673 audio.music_channel = 0;
676 #if defined(TARGET_SDL)
678 #elif defined(PLATFORM_UNIX)
680 #elif defined(PLATFORM_MSDOS)
685 inline void CloseAudio(void)
687 #if defined(TARGET_SDL)
689 #elif defined(PLATFORM_UNIX)
691 #elif defined(PLATFORM_MSDOS)
695 audio.sound_enabled = FALSE;
698 inline void SetAudioMode(boolean enabled)
700 if (!audio.sound_available)
703 audio.sound_enabled = enabled;
707 /* ========================================================================= */
708 /* event functions */
709 /* ========================================================================= */
711 inline void InitEventFilter(EventFilter filter_function)
714 /* set event filter to filter out certain events */
715 SDL_SetEventFilter(filter_function);
719 inline boolean PendingEvent(void)
722 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
724 return (XPending(display) ? TRUE : FALSE);
728 inline void NextEvent(Event *event)
733 XNextEvent(display, event);
737 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
741 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
742 (int)event->keysym.unicode,
743 (int)event->keysym.sym,
744 (int)SDL_GetModState());
747 if (with_modifiers && event->keysym.unicode != 0)
748 return event->keysym.unicode;
750 return event->keysym.sym;
753 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
754 (int)XLookupKeysym(event, event->state),
755 (int)XLookupKeysym(event, 0));
759 return XLookupKeysym(event, event->state);
761 return XLookupKeysym(event, 0);
765 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
767 if (event->type != EVENT_CLIENTMESSAGE)
770 #if defined(TARGET_SDL)
771 return TRUE; /* the only possible message here is SDL_QUIT */
772 #elif defined(PLATFORM_UNIX)
773 if ((event->window == window->drawable) &&
774 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
782 /* ========================================================================= */
783 /* joystick functions */
784 /* ========================================================================= */
786 inline void InitJoysticks()
791 return; /* joysticks generally deactivated by compile-time directive */
794 /* always start with reliable default values */
795 joystick.status = JOYSTICK_NOT_AVAILABLE;
796 for (i=0; i<MAX_PLAYERS; i++)
797 joystick.fd[i] = -1; /* joystick device closed */
799 #if defined(TARGET_SDL)
801 #elif defined(PLATFORM_UNIX)
803 #elif defined(PLATFORM_MSDOS)
804 MSDOSInitJoysticks();
808 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
810 #if defined(TARGET_SDL)
811 return SDLReadJoystick(nr, x, y, b1, b2);
812 #elif defined(PLATFORM_UNIX)
813 return UnixReadJoystick(nr, x, y, b1, b2);
814 #elif defined(PLATFORM_MSDOS)
815 return MSDOSReadJoystick(nr, x, y, b1, b2);