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 JoystickInfo joystick;
40 struct SetupInfo setup;
42 struct LevelDirInfo *leveldir_first = NULL;
43 struct LevelDirInfo *leveldir_current = NULL;
46 Display *display = NULL;
47 Visual *visual = NULL;
51 DrawWindow *window = NULL;
52 DrawBuffer *backbuffer = NULL;
53 DrawBuffer *drawto = NULL;
55 int button_status = MB_NOT_PRESSED;
56 boolean motion_status = FALSE;
58 int redraw_mask = REDRAW_NONE;
64 /* ========================================================================= */
65 /* init/close functions */
66 /* ========================================================================= */
68 void InitCommandName(char *argv0)
70 program.command_basename =
71 (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
74 void InitExitFunction(void (*exit_function)(int))
76 program.exit_function = exit_function;
78 /* set signal handlers to custom exit function */
79 signal(SIGINT, exit_function);
80 signal(SIGTERM, exit_function);
82 #if defined(TARGET_SDL)
83 /* set exit function to automatically cleanup SDL stuff after exit() */
88 void InitPlatformDependantStuff(void)
90 #if defined(PLATFORM_MSDOS)
94 #if !defined(PLATFORM_UNIX)
95 program.userdata_directory = "userdata";
99 #if defined(TARGET_SDL)
100 if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
101 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
105 void ClosePlatformDependantStuff(void)
107 #if !defined(PLATFORM_UNIX)
112 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
113 char *window_title, char *icon_title,
114 char *x11_icon_basename, char *x11_iconmask_basename,
115 char *msdos_pointer_basename,
116 char *cookie_prefix, int program_version)
118 char *x11_icon_filename =
119 getPath2(options.graphics_directory, x11_icon_basename);
120 char *x11_iconmask_filename =
121 getPath2(options.graphics_directory, x11_iconmask_basename);
122 char *msdos_pointer_filename =
123 getPath2(options.graphics_directory, msdos_pointer_basename);
125 #if defined(PLATFORM_UNIX)
126 program.userdata_directory = unix_userdata_directory;
128 program.userdata_directory = "userdata";
131 program.program_title = program_title;
132 program.window_title = window_title;
133 program.icon_title = icon_title;
134 program.x11_icon_filename = x11_icon_filename;
135 program.x11_iconmask_filename = x11_iconmask_filename;
136 program.msdos_pointer_filename = msdos_pointer_filename;
137 program.cookie_prefix = cookie_prefix;
138 program.version_major = VERSION_MAJOR(program_version);
139 program.version_minor = VERSION_MINOR(program_version);
140 program.version_patch = VERSION_PATCH(program_version);
143 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
144 int real_sx, int real_sy,
145 int full_sxsize, int full_sysize)
151 gfx.real_sx = real_sx;
152 gfx.real_sy = real_sy;
153 gfx.full_sxsize = full_sxsize;
154 gfx.full_sysize = full_sysize;
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;
181 /* ========================================================================= */
182 /* video functions */
183 /* ========================================================================= */
185 inline static int GetRealDepth(int depth)
187 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
190 inline void InitVideoDisplay(void)
192 #if defined(TARGET_SDL)
193 SDLInitVideoDisplay();
195 X11InitVideoDisplay();
199 inline void CloseVideoDisplay(void)
201 KeyboardAutoRepeatOn();
203 #if defined(TARGET_SDL)
204 SDL_QuitSubSystem(SDL_INIT_VIDEO);
207 XCloseDisplay(display);
211 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
212 int width, int height, int depth,
216 video.height = height;
217 video.depth = GetRealDepth(depth);
218 video.fullscreen_available = FULLSCREEN_STATUS;
219 video.fullscreen_enabled = FALSE;
222 SDLInitVideoBuffer(backbuffer, window, fullscreen);
224 X11InitVideoBuffer(backbuffer, window);
228 inline Bitmap *CreateBitmapStruct(void)
231 return checked_calloc(sizeof(struct SDLSurfaceInfo));
233 return checked_calloc(sizeof(struct X11DrawableInfo));
237 inline Bitmap *CreateBitmap(int width, int height, int depth)
239 Bitmap *new_bitmap = CreateBitmapStruct();
240 int real_depth = GetRealDepth(depth);
243 SDL_Surface *surface_tmp, *surface_native;
245 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
246 real_depth, 0, 0, 0, 0))
248 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
250 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
251 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
253 SDL_FreeSurface(surface_tmp);
255 new_bitmap->surface = surface_native;
259 if ((pixmap = XCreatePixmap(display, window->drawable,
260 width, height, real_depth))
262 Error(ERR_EXIT, "cannot create pixmap");
264 new_bitmap->drawable = pixmap;
267 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
269 new_bitmap->gc = window->gc;
271 new_bitmap->line_gc[0] = window->line_gc[0];
272 new_bitmap->line_gc[1] = window->line_gc[1];
278 inline void FreeBitmap(Bitmap *bitmap)
285 SDL_FreeSurface(bitmap->surface);
286 if (bitmap->surface_masked)
287 SDL_FreeSurface(bitmap->surface_masked);
289 if (bitmap->drawable)
290 XFreePixmap(display, bitmap->drawable);
291 if (bitmap->clip_mask)
292 XFreePixmap(display, bitmap->clip_mask);
293 if (bitmap->stored_clip_gc)
294 XFreeGC(display, bitmap->stored_clip_gc);
300 inline void CloseWindow(DrawWindow *window)
303 if (window->drawable)
305 XUnmapWindow(display, window->drawable);
306 XDestroyWindow(display, window->drawable);
309 XFreeGC(display, window->gc);
313 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
314 int src_x, int src_y,
315 int width, int height,
316 int dst_x, int dst_y)
319 SDLCopyArea(src_bitmap, dst_bitmap,
320 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
322 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
323 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
327 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
330 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
332 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
338 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
342 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
347 bitmap->clip_gc = clip_gc;
348 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
351 last_clip_gc = clip_gc;
356 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
361 bitmap->clip_gc = clip_gc;
362 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
365 last_clip_gc = clip_gc;
370 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
371 int src_x, int src_y,
372 int width, int height,
373 int dst_x, int dst_y)
376 SDLCopyArea(src_bitmap, dst_bitmap,
377 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
379 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
380 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
384 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
388 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
390 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
391 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
392 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
396 #if !defined(TARGET_X11_NATIVE)
397 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
398 int to_x, int to_y, Pixel pixel, int line_width)
402 for (x=0; x<line_width; x++)
404 for (y=0; y<line_width; y++)
406 int dx = x - line_width / 2;
407 int dy = y - line_width / 2;
409 if ((x == 0 && y == 0) ||
410 (x == 0 && y == line_width - 1) ||
411 (x == line_width - 1 && y == 0) ||
412 (x == line_width - 1 && y == line_width - 1))
415 #if defined(TARGET_SDL)
417 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
418 #elif defined(TARGET_ALLEGRO)
419 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
420 to_x + dx, to_y + dy, pixel);
427 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
430 #if !defined(TARGET_X11_NATIVE)
434 for (i=0; i<num_points - 1; i++)
435 DrawLine(bitmap, points[i].x, points[i].y,
436 points[i + 1].x, points[i + 1].y, pixel, line_width);
439 SDLDrawLines(bitmap->surface, points, num_points, pixel);
442 XSetForeground(display, bitmap->line_gc[1], pixel);
443 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
444 (XPoint *)points, num_points, CoordModeOrigin);
446 XSetForeground(display, gc, BlackPixel(display, screen));
451 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
453 #if defined(TARGET_SDL)
454 return SDLGetPixel(bitmap, x, y);
455 #elif defined(TARGET_ALLEGRO)
456 return AllegroGetPixel(bitmap->drawable, x, y);
458 unsigned long pixel_value;
461 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
463 pixel_value = XGetPixel(pixel_image, 0, 0);
465 XDestroyImage(pixel_image);
471 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
472 unsigned int color_g, unsigned int color_b)
476 #if defined(TARGET_SDL)
477 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
478 #elif defined(TARGET_ALLEGRO)
479 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
480 #elif defined(TARGET_X11_NATIVE)
483 xcolor.flags = DoRed | DoGreen | DoBlue;
484 xcolor.red = (color_r << 8);
485 xcolor.green = (color_g << 8);
486 xcolor.blue = (color_b << 8);
487 XAllocColor(display, cmap, &xcolor);
488 pixel = xcolor.pixel;
494 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
496 unsigned int color_r = (color >> 16) & 0xff;
497 unsigned int color_g = (color >> 8) & 0xff;
498 unsigned int color_b = (color >> 0) & 0xff;
500 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
503 /* execute all pending screen drawing operations */
504 inline void FlushDisplay(void)
511 /* execute and wait for all pending screen drawing operations */
512 inline void SyncDisplay(void)
515 XSync(display, FALSE);
519 inline void KeyboardAutoRepeatOn(void)
522 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
523 SDL_DEFAULT_REPEAT_INTERVAL / 2);
524 SDL_EnableUNICODE(1);
527 XAutoRepeatOn(display);
531 inline void KeyboardAutoRepeatOff(void)
534 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
535 SDL_EnableUNICODE(0);
538 XAutoRepeatOff(display);
542 inline boolean PointerInWindow(DrawWindow *window)
552 /* if XQueryPointer() returns False, the pointer
553 is not on the same screen as the specified window */
554 return XQueryPointer(display, window->drawable, &root, &child,
555 &root_x, &root_y, &win_x, &win_y, &mask);
559 inline boolean SetVideoMode(boolean fullscreen)
562 return SDLSetVideoMode(&backbuffer, fullscreen);
564 boolean success = TRUE;
566 if (fullscreen && video.fullscreen_available)
568 Error(ERR_WARN, "fullscreen not available in X11 version");
570 /* display error message only once */
571 video.fullscreen_available = FALSE;
580 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
583 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
584 (!fullscreen && video.fullscreen_enabled))
585 fullscreen = SetVideoMode(fullscreen);
591 Bitmap *LoadImage(char *basename)
594 char *filename = getPath2(options.graphics_directory, basename);
596 #if defined(TARGET_SDL)
597 new_bitmap = SDLLoadImage(filename);
599 new_bitmap = X11LoadImage(filename);
608 /* ========================================================================= */
609 /* audio functions */
610 /* ========================================================================= */
612 inline void OpenAudio(void)
614 /* always start with reliable default values */
615 audio.sound_available = FALSE;
616 audio.music_available = FALSE;
617 audio.loops_available = FALSE;
618 audio.mods_available = FALSE;
619 audio.sound_enabled = FALSE;
621 audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0;
622 audio.soundserver_pid = 0;
623 audio.device_name = NULL;
627 audio.music_channel = 0;
630 #if defined(TARGET_SDL)
632 #elif defined(PLATFORM_UNIX)
634 #elif defined(PLATFORM_MSDOS)
639 inline void CloseAudio(void)
641 #if defined(TARGET_SDL)
643 #elif defined(PLATFORM_UNIX)
645 #elif defined(PLATFORM_MSDOS)
649 audio.sound_enabled = FALSE;
652 inline void SetAudioMode(boolean enabled)
654 if (!audio.sound_available)
657 audio.sound_enabled = enabled;
661 /* ========================================================================= */
662 /* event functions */
663 /* ========================================================================= */
665 inline void InitEventFilter(EventFilter filter_function)
668 /* set event filter to filter out certain events */
669 SDL_SetEventFilter(filter_function);
673 inline boolean PendingEvent(void)
676 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
678 return (XPending(display) ? TRUE : FALSE);
682 inline void NextEvent(Event *event)
687 XNextEvent(display, event);
691 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
695 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
696 (int)event->keysym.unicode,
697 (int)event->keysym.sym,
698 (int)SDL_GetModState());
701 if (with_modifiers && event->keysym.unicode != 0)
702 return event->keysym.unicode;
704 return event->keysym.sym;
707 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
708 (int)XLookupKeysym(event, event->state),
709 (int)XLookupKeysym(event, 0));
713 return XLookupKeysym(event, event->state);
715 return XLookupKeysym(event, 0);
719 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
721 if (event->type != EVENT_CLIENTMESSAGE)
724 #if defined(TARGET_SDL)
725 return TRUE; /* the only possible message here is SDL_QUIT */
726 #elif defined(PLATFORM_UNIX)
727 if ((event->window == window->drawable) &&
728 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
736 /* ========================================================================= */
737 /* joystick functions */
738 /* ========================================================================= */
740 inline void InitJoysticks()
745 return; /* joysticks generally deactivated by compile-time directive */
748 /* always start with reliable default values */
749 joystick.status = JOYSTICK_NOT_AVAILABLE;
750 for (i=0; i<MAX_PLAYERS; i++)
751 joystick.fd[i] = -1; /* joystick device closed */
753 #if defined(TARGET_SDL)
755 #elif defined(PLATFORM_UNIX)
757 #elif defined(PLATFORM_MSDOS)
758 MSDOSInitJoysticks();
762 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
764 #if defined(TARGET_SDL)
765 return SDLReadJoystick(nr, x, y, b1, b2);
766 #elif defined(PLATFORM_UNIX)
767 return UnixReadJoystick(nr, x, y, b1, b2);
768 #elif defined(PLATFORM_MSDOS)
769 return MSDOSReadJoystick(nr, x, y, b1, b2);