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)))
212 Error(ERR_EXIT, "cannot create pixmap");
213 new_bitmap->drawable = pixmap;
216 Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
217 new_bitmap->gc = window->gc;
224 inline void FreeBitmap(Bitmap bitmap)
231 SDL_FreeSurface(bitmap->surface);
232 if (bitmap->surface_masked)
233 SDL_FreeSurface(bitmap->surface_masked);
235 if (bitmap->drawable)
236 XFreePixmap(display, bitmap->drawable);
237 if (bitmap->clip_mask)
238 XFreePixmap(display, bitmap->clip_mask);
239 if (bitmap->stored_clip_gc)
240 XFreeGC(display, bitmap->stored_clip_gc);
246 inline void CloseWindow(DrawWindow window)
249 if (window->drawable)
251 XUnmapWindow(display, window->drawable);
252 XDestroyWindow(display, window->drawable);
255 XFreeGC(display, window->gc);
259 inline void BlitBitmap(Bitmap src_bitmap, Bitmap dst_bitmap,
260 int src_x, int src_y,
261 int width, int height,
262 int dst_x, int dst_y)
265 SDLCopyArea(src_bitmap, dst_bitmap,
266 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
268 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
269 dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
273 inline void ClearRectangle(Bitmap bitmap, int x, int y, int width, int height)
276 SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
278 XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
284 static GC last_clip_gc = 0; /* needed for XCopyArea() through clip mask */
288 inline void SetClipMask(Bitmap bitmap, GC clip_gc, Pixmap clip_pixmap)
293 bitmap->clip_gc = clip_gc;
294 XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
297 last_clip_gc = clip_gc;
302 inline void SetClipOrigin(Bitmap bitmap, GC clip_gc, int clip_x, int clip_y)
307 bitmap->clip_gc = clip_gc;
308 XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
311 last_clip_gc = clip_gc;
316 inline void BlitBitmapMasked(Bitmap src_bitmap, Bitmap dst_bitmap,
317 int src_x, int src_y,
318 int width, int height,
319 int dst_x, int dst_y)
322 SDLCopyArea(src_bitmap, dst_bitmap,
323 src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
325 XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
326 src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
330 inline void DrawSimpleWhiteLine(Bitmap bitmap, int from_x, int from_y,
334 SDLDrawSimpleLine(bitmap->surface, from_x, from_y, to_x, to_y, 0xffffff);
336 XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
337 XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
338 XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
342 /* execute all pending screen drawing operations */
343 inline void FlushDisplay(void)
350 /* execute and wait for all pending screen drawing operations */
351 inline void SyncDisplay(void)
354 XSync(display, FALSE);
358 inline void KeyboardAutoRepeatOn(void)
361 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
362 SDL_DEFAULT_REPEAT_INTERVAL / 2);
363 SDL_EnableUNICODE(1);
365 XAutoRepeatOn(display);
369 inline void KeyboardAutoRepeatOff(void)
372 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
373 SDL_EnableUNICODE(0);
375 XAutoRepeatOff(display);
379 inline boolean PointerInWindow(DrawWindow window)
389 /* if XQueryPointer() returns False, the pointer
390 is not on the same screen as the specified window */
391 return XQueryPointer(display, window->drawable, &root, &child,
392 &root_x, &root_y, &win_x, &win_y, &mask);
396 inline boolean SetVideoMode(boolean fullscreen)
399 return SDLSetVideoMode(&backbuffer, fullscreen);
401 boolean success = TRUE;
403 if (fullscreen && video.fullscreen_available)
405 Error(ERR_WARN, "fullscreen not available in X11 version");
407 /* display error message only once */
408 video.fullscreen_available = FALSE;
417 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
420 if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
421 (!fullscreen && video.fullscreen_enabled))
422 fullscreen = SetVideoMode(fullscreen);
429 /* ========================================================================= */
430 /* audio functions */
431 /* ========================================================================= */
433 inline boolean OpenAudio(struct AudioSystemInfo *audio)
435 audio->sound_available = FALSE;
436 audio->loops_available = FALSE;
437 audio->sound_enabled = FALSE;
438 audio->soundserver_pipe[0] = audio->soundserver_pipe[1] = 0;
439 audio->soundserver_pid = 0;
440 audio->device_name = NULL;
441 audio->device_fd = 0;
443 #if defined(TARGET_SDL)
446 audio->sound_available = TRUE;
447 audio->loops_available = TRUE;
448 audio->sound_enabled = TRUE;
450 #elif defined(PLATFORM_MSDOS)
451 if (MSDOSOpenAudio())
453 audio->sound_available = TRUE;
454 audio->loops_available = TRUE;
455 audio->sound_enabled = TRUE;
457 #elif defined(PLATFORM_UNIX)
458 UnixOpenAudio(audio);
461 return audio->sound_available;
464 inline void CloseAudio(struct AudioSystemInfo *audio)
466 #if defined(TARGET_SDL)
468 #elif defined(PLATFORM_MSDOS)
470 #elif defined(PLATFORM_UNIX)
471 UnixCloseAudio(audio);
474 audio->sound_available = FALSE;
475 audio->loops_available = FALSE;
476 audio->sound_enabled = FALSE;
479 inline void SetAudioMode(boolean enabled)
481 if (!audio.sound_available)
484 audio.sound_enabled = enabled;
488 /* ========================================================================= */
489 /* event functions */
490 /* ========================================================================= */
492 inline void InitEventFilter(EventFilter filter_function)
495 /* set event filter to filter out certain events */
496 SDL_SetEventFilter(filter_function);
500 inline boolean PendingEvent(void)
503 return (SDL_PollEvent(NULL) ? TRUE : FALSE);
505 return (XPending(display) ? TRUE : FALSE);
509 inline void NextEvent(Event *event)
512 SDL_WaitEvent(event);
514 XNextEvent(display, event);
518 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
522 printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
523 (int)event->keysym.unicode,
524 (int)event->keysym.sym,
525 (int)SDL_GetModState());
528 if (with_modifiers && event->keysym.unicode != 0)
529 return event->keysym.unicode;
531 return event->keysym.sym;
534 printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
535 (int)XLookupKeysym(event, event->state),
536 (int)XLookupKeysym(event, 0));
540 return XLookupKeysym(event, event->state);
542 return XLookupKeysym(event, 0);
546 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
548 if (event->type != EVENT_CLIENTMESSAGE)
551 #if defined(TARGET_SDL)
552 return TRUE; /* the only possible message here is SDL_QUIT */
553 #elif defined(PLATFORM_UNIX)
554 if ((event->window == window->drawable) &&
555 (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
563 inline void dummy(void)