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