forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / Games / XInvaders3D / main-x11.c
blob8851b051123da0c0235aea39fe227eedb22e7503
1 /*------------------------------------------------------------------
2 main-x11.c:
4 XINVADERS 3D - 3d Shoot'em up
5 Copyright (C) 2000 Don Llopis
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ------------------------------------------------------------------*/
22 #include <X11/Xlib.h>
23 #include <X11/Xutil.h>
24 #include <X11/keysym.h>
25 #include <sys/types.h>
27 #ifdef __FreeBSD__
28 #include <floatingpoint.h>
29 #endif
31 #include "game.h"
33 /*================================================================*/
35 /* window structs */
36 Display *display;
37 int screen_num;
38 Window win;
39 Screen *screen_ptr;
40 XFontStruct *font_info;
42 /* gc's & color data */
43 GC black_gc, color_gc, text_gc;
44 XGCValues gc_values;
45 unsigned long gc_valuemask = 0;
46 XColor color_info;
47 unsigned int color_table [MAX_COLORS];
48 unsigned short color_data [MAX_COLORS][3];
50 /* window buffers */
51 Pixmap double_buffer;
53 /* misc window info */
54 char *display_name = NULL;
55 char *window_name = "XInvaders 3D";
56 XTextProperty wname;
57 char *font_name = "10x20";
58 unsigned int window_width, window_height,
59 display_width, display_height;
61 /*------------------------------------------------------------------
62 * main
65 ------------------------------------------------------------------*/
66 int main ( int argc, char **argv )
68 int i;
70 #ifdef __FreeBSD__
71 fpsetmask(0);
72 #endif
74 if ( !Graphics_init ( WIN_WIDTH, WIN_HEIGHT ) )
76 fprintf ( stderr, "Error: could not initialize graphics!\n" );
77 exit ( -1 );
80 if ( !Game_init ( WIN_WIDTH, WIN_HEIGHT ) )
82 fprintf ( stderr, "Error: could not initialize game data!\n" );
83 exit ( -1 );
86 Game_main ();
88 Graphics_shutdown ();
90 /* print contact information */
91 i = 0;
92 while ( game_about_info[i] )
94 fprintf ( stderr, "%s\n", game_about_info[i] );
95 i++;
98 return 0;
101 /*================================================================*/
103 int Graphics_init ( unsigned int win_width, unsigned int win_height )
105 int i, j;
107 window_width = win_width;
108 window_height = win_height;
110 display = XOpenDisplay ( display_name );
111 if ( !display )
112 return FALSE;
114 screen_num = DefaultScreen ( display );
115 screen_ptr = DefaultScreenOfDisplay ( display );
117 win = XCreateSimpleWindow ( display,
118 RootWindow ( display, screen_num ),
119 0, 0,
120 window_width,
121 window_height,
123 BlackPixel ( display, screen_num ),
124 WhitePixel ( display, screen_num ) );
126 XSelectInput ( display, win, ExposureMask | KeyPressMask |
127 KeyReleaseMask | ButtonPressMask |
128 ButtonReleaseMask | EnterWindowMask |
129 LeaveWindowMask );
131 XStringListToTextProperty ( &window_name, 1, &wname );
132 XSetWMProperties ( display, win, &wname, NULL,
133 NULL, 0, NULL, NULL, NULL );
135 /* create GCs for clearing, text, & drawing */
136 black_gc = XCreateGC ( display, win, gc_valuemask, &gc_values );
137 color_gc = XCreateGC ( display, win, gc_valuemask, &gc_values );
138 text_gc = XCreateGC ( display, win, gc_valuemask, &gc_values );
141 /* load default font */
142 font_info = XLoadQueryFont ( display, font_name );
143 if ( !font_info )
144 perror ( "WARNING: could not load default font.\n" );
145 else
146 XSetFont ( display, text_gc, font_info->fid );
148 /* load default color scheme */
150 /* red */
151 for ( i=0, j=0; i<64; i++, j++ )
153 color_data[i][0] = 1024 * j;
154 color_data[i][1] = color_data[i][2] = 0;
157 /* green */
158 for ( i=64, j=0; i<128; i++, j++ )
160 color_data[i][1] = 1024 * j;
161 color_data[i][0] = color_data[i][2] = 0;
164 /* blue */
165 for ( i=128, j=0; i<192; i++, j++ )
167 color_data[i][2] = 1024 * j;
168 color_data[i][0] = color_data[i][1] = 0;
171 /* white */
172 for ( i=192, j=0; i<256; i++, j++ )
174 color_data[i][0] = color_data[i][1] = color_data[i][2] = j * 1024;
177 /* yellow */
178 color_data[192][0] = 63 * 1024;
179 color_data[192][1] = 63 * 1024;
180 color_data[192][2] = 32 * 1024;
183 for ( i=0; i<MAX_COLORS; i++ )
185 color_info.red = color_data[i][0];
186 color_info.green = color_data[i][1];
187 color_info.blue = color_data[i][2];
189 XAllocColor ( display,
190 DefaultColormap ( display, screen_num ),
191 &color_info );
193 color_table[i] = color_info.pixel;
196 XSetForeground ( display, black_gc,
197 color_table[BLACK] );
198 XSetForeground ( display, color_gc,
199 color_table[GREEN] );
200 XSetForeground ( display, text_gc,
201 color_table[GREEN] );
203 double_buffer = XCreatePixmap ( display, win,
204 window_width,
205 window_height,
206 DefaultDepth ( display, screen_num ) );
207 XFillRectangle ( display,
208 double_buffer,
209 black_gc,
210 0, 0,
211 window_width,
212 window_height );
214 XAutoRepeatOff ( display );
216 XMapWindow ( display, win );
218 #ifdef GAME_DEBUG_KEYBOARD
219 fprintf ( stderr, "SPACE is %d\n", XKeysymToKeycode ( display, XK_space ) );
220 fprintf ( stderr, "Left Shift is %d\n", XKeysymToKeycode ( display, XK_Shift_L ) );
221 fprintf ( stderr, "UP is %d\n", XKeysymToKeycode ( display, XK_Up ) );
222 fprintf ( stderr, "DOWN is %d\n", XKeysymToKeycode ( display, XK_Down ) );
223 fprintf ( stderr, "LEFT is %d\n", XKeysymToKeycode ( display, XK_Left ) );
224 fprintf ( stderr, "KP UP is %d\n", XKeysymToKeycode ( display, XK_KP_Up ) );
225 fprintf ( stderr, "KP DOWN is %d\n", XKeysymToKeycode ( display, XK_KP_Down ) );
226 fprintf ( stderr, "KP LEFT is %d\n", XKeysymToKeycode ( display, XK_KP_Left ) );
227 fprintf ( stderr, "KP RIGHT is %d\n", XKeysymToKeycode ( display, XK_KP_Right ) );
228 fprintf ( stderr, "p is %d\n", XKeysymToKeycode ( display, XK_p ) );
229 fprintf ( stderr, "P is %d\n", XKeysymToKeycode ( display, XK_P ) );
230 fprintf ( stderr, "q is %d\n", XKeysymToKeycode ( display, XK_q ) );
231 fprintf ( stderr, "Q is %d\n", XKeysymToKeycode ( display, XK_Q ) );
232 fprintf ( stderr, "f is %d\n", XKeysymToKeycode ( display, XK_f ) );
233 fprintf ( stderr, "F is %d\n", XKeysymToKeycode ( display, XK_F ) );
234 fprintf ( stderr, "ESC is %d\n", XKeysymToKeycode ( display, XK_Escape ) );
235 #endif
237 return TRUE;
240 /*================================================================*/
242 void Graphics_shutdown ( void )
244 if ( font_info )
245 XUnloadFont ( display, font_info->fid );
247 XFreeGC ( display, black_gc );
248 XFreeGC ( display, color_gc );
249 XFreeGC ( display, text_gc );
251 XAutoRepeatOn ( display );
253 XCloseDisplay ( display );
256 /*================================================================*/
258 int Update_display ( void )
260 XCopyArea ( display, double_buffer, win, black_gc,
261 0, 0, window_width, window_height, 0, 0 );
263 XFillRectangle ( display, double_buffer, black_gc,
264 0, 0, window_width, window_height );
267 return TRUE;
270 /*================================================================*/
272 int Handle_events ( void )
274 XEvent event;
275 KeySym keysym;
277 XSync ( display, False );
278 while ( XPending ( display ) )
280 XNextEvent ( display, &event );
282 switch ( event.type )
284 case Expose:
285 break;
287 case EnterNotify:
288 /* turn off auto-repeat */
289 XAutoRepeatOff ( display );
290 break;
292 case LeaveNotify:
293 /* turn on auto-repeat */
294 XAutoRepeatOn ( display );
295 break;
297 case KeyPress:
298 keysym = XLookupKeysym ( &(event.xkey), 0 );
299 switch ( keysym )
301 case XK_space:
302 gv->key_FIRE = TRUE;
303 break;
305 case XK_Up:
306 case XK_KP_Up:
307 gv->key_UP = TRUE;
308 break;
310 case XK_Down:
311 case XK_KP_Down:
312 gv->key_DOWN = TRUE;
313 break;
315 case XK_Left:
316 case XK_KP_Left:
317 gv->key_LEFT = TRUE;
318 break;
320 case XK_Right:
321 case XK_KP_Right:
322 gv->key_RIGHT = TRUE;
323 break;
325 case XK_f:
326 case XK_F:
327 /* display frames per second */
328 gv->display_fps ^= TRUE;
329 break;
331 case XK_p:
332 case XK_P:
333 /* pause */
334 Game_paused_toggle ();
335 break;
337 case XK_q:
338 case XK_Q:
339 Game_reset ();
340 break;
342 case XK_Escape:
343 /* quit! */
344 return FALSE;
345 break;
347 default:
348 break;
350 break;
352 case KeyRelease:
353 keysym = XLookupKeysym ( &(event.xkey), 0 );
354 switch ( keysym )
356 case XK_space:
357 gv->key_FIRE = FALSE;
358 break;
360 case XK_Up:
361 case XK_KP_Up:
362 gv->key_UP = FALSE;
363 break;
365 case XK_Down:
366 case XK_KP_Down:
367 gv->key_DOWN = FALSE;
368 break;
370 case XK_Left:
371 case XK_KP_Left:
372 gv->key_LEFT = FALSE;
373 break;
375 case XK_Right:
376 case XK_KP_Right:
377 gv->key_RIGHT = FALSE;
378 break;
380 default:
381 break;
383 break;
385 default:
386 break;
390 return TRUE;
393 /*================================================================*/
395 void Draw_line ( int x0, int y0, int x1, int y1, unsigned int color )
397 XSetForeground ( display, color_gc,
398 color_table[color] );
400 XDrawLine ( display, double_buffer, color_gc,
401 x0, y0, x1, y1 );
404 /*================================================================*/
406 void Draw_point ( int x0, int y0, unsigned int color )
408 XSetForeground ( display, color_gc,
409 color_table[color] );
411 XFillRectangle ( display, double_buffer, color_gc,
412 x0-3, y0+3, 3, 3 );
415 /*================================================================*/
417 void Draw_text ( char *message, int x0, int y0, unsigned int color )
419 XSetForeground ( display, text_gc,
420 color_table[color] );
422 XDrawString ( display, double_buffer, text_gc,
423 x0, y0,
424 message, strlen ( message ) );
427 /*================================================================*/
429 /*------------------------------------------------------------------
431 * System msec & sec Timer functions
433 ------------------------------------------------------------------*/
435 void Timer_init ( TIMER *t )
437 t->init_time_stamp = time ( NULL );
439 gettimeofday ( &(t->t0), NULL );
440 gettimeofday ( &(t->t1), NULL );
443 /*================================================================*/
445 CLOCK_T Timer_ticks ( void )
447 return clock ();
450 /*================================================================*/
452 double Timer_sec ( TIMER *t )
454 return difftime ( time(NULL), t->init_time_stamp );
457 /*================================================================*/
459 long Timer_msec ( TIMER *t )
461 long msec;
463 if ( gettimeofday ( &(t->t1), NULL ) < 0 ) return -1;
465 msec = ((t->t1.tv_sec-t->t0.tv_sec)*1000L)+
466 ((t->t1.tv_usec-t->t0.tv_usec)/1000L);
468 t->t0.tv_sec = t->t1.tv_sec;
469 t->t0.tv_usec = t->t1.tv_usec;
471 return msec;
474 /*================================================================*/