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)
29 /* ========================================================================= */
30 /* exported variables */
31 /* ========================================================================= */
33 struct ProgramInfo program;
34 struct OptionInfo options;
35 struct VideoSystemInfo video;
36 struct AudioSystemInfo audio;
38 struct JoystickInfo joystick;
39 struct SetupInfo setup;
41 struct LevelDirInfo *leveldir_first = NULL;
42 struct LevelDirInfo *leveldir_current = NULL;
45 Display *display = NULL;
46 Visual *visual = NULL;
50 DrawWindow *window = NULL;
51 DrawBuffer *backbuffer = NULL;
52 DrawBuffer *drawto = NULL;
54 int button_status = MB_NOT_PRESSED;
55 boolean motion_status = FALSE;
57 int redraw_mask = REDRAW_NONE;
63 /* ========================================================================= */
64 /* init/close functions */
65 /* ========================================================================= */
67 void InitCommandName(char *argv0)
69 program.command_basename =
70 (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
73 void InitExitFunction(void (*exit_function)(int))
75 program.exit_function = exit_function;
77 /* set signal handlers to custom exit function */
78 signal(SIGINT, exit_function);
79 signal(SIGTERM, exit_function);
81 #if defined(TARGET_SDL)
82 /* set exit function to automatically cleanup SDL stuff after exit() */
87 void InitPlatformDependantStuff(void)
89 #if defined(PLATFORM_MSDOS)
93 #if !defined(PLATFORM_UNIX)
94 program.userdata_directory = "userdata";
98 #if defined(TARGET_SDL)
99 if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
100 Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
104 void ClosePlatformDependantStuff(void)
106 #if !defined(PLATFORM_UNIX)
111 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
112 char *window_title, char *icon_title,
113 char *x11_icon_basename, char *x11_iconmask_basename,
114 char *msdos_pointer_basename)
116 char *x11_icon_filename =
117 getPath2(options.graphics_directory, x11_icon_basename);
118 char *x11_iconmask_filename =
119 getPath2(options.graphics_directory, x11_iconmask_basename);
120 char *msdos_pointer_filename =
121 getPath2(options.graphics_directory, msdos_pointer_basename);
123 #if defined(PLATFORM_UNIX)
124 program.userdata_directory = unix_userdata_directory;
126 program.userdata_directory = "userdata";
129 program.program_title = program_title;
130 program.window_title = window_title;
131 program.icon_title = icon_title;
132 program.x11_icon_filename = x11_icon_filename;
133 program.x11_iconmask_filename = x11_iconmask_filename;
134 program.msdos_pointer_filename = msdos_pointer_filename;
137 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
138 int real_sx, int real_sy,
139 int full_sxsize, int full_sysize)
145 gfx.real_sx = real_sx;
146 gfx.real_sy = real_sy;
147 gfx.full_sxsize = full_sxsize;
148 gfx.full_sysize = full_sysize;
151 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
159 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
167 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
169 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
170 gfx.scrollbuffer_width = scrollbuffer_width;
171 gfx.scrollbuffer_height = scrollbuffer_height;
175 /* ========================================================================= */
176 /* video functions */
177 /* ========================================================================= */
179 inline static int GetRealDepth(int depth)
181 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
184 inline void InitVideoDisplay(void)
186 #if defined(TARGET_SDL)
187 SDLInitVideoDisplay();
189 X11InitVideoDisplay();
193 inline void CloseVideoDisplay(void)
195 KeyboardAutoRepeatOn();
197 #if defined(TARGET_SDL)
198 SDL_QuitSubSystem(SDL_INIT_VIDEO);
201 XCloseDisplay(display);
205 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
206 int width, int height, int depth,
210 video.height = height;
211 video.depth = GetRealDepth(depth);
212 video.fullscreen_available = FULLSCREEN_STATUS;
213 video.fullscreen_enabled = FALSE;
216 SDLInitVideoBuffer(backbuffer, window, fullscreen);
218 X11InitVideoBuffer(backbuffer, window);
222 inline Bitmap *CreateBitmapStruct(void)
225 return checked_calloc(sizeof(struct SDLSurfaceInfo));
227 return checked_calloc(sizeof(struct X11DrawableInfo));
231 inline Bitmap *CreateBitmap(int width, int height, int depth)
233 Bitmap *new_bitmap = CreateBitmapStruct();
234 int real_depth = GetRealDepth(depth);
237 SDL_Surface *surface_tmp, *surface_native;
239 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
240 real_depth, 0, 0, 0, 0))
242 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
244 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
245 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
247 SDL_FreeSurface(surface_tmp);
249 new_bitmap->surface = surface_native;
253 if ((pixmap = XCreatePixmap(display, window->drawable,
254 width, height, real_depth))
256 Error(ERR_EXIT, "cannot create pixmap");
258 new_bitmap->drawable = pixmap;
261 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
263 new_bitmap->gc = window->gc;
265 new_bitmap->line_gc[0] = window->line_gc[0];
266 new_bitmap->line_gc[1] = window->line_gc[1];
272 inline void FreeBitmap(Bitmap *bitmap)
279 SDL_FreeSurface(bitmap->surface);
280 if (bitmap->surface_masked)
281 SDL_FreeSurface(bitmap->surface_masked);
283 if (bitmap->drawable)
284 XFreePixmap(display, bitmap->drawable);
285 if (bitmap->clip_mask)
286 XFreePixmap(display, bitmap->clip_mask);
287 if (bitmap->stored_clip_gc)
288 XFreeGC(display, bitmap->stored_clip_gc);
294 inline void CloseWindow(DrawWindow *window)
297 if (window->drawable)
299 XUnmapWindow(display, window->drawable);
300 XDestroyWindow(display, window->drawable);
303 XFreeGC(display, window->gc);
307 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
308 int src_x, int src_y,
309 int width, int height,
310 int dst_x, int dst_y)
313 SDLCopyArea(src_bitmap, dst_bitmap,
314 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
316 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
317 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
321 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
324 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
326 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
332 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
336 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
341 bitmap->clip_gc = clip_gc;
342 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
345 last_clip_gc = clip_gc;
350 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
355 bitmap->clip_gc = clip_gc;
356 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
359 last_clip_gc = clip_gc;
364 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
365 int src_x, int src_y,
366 int width, int height,
367 int dst_x, int dst_y)
370 SDLCopyArea(src_bitmap, dst_bitmap,
371 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
373 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
374 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
378 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
382 SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
384 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
385 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
386 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
390 #if !defined(TARGET_X11_NATIVE)
391 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
392 int to_x, int to_y, Pixel pixel, int line_width)
396 for (x=0; x<line_width; x++)
398 for (y=0; y<line_width; y++)
400 int dx = x - line_width / 2;
401 int dy = y - line_width / 2;
403 if ((x == 0 && y == 0) ||
404 (x == 0 && y == line_width - 1) ||
405 (x == line_width - 1 && y == 0) ||
406 (x == line_width - 1 && y == line_width - 1))
409 #if defined(TARGET_SDL)
411 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
412 #elif defined(TARGET_ALLEGRO)
413 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
414 to_x + dx, to_y + dy, pixel);
421 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
424 #if !defined(TARGET_X11_NATIVE)
428 for (i=0; i<num_points - 1; i++)
429 DrawLine(bitmap, points[i].x, points[i].y,
430 points[i + 1].x, points[i + 1].y, pixel, line_width);
433 SDLDrawLines(bitmap->surface, points, num_points, pixel);
436 XSetForeground(display, bitmap->line_gc[1], pixel);
437 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
438 (XPoint *)points, num_points, CoordModeOrigin);
440 XSetForeground(display, gc, BlackPixel(display, screen));
445 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
447 #if defined(TARGET_SDL)
448 return SDLGetPixel(bitmap, x, y);
449 #elif defined(TARGET_ALLEGRO)
450 return AllegroGetPixel(bitmap->drawable, x, y);
452 unsigned long pixel_value;
455 pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
457 pixel_value = XGetPixel(pixel_image, 0, 0);
459 XDestroyImage(pixel_image);
465 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
466 unsigned int color_g, unsigned int color_b)
470 #if defined(TARGET_SDL)
471 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
472 #elif defined(TARGET_ALLEGRO)
473 pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
474 #elif defined(TARGET_X11_NATIVE)
477 xcolor.flags = DoRed | DoGreen | DoBlue;
478 xcolor.red = (color_r << 8);
479 xcolor.green = (color_g << 8);
480 xcolor.blue = (color_b << 8);
481 XAllocColor(display, cmap, &xcolor);
482 pixel = xcolor.pixel;
488 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
490 unsigned int color_r = (color >> 16) & 0xff;
491 unsigned int color_g = (color >> 8) & 0xff;
492 unsigned int color_b = (color >> 0) & 0xff;
494 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
497 /* execute all pending screen drawing operations */
498 inline void FlushDisplay(void)
505 /* execute and wait for all pending screen drawing operations */
506 inline void SyncDisplay(void)
509 XSync(display, FALSE);
513 inline void KeyboardAutoRepeatOn(void)
516 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
517 SDL_DEFAULT_REPEAT_INTERVAL / 2);
518 SDL_EnableUNICODE(1);
521 XAutoRepeatOn(display);
525 inline void KeyboardAutoRepeatOff(void)
528 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
529 SDL_EnableUNICODE(0);
532 XAutoRepeatOff(display);
536 inline boolean PointerInWindow(DrawWindow *window)
546 /* if XQueryPointer() returns False, the pointer
547 is not on the same screen as the specified window */
548 return XQueryPointer(display, window->drawable, &root, &child,
549 &root_x, &root_y, &win_x, &win_y, &mask);
553 inline boolean SetVideoMode(boolean fullscreen)
556 return SDLSetVideoMode(&backbuffer, fullscreen);
558 boolean success = TRUE;
560 if (fullscreen && video.fullscreen_available)
562 Error(ERR_WARN, "fullscreen not available in X11 version");
564 /* display error message only once */
565 video.fullscreen_available = FALSE;
574 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
577 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
578 (!fullscreen && video.fullscreen_enabled))
579 fullscreen = SetVideoMode(fullscreen);
585 Bitmap *LoadImage(char *basename)
588 char *filename = getPath2(options.graphics_directory, basename);
590 #if defined(TARGET_SDL)
591 new_bitmap = SDLLoadImage(filename);
593 new_bitmap = X11LoadImage(filename);
602 /* ========================================================================= */
603 /* audio functions */
604 /* ========================================================================= */
606 inline void OpenAudio(void)
608 /* always start with reliable default values */
609 audio.sound_available = FALSE;
610 audio.music_available = FALSE;
611 audio.loops_available = FALSE;
612 audio.mods_available = FALSE;
613 audio.sound_enabled = FALSE;
615 audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0;
616 audio.soundserver_pid = 0;
617 audio.device_name = NULL;
621 audio.music_channel = 0;
624 #if defined(TARGET_SDL)
626 #elif defined(PLATFORM_MSDOS)
628 #elif defined(PLATFORM_UNIX)
633 inline void CloseAudio(void)
635 #if defined(TARGET_SDL)
637 #elif defined(PLATFORM_MSDOS)
639 #elif defined(PLATFORM_UNIX)
643 audio.sound_enabled = FALSE;
646 inline void SetAudioMode(boolean enabled)
648 if (!audio.sound_available)
651 audio.sound_enabled = enabled;
655 /* ========================================================================= */
656 /* event functions */
657 /* ========================================================================= */
659 inline void InitEventFilter(EventFilter filter_function)
662 /* set event filter to filter out certain events */
663 SDL_SetEventFilter(filter_function);
667 inline boolean PendingEvent(void)
670 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
672 return (XPending(display) ? TRUE : FALSE);
676 inline void NextEvent(Event *event)
681 XNextEvent(display, event);
685 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
689 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
690 (int)event->keysym.unicode,
691 (int)event->keysym.sym,
692 (int)SDL_GetModState());
695 if (with_modifiers && event->keysym.unicode != 0)
696 return event->keysym.unicode;
698 return event->keysym.sym;
701 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
702 (int)XLookupKeysym(event, event->state),
703 (int)XLookupKeysym(event, 0));
707 return XLookupKeysym(event, event->state);
709 return XLookupKeysym(event, 0);
713 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
715 if (event->type != EVENT_CLIENTMESSAGE)
718 #if defined(TARGET_SDL)
719 return TRUE; /* the only possible message here is SDL_QUIT */
720 #elif defined(PLATFORM_UNIX)
721 if ((event->window == window->drawable) &&
722 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
730 inline void dummy(void)