1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2000 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 #if defined(PLATFORM_MSDOS)
27 /* ========================================================================= */
28 /* exported variables */
29 /* ========================================================================= */
31 struct ProgramInfo program;
32 struct OptionInfo options;
33 struct VideoSystemInfo video;
34 struct AudioSystemInfo audio;
37 struct LevelDirInfo *leveldir_first = NULL;
38 struct LevelDirInfo *leveldir_current = NULL;
40 Display *display = NULL;
41 Visual *visual = NULL;
45 DrawWindow window = NULL;
46 DrawBuffer backbuffer = NULL;
47 DrawBuffer drawto = NULL;
49 int button_status = MB_NOT_PRESSED;
50 boolean motion_status = FALSE;
52 int redraw_mask = REDRAW_NONE;
58 /* ========================================================================= */
60 /* ========================================================================= */
62 void InitCommandName(char *argv0)
64 program.command_basename =
65 (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
68 void InitExitFunction(void (*exit_function)(int))
70 program.exit_function = exit_function;
73 void InitPlatformDependantStuff(void)
75 #if defined(PLATFORM_MSDOS)
80 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
81 char *window_title, char *icon_title,
82 char *x11_icon_basename, char *x11_iconmask_basename,
83 char *msdos_pointer_basename)
85 char *gfx_dir = getPath2(options.ro_base_directory, GRAPHICS_DIRECTORY);
86 char *x11_icon_filename = getPath2(gfx_dir, x11_icon_basename);
87 char *x11_iconmask_filename = getPath2(gfx_dir, x11_iconmask_basename);
88 char *msdos_pointer_filename = getPath2(gfx_dir, msdos_pointer_basename);
92 #if defined(PLATFORM_UNIX)
93 program.userdata_directory = unix_userdata_directory;
95 program.userdata_directory = "userdata";
98 program.program_title = program_title;
99 program.window_title = window_title;
100 program.icon_title = icon_title;
101 program.x11_icon_filename = x11_icon_filename;
102 program.x11_iconmask_filename = x11_iconmask_filename;
103 program.msdos_pointer_filename = msdos_pointer_filename;
106 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
107 int real_sx, int real_sy,
108 int full_sxsize, int full_sysize)
114 gfx.real_sx = real_sx;
115 gfx.real_sy = real_sy;
116 gfx.full_sxsize = full_sxsize;
117 gfx.full_sysize = full_sysize;
120 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
128 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
136 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
138 /* currently only used by MSDOS code to alloc VRAM buffer, if available */
139 gfx.scrollbuffer_width = scrollbuffer_width;
140 gfx.scrollbuffer_height = scrollbuffer_height;
144 /* ========================================================================= */
145 /* video functions */
146 /* ========================================================================= */
148 inline static int GetRealDepth(int depth)
150 return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
153 inline void InitVideoDisplay(void)
156 SDLInitVideoDisplay();
158 X11InitVideoDisplay();
162 inline void InitVideoBuffer(DrawBuffer *backbuffer, DrawWindow *window,
163 int width, int height, int depth,
167 video.height = height;
168 video.depth = GetRealDepth(depth);
169 video.fullscreen_available = FULLSCREEN_STATUS;
170 video.fullscreen_enabled = FALSE;
173 SDLInitVideoBuffer(backbuffer, window, fullscreen);
175 X11InitVideoBuffer(backbuffer, window);
179 inline Bitmap CreateBitmapStruct(void)
182 return checked_calloc(sizeof(struct SDLSurfaceInfo));
184 return checked_calloc(sizeof(struct X11DrawableInfo));
188 inline Bitmap CreateBitmap(int width, int height, int depth)
190 Bitmap new_bitmap = CreateBitmapStruct();
191 int real_depth = GetRealDepth(depth);
194 SDL_Surface *surface_tmp, *surface_native;
196 if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
197 real_depth, 0, 0, 0, 0))
199 Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
201 if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
202 Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
204 SDL_FreeSurface(surface_tmp);
206 new_bitmap->surface = surface_native;
210 if ((pixmap = XCreatePixmap(display, window->drawable,
211 width, height, real_depth))
213 Error(ERR_EXIT, "cannot create pixmap");
215 new_bitmap->drawable = pixmap;
218 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
220 new_bitmap->gc = window->gc;
222 new_bitmap->line_gc[0] = window->line_gc[0];
223 new_bitmap->line_gc[1] = window->line_gc[1];
229 inline void FreeBitmap(Bitmap bitmap)
236 SDL_FreeSurface(bitmap->surface);
237 if (bitmap->surface_masked)
238 SDL_FreeSurface(bitmap->surface_masked);
240 if (bitmap->drawable)
241 XFreePixmap(display, bitmap->drawable);
242 if (bitmap->clip_mask)
243 XFreePixmap(display, bitmap->clip_mask);
244 if (bitmap->stored_clip_gc)
245 XFreeGC(display, bitmap->stored_clip_gc);
251 inline void CloseWindow(DrawWindow window)
254 if (window->drawable)
256 XUnmapWindow(display, window->drawable);
257 XDestroyWindow(display, window->drawable);
260 XFreeGC(display, window->gc);
264 inline void BlitBitmap(Bitmap src_bitmap, Bitmap dst_bitmap,
265 int src_x, int src_y,
266 int width, int height,
267 int dst_x, int dst_y)
270 SDLCopyArea(src_bitmap, dst_bitmap,
271 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
273 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
274 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
278 inline void ClearRectangle(Bitmap bitmap, int x, int y, int width, int height)
281 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
283 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
289 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
293 inline void SetClipMask(Bitmap bitmap, GC clip_gc, Pixmap clip_pixmap)
298 bitmap->clip_gc = clip_gc;
299 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
302 last_clip_gc = clip_gc;
307 inline void SetClipOrigin(Bitmap bitmap, GC clip_gc, int clip_x, int clip_y)
312 bitmap->clip_gc = clip_gc;
313 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
316 last_clip_gc = clip_gc;
321 inline void BlitBitmapMasked(Bitmap src_bitmap, Bitmap dst_bitmap,
322 int src_x, int src_y,
323 int width, int height,
324 int dst_x, int dst_y)
327 SDLCopyArea(src_bitmap, dst_bitmap,
328 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
330 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
331 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
335 inline void DrawSimpleWhiteLine(Bitmap bitmap, int from_x, int from_y,
339 SDLDrawSimpleLine(bitmap->surface, from_x, from_y, to_x, to_y, 0xffffff);
341 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
342 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
343 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
347 #if !defined(TARGET_X11_NATIVE)
348 inline void DrawLine(Bitmap bitmap, int from_x, int from_y,
349 int to_x, int to_y, Pixel pixel, int line_width)
353 for (x=0; x<line_width; x++)
355 for (y=0; y<line_width; y++)
357 int dx = x - line_width / 2;
358 int dy = y - line_width / 2;
360 if ((x == 0 && y == 0) ||
361 (x == 0 && y == line_width - 1) ||
362 (x == line_width - 1 && y == 0) ||
363 (x == line_width - 1 && y == line_width - 1))
366 #if defined(TARGET_SDL)
367 sge_Line(bitmap->surface,
368 from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
369 #elif defined(TARGET_ALLEGRO)
370 AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
371 to_x + dx, to_y + dy, pixel);
378 inline void DrawLines(Bitmap bitmap, struct XY *points, int num_points,
381 #if !defined(TARGET_X11_NATIVE)
385 for (i=0; i<num_points - 1; i++)
386 DrawLine(bitmap, points[i].x, points[i].y,
387 points[i + 1].x, points[i + 1].y, pixel, line_width);
390 SDLDrawLines(bitmap->surface, points, num_points, pixel);
393 XSetForeground(display, bitmap->line_gc[1], pixel);
394 XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
395 (XPoint *)points, num_points, CoordModeOrigin);
397 XSetForeground(display, gc, BlackPixel(display, screen));
402 inline Pixel GetPixelFromRGB(Bitmap bitmap, unsigned int color_r,
403 unsigned int color_g, unsigned int color_b)
407 #if defined(TARGET_SDL)
408 pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
409 #elif defined(TARGET_X11_NATIVE)
412 xcolor.flags = DoRed | DoGreen | DoBlue;
413 xcolor.red = (color_r << 8);
414 xcolor.green = (color_g << 8);
415 xcolor.blue = (color_b << 8);
416 XAllocColor(display, cmap, &xcolor);
417 pixel = xcolor.pixel;
423 inline Pixel GetPixelFromRGBcompact(Bitmap bitmap, unsigned int color)
425 unsigned int color_r = (color >> 16) & 0xff;
426 unsigned int color_g = (color >> 8) & 0xff;
427 unsigned int color_b = (color >> 0) & 0xff;
429 return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
432 /* execute all pending screen drawing operations */
433 inline void FlushDisplay(void)
440 /* execute and wait for all pending screen drawing operations */
441 inline void SyncDisplay(void)
444 XSync(display, FALSE);
448 inline void KeyboardAutoRepeatOn(void)
451 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
452 SDL_DEFAULT_REPEAT_INTERVAL / 2);
453 SDL_EnableUNICODE(1);
455 XAutoRepeatOn(display);
459 inline void KeyboardAutoRepeatOff(void)
462 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
463 SDL_EnableUNICODE(0);
465 XAutoRepeatOff(display);
469 inline boolean PointerInWindow(DrawWindow window)
479 /* if XQueryPointer() returns False, the pointer
480 is not on the same screen as the specified window */
481 return XQueryPointer(display, window->drawable, &root, &child,
482 &root_x, &root_y, &win_x, &win_y, &mask);
486 inline boolean SetVideoMode(boolean fullscreen)
489 return SDLSetVideoMode(&backbuffer, fullscreen);
491 boolean success = TRUE;
493 if (fullscreen && video.fullscreen_available)
495 Error(ERR_WARN, "fullscreen not available in X11 version");
497 /* display error message only once */
498 video.fullscreen_available = FALSE;
507 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
510 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
511 (!fullscreen && video.fullscreen_enabled))
512 fullscreen = SetVideoMode(fullscreen);
519 /* ========================================================================= */
520 /* audio functions */
521 /* ========================================================================= */
523 inline boolean OpenAudio(struct AudioSystemInfo *audio)
525 audio->sound_available = FALSE;
526 audio->loops_available = FALSE;
527 audio->sound_enabled = FALSE;
528 audio->soundserver_pipe[0] = audio->soundserver_pipe[1] = 0;
529 audio->soundserver_pid = 0;
530 audio->device_name = NULL;
531 audio->device_fd = 0;
533 #if defined(TARGET_SDL)
536 audio->sound_available = TRUE;
537 audio->loops_available = TRUE;
538 audio->sound_enabled = TRUE;
540 #elif defined(PLATFORM_MSDOS)
541 if (MSDOSOpenAudio())
543 audio->sound_available = TRUE;
544 audio->loops_available = TRUE;
545 audio->sound_enabled = TRUE;
547 #elif defined(PLATFORM_UNIX)
548 UnixOpenAudio(audio);
551 return audio->sound_available;
554 inline void CloseAudio(struct AudioSystemInfo *audio)
556 #if defined(TARGET_SDL)
558 #elif defined(PLATFORM_MSDOS)
560 #elif defined(PLATFORM_UNIX)
561 UnixCloseAudio(audio);
564 audio->sound_available = FALSE;
565 audio->loops_available = FALSE;
566 audio->sound_enabled = FALSE;
569 inline void SetAudioMode(boolean enabled)
571 if (!audio.sound_available)
574 audio.sound_enabled = enabled;
578 /* ========================================================================= */
579 /* event functions */
580 /* ========================================================================= */
582 inline void InitEventFilter(EventFilter filter_function)
585 /* set event filter to filter out certain events */
586 SDL_SetEventFilter(filter_function);
590 inline boolean PendingEvent(void)
593 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
595 return (XPending(display) ? TRUE : FALSE);
599 inline void NextEvent(Event *event)
602 SDL_WaitEvent(event);
604 XNextEvent(display, event);
608 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
612 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
613 (int)event->keysym.unicode,
614 (int)event->keysym.sym,
615 (int)SDL_GetModState());
618 if (with_modifiers && event->keysym.unicode != 0)
619 return event->keysym.unicode;
621 return event->keysym.sym;
624 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
625 (int)XLookupKeysym(event, event->state),
626 (int)XLookupKeysym(event, 0));
630 return XLookupKeysym(event, event->state);
632 return XLookupKeysym(event, 0);
636 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
638 if (event->type != EVENT_CLIENTMESSAGE)
641 #if defined(TARGET_SDL)
642 return TRUE; /* the only possible message here is SDL_QUIT */
643 #elif defined(PLATFORM_UNIX)
644 if ((event->window == window->drawable) &&
645 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
653 inline void dummy(void)