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 ------------------------------------------------------------------*/
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>
35 /*================================================================*/
37 /* Cohan-Sutherland clipping algorithm */
44 int clipline(int *px1
, int *py1
, int *px2
, int *py2
);
46 /*================================================================*/
48 struct GfxBase
*GfxBase
;
49 struct IntuitionBase
*IntuitionBase
;
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 /*------------------------------------------------------------------
64 ------------------------------------------------------------------*/
65 int main ( int argc
, char **argv
)
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" );
78 if ( !Graphics_init ( WIN_WIDTH
, WIN_HEIGHT
) )
80 fprintf ( stderr
, "Error: could not initialize graphics!\n" );
84 if ( !Game_init ( WIN_WIDTH
, WIN_HEIGHT
) )
86 fprintf ( stderr
, "Error: could not initialize game data!\n" );
95 if (IntuitionBase
) CloseLibrary((struct Library
*)IntuitionBase
);
96 if (GfxBase
) CloseLibrary((struct Library
*)GfxBase
);
98 /* print contact information */
100 while ( game_about_info
[i
] )
102 fprintf ( stderr
, "%s\n", game_about_info
[i
] );
109 /*================================================================*/
111 #if defined(__AROS__)
112 static inline ULONG
makecolor(UBYTE val
)
114 return (val
<< 24) | (val
<< 16) | (val
<< 8) | (val
);
117 #define makecolor(val) ((val) * 0x01010101)
120 int Graphics_init ( unsigned int win_width
, unsigned int win_height
)
125 window_width
= win_width
;
126 window_height
= win_height
;
130 XMax
= win_width
- 1;
131 YMax
= win_height
- 1;
133 scr
= OpenScreenTags(0, SA_Width
, window_width
,
134 SA_Height
, window_height
,
140 fprintf ( stderr
, "Error: could not open screen!\n" );
144 if (GetBitMapAttr(scr
->RastPort
.BitMap
, BMA_DEPTH
) < 8)
146 fprintf ( stderr
, "Error: need at least a 256 color screen!\n" );
150 draw_bm
= AllocBitMap(window_width
, window_height
, 0, BMF_MINPLANES
, scr
->RastPort
.BitMap
);
153 fprintf ( stderr
, "Error: can't alloc draw bitmap!\n" );
157 draw_rp
= CreateRastPort();
160 fprintf ( stderr
, "Error: can't alloc draw rastport!\n" );
164 draw_rp
->BitMap
= draw_bm
;
166 win
= OpenWindowTags(0, WA_CustomScreen
, (IPTR
) scr
,
169 WA_Width
, window_width
,
170 WA_Height
, window_height
,
172 WA_Borderless
, TRUE
,
173 WA_IDCMP
, IDCMP_RAWKEY
,
178 fprintf ( stderr
, "Error: can't open window!\n" );
184 /* load default color scheme */
186 coltable
[0] = 256 << 16L;
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;
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;
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);
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
;
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
);
235 /*================================================================*/
237 void Graphics_shutdown ( void )
239 if (win
) CloseWindow(win
);
240 if (draw_rp
) FreeRastPort(draw_rp
);
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);
260 /*================================================================*/
262 int Handle_events ( void )
264 struct IntuiMessage
*imsg
;
267 while((imsg
= (struct IntuiMessage
*)GetMsg(win
->UserPort
)))
274 case 0x40: /* SPACE */
275 case 0x63: /* CTRL */
279 case 0x40 | IECODE_UP_PREFIX
:
280 case 0x63 | IECODE_UP_PREFIX
:
281 gv
->key_FIRE
= FALSE
;
284 case 0x4C: /* CURSOR UP */
285 case 0x3E: /* NUM 8 */
289 case 0x4C | IECODE_UP_PREFIX
:
290 case 0x3E | IECODE_UP_PREFIX
:
294 case 0x4D: /* CURSOR DOWN */
295 case 0x1E: /* NUM 2 */
299 case 0x4D | IECODE_UP_PREFIX
:
300 case 0x1E | IECODE_UP_PREFIX
:
301 gv
->key_DOWN
= FALSE
;
304 case 0x4F: /* CURSOR LEFT */
305 case 0x2D: /* NUM 4 */
309 case 0x4F | IECODE_UP_PREFIX
:
310 case 0x2D | IECODE_UP_PREFIX
:
311 gv
->key_LEFT
= FALSE
;
314 case 0x4E: /* CURSOR RIGHT */
315 case 0x2F: /* NUM 2 */
316 gv
->key_RIGHT
= TRUE
;
319 case 0x4E | IECODE_UP_PREFIX
:
320 case 0x2F | IECODE_UP_PREFIX
:
321 gv
->key_RIGHT
= FALSE
;
325 gv
->display_fps
^= TRUE
;
329 Game_paused_toggle ();
341 } /* switch(imsg->Code) */
345 } /* switch(imsg->Class) */
347 ReplyMsg((struct Message
*)imsg
);
349 } /* while((imsg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
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 )
410 /*================================================================*/
412 double Timer_sec ( TIMER
*t
)
414 return difftime ( time(NULL
), t
->init_time_stamp
);
417 /*================================================================*/
419 long Timer_msec ( TIMER
*t
)
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
;
434 /*================================================================*/
436 int clipline(int *px1
, int *py1
, int *px2
, int *py2
)
442 x1
= *px1
; y1
= *py1
; x2
= *px2
; y2
= *py2
;
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
;
470 else if(K1
& CLIPRIGHT
)
481 else if(K1
& CLIPUPPER
)
488 if(y1
<YMin
) K1
=CLIPLOWER
;
489 if(y1
>YMax
) K1
=CLIPUPPER
;
490 if(x1
<XMin
) K1
|=CLIPLEFT
;
491 if(x1
>XMax
) K1
|=CLIPRIGHT
;
504 else if(K2
& CLIPRIGHT
)
515 else if(K2
& CLIPUPPER
)
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
;
533 /*================================================================*/