rnd-20001125-1-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 USE_SDL_LIBRARY
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 USE_SDL_LIBRARY
29   SDLInitBufferedDisplay(backbuffer, window);
30 #else
31   X11InitBufferedDisplay(backbuffer, window);
32 #endif
33 }
34
35 inline int GetDisplayDepth(void)
36 {
37 #ifdef USE_SDL_LIBRARY
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 USE_SDL_LIBRARY
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 ClearRectangle(Bitmap bitmap, int x, int y, int width, int height)
73 {
74 #ifdef USE_SDL_LIBRARY
75   SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
76 #else
77   XFillRectangle(display, bitmap, gc, x, y, width, height);
78 #endif
79 }
80
81 inline void BlitBitmap(Bitmap src_bitmap, Bitmap dst_bitmap,
82                        int src_x, int src_y,
83                        int width, int height,
84                        int dst_x, int dst_y)
85 {
86 #ifdef USE_SDL_LIBRARY
87   SDLCopyArea(src_bitmap, dst_bitmap,
88               src_x, src_y, width, height, dst_x, dst_y);
89 #else
90   XCopyArea(display, src_bitmap, dst_bitmap, gc,
91             src_x, src_y, width, height, dst_x, dst_y);
92 #endif
93 }
94
95 #ifndef USE_SDL_LIBRARY
96 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
97 #endif
98
99 inline void SetClipMask(GC clip_gc, Pixmap clip_pixmap)
100 {
101 #ifndef USE_SDL_LIBRARY
102   XSetClipMask(display, clip_gc, clip_pixmap);
103   last_clip_gc = clip_gc;
104 #endif
105 }
106
107 inline void SetClipOrigin(GC clip_gc, int clip_x, int clip_y)
108 {
109 #ifndef USE_SDL_LIBRARY
110   XSetClipOrigin(display, clip_gc, clip_x, clip_y);
111   last_clip_gc = clip_gc;
112 #endif
113 }
114
115 inline void BlitBitmapMasked(Bitmap src_bitmap, Bitmap dst_bitmap,
116                              int src_x, int src_y,
117                              int width, int height,
118                              int dst_x, int dst_y)
119 {
120 #ifdef USE_SDL_LIBRARY
121   SDLCopyArea(src_bitmap, dst_bitmap,
122               src_x, src_y, width, height, dst_x, dst_y);
123 #else
124   XCopyArea(display, src_bitmap, dst_bitmap, last_clip_gc,
125             src_x, src_y, width, height, dst_x, dst_y);
126 #endif
127 }
128
129 inline void DrawSimpleWhiteLine(Bitmap bitmap, int from_x, int from_y,
130                                 int to_x, int to_y)
131 {
132 #ifdef USE_SDL_LIBRARY
133   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
134 #else
135   XSetForeground(display, gc, WhitePixel(display, screen));
136   XDrawLine(display, bitmap, gc, from_x, from_y, to_x, to_y);
137   XSetForeground(display, gc, BlackPixel(display, screen));
138 #endif
139 }
140
141 /* execute all pending screen drawing operations */
142 inline void FlushDisplay(void)
143 {
144 #ifndef USE_SDL_LIBRARY
145   XFlush(display);
146 #endif
147 }
148
149 /* execute and wait for all pending screen drawing operations */
150 inline void SyncDisplay(void)
151 {
152 #ifndef USE_SDL_LIBRARY
153   XSync(display, FALSE);
154 #endif
155 }
156
157 inline void KeyboardAutoRepeatOn(void)
158 {
159 #ifdef USE_SDL_LIBRARY
160   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
161                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
162   SDL_EnableUNICODE(1);
163 #else
164   XAutoRepeatOn(display);
165 #endif
166 }
167
168 inline void KeyboardAutoRepeatOff(void)
169 {
170 #ifdef USE_SDL_LIBRARY
171   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
172   SDL_EnableUNICODE(0);
173 #else
174   XAutoRepeatOff(display);
175 #endif
176 }
177
178 inline boolean PointerInWindow(DrawWindow window)
179 {
180 #ifdef USE_SDL_LIBRARY
181   return TRUE;
182 #else
183   DrawWindow root, child;
184   int root_x, root_y;
185   unsigned int mask;
186   int win_x, win_y;
187
188   /* if XQueryPointer() returns False, the pointer
189      is not on the same screen as the specified window */
190   return XQueryPointer(display, window, &root, &child, &root_x, &root_y,
191                        &win_x, &win_y, &mask);
192 #endif
193 }
194
195 inline boolean PendingEvent(void)
196 {
197 #ifdef USE_SDL_LIBRARY
198   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
199 #else
200   return (XPending(display) ? TRUE : FALSE);
201 #endif
202 }
203
204 inline void NextEvent(Event *event)
205 {
206 #ifdef USE_SDL_LIBRARY
207   SDL_WaitEvent(event);
208 #else
209   XNextEvent(display, event);
210 #endif
211 }
212
213 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
214 {
215 #ifdef USE_SDL_LIBRARY
216 #if 0
217   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
218          (int)event->keysym.unicode,
219          (int)event->keysym.sym,
220          (int)SDL_GetModState());
221 #endif
222
223   if (with_modifiers && event->keysym.unicode != 0)
224     return event->keysym.unicode;
225   else
226     return event->keysym.sym;
227 #else
228 #if 0
229   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
230          (int)XLookupKeysym(event, event->state),
231          (int)XLookupKeysym(event, 0));
232 #endif
233
234   if (with_modifiers)
235     return XLookupKeysym(event, event->state);
236   else
237     return XLookupKeysym(event, 0);
238 #endif
239 }
240
241 inline boolean SetVideoMode(void)
242 {
243 #ifdef USE_SDL_LIBRARY
244   return SDLSetVideoMode(&backbuffer, &window);
245 #else
246   boolean success = TRUE;
247
248   if (setup.fullscreen && fullscreen_available)
249   {
250     Error(ERR_WARN, "fullscreen not available in X11 version");
251
252     /* display error message only once */
253     fullscreen_available = FALSE;
254
255     success = FALSE;
256   }
257
258   return success;
259 #endif
260 }
261
262 inline void ChangeVideoModeIfNeeded(void)
263 {
264 #ifdef USE_SDL_LIBRARY
265   if ((setup.fullscreen && !fullscreen_enabled && fullscreen_available) ||
266       (!setup.fullscreen && fullscreen_enabled))
267     SetVideoMode();
268 #endif
269 }
270
271 inline void dummy(void)
272 {
273 #ifdef USE_SDL_LIBRARY
274 #else
275 #endif
276 }