forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / Games / XInvaders3D / main-aros.c
blob488ba54e796fc8a09298e51b48b9be832a94364c
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 ------------------------------------------------------------------*/
23 #include "game.h"
25 #include <graphics/gfx.h>
26 #include <intuition/intuition.h>
27 #include <devices/inputevent.h>
28 #include <proto/arossupport.h>
29 #include <proto/exec.h>
30 #include <proto/graphics.h>
31 #include <proto/intuition.h>
33 #include <string.h>
35 /*================================================================*/
37 /* Cohan-Sutherland clipping algorithm */
39 #define CLIPLEFT 1
40 #define CLIPRIGHT 2
41 #define CLIPLOWER 4
42 #define CLIPUPPER 8
44 int clipline(int *px1, int *py1, int *px2, int *py2);
46 /*================================================================*/
48 struct GfxBase *GfxBase;
49 struct IntuitionBase *IntuitionBase;
50 struct Screen *scr;
51 struct Window *win;
52 struct BitMap *draw_bm;
53 struct RastPort *win_rp, *draw_rp;
55 ULONG coltable[256 * 3 + 2];
57 int window_width, window_height;
58 int XMax, YMax, XMin, YMin;
60 /*------------------------------------------------------------------
61 * main
64 ------------------------------------------------------------------*/
65 int main ( int argc, char **argv )
67 int i;
69 IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39);
70 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39);
72 if (!IntuitionBase || !GfxBase)
74 fprintf ( stderr, "Error: could not open libraries!\n" );
75 goto error;
78 if ( !Graphics_init ( WIN_WIDTH, WIN_HEIGHT ) )
80 fprintf ( stderr, "Error: could not initialize graphics!\n" );
81 goto error;
84 if ( !Game_init ( WIN_WIDTH, WIN_HEIGHT ) )
86 fprintf ( stderr, "Error: could not initialize game data!\n" );
87 goto error;
90 Game_main ();
92 error:
93 Graphics_shutdown ();
95 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
96 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
98 /* print contact information */
99 i = 0;
100 while ( game_about_info[i] )
102 fprintf ( stderr, "%s\n", game_about_info[i] );
103 i++;
106 return 0;
109 /*================================================================*/
111 #if defined(__AROS__)
112 static inline ULONG makecolor(UBYTE val)
114 return (val << 24) | (val << 16) | (val << 8) | (val);
116 #else
117 #define makecolor(val) ((val) * 0x01010101)
118 #endif
120 int Graphics_init ( unsigned int win_width, unsigned int win_height )
122 ULONG newcol;
123 int i, j;
125 window_width = win_width;
126 window_height = win_height;
128 XMin = 0;
129 YMin = 0;
130 XMax = win_width - 1;
131 YMax = win_height - 1;
133 scr = OpenScreenTags(0, SA_Width , window_width ,
134 SA_Height , window_height ,
135 SA_Depth , 8 ,
136 SA_Quiet , TRUE ,
137 TAG_DONE);
138 if (!scr)
140 fprintf ( stderr, "Error: could not open screen!\n" );
141 return FALSE;
144 if (GetBitMapAttr(scr->RastPort.BitMap, BMA_DEPTH) < 8)
146 fprintf ( stderr, "Error: need at least a 256 color screen!\n" );
147 return FALSE;
150 draw_bm = AllocBitMap(window_width, window_height, 0, BMF_MINPLANES, scr->RastPort.BitMap);
151 if (!draw_bm)
153 fprintf ( stderr, "Error: can't alloc draw bitmap!\n" );
154 return FALSE;
157 draw_rp = CreateRastPort();
158 if (!draw_rp)
160 fprintf ( stderr, "Error: can't alloc draw rastport!\n" );
161 return FALSE;
164 draw_rp->BitMap = draw_bm;
166 win = OpenWindowTags(0, WA_CustomScreen , (IPTR) scr ,
167 WA_Left , 0 ,
168 WA_Top , 0 ,
169 WA_Width , window_width ,
170 WA_Height , window_height ,
171 WA_Activate , TRUE ,
172 WA_Borderless , TRUE ,
173 WA_IDCMP , IDCMP_RAWKEY ,
174 TAG_DONE);
176 if (!win)
178 fprintf ( stderr, "Error: can't open window!\n" );
179 return FALSE;
182 win_rp = win->RPort;
184 /* load default color scheme */
186 coltable[0] = 256 << 16L;
188 /* red */
189 for ( i=0, j=0; i<64; i++, j++ )
191 coltable[1 + i * 3] = makecolor(j * 4);
192 coltable[1 + i * 3 + 1] = 0;
193 coltable[1 + i * 3 + 2] = 0;
196 /* green */
197 for ( i=64, j=0; i<128; i++, j++ )
199 coltable[1 + i * 3] = 0;
200 coltable[1 + i * 3 + 1] = makecolor(j * 4);
201 coltable[1 + i * 3 + 2] = 0;
204 /* blue */
205 for ( i=128, j=0; i<192; i++, j++ )
207 coltable[1 + i * 3] = 0;
208 coltable[1 + i * 3 + 1] = 0;
209 coltable[1 + i * 3 + 2] = makecolor(j * 4);
212 /* white */
213 for ( i=192, j=0; i<256; i++, j++ )
215 newcol = makecolor(j * 4);
216 coltable[1 + i * 3] = newcol;
217 coltable[1 + i * 3 + 1] = newcol;
218 coltable[1 + i * 3 + 2] = newcol;
221 /* yellow */
222 newcol = makecolor(255UL);
223 coltable[1 + 192 * 3] = newcol;
224 coltable[1 + 192 * 3 + 1] = newcol;
225 coltable[1 + 192 * 3 + 2] = makecolor(128UL);
227 LoadRGB32(&scr->ViewPort, coltable);
229 SetRast(win_rp, 0);
230 SetRast(draw_rp, 0);
232 return TRUE;
235 /*================================================================*/
237 void Graphics_shutdown ( void )
239 if (win) CloseWindow(win);
240 if (draw_rp) FreeRastPort(draw_rp);
241 if (draw_bm)
243 WaitBlit();
244 FreeBitMap(draw_bm);
246 if (scr) CloseScreen(scr);
249 /*================================================================*/
251 int Update_display ( void )
253 BltBitMapRastPort(draw_bm, 0, 0,
254 win_rp, 0, 0, window_width, window_height, 192);
255 SetRast(draw_rp, 0);
257 return TRUE;
260 /*================================================================*/
262 int Handle_events ( void )
264 struct IntuiMessage *imsg;
265 int success = TRUE;
267 while((imsg = (struct IntuiMessage *)GetMsg(win->UserPort)))
269 switch(imsg->Class)
271 case IDCMP_RAWKEY:
272 switch(imsg->Code)
274 case 0x40: /* SPACE */
275 case 0x63: /* CTRL */
276 gv->key_FIRE = TRUE;
277 break;
279 case 0x40 | IECODE_UP_PREFIX:
280 case 0x63 | IECODE_UP_PREFIX:
281 gv->key_FIRE = FALSE;
282 break;
284 case 0x4C: /* CURSOR UP */
285 case 0x3E: /* NUM 8 */
286 gv->key_UP = TRUE;
287 break;
289 case 0x4C | IECODE_UP_PREFIX:
290 case 0x3E | IECODE_UP_PREFIX:
291 gv->key_UP = FALSE;
292 break;
294 case 0x4D: /* CURSOR DOWN */
295 case 0x1E: /* NUM 2 */
296 gv->key_DOWN = TRUE;
297 break;
299 case 0x4D | IECODE_UP_PREFIX:
300 case 0x1E | IECODE_UP_PREFIX:
301 gv->key_DOWN = FALSE;
302 break;
304 case 0x4F: /* CURSOR LEFT */
305 case 0x2D: /* NUM 4 */
306 gv->key_LEFT = TRUE;
307 break;
309 case 0x4F | IECODE_UP_PREFIX:
310 case 0x2D | IECODE_UP_PREFIX:
311 gv->key_LEFT = FALSE;
312 break;
314 case 0x4E: /* CURSOR RIGHT */
315 case 0x2F: /* NUM 2 */
316 gv->key_RIGHT = TRUE;
317 break;
319 case 0x4E | IECODE_UP_PREFIX:
320 case 0x2F | IECODE_UP_PREFIX:
321 gv->key_RIGHT = FALSE;
322 break;
324 case 0x23: /* F */
325 gv->display_fps ^= TRUE;
326 break;
328 case 0x19: /* P */
329 Game_paused_toggle ();
330 break;
332 case 0x10: /* Q */
333 Game_reset ();
334 break;
336 case 0x45: /* ESC */
337 /* quit! */
338 success = FALSE;
339 break;
341 } /* switch(imsg->Code) */
343 break;
345 } /* switch(imsg->Class) */
347 ReplyMsg((struct Message *)imsg);
349 } /* while((imsg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
351 return success;
354 /*================================================================*/
356 void Draw_line ( int x0, int y0, int x1, int y1, unsigned int color )
358 if (clipline(&x0, &y0, &x1, &y1))
360 SetAPen(draw_rp, color);
361 Move(draw_rp, x0, y0);
362 Draw(draw_rp, x1, y1);
366 /*================================================================*/
368 void Draw_point ( int x0, int y0, unsigned int color )
370 if ( x0 > 2 && y0 > -2 && x0 < XMax && y0 < YMax - 5 )
372 SetAPen(draw_rp, color);
373 RectFill(draw_rp, x0 - 3, y0 + 3, x0 - 3 + 3 - 1, y0 + 3 + 3 - 1);
377 /*================================================================*/
379 void Draw_text ( char *message, int x0, int y0, unsigned int color )
381 SetAPen(draw_rp, color);
382 SetDrMd(draw_rp, JAM1);
383 Move(draw_rp, x0, y0);
384 Text(draw_rp, message, strlen(message));
387 /*================================================================*/
389 /*------------------------------------------------------------------
391 * System msec & sec Timer functions
393 ------------------------------------------------------------------*/
395 void Timer_init ( TIMER *t )
397 t->init_time_stamp = time ( NULL );
399 gettimeofday ( &(t->t0), NULL );
400 gettimeofday ( &(t->t1), NULL );
403 /*================================================================*/
405 CLOCK_T Timer_ticks ( void )
407 return clock ();
410 /*================================================================*/
412 double Timer_sec ( TIMER *t )
414 return difftime ( time(NULL), t->init_time_stamp );
417 /*================================================================*/
419 long Timer_msec ( TIMER *t )
421 long msec;
423 if ( gettimeofday ( &(t->t1), NULL ) < 0 ) return -1;
425 msec = ((t->t1.tv_sec-t->t0.tv_sec)*1000L)+
426 ((t->t1.tv_usec-t->t0.tv_usec)/1000L);
428 t->t0.tv_sec = t->t1.tv_sec;
429 t->t0.tv_usec = t->t1.tv_usec;
431 return msec;
434 /*================================================================*/
436 int clipline(int *px1, int *py1, int *px2, int *py2)
438 int x1, y1, x2, y2;
439 int K1=0,K2=0;
440 int dx,dy;
442 x1 = *px1; y1 = *py1; x2 = *px2; y2 = *py2;
444 dx=x2-x1;
445 dy=y2-y1;
447 if(y1<YMin) K1 =CLIPLOWER;
448 if(y1>YMax) K1 =CLIPUPPER;
449 if(x1<XMin) K1|=CLIPLEFT;
450 if(x1>XMax) K1|=CLIPRIGHT;
452 if(y2<YMin) K2 =CLIPLOWER;
453 if(y2>YMax) K2 =CLIPUPPER;
454 if(x2<XMin) K2|=CLIPLEFT;
455 if(x2>XMax) K2|=CLIPRIGHT;
457 while(K1 || K2)
460 if(K1 & K2)
461 return FALSE;
463 if(K1)
465 if(K1 & CLIPLEFT)
467 y1+=(XMin-x1)*dy/dx;
468 x1=XMin;
470 else if(K1 & CLIPRIGHT)
472 y1+=(XMax-x1)*dy/dx;
473 x1=XMax;
476 if(K1 & CLIPLOWER)
478 x1+=(YMin-y1)*dx/dy;
479 y1=YMin;
481 else if(K1 & CLIPUPPER)
483 x1+=(YMax-y1)*dx/dy;
484 y1=YMax;
486 K1 = 0;
488 if(y1<YMin) K1 =CLIPLOWER;
489 if(y1>YMax) K1 =CLIPUPPER;
490 if(x1<XMin) K1|=CLIPLEFT;
491 if(x1>XMax) K1|=CLIPRIGHT;
494 if(K1 & K2)
495 return FALSE;
497 if(K2)
499 if(K2 & CLIPLEFT)
501 y2+=(XMin-x2)*dy/dx;
502 x2=XMin;
504 else if(K2 & CLIPRIGHT)
506 y2+=(XMax-x2)*dy/dx;
507 x2=XMax;
510 if(K2 & CLIPLOWER)
512 x2+=(YMin-y2)*dx/dy;
513 y2=YMin;
515 else if(K2 & CLIPUPPER)
517 x2+=(YMax-y2)*dx/dy;
518 y2=YMax;
521 K2 = 0;
522 if(y2<YMin) K2 =CLIPLOWER;
523 if(y2>YMax) K2 =CLIPUPPER;
524 if(x2<XMin) K2|=CLIPLEFT;
525 if(x2>XMax) K2|=CLIPRIGHT;
529 *px1 = x1; *py1 = y1; *px2 = x2; *py2 = y2;
530 return TRUE;
533 /*================================================================*/