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;
162 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
170 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
178 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
180 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
181 gfx.scrollbuffer_width = scrollbuffer_width;
182 gfx.scrollbuffer_height = scrollbuffer_height;
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);
212 XCloseDisplay(display);
216 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
217 int width, int height, int depth,
221 video.height = height;
222 video.depth = GetRealDepth(depth);
223 video.fullscreen_available = FULLSCREEN_STATUS;
224 video.fullscreen_enabled = FALSE;
227 SDLInitVideoBuffer(backbuffer, window, fullscreen);
229 X11InitVideoBuffer(backbuffer, window);
233 inline Bitmap *CreateBitmapStruct(void)
236 return checked_calloc(sizeof(struct SDLSurfaceInfo));
238 return checked_calloc(sizeof(struct X11DrawableInfo));
242 inline Bitmap *CreateBitmap(int width, int height, int depth)
244 Bitmap *new_bitmap = CreateBitmapStruct();
245 int real_depth = GetRealDepth(depth);
248 SDL_Surface *surface_tmp, *surface_native;
250 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
251 real_depth, 0, 0, 0, 0))
253 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
255 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
256 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
258 SDL_FreeSurface(surface_tmp);
260 new_bitmap->surface = surface_native;
264 if ((pixmap = XCreatePixmap(display, window->drawable,
265 width, height, real_depth))
267 Error(ERR_EXIT, "cannot create pixmap");
269 new_bitmap->drawable = pixmap;
272 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
274 new_bitmap->gc = window->gc;
276 new_bitmap->line_gc[0] = window->line_gc[0];
277 new_bitmap->line_gc[1] = window->line_gc[1];
283 inline void FreeBitmap(Bitmap *bitmap)
290 SDL_FreeSurface(bitmap->surface);
291 if (bitmap->surface_masked)
292 SDL_FreeSurface(bitmap->surface_masked);
294 if (bitmap->drawable)
295 XFreePixmap(display, bitmap->drawable);
296 if (bitmap->clip_mask)
297 XFreePixmap(display, bitmap->clip_mask);
298 if (bitmap->stored_clip_gc)
299 XFreeGC(display, bitmap->stored_clip_gc);
305 inline void CloseWindow(DrawWindow *window)
308 if (window->drawable)
310 XUnmapWindow(display, window->drawable);
311 XDestroyWindow(display, window->drawable);
314 XFreeGC(display, window->gc);
318 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
319 int src_x, int src_y,
320 int width, int height,
321 int dst_x, int dst_y)
324 SDLCopyArea(src_bitmap, dst_bitmap,
325 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
327 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
328 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
332 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
335 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
337 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
343 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
347 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
352 bitmap->clip_gc = clip_gc;
353 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
356 last_clip_gc = clip_gc;
361 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
366 bitmap->clip_gc = clip_gc;
367 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
370 last_clip_gc = clip_gc;
375 inline void BlitBitmapMasked(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)
381 SDLCopyArea(src_bitmap, dst_bitmap,
382 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
384 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
385 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
389 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
393 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
395 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
396 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
397 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
401 #if !defined(TARGET_X11_NATIVE)
402 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
403 int to_x, int to_y, Pixel pixel, int line_width)
407 for (x=0; x<line_width; x++)
409 for (y=0; y<line_width; y++)
411 int dx = x - line_width / 2;
412 int dy = y - line_width / 2;
414 if ((x == 0 && y == 0) ||
415 (x == 0 && y == line_width - 1) ||
416 (x == line_width - 1 && y == 0) ||
417 (x == line_width - 1 && y == line_width - 1))
420 #if defined(TARGET_SDL)
422 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
423 #elif defined(TARGET_ALLEGRO)
424 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
425 to_x + dx, to_y + dy, pixel);
432 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
435 #if !defined(TARGET_X11_NATIVE)
439 for (i=0; i<num_points - 1; i++)
440 DrawLine(bitmap, points[i].x, points[i].y,
441 points[i + 1].x, points[i + 1].y, pixel, line_width);
444 SDLDrawLines(bitmap->surface, points, num_points, pixel);
447 XSetForeground(display, bitmap->line_gc[1], pixel);
448 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
449 (XPoint *)points, num_points, CoordModeOrigin);
451 XSetForeground(display, gc, BlackPixel(display, screen));
456 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
458 #if defined(TARGET_SDL)
459 return SDLGetPixel(bitmap, x, y);
460 #elif defined(TARGET_ALLEGRO)
461 return AllegroGetPixel(bitmap->drawable, x, y);
463 unsigned long pixel_value;
466 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
468 pixel_value = XGetPixel(pixel_image, 0, 0);
470 XDestroyImage(pixel_image);
476 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
477 unsigned int color_g, unsigned int color_b)
481 #if defined(TARGET_SDL)
482 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
483 #elif defined(TARGET_ALLEGRO)
484 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
485 #elif defined(TARGET_X11_NATIVE)
488 xcolor.flags = DoRed | DoGreen | DoBlue;
489 xcolor.red = (color_r << 8);
490 xcolor.green = (color_g << 8);
491 xcolor.blue = (color_b << 8);
492 XAllocColor(display, cmap, &xcolor);
493 pixel = xcolor.pixel;
499 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
501 unsigned int color_r = (color >> 16) & 0xff;
502 unsigned int color_g = (color >> 8) & 0xff;
503 unsigned int color_b = (color >> 0) & 0xff;
505 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
508 /* execute all pending screen drawing operations */
509 inline void FlushDisplay(void)
516 /* execute and wait for all pending screen drawing operations */
517 inline void SyncDisplay(void)
520 XSync(display, FALSE);
524 inline void KeyboardAutoRepeatOn(void)
527 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
528 SDL_DEFAULT_REPEAT_INTERVAL / 2);
529 SDL_EnableUNICODE(1);
532 XAutoRepeatOn(display);
536 inline void KeyboardAutoRepeatOff(void)
539 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
540 SDL_EnableUNICODE(0);
543 XAutoRepeatOff(display);
547 inline boolean PointerInWindow(DrawWindow *window)
557 /* if XQueryPointer() returns False, the pointer
558 is not on the same screen as the specified window */
559 return XQueryPointer(display, window->drawable, &root, &child,
560 &root_x, &root_y, &win_x, &win_y, &mask);
564 inline boolean SetVideoMode(boolean fullscreen)
567 return SDLSetVideoMode(&backbuffer, fullscreen);
569 boolean success = TRUE;
571 if (fullscreen && video.fullscreen_available)
573 Error(ERR_WARN, "fullscreen not available in X11 version");
575 /* display error message only once */
576 video.fullscreen_available = FALSE;
585 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
588 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
589 (!fullscreen && video.fullscreen_enabled))
590 fullscreen = SetVideoMode(fullscreen);
596 Bitmap *LoadImage(char *filename)
600 #if defined(TARGET_SDL)
601 new_bitmap = SDLLoadImage(filename);
603 new_bitmap = X11LoadImage(filename);
610 /* ========================================================================= */
611 /* audio functions */
612 /* ========================================================================= */
614 inline void OpenAudio(void)
616 /* always start with reliable default values */
617 audio.sound_available = FALSE;
618 audio.music_available = FALSE;
619 audio.loops_available = FALSE;
620 audio.mods_available = FALSE;
621 audio.sound_enabled = FALSE;
623 audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0;
624 audio.soundserver_pid = 0;
625 audio.device_name = NULL;
629 audio.music_channel = 0;
632 #if defined(TARGET_SDL)
634 #elif defined(PLATFORM_UNIX)
636 #elif defined(PLATFORM_MSDOS)
641 inline void CloseAudio(void)
643 #if defined(TARGET_SDL)
645 #elif defined(PLATFORM_UNIX)
647 #elif defined(PLATFORM_MSDOS)
651 audio.sound_enabled = FALSE;
654 inline void SetAudioMode(boolean enabled)
656 if (!audio.sound_available)
659 audio.sound_enabled = enabled;
663 /* ========================================================================= */
664 /* event functions */
665 /* ========================================================================= */
667 inline void InitEventFilter(EventFilter filter_function)
670 /* set event filter to filter out certain events */
671 SDL_SetEventFilter(filter_function);
675 inline boolean PendingEvent(void)
678 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
680 return (XPending(display) ? TRUE : FALSE);
684 inline void NextEvent(Event *event)
689 XNextEvent(display, event);
693 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
697 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
698 (int)event->keysym.unicode,
699 (int)event->keysym.sym,
700 (int)SDL_GetModState());
703 if (with_modifiers && event->keysym.unicode != 0)
704 return event->keysym.unicode;
706 return event->keysym.sym;
709 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
710 (int)XLookupKeysym(event, event->state),
711 (int)XLookupKeysym(event, 0));
715 return XLookupKeysym(event, event->state);
717 return XLookupKeysym(event, 0);
721 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
723 if (event->type != EVENT_CLIENTMESSAGE)
726 #if defined(TARGET_SDL)
727 return TRUE; /* the only possible message here is SDL_QUIT */
728 #elif defined(PLATFORM_UNIX)
729 if ((event->window == window->drawable) &&
730 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
738 /* ========================================================================= */
739 /* joystick functions */
740 /* ========================================================================= */
742 inline void InitJoysticks()
747 return; /* joysticks generally deactivated by compile-time directive */
750 /* always start with reliable default values */
751 joystick.status = JOYSTICK_NOT_AVAILABLE;
752 for (i=0; i<MAX_PLAYERS; i++)
753 joystick.fd[i] = -1; /* joystick device closed */
755 #if defined(TARGET_SDL)
757 #elif defined(PLATFORM_UNIX)
759 #elif defined(PLATFORM_MSDOS)
760 MSDOSInitJoysticks();
764 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
766 #if defined(TARGET_SDL)
767 return SDLReadJoystick(nr, x, y, b1, b2);
768 #elif defined(PLATFORM_UNIX)
769 return UnixReadJoystick(nr, x, y, b1, b2);
770 #elif defined(PLATFORM_MSDOS)
771 return MSDOSReadJoystick(nr, x, y, b1, b2);