1 /*------------------------------------------------------------------
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 ------------------------------------------------------------------*/
23 #include <X11/Xutil.h>
24 #include <X11/keysym.h>
25 #include <sys/types.h>
28 #include <floatingpoint.h>
33 /*================================================================*/
40 XFontStruct
*font_info
;
42 /* gc's & color data */
43 GC black_gc
, color_gc
, text_gc
;
45 unsigned long gc_valuemask
= 0;
47 unsigned int color_table
[MAX_COLORS
];
48 unsigned short color_data
[MAX_COLORS
][3];
53 /* misc window info */
54 char *display_name
= NULL
;
55 char *window_name
= "XInvaders 3D";
57 char *font_name
= "10x20";
58 unsigned int window_width
, window_height
,
59 display_width
, display_height
;
61 /*------------------------------------------------------------------
65 ------------------------------------------------------------------*/
66 int main ( int argc
, char **argv
)
74 if ( !Graphics_init ( WIN_WIDTH
, WIN_HEIGHT
) )
76 fprintf ( stderr
, "Error: could not initialize graphics!\n" );
80 if ( !Game_init ( WIN_WIDTH
, WIN_HEIGHT
) )
82 fprintf ( stderr
, "Error: could not initialize game data!\n" );
90 /* print contact information */
92 while ( game_about_info
[i
] )
94 fprintf ( stderr
, "%s\n", game_about_info
[i
] );
101 /*================================================================*/
103 int Graphics_init ( unsigned int win_width
, unsigned int win_height
)
107 window_width
= win_width
;
108 window_height
= win_height
;
110 display
= XOpenDisplay ( display_name
);
114 screen_num
= DefaultScreen ( display
);
115 screen_ptr
= DefaultScreenOfDisplay ( display
);
117 win
= XCreateSimpleWindow ( display
,
118 RootWindow ( display
, screen_num
),
123 BlackPixel ( display
, screen_num
),
124 WhitePixel ( display
, screen_num
) );
126 XSelectInput ( display
, win
, ExposureMask
| KeyPressMask
|
127 KeyReleaseMask
| ButtonPressMask
|
128 ButtonReleaseMask
| EnterWindowMask
|
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
);
144 perror ( "WARNING: could not load default font.\n" );
146 XSetFont ( display
, text_gc
, font_info
->fid
);
148 /* load default color scheme */
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;
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;
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;
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;
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
),
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
,
206 DefaultDepth ( display
, screen_num
) );
207 XFillRectangle ( display
,
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
) );
240 /*================================================================*/
242 void Graphics_shutdown ( void )
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
);
270 /*================================================================*/
272 int Handle_events ( void )
277 XSync ( display
, False
);
278 while ( XPending ( display
) )
280 XNextEvent ( display
, &event
);
282 switch ( event
.type
)
288 /* turn off auto-repeat */
289 XAutoRepeatOff ( display
);
293 /* turn on auto-repeat */
294 XAutoRepeatOn ( display
);
298 keysym
= XLookupKeysym ( &(event
.xkey
), 0 );
322 gv
->key_RIGHT
= TRUE
;
327 /* display frames per second */
328 gv
->display_fps
^= TRUE
;
334 Game_paused_toggle ();
353 keysym
= XLookupKeysym ( &(event
.xkey
), 0 );
357 gv
->key_FIRE
= FALSE
;
367 gv
->key_DOWN
= FALSE
;
372 gv
->key_LEFT
= FALSE
;
377 gv
->key_RIGHT
= FALSE
;
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
,
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
,
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
,
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 )
450 /*================================================================*/
452 double Timer_sec ( TIMER
*t
)
454 return difftime ( time(NULL
), t
->init_time_stamp
);
457 /*================================================================*/
459 long Timer_msec ( TIMER
*t
)
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
;
474 /*================================================================*/