rnd-20001125-3-src
[rocksndiamonds.git] / src / system.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  ©1995 Artsoft Development                               *
5 *        Holger Schemel                                    *
6 *        33659 Bielefeld-Senne                             *
7 *        Telefon: (0521) 493245                            *
8 *        eMail: aeglos@valinor.owl.de                      *
9 *               aeglos@uni-paderborn.de                    *
10 *               q99492@pbhrzx.uni-paderborn.de             *
11 *----------------------------------------------------------*
12 *  system.c                                                *
13 ***********************************************************/
14
15 #include "main.h"
16 #include "misc.h"
17
18 inline void InitEventFilter(EventFilter filter_function)
19 {
20 #ifdef TARGET_SDL
21   /* set event filter to filter out certain events */
22   SDL_SetEventFilter(filter_function);
23 #endif
24 }
25
26 inline void InitBufferedDisplay(DrawBuffer *backbuffer, DrawWindow *window)
27 {
28 #ifdef TARGET_SDL
29   SDLInitBufferedDisplay(backbuffer, window);
30 #else
31   X11InitBufferedDisplay(backbuffer, window);
32 #endif
33 }
34
35 inline int GetDisplayDepth(void)
36 {
37 #ifdef TARGET_SDL
38   return SDL_GetVideoSurface()->format->BitsPerPixel;
39 #else
40   return XDefaultDepth(display, screen);
41 #endif
42 }
43
44 inline Bitmap CreateBitmap(int width, int height, int depth)
45 {
46   int real_depth = (depth == DEFAULT_DEPTH ? GetDisplayDepth() : depth);
47
48 #ifdef TARGET_SDL
49   SDL_Surface *surface_tmp, *surface_native;
50
51   if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
52                                           real_depth, 0, 0, 0, 0))
53       == NULL)
54     Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
55
56   if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
57     Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
58
59   SDL_FreeSurface(surface_tmp);
60
61   return surface_native;
62 #else
63   Pixmap pixmap;
64
65   if (!(pixmap = XCreatePixmap(display, window, width, height, real_depth)))
66     Error(ERR_EXIT, "cannot create pixmap");
67
68   return pixmap;
69 #endif
70 }
71
72 inline void FreeBitmap(Bitmap bitmap)
73 {
74 #ifdef TARGET_SDL
75   SDL_FreeSurface(bitmap);
76 #else
77   XFreePixmap(display, bitmap);
78 #endif
79 }
80
81 inline void ClearRectangle(Bitmap bitmap, int x, int y, int width, int height)
82 {
83 #ifdef TARGET_SDL
84   SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
85 #else
86   XFillRectangle(display, bitmap, gc, x, y, width, height);
87 #endif
88 }
89
90 inline void BlitBitmap(Bitmap src_bitmap, Bitmap dst_bitmap,
91                        int src_x, int src_y,
92                        int width, int height,
93                        int dst_x, int dst_y)
94 {
95 #ifdef TARGET_SDL
96   SDLCopyArea(src_bitmap, dst_bitmap,
97               src_x, src_y, width, height, dst_x, dst_y);
98 #else
99   XCopyArea(display, src_bitmap, dst_bitmap, gc,
100             src_x, src_y, width, height, dst_x, dst_y);
101 #endif
102 }
103
104 #ifndef TARGET_SDL
105 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
106 #endif
107
108 inline void SetClipMask(GC clip_gc, Pixmap clip_pixmap)
109 {
110 #ifndef TARGET_SDL
111   XSetClipMask(display, clip_gc, clip_pixmap);
112   last_clip_gc = clip_gc;
113 #endif
114 }
115
116 inline void SetClipOrigin(GC clip_gc, int clip_x, int clip_y)
117 {
118 #ifndef TARGET_SDL
119   XSetClipOrigin(display, clip_gc, clip_x, clip_y);
120   last_clip_gc = clip_gc;
121 #endif
122 }
123
124 inline void BlitBitmapMasked(Bitmap src_bitmap, Bitmap dst_bitmap,
125                              int src_x, int src_y,
126                              int width, int height,
127                              int dst_x, int dst_y)
128 {
129 #ifdef TARGET_SDL
130   SDLCopyArea(src_bitmap, dst_bitmap,
131               src_x, src_y, width, height, dst_x, dst_y);
132 #else
133   XCopyArea(display, src_bitmap, dst_bitmap, last_clip_gc,
134             src_x, src_y, width, height, dst_x, dst_y);
135 #endif
136 }
137
138 inline void DrawSimpleWhiteLine(Bitmap bitmap, int from_x, int from_y,
139                                 int to_x, int to_y)
140 {
141 #ifdef TARGET_SDL
142   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
143 #else
144   XSetForeground(display, gc, WhitePixel(display, screen));
145   XDrawLine(display, bitmap, gc, from_x, from_y, to_x, to_y);
146   XSetForeground(display, gc, BlackPixel(display, screen));
147 #endif
148 }
149
150 /* execute all pending screen drawing operations */
151 inline void FlushDisplay(void)
152 {
153 #ifndef TARGET_SDL
154   XFlush(display);
155 #endif
156 }
157
158 /* execute and wait for all pending screen drawing operations */
159 inline void SyncDisplay(void)
160 {
161 #ifndef TARGET_SDL
162   XSync(display, FALSE);
163 #endif
164 }
165
166 inline void KeyboardAutoRepeatOn(void)
167 {
168 #ifdef TARGET_SDL
169   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
170                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
171   SDL_EnableUNICODE(1);
172 #else
173   XAutoRepeatOn(display);
174 #endif
175 }
176
177 inline void KeyboardAutoRepeatOff(void)
178 {
179 #ifdef TARGET_SDL
180   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
181   SDL_EnableUNICODE(0);
182 #else
183   XAutoRepeatOff(display);
184 #endif
185 }
186
187 inline boolean PointerInWindow(DrawWindow window)
188 {
189 #ifdef TARGET_SDL
190   return TRUE;
191 #else
192   DrawWindow root, child;
193   int root_x, root_y;
194   unsigned int mask;
195   int win_x, win_y;
196
197   /* if XQueryPointer() returns False, the pointer
198      is not on the same screen as the specified window */
199   return XQueryPointer(display, window, &root, &child, &root_x, &root_y,
200                        &win_x, &win_y, &mask);
201 #endif
202 }
203
204 inline boolean PendingEvent(void)
205 {
206 #ifdef TARGET_SDL
207   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
208 #else
209   return (XPending(display) ? TRUE : FALSE);
210 #endif
211 }
212
213 inline void NextEvent(Event *event)
214 {
215 #ifdef TARGET_SDL
216   SDL_WaitEvent(event);
217 #else
218   XNextEvent(display, event);
219 #endif
220 }
221
222 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
223 {
224 #ifdef TARGET_SDL
225 #if 0
226   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
227          (int)event->keysym.unicode,
228          (int)event->keysym.sym,
229          (int)SDL_GetModState());
230 #endif
231
232   if (with_modifiers && event->keysym.unicode != 0)
233     return event->keysym.unicode;
234   else
235     return event->keysym.sym;
236 #else
237 #if 0
238   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
239          (int)XLookupKeysym(event, event->state),
240          (int)XLookupKeysym(event, 0));
241 #endif
242
243   if (with_modifiers)
244     return XLookupKeysym(event, event->state);
245   else
246     return XLookupKeysym(event, 0);
247 #endif
248 }
249
250 inline boolean SetVideoMode(void)
251 {
252 #ifdef TARGET_SDL
253   return SDLSetVideoMode(&backbuffer);
254 #else
255   boolean success = TRUE;
256
257   if (setup.fullscreen && fullscreen_available)
258   {
259     Error(ERR_WARN, "fullscreen not available in X11 version");
260
261     /* display error message only once */
262     fullscreen_available = FALSE;
263
264     success = FALSE;
265   }
266
267   return success;
268 #endif
269 }
270
271 inline void ChangeVideoModeIfNeeded(void)
272 {
273 #ifdef TARGET_SDL
274   if ((setup.fullscreen && !fullscreen_enabled && fullscreen_available) ||
275       (!setup.fullscreen && fullscreen_enabled))
276     SetVideoMode();
277 #endif
278 }
279
280 inline boolean InitAudio(void)
281 {
282 #ifdef TARGET_SDL
283   return SDLInitAudio();
284 #else
285   return TRUE;
286 #endif
287 }
288
289 inline void dummy(void)
290 {
291 #ifdef TARGET_SDL
292 #else
293 #endif
294 }