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)
28 /* ========================================================================= */
29 /* exported variables */
30 /* ========================================================================= */
32 struct ProgramInfo program;
33 struct OptionInfo options;
34 struct VideoSystemInfo video;
35 struct AudioSystemInfo audio;
38 struct LevelDirInfo *leveldir_first = NULL;
39 struct LevelDirInfo *leveldir_current = NULL;
41 Display *display = NULL;
42 Visual *visual = NULL;
46 DrawWindow *window = NULL;
47 DrawBuffer *backbuffer = NULL;
48 DrawBuffer *drawto = NULL;
50 int button_status = MB_NOT_PRESSED;
51 boolean motion_status = FALSE;
53 int redraw_mask = REDRAW_NONE;
59 /* ========================================================================= */
60 /* init/close functions */
61 /* ========================================================================= */
63 void InitCommandName(char *argv0)
65 program.command_basename =
66 (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
69 void InitExitFunction(void (*exit_function)(int))
71 program.exit_function = exit_function;
73 /* set signal handlers to custom exit function */
74 signal(SIGINT, exit_function);
75 signal(SIGTERM, exit_function);
77 #if defined(TARGET_SDL)
78 /* set exit function to automatically cleanup SDL stuff after exit() */
83 void InitPlatformDependantStuff(void)
85 #if defined(PLATFORM_MSDOS)
89 #if !defined(PLATFORM_UNIX)
90 program.userdata_directory = "userdata";
94 #if defined(TARGET_SDL)
95 if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
96 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
100 void ClosePlatformDependantStuff(void)
102 #if !defined(PLATFORM_UNIX)
107 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
108 char *window_title, char *icon_title,
109 char *x11_icon_basename, char *x11_iconmask_basename,
110 char *msdos_pointer_basename)
112 char *x11_icon_filename =
113 getPath2(options.graphics_directory, x11_icon_basename);
114 char *x11_iconmask_filename =
115 getPath2(options.graphics_directory, x11_iconmask_basename);
116 char *msdos_pointer_filename =
117 getPath2(options.graphics_directory, msdos_pointer_basename);
119 #if defined(PLATFORM_UNIX)
120 program.userdata_directory = unix_userdata_directory;
122 program.userdata_directory = "userdata";
125 program.program_title = program_title;
126 program.window_title = window_title;
127 program.icon_title = icon_title;
128 program.x11_icon_filename = x11_icon_filename;
129 program.x11_iconmask_filename = x11_iconmask_filename;
130 program.msdos_pointer_filename = msdos_pointer_filename;
133 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
134 int real_sx, int real_sy,
135 int full_sxsize, int full_sysize)
141 gfx.real_sx = real_sx;
142 gfx.real_sy = real_sy;
143 gfx.full_sxsize = full_sxsize;
144 gfx.full_sysize = full_sysize;
147 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
155 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
163 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
165 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
166 gfx.scrollbuffer_width = scrollbuffer_width;
167 gfx.scrollbuffer_height = scrollbuffer_height;
171 /* ========================================================================= */
172 /* video functions */
173 /* ========================================================================= */
175 inline static int GetRealDepth(int depth)
177 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
180 inline void InitVideoDisplay(void)
182 #if defined(TARGET_SDL)
183 SDLInitVideoDisplay();
185 X11InitVideoDisplay();
189 inline void CloseVideoDisplay(void)
191 KeyboardAutoRepeatOn();
193 #if defined(TARGET_SDL)
194 SDL_QuitSubSystem(SDL_INIT_VIDEO);
197 XCloseDisplay(display);
201 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
202 int width, int height, int depth,
206 video.height = height;
207 video.depth = GetRealDepth(depth);
208 video.fullscreen_available = FULLSCREEN_STATUS;
209 video.fullscreen_enabled = FALSE;
212 SDLInitVideoBuffer(backbuffer, window, fullscreen);
214 X11InitVideoBuffer(backbuffer, window);
218 inline Bitmap *CreateBitmapStruct(void)
221 return checked_calloc(sizeof(struct SDLSurfaceInfo));
223 return checked_calloc(sizeof(struct X11DrawableInfo));
227 inline Bitmap *CreateBitmap(int width, int height, int depth)
229 Bitmap *new_bitmap = CreateBitmapStruct();
230 int real_depth = GetRealDepth(depth);
233 SDL_Surface *surface_tmp, *surface_native;
235 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
236 real_depth, 0, 0, 0, 0))
238 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
240 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
241 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
243 SDL_FreeSurface(surface_tmp);
245 new_bitmap->surface = surface_native;
249 if ((pixmap = XCreatePixmap(display, window->drawable,
250 width, height, real_depth))
252 Error(ERR_EXIT, "cannot create pixmap");
254 new_bitmap->drawable = pixmap;
257 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
259 new_bitmap->gc = window->gc;
261 new_bitmap->line_gc[0] = window->line_gc[0];
262 new_bitmap->line_gc[1] = window->line_gc[1];
268 inline void FreeBitmap(Bitmap *bitmap)
275 SDL_FreeSurface(bitmap->surface);
276 if (bitmap->surface_masked)
277 SDL_FreeSurface(bitmap->surface_masked);
279 if (bitmap->drawable)
280 XFreePixmap(display, bitmap->drawable);
281 if (bitmap->clip_mask)
282 XFreePixmap(display, bitmap->clip_mask);
283 if (bitmap->stored_clip_gc)
284 XFreeGC(display, bitmap->stored_clip_gc);
290 inline void CloseWindow(DrawWindow *window)
293 if (window->drawable)
295 XUnmapWindow(display, window->drawable);
296 XDestroyWindow(display, window->drawable);
299 XFreeGC(display, window->gc);
303 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
304 int src_x, int src_y,
305 int width, int height,
306 int dst_x, int dst_y)
309 SDLCopyArea(src_bitmap, dst_bitmap,
310 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
312 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
313 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
317 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
320 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
322 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
328 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
332 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
337 bitmap->clip_gc = clip_gc;
338 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
341 last_clip_gc = clip_gc;
346 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
351 bitmap->clip_gc = clip_gc;
352 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
355 last_clip_gc = clip_gc;
360 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
361 int src_x, int src_y,
362 int width, int height,
363 int dst_x, int dst_y)
366 SDLCopyArea(src_bitmap, dst_bitmap,
367 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
369 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
370 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
374 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
378 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
380 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
381 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
382 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
386 #if !defined(TARGET_X11_NATIVE)
387 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
388 int to_x, int to_y, Pixel pixel, int line_width)
392 for (x=0; x<line_width; x++)
394 for (y=0; y<line_width; y++)
396 int dx = x - line_width / 2;
397 int dy = y - line_width / 2;
399 if ((x == 0 && y == 0) ||
400 (x == 0 && y == line_width - 1) ||
401 (x == line_width - 1 && y == 0) ||
402 (x == line_width - 1 && y == line_width - 1))
405 #if defined(TARGET_SDL)
407 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
408 #elif defined(TARGET_ALLEGRO)
409 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
410 to_x + dx, to_y + dy, pixel);
417 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
420 #if !defined(TARGET_X11_NATIVE)
424 for (i=0; i<num_points - 1; i++)
425 DrawLine(bitmap, points[i].x, points[i].y,
426 points[i + 1].x, points[i + 1].y, pixel, line_width);
429 SDLDrawLines(bitmap->surface, points, num_points, pixel);
432 XSetForeground(display, bitmap->line_gc[1], pixel);
433 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
434 (XPoint *)points, num_points, CoordModeOrigin);
436 XSetForeground(display, gc, BlackPixel(display, screen));
441 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
443 #if defined(TARGET_SDL)
444 return SDLGetPixel(bitmap, x, y);
445 #elif defined(TARGET_ALLEGRO)
446 return AllegroGetPixel(bitmap->drawable, x, y);
448 unsigned long pixel_value;
451 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
453 pixel_value = XGetPixel(pixel_image, 0, 0);
455 XDestroyImage(pixel_image);
461 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
462 unsigned int color_g, unsigned int color_b)
466 #if defined(TARGET_SDL)
467 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
468 #elif defined(TARGET_ALLEGRO)
469 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
470 #elif defined(TARGET_X11_NATIVE)
473 xcolor.flags = DoRed | DoGreen | DoBlue;
474 xcolor.red = (color_r << 8);
475 xcolor.green = (color_g << 8);
476 xcolor.blue = (color_b << 8);
477 XAllocColor(display, cmap, &xcolor);
478 pixel = xcolor.pixel;
484 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
486 unsigned int color_r = (color >> 16) & 0xff;
487 unsigned int color_g = (color >> 8) & 0xff;
488 unsigned int color_b = (color >> 0) & 0xff;
490 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
493 /* execute all pending screen drawing operations */
494 inline void FlushDisplay(void)
501 /* execute and wait for all pending screen drawing operations */
502 inline void SyncDisplay(void)
505 XSync(display, FALSE);
509 inline void KeyboardAutoRepeatOn(void)
512 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
513 SDL_DEFAULT_REPEAT_INTERVAL / 2);
514 SDL_EnableUNICODE(1);
517 XAutoRepeatOn(display);
521 inline void KeyboardAutoRepeatOff(void)
524 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
525 SDL_EnableUNICODE(0);
528 XAutoRepeatOff(display);
532 inline boolean PointerInWindow(DrawWindow *window)
542 /* if XQueryPointer() returns False, the pointer
543 is not on the same screen as the specified window */
544 return XQueryPointer(display, window->drawable, &root, &child,
545 &root_x, &root_y, &win_x, &win_y, &mask);
549 inline boolean SetVideoMode(boolean fullscreen)
552 return SDLSetVideoMode(&backbuffer, fullscreen);
554 boolean success = TRUE;
556 if (fullscreen && video.fullscreen_available)
558 Error(ERR_WARN, "fullscreen not available in X11 version");
560 /* display error message only once */
561 video.fullscreen_available = FALSE;
570 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
573 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
574 (!fullscreen && video.fullscreen_enabled))
575 fullscreen = SetVideoMode(fullscreen);
581 Bitmap *LoadImage(char *basename)
584 char *filename = getPath2(options.graphics_directory, basename);
586 #if defined(TARGET_SDL)
587 new_bitmap = SDLLoadImage(filename);
589 new_bitmap = X11LoadImage(filename);
598 /* ========================================================================= */
599 /* audio functions */
600 /* ========================================================================= */
602 inline void OpenAudio(void)
604 /* always start with reliable default values */
605 audio.sound_available = FALSE;
606 audio.music_available = FALSE;
607 audio.loops_available = FALSE;
608 audio.mods_available = FALSE;
609 audio.sound_enabled = FALSE;
611 audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0;
612 audio.soundserver_pid = 0;
613 audio.device_name = NULL;
617 audio.music_channel = 0;
620 #if defined(TARGET_SDL)
622 #elif defined(PLATFORM_MSDOS)
624 #elif defined(PLATFORM_UNIX)
629 inline void CloseAudio(void)
631 #if defined(TARGET_SDL)
633 #elif defined(PLATFORM_MSDOS)
635 #elif defined(PLATFORM_UNIX)
639 audio.sound_enabled = FALSE;
642 inline void SetAudioMode(boolean enabled)
644 if (!audio.sound_available)
647 audio.sound_enabled = enabled;
651 /* ========================================================================= */
652 /* event functions */
653 /* ========================================================================= */
655 inline void InitEventFilter(EventFilter filter_function)
658 /* set event filter to filter out certain events */
659 SDL_SetEventFilter(filter_function);
663 inline boolean PendingEvent(void)
666 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
668 return (XPending(display) ? TRUE : FALSE);
672 inline void NextEvent(Event *event)
677 XNextEvent(display, event);
681 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
685 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
686 (int)event->keysym.unicode,
687 (int)event->keysym.sym,
688 (int)SDL_GetModState());
691 if (with_modifiers && event->keysym.unicode != 0)
692 return event->keysym.unicode;
694 return event->keysym.sym;
697 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
698 (int)XLookupKeysym(event, event->state),
699 (int)XLookupKeysym(event, 0));
703 return XLookupKeysym(event, event->state);
705 return XLookupKeysym(event, 0);
709 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
711 if (event->type != EVENT_CLIENTMESSAGE)
714 #if defined(TARGET_SDL)
715 return TRUE; /* the only possible message here is SDL_QUIT */
716 #elif defined(PLATFORM_UNIX)
717 if ((event->window == window->drawable) &&
718 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
726 inline void dummy(void)