1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Albert Veli
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* Improvised creds goes to:
24 * - Anders Clausen for ingeniously inventing the name Invadrox.
25 * - Linus Nielsen-Feltzing for patiently answering n00b questions.
29 #include "lib/highscore.h"
30 #include "lib/helper.h"
33 #include "pluginbitmaps/invadrox_background.h"
35 /* get dimensions for later use from the bitmaps */
36 #include "pluginbitmaps/invadrox_aliens.h"
37 #include "pluginbitmaps/invadrox_ships.h"
38 #include "pluginbitmaps/invadrox_bombs.h"
39 #include "pluginbitmaps/invadrox_alien_explode.h"
40 #include "pluginbitmaps/invadrox_shield.h"
41 #include "pluginbitmaps/invadrox_ufo.h"
42 #include "pluginbitmaps/invadrox_ufo_explode.h"
43 #include "pluginbitmaps/invadrox_numbers.h"
44 #include "pluginbitmaps/invadrox_fire.h"
45 #define ALIEN_WIDTH (BMPWIDTH_invadrox_aliens/2)
46 #define ALIEN_HEIGHT (BMPHEIGHT_invadrox_aliens/3)
47 #define SHIP_WIDTH BMPWIDTH_invadrox_ships
48 #define SHIP_HEIGHT (BMPHEIGHT_invadrox_ships/3)
49 #define BOMB_WIDTH (BMPWIDTH_invadrox_bombs/3)
50 #define BOMB_HEIGHT (BMPHEIGHT_invadrox_bombs/6)
51 #define ALIEN_EXPLODE_WIDTH BMPWIDTH_invadrox_alien_explode
52 #define ALIEN_EXPLODE_HEIGHT BMPHEIGHT_invadrox_alien_explode
53 #define SHIELD_WIDTH BMPWIDTH_invadrox_shield
54 #define SHIELD_HEIGHT BMPHEIGHT_invadrox_shield
55 #define UFO_WIDTH BMPWIDTH_invadrox_ufo
56 #define UFO_HEIGHT BMPHEIGHT_invadrox_ufo
57 #define UFO_EXPLODE_WIDTH BMPWIDTH_invadrox_ufo_explode
58 #define UFO_EXPLODE_HEIGHT BMPHEIGHT_invadrox_ufo_explode
59 #define NUMBERS_WIDTH (BMPWIDTH_invadrox_numbers/10)
60 #define FONT_HEIGHT BMPHEIGHT_invadrox_numbers
61 #define FIRE_WIDTH BMPWIDTH_invadrox_fire
62 #define FIRE_HEIGHT BMPHEIGHT_invadrox_fire
66 /* Original graphics is only 1bpp so it should be portable
67 * to most targets. But for now, only support the simple ones.
69 #ifndef HAVE_LCD_BITMAP
70 #error INVADROX: Unsupported LCD
74 #error INVADROX: Unsupported LCD
79 #define DBG(format, arg...) { DEBUGF("%s: " format, __FUNCTION__, ## arg); }
81 #define DBG(format, arg...) {}
84 #if CONFIG_KEYPAD == IRIVER_H100_PAD
86 #define QUIT BUTTON_OFF
87 #define LEFT BUTTON_LEFT
88 #define RIGHT BUTTON_RIGHT
89 #define FIRE BUTTON_ON
91 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
93 #define QUIT BUTTON_OFF
94 #define LEFT BUTTON_LEFT
95 #define RIGHT BUTTON_RIGHT
96 #define FIRE BUTTON_SELECT
98 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
100 #define QUIT BUTTON_POWER
101 #define LEFT BUTTON_LEFT
102 #define RIGHT BUTTON_RIGHT
103 #define FIRE BUTTON_PLAY
105 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
106 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
107 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
109 #define QUIT BUTTON_MENU
110 #define LEFT BUTTON_LEFT
111 #define RIGHT BUTTON_RIGHT
112 #define FIRE BUTTON_SELECT
114 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
116 #define QUIT BUTTON_POWER
117 #define LEFT BUTTON_LEFT
118 #define RIGHT BUTTON_RIGHT
119 #define FIRE BUTTON_SELECT
121 #elif CONFIG_KEYPAD == GIGABEAT_PAD
123 #define QUIT BUTTON_POWER
124 #define LEFT BUTTON_LEFT
125 #define RIGHT BUTTON_RIGHT
126 #define FIRE BUTTON_SELECT
128 #elif CONFIG_KEYPAD == SANSA_E200_PAD
130 #define QUIT BUTTON_POWER
131 #define LEFT BUTTON_LEFT
132 #define RIGHT BUTTON_RIGHT
133 #define FIRE BUTTON_SELECT
135 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
137 #define QUIT (BUTTON_HOME|BUTTON_REPEAT)
138 #define LEFT BUTTON_LEFT
139 #define RIGHT BUTTON_RIGHT
140 #define FIRE BUTTON_SELECT
142 #elif CONFIG_KEYPAD == TATUNG_TPJ1022_PAD
144 /* TODO: Figure out which buttons to use for Tatung Elio TPJ-1022 */
145 #define QUIT BUTTON_AB
146 #define LEFT BUTTON_LEFT
147 #define RIGHT BUTTON_RIGHT
148 #define FIRE BUTTON_MENU
150 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
152 #define QUIT BUTTON_BACK
153 #define LEFT BUTTON_LEFT
154 #define RIGHT BUTTON_RIGHT
155 #define FIRE BUTTON_SELECT
157 #elif CONFIG_KEYPAD == COWON_D2_PAD
159 #define QUIT BUTTON_POWER
160 #define LEFT BUTTON_MINUS
161 #define RIGHT BUTTON_PLUS
162 #define FIRE BUTTON_MENU
164 #elif CONFIG_KEYPAD == IAUDIO67_PAD
166 #define QUIT BUTTON_POWER
167 #define LEFT BUTTON_LEFT
168 #define RIGHT BUTTON_RIGHT
169 #define FIRE BUTTON_PLAY
171 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
173 #define QUIT BUTTON_BACK
174 #define LEFT BUTTON_LEFT
175 #define RIGHT BUTTON_RIGHT
176 #define FIRE BUTTON_SELECT
178 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
180 #define QUIT BUTTON_POWER
181 #define LEFT BUTTON_PREV
182 #define RIGHT BUTTON_NEXT
183 #define FIRE BUTTON_PLAY
185 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
186 CONFIG_KEYPAD == ONDAVX777_PAD || \
187 CONFIG_KEYPAD == MROBE500_PAD
189 #define QUIT BUTTON_POWER
191 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
193 #define QUIT BUTTON_REC
194 #define LEFT BUTTON_LEFT
195 #define RIGHT BUTTON_RIGHT
196 #define FIRE BUTTON_PLAY
199 #error INVADROX: Unsupported keypad
206 #ifdef HAVE_TOUCHSCREEN
221 #define TOUCHSCREEN_QUIT BUTTON_TOPLEFT
222 #define TOUCHSCREEN_LEFT (BUTTON_MIDLEFT | BUTTON_BOTTOMLEFT)
223 #define TOUCHSCREEN_RIGHT (BUTTON_MIDRIGHT | BUTTON_BOTTOMRIGHT)
224 #define TOUCHSCREEN_FIRE (BUTTON_CENTER | BUTTON_BOTTOMMIDDLE)
226 #define ACTION_QUIT (QUIT | TOUCHSCREEN_QUIT | RC_QUIT)
227 #define ACTION_LEFT (LEFT | TOUCHSCREEN_LEFT)
228 #define ACTION_RIGHT (RIGHT | TOUCHSCREEN_RIGHT)
229 #define ACTION_FIRE (FIRE | TOUCHSCREEN_FIRE)
231 #else /* HAVE_TOUCHSCREEN */
233 #define ACTION_QUIT (QUIT | RC_QUIT)
234 #define ACTION_LEFT LEFT
235 #define ACTION_RIGHT RIGHT
236 #define ACTION_FIRE FIRE
241 #define UNUSED __attribute__ ((unused))
244 /* Defines common to all models */
245 #define UFO_Y (SCORENUM_Y + FONT_HEIGHT + ALIEN_HEIGHT)
246 #define PLAYFIELD_Y (LCD_HEIGHT - SHIP_HEIGHT - 2)
247 #define PLAYFIELD_WIDTH (LCD_WIDTH - 2 * PLAYFIELD_X)
248 #define LEVEL_X (LCD_WIDTH - PLAYFIELD_X - LIVES_X - 2 * NUMBERS_WIDTH - 3 * NUM_SPACING)
249 #define SHIP_MIN_X (PLAYFIELD_X + PLAYFIELD_WIDTH / 5 - SHIELD_WIDTH / 2 - SHIP_WIDTH)
250 #define SHIP_MAX_X (PLAYFIELD_X + 4 * PLAYFIELD_WIDTH / 5 + SHIELD_WIDTH / 2)
251 /* SCORE_Y = 0 for most targets. Gigabeat redefines it later. */
256 /* m:robe 500 defines */
257 #if ((LCD_WIDTH == 640) && (LCD_HEIGHT == 480)) || \
258 ((LCD_WIDTH == 480) && (LCD_HEIGHT == 640))
260 /* Original arcade game size 224x240, 1bpp with
261 * red overlay at top and green overlay at bottom.
263 * M:Robe 500: 640x480x16
264 * ======================
267 #define ARCADISH_GRAPHICS
268 #define PLAYFIELD_X 48
269 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
270 #define ALIEN_START_Y (UFO_Y + ALIEN_HEIGHT)
271 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
272 #define SCORENUM_Y (SCORE_Y + FONT_HEIGHT + 2)
273 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
274 #define SHIELD_Y (PLAYFIELD_Y - 5 * SHIP_HEIGHT)
278 /* iPod Video defines */
279 #elif (LCD_WIDTH == 320) && (LCD_HEIGHT == 240)
281 /* Original arcade game size 224x240, 1bpp with
282 * red overlay at top and green overlay at bottom.
284 * iPod Video: 320x240x16
285 * ======================
286 * X: 48p padding at left/right gives 224p playfield in middle.
287 * 10p "border" gives 204p actual playfield. UFO use full 224p.
290 * MAX_X = (204 - 12) / 2 - 1 = 95
297 * 2 Space Aliens start at 32 + 3 * 8 = 56
299 * space ~7*8 128 | 18.75 aliens space between
300 * shield 2*8 182 | first alien and ship.
301 * space 8 198 | MAX_Y = 18
304 * hline 1 230 - PLAYFIELD_Y
305 * bottom border 10 240
306 * Lives and Level goes inside bottom border
309 #define ARCADISH_GRAPHICS
310 #define PLAYFIELD_X 48
311 #define SHIP_Y (PLAYFIELD_Y - 3 * SHIP_HEIGHT)
312 #define ALIEN_START_Y (UFO_Y + 3 * ALIEN_HEIGHT)
313 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
314 #define SCORENUM_Y SCORE_Y + (2 * (FONT_HEIGHT + 1) + 1)
315 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
316 #define SHIELD_Y (PLAYFIELD_Y - 6 * SHIP_HEIGHT)
320 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 220)
322 /* Sandisk Sansa e200: 176x220x16
323 * ==============================
324 * X: No padding. 8p border -> 160p playfield.
326 * 8p Aliens with 3p spacing -> 88 + 30 = 118p aliens block.
327 * (160 - 118) / 2 = 21 rounds for whole block (more than original)
328 * MAX_X = (160 - 8) / 2 - 1 = 75 rounds for single alien (less than original)
338 * space ~7*5 155 | 18.6 aliens space between
339 * shield 2*5 188 | first alien and ship.
340 * space 5 198 | MAX_Y = 18
343 * hline 1 213 PLAYFIELD_Y
346 * Lives and Level goes inside bottom border
349 #define SMALL_GRAPHICS
350 #define PLAYFIELD_X 0
351 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
352 #define SHIELD_Y (SHIP_Y - SHIP_HEIGHT - SHIELD_HEIGHT)
353 #define ALIEN_START_Y (UFO_Y + 3 * SHIP_HEIGHT)
354 /* Redefine SCORE_Y */
357 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
358 #define SCORENUM_Y (SCORE_Y + 2 * FONT_HEIGHT)
359 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
364 #elif (LCD_WIDTH == 176) && (LCD_HEIGHT == 132)
366 /* iPod Nano: 176x132x16
367 * ======================
368 * X: No padding. 8p border -> 160p playfield.
383 * ALIEN_START_Y (UFO_Y + 12)
385 * 8p Aliens with 3p spacing -> 88 + 30 = 118p aliens block.
386 * (160 - 118) / 2 = 21 rounds for whole block (more than original)
387 * MAX_X = (160 - 8) / 2 - 1 = 75 rounds for single alien (less than original)
389 * Y: Scoreline 5 0 (combine scoretext and numbers on same line)
394 * space ~7*5 67 | Just above 18 aliens space between
395 * shield 2*5 100 | first alien and ship.
396 * space 5 110 | MAX_Y = 18
399 * hline 1 125 PLAYFIELD_Y
400 * bottom border 6 126
402 * Lives and Level goes inside bottom border
405 #define SMALL_GRAPHICS
406 #define PLAYFIELD_X 0
407 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
408 #define ALIEN_START_Y (UFO_Y + 12)
409 #define SCORENUM_X (PLAYFIELD_X + 6 * NUMBERS_WIDTH + 5 * NUM_SPACING)
410 #define SCORENUM_Y SCORE_Y
411 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 4 * NUMBERS_WIDTH - 3 * NUM_SPACING)
412 #define SHIELD_Y (SHIP_Y - SHIP_HEIGHT - SHIELD_HEIGHT)
416 #elif (LCD_WIDTH == 160) && (LCD_HEIGHT == 128)
418 /* iAudio X5, iRiver H10 20Gb, iPod 3g/4g, H100, M5: 160x128
419 * =========================================================
420 * X: No padding. No border -> 160p playfield.
435 * ALIEN_START_Y (UFO_Y + 10)
437 * 8p Aliens with 3p spacing -> 88 + 30 = 118p aliens block.
438 * (160 - 118) / 2 = 21 rounds for whole block (more than original)
439 * MAX_X = (160 - 8) / 2 - 1 = 75 rounds for single alien (less than original)
441 * Y: Scoreline 5 0 (combine scoretext and numbers on same line)
446 * space ~6*5 65 | Just above 18 aliens space between
447 * shield 2*5 96 | first alien and ship.
448 * space 5 106 | MAX_Y = 18
451 * hline 1 121 PLAYFIELD_Y
452 * bottom border 6 122
454 * Lives and Level goes inside bottom border
457 #define SMALL_GRAPHICS
458 #define PLAYFIELD_X 0
459 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
460 #define ALIEN_START_Y (UFO_Y + 10)
461 #define SCORENUM_X (PLAYFIELD_X + 6 * NUMBERS_WIDTH + 5 * NUM_SPACING)
462 #define SCORENUM_Y SCORE_Y
463 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 4 * NUMBERS_WIDTH - 3 * NUM_SPACING)
464 #define SHIELD_Y (SHIP_Y - SHIP_HEIGHT - SHIELD_HEIGHT)
469 #elif (LCD_WIDTH == 240) && ((LCD_HEIGHT == 320) || (LCD_HEIGHT == 400))
471 /* Gigabeat: 240x320x16
472 * ======================
473 * X: 8p padding at left/right gives 224p playfield in middle.
474 * 10p "border" gives 204p actual playfield. UFO use full 224p.
475 * Y: Use bottom 240p for playfield and top 80 pixels for logo.
477 * MAX_X = (204 - 12) / 2 - 1 = 95
479 * Y: Score text 7 0 + 80
484 * 2 Space Aliens start at 32 + 3 * 8 = 56
486 * space ~7*8 128 | 18.75 aliens space between
487 * shield 2*8 182 | first alien and ship.
488 * space 8 198 | MAX_Y = 18
491 * hline 1 230 310 - PLAYFIELD_Y
492 * bottom border 10 240 320
493 * Lives and Level goes inside bottom border
496 #define ARCADISH_GRAPHICS
497 #define PLAYFIELD_X 8
498 #define SHIP_Y (PLAYFIELD_Y - 3 * SHIP_HEIGHT)
499 #define ALIEN_START_Y (UFO_Y + 3 * ALIEN_HEIGHT)
500 /* Redefine SCORE_Y */
503 #define SCORENUM_X (PLAYFIELD_X + NUMBERS_WIDTH)
504 #define SCORENUM_Y SCORE_Y + (2 * (FONT_HEIGHT + 1) + 1)
505 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 1 - 6 * NUMBERS_WIDTH - 5 * NUM_SPACING)
506 #define SHIELD_Y (PLAYFIELD_Y - 6 * SHIP_HEIGHT)
510 #elif (LCD_WIDTH == 220) && (LCD_HEIGHT == 176)
512 /* TPJ1022, H300, iPod Color: 220x176x16
513 * ============================
514 * X: 0p padding at left/right gives 220p playfield in middle.
515 * 8p "border" gives 204p actual playfield. UFO use full 220p.
516 * Y: Use full 176p for playfield.
518 * MAX_X = (204 - 12) / 2 - 1 = 95
523 * 7 Space Aliens start at 15 + 3 * 8 = 56
525 * space ~7*8 103 | 15.6 aliens space between
526 * shield 2*8 126 | first alien and ship.
527 * space 8 142 | MAX_Y = 15
530 * hline 1 166 - PLAYFIELD_Y
531 * bottom border 10 176
532 * Lives and Level goes inside bottom border
535 #define ARCADISH_GRAPHICS
536 #define PLAYFIELD_X 0
537 #define SHIP_Y (PLAYFIELD_Y - 2 * SHIP_HEIGHT)
538 #define ALIEN_START_Y (UFO_Y + 10)
539 #define SCORENUM_Y SCORE_Y
540 #define SCORENUM_X (PLAYFIELD_X + 6 * NUMBERS_WIDTH + 6 * NUM_SPACING)
541 #define HISCORENUM_X (LCD_WIDTH - PLAYFIELD_X - 4 * NUMBERS_WIDTH - 3 * NUM_SPACING)
542 #define SHIELD_Y (PLAYFIELD_Y - 5 * SHIP_HEIGHT)
548 #error INVADROX: Unsupported LCD type
551 #define MAX_X ((LCD_WIDTH-LIVES_X*2-PLAYFIELD_X*2 - ALIEN_WIDTH)/2 - 1)
553 /* Defines common to each "graphic type" */
554 #ifdef ARCADISH_GRAPHICS
556 #define SHOT_HEIGHT 5
557 #define ALIEN_SPACING 4
558 #define ALIEN_SPEED 2
560 #define NUM_SPACING 3
565 #elif defined SMALL_GRAPHICS
567 #define SHOT_HEIGHT 4
568 #define ALIEN_SPACING 3
569 #define ALIEN_SPEED 2
571 #define NUM_SPACING 2
577 #error Graphic type not defined
583 #define SLIME_GREEN LCD_RGBPACK(31, 254, 31)
584 #define UFO_RED LCD_RGBPACK(254, 31, 31)
585 #elif (LCD_DEPTH == 2)
586 #define SLIME_GREEN LCD_LIGHTGRAY
587 #define UFO_RED LCD_LIGHTGRAY
589 #error LCD type not implemented yet
597 /* Fire/bomb/ufo states */
600 #define S_SHOWSCORE 2
603 /* Fire/bomb targets */
605 #define TARGET_SHIELD 1
606 #define TARGET_SHIP 2
607 #define TARGET_BOTTOM 3
610 #define HISCOREFILE PLUGIN_GAMES_DIR "/invadrox.high"
613 /* The time (in ms) for one iteration through the game loop - decrease this
614 * to speed up the game - note that current_tick is (currently) only accurate
620 /* Physical x is at PLAYFIELD_X + LIVES_X + x * ALIEN_SPEED
621 * Physical y is at y * ALIEN_HEIGHT
624 int x
; /* x-coordinate (0 - 95) */
625 int y
; /* y-coordinate (0 - 18) */
626 unsigned char type
; /* 0 (Kang), 1 (Kodos), 2 (Serak) */
627 unsigned char state
; /* Dead, alive or bomber */
630 /* Aliens box 5 rows * ALIENS aliens in each row */
631 struct alien aliens
[5 * ALIENS
];
637 unsigned char frame
; /* Current animation frame */
638 unsigned char frames
; /* Number of frames in animation */
639 unsigned char target
; /* Remember target during explosion frames */
640 int state
; /* 0 (IDLE) = inactive, 1 (FIRE) or negative, exploding */
642 struct bomb bombs
[MAX_BOMBS
];
643 /* Increase max_bombs at higher levels */
646 /* Raw framebuffer value of shield/ship green color */
647 fb_data screen_green
, screen_white
;
649 /* For optimization, precalculate startoffset of each scanline */
650 unsigned int ytab
[LCD_HEIGHT
];
654 int scores
[3] = { 30, 20, 10 };
656 struct highscore hiscore
;
657 bool game_over
= false;
658 int ship_x
, old_ship_x
, ship_dir
, ship_acc
, max_ship_speed
;
659 int ship_frame
, ship_frame_counter
;
661 int fire
, fire_target
, fire_x
, fire_y
;
662 int curr_alien
, aliens_paralyzed
, gamespeed
;
663 int ufo_state
, ufo_x
;
665 bool aliens_down
, aliens_right
, hit_left_border
, hit_right_border
;
668 /* No standard get_pixel function yet, use this hack instead */
671 #if defined(LCD_STRIDEFORMAT) && LCD_STRIDEFORMAT == VERTICAL_STRIDE
672 inline fb_data
get_pixel(int x
, int y
)
674 return rb
->lcd_framebuffer
[x
*LCD_HEIGHT
+y
];
677 inline fb_data
get_pixel(int x
, int y
)
679 return rb
->lcd_framebuffer
[ytab
[y
] + x
];
683 #elif (LCD_DEPTH == 2)
685 #if (LCD_PIXELFORMAT == HORIZONTAL_PACKING)
686 static const unsigned char shifts
[4] = {
689 /* Horizontal packing */
690 inline fb_data
get_pixel(int x
, int y
)
692 return (rb
->lcd_framebuffer
[ytab
[y
] + (x
>> 2)] >> shifts
[x
& 3]) & 3;
695 /* Vertical packing */
696 static const unsigned char shifts
[4] = {
699 inline fb_data
get_pixel(int x
, int y
)
701 return (rb
->lcd_framebuffer
[ytab
[y
] + x
] >> shifts
[y
& 3]) & 3;
703 #endif /* Horizontal/Vertical packing */
706 #error get_pixel: pixelformat not implemented yet
710 /* Draw "digits" least significant digits of num at (x,y) */
711 void draw_number(int x
, int y
, int num
, int digits
)
716 for (i
= digits
- 1; i
>= 0; i
--) {
719 rb
->lcd_bitmap_part(invadrox_numbers
, d
* NUMBERS_WIDTH
, 0,
721 BMPWIDTH_invadrox_numbers
,
722 BMPHEIGHT_invadrox_numbers
),
723 x
+ i
* (NUMBERS_WIDTH
+ NUM_SPACING
), y
,
724 NUMBERS_WIDTH
, FONT_HEIGHT
);
727 rb
->lcd_update_rect(x
, y
, 4 * NUMBERS_WIDTH
+ 3 * NUM_SPACING
, FONT_HEIGHT
);
731 inline void draw_score(void)
733 draw_number(SCORENUM_X
, SCORENUM_Y
, score
, 4);
734 if (score
> hiscore
.score
) {
735 /* Draw new hiscore (same as score) */
736 draw_number(HISCORENUM_X
, SCORENUM_Y
, score
, 4);
741 void draw_level(void)
743 draw_number(LEVEL_X
+ 2 * NUM_SPACING
, PLAYFIELD_Y
+ 2, level
, 2);
747 void draw_lives(void)
751 rb
->lcd_bitmap_part(invadrox_numbers
, lives
* NUMBERS_WIDTH
, 0,
753 BMPWIDTH_invadrox_numbers
,
754 BMPHEIGHT_invadrox_numbers
),
755 PLAYFIELD_X
+ LIVES_X
, PLAYFIELD_Y
+ 2,
756 NUMBERS_WIDTH
, FONT_HEIGHT
);
759 for (i
= 0; i
< (lives
- 1); i
++) {
760 rb
->lcd_bitmap_part(invadrox_ships
, 0, 0,
762 BMPWIDTH_invadrox_ships
,
763 BMPHEIGHT_invadrox_ships
),
764 PLAYFIELD_X
+ LIVES_X
+ SHIP_WIDTH
+ i
* (SHIP_WIDTH
+ NUM_SPACING
),
765 PLAYFIELD_Y
+ 1, SHIP_WIDTH
, SHIP_HEIGHT
);
768 /* Erase ship to the right (if less than MAX_LIVES) */
769 if (lives
< MAX_LIVES
) {
770 rb
->lcd_fillrect(PLAYFIELD_X
+ LIVES_X
+ SHIP_WIDTH
+ i
* (SHIP_WIDTH
+ NUM_SPACING
),
771 PLAYFIELD_Y
+ 1, SHIP_WIDTH
, SHIP_HEIGHT
);
773 /* Update lives (and level) part of screen */
774 rb
->lcd_update_rect(PLAYFIELD_X
+ LIVES_X
, PLAYFIELD_Y
+ 1,
775 PLAYFIELD_WIDTH
- 2 * LIVES_X
, MAX(FONT_HEIGHT
+ 1, SHIP_HEIGHT
+ 1));
779 inline void draw_aliens(void)
783 for (i
= 0; i
< 5 * ALIENS
; i
++) {
784 rb
->lcd_bitmap_part(invadrox_aliens
, aliens
[i
].x
& 1 ? ALIEN_WIDTH
: 0,
785 aliens
[i
].type
* ALIEN_HEIGHT
,
787 BMPWIDTH_invadrox_aliens
,
788 BMPHEIGHT_invadrox_aliens
),
789 PLAYFIELD_X
+ LIVES_X
+ aliens
[i
].x
* ALIEN_SPEED
,
790 ALIEN_START_Y
+ aliens
[i
].y
* ALIEN_HEIGHT
,
791 ALIEN_WIDTH
, ALIEN_HEIGHT
);
796 /* Return false if there is no next alive alien (round is over) */
797 inline bool next_alien(void)
803 if (curr_alien
% ALIENS
== 0) {
804 /* End of this row. Move up one row. */
805 curr_alien
-= 2 * ALIENS
;
806 if (curr_alien
< 0) {
807 /* No more aliens in this round. */
808 curr_alien
= 4 * ALIENS
;
812 } while (aliens
[curr_alien
].state
== DEAD
&& ret
);
815 /* No more alive aliens. Round finished. */
816 if (hit_right_border
) {
817 if (hit_left_border
) {
818 DBG("ERROR: both left and right borders are set (%d)\n", curr_alien
);
820 /* Move down-left next round */
821 aliens_right
= false;
823 hit_right_border
= false;
824 } else if (hit_left_border
) {
825 /* Move down-right next round */
828 hit_left_border
= false;
830 /* Not left nor right. Set down to false. */
839 /* All aliens have been moved.
840 * Set curr_alien to first alive.
841 * Return false if no-one is left alive.
843 bool first_alien(void)
847 for (y
= 4; y
>= 0; y
--) {
848 for (i
= y
* ALIENS
; i
< (y
+ 1) * ALIENS
; i
++) {
849 if (aliens
[i
].state
!= DEAD
) {
856 /* All aliens dead. */
857 level_finished
= true;
863 bool move_aliens(void)
865 int x
, y
, old_x
, old_y
;
867 /* Move current alien (curr_alien is pointing to a living alien) */
869 old_x
= aliens
[curr_alien
].x
;
870 old_y
= aliens
[curr_alien
].y
;
873 aliens
[curr_alien
].y
++;
874 if (aliens
[curr_alien
].y
== MAX_Y
) {
875 /* Alien is at bottom. Game Over. */
876 DBG("Alien %d is at bottom. Game Over.\n", curr_alien
);
884 if (aliens
[curr_alien
].x
< MAX_X
) {
885 aliens
[curr_alien
].x
++;
888 /* Now, after move, check if we hit the right border. */
889 if (aliens
[curr_alien
].x
== MAX_X
) {
890 hit_right_border
= true;
895 if (aliens
[curr_alien
].x
> 0) {
896 aliens
[curr_alien
].x
--;
899 /* Now, after move, check if we hit the left border. */
900 if (aliens
[curr_alien
].x
== 0) {
901 hit_left_border
= true;
905 /* Erase old position */
906 x
= PLAYFIELD_X
+ LIVES_X
+ old_x
* ALIEN_SPEED
;
907 y
= ALIEN_START_Y
+ old_y
* ALIEN_HEIGHT
;
908 if (aliens
[curr_alien
].y
!= old_y
) {
909 /* Moved in y-dir. Erase whole alien. */
910 rb
->lcd_fillrect(x
, y
, ALIEN_WIDTH
, ALIEN_HEIGHT
);
913 /* Erase left edge */
914 rb
->lcd_fillrect(x
, y
, ALIEN_SPEED
, ALIEN_HEIGHT
);
916 /* Erase right edge */
917 x
+= ALIEN_WIDTH
- ALIEN_SPEED
;
918 rb
->lcd_fillrect(x
, y
, ALIEN_SPEED
, ALIEN_HEIGHT
);
922 /* Draw alien at new pos */
923 x
= PLAYFIELD_X
+ LIVES_X
+ aliens
[curr_alien
].x
* ALIEN_SPEED
;
924 y
= ALIEN_START_Y
+ aliens
[curr_alien
].y
* ALIEN_HEIGHT
;
925 rb
->lcd_bitmap_part(invadrox_aliens
,
926 aliens
[curr_alien
].x
& 1 ? ALIEN_WIDTH
: 0,
927 aliens
[curr_alien
].type
* ALIEN_HEIGHT
,
929 BMPWIDTH_invadrox_aliens
,
930 BMPHEIGHT_invadrox_aliens
),
931 x
, y
, ALIEN_WIDTH
, ALIEN_HEIGHT
);
934 /* Round finished. Set curr_alien to first alive from bottom. */
935 if (!first_alien()) {
936 /* Should never happen. Taken care of in move_fire(). */
939 /* TODO: Play next background sound */
946 inline void draw_ship(void)
949 if (old_ship_x
< ship_x
) {
950 /* Move right. Erase leftmost part of ship. */
951 rb
->lcd_fillrect(old_ship_x
, SHIP_Y
, ship_x
- old_ship_x
, SHIP_HEIGHT
);
952 } else if (old_ship_x
> ship_x
) {
953 /* Move left. Erase rightmost part of ship. */
954 rb
->lcd_fillrect(ship_x
+ SHIP_WIDTH
, SHIP_Y
, old_ship_x
- ship_x
, SHIP_HEIGHT
);
958 rb
->lcd_bitmap_part(invadrox_ships
, 0, ship_frame
* SHIP_HEIGHT
,
960 BMPWIDTH_invadrox_ships
,
961 BMPHEIGHT_invadrox_ships
),
962 ship_x
, SHIP_Y
, SHIP_WIDTH
, SHIP_HEIGHT
);
964 /* Alternate between frame 1 and 2 during hit */
965 ship_frame_counter
++;
966 if (ship_frame_counter
> 2) {
967 ship_frame_counter
= 0;
969 if (ship_frame
> 2) {
975 /* Save ship_x for next time */
980 inline void fire_alpha(int xc
, int yc
, fb_data color
)
982 int oldmode
= rb
->lcd_get_drawmode();
984 rb
->lcd_set_foreground(color
);
985 rb
->lcd_set_drawmode(DRMODE_FG
);
987 rb
->lcd_mono_bitmap(invadrox_fire
, xc
- (FIRE_WIDTH
/2), yc
, FIRE_WIDTH
, FIRE_HEIGHT
);
989 rb
->lcd_set_foreground(LCD_BLACK
);
990 rb
->lcd_set_drawmode(oldmode
);
996 bool hit_green
= false;
997 bool hit_white
= false;
999 static int exploding_alien
= -1;
1002 if (fire
== S_IDLE
) {
1006 /* Alien hit. Wait until explosion is finished. */
1007 if (aliens_paralyzed
< 0) {
1009 if (aliens_paralyzed
== 0) {
1010 /* Erase exploding_alien */
1011 rb
->lcd_fillrect(PLAYFIELD_X
+ LIVES_X
+ aliens
[exploding_alien
].x
* ALIEN_SPEED
,
1012 ALIEN_START_Y
+ aliens
[exploding_alien
].y
* ALIEN_HEIGHT
,
1013 ALIEN_EXPLODE_WIDTH
, ALIEN_HEIGHT
);
1015 /* Special case. We killed curr_alien. */
1016 if (exploding_alien
== curr_alien
) {
1017 if (!next_alien()) {
1018 /* Round finished. Set curr_alien to first alive from bottom. */
1026 if (fire
== S_ACTIVE
) {
1029 rb
->lcd_vline(fire_x
, fire_y
, fire_y
+ SHOT_HEIGHT
);
1032 if (fire_y
<= SCORENUM_Y
+ FONT_HEIGHT
+ 4) {
1034 /* TODO: Play explode sound */
1037 fire_target
= TARGET_TOP
;
1038 fire_alpha(fire_x
, fire_y
, UFO_RED
);
1043 fire_y
-= FIRE_SPEED
;
1046 if (ufo_state
== S_ACTIVE
) {
1047 if ((ABS(ufo_x
+ UFO_WIDTH
/ 2 - fire_x
) <= UFO_WIDTH
/ 2) &&
1048 (fire_y
<= UFO_Y
+ UFO_HEIGHT
)) {
1049 ufo_state
= S_EXPLODE
;
1051 fire_target
= TARGET_UFO
;
1052 /* Center explosion */
1053 ufo_x
-= (UFO_EXPLODE_WIDTH
- UFO_WIDTH
) / 2;
1054 rb
->lcd_bitmap(invadrox_ufo_explode
, ufo_x
, UFO_Y
- 1,
1055 UFO_EXPLODE_WIDTH
, UFO_EXPLODE_HEIGHT
);
1060 /* Hit bomb? (check position, not pixel value) */
1061 for (i
= 0; i
< max_bombs
; i
++) {
1062 if (bombs
[i
].state
== S_ACTIVE
) {
1063 /* Count as hit if within BOMB_WIDTH pixels */
1064 if ((ABS(bombs
[i
].x
- fire_x
) < BOMB_WIDTH
) &&
1065 (fire_y
- bombs
[i
].y
< BOMB_HEIGHT
)) {
1067 rb
->lcd_fillrect(bombs
[i
].x
, bombs
[i
].y
, BOMB_WIDTH
, BOMB_HEIGHT
);
1068 bombs
[i
].state
= S_IDLE
;
1069 /* Explode ship fire */
1071 fire_target
= TARGET_SHIELD
;
1072 fire_alpha(fire_x
, fire_y
, LCD_WHITE
);
1079 for (i
= FIRE_SPEED
; i
>= 0; i
--) {
1080 pix
= get_pixel(fire_x
, fire_y
+ i
);
1081 if(pix
== screen_white
) {
1086 if(pix
== screen_green
) {
1096 /* TODO: Play explode sound */
1099 fire_target
= TARGET_SHIELD
;
1100 /* Center explosion around hit pixel */
1101 fire_y
-= FIRE_HEIGHT
/ 2;
1102 fire_alpha(fire_x
, fire_y
, SLIME_GREEN
);
1109 for (i
= 0; i
< 5 * ALIENS
; i
++) {
1110 if (aliens
[i
].state
!= DEAD
&&
1111 (ABS(fire_x
- (PLAYFIELD_X
+ LIVES_X
+ aliens
[i
].x
* ALIEN_SPEED
+
1112 ALIEN_WIDTH
/ 2)) <= ALIEN_WIDTH
/ 2) &&
1113 (ABS(fire_y
- (ALIEN_START_Y
+ aliens
[i
].y
* ALIEN_HEIGHT
+
1114 ALIEN_HEIGHT
/ 2)) <= ALIEN_HEIGHT
/ 2)) {
1116 /* TODO: play alien hit sound */
1118 if (aliens
[i
].state
== BOMBER
) {
1119 /* Set (possible) alien above to bomber */
1120 for (j
= i
- ALIENS
; j
>= 0; j
-= ALIENS
) {
1121 if (aliens
[j
].state
!= DEAD
) {
1122 /* printf("New bomber (%d, %d)\n", j % ALIENS, j / ALIENS); */
1123 aliens
[j
].state
= BOMBER
;
1128 aliens
[i
].state
= DEAD
;
1129 exploding_alien
= i
;
1130 score
+= scores
[aliens
[i
].type
];
1132 /* Update score part of screen */
1133 rb
->lcd_update_rect(SCORENUM_X
, SCORENUM_Y
,
1134 PLAYFIELD_WIDTH
- 2 * NUMBERS_WIDTH
, FONT_HEIGHT
);
1136 /* Paralyze aliens S_EXPLODE frames */
1137 aliens_paralyzed
= S_EXPLODE
;
1138 rb
->lcd_bitmap(invadrox_alien_explode
,
1139 PLAYFIELD_X
+ LIVES_X
+ aliens
[i
].x
* ALIEN_SPEED
,
1140 ALIEN_START_Y
+ aliens
[i
].y
* ALIEN_HEIGHT
,
1141 ALIEN_EXPLODE_WIDTH
, ALIEN_EXPLODE_HEIGHT
);
1142 /* Since alien is 1 pixel taller than explosion sprite, erase bottom line */
1143 rb
->lcd_hline(PLAYFIELD_X
+ LIVES_X
+ aliens
[i
].x
* ALIEN_SPEED
,
1144 PLAYFIELD_X
+ LIVES_X
+ aliens
[i
].x
* ALIEN_SPEED
+ ALIEN_WIDTH
,
1145 ALIEN_START_Y
+ (aliens
[i
].y
+ 1) * ALIEN_HEIGHT
- 1);
1152 rb
->lcd_set_foreground(LCD_WHITE
);
1153 rb
->lcd_vline(fire_x
, fire_y
, fire_y
+ SHOT_HEIGHT
);
1154 rb
->lcd_set_foreground(LCD_BLACK
);
1155 } else if (fire
< S_IDLE
) {
1156 /* Count up towards S_IDLE, then erase explosion */
1158 if (fire
== S_IDLE
) {
1159 /* Erase explosion */
1160 if (fire_target
== TARGET_TOP
) {
1161 rb
->lcd_fillrect(fire_x
- (FIRE_WIDTH
/ 2), fire_y
, FIRE_WIDTH
, FIRE_HEIGHT
);
1162 } else if (fire_target
== TARGET_SHIELD
) {
1163 /* Draw explosion with black pixels */
1164 fire_alpha(fire_x
, fire_y
, LCD_BLACK
);
1171 /* Return a BOMBER alien */
1172 inline int random_bomber(void)
1176 /* TODO: Weigh higher probability near ship */
1177 col
= rb
->rand() % ALIENS
;
1178 for (i
= col
+ 4 * ALIENS
; i
>= 0; i
-= ALIENS
) {
1179 if (aliens
[i
].state
== BOMBER
) {
1184 /* No BOMBER found in this col */
1186 for (i
= 0; i
< 5 * ALIENS
; i
++) {
1187 if (aliens
[i
].state
== BOMBER
) {
1192 /* No BOMBER found at all (error?) */
1198 inline void draw_bomb(int i
)
1200 rb
->lcd_bitmap_part(invadrox_bombs
, bombs
[i
].type
* BOMB_WIDTH
,
1201 bombs
[i
].frame
* BOMB_HEIGHT
,
1202 STRIDE( SCREEN_MAIN
,
1203 BMPWIDTH_invadrox_bombs
,
1204 BMPHEIGHT_invadrox_bombs
),
1205 bombs
[i
].x
, bombs
[i
].y
,
1206 BOMB_WIDTH
, BOMB_HEIGHT
);
1209 if (bombs
[i
].frame
== bombs
[i
].frames
) {
1215 void move_bombs(void)
1220 for (i
= 0; i
< max_bombs
; i
++) {
1222 switch (bombs
[i
].state
) {
1228 bomber
= random_bomber();
1230 DBG("ERROR: No bomber available\n");
1234 bombs
[i
].x
= PLAYFIELD_X
+ LIVES_X
+ aliens
[bomber
].x
* ALIEN_SPEED
+ ALIEN_WIDTH
/ 2;
1235 bombs
[i
].y
= ALIEN_START_Y
+ (aliens
[bomber
].y
+ 1) * ALIEN_HEIGHT
;
1237 /* Check for duplets in x and y direction */
1239 for (j
= i
- 1; j
>= 0; j
--) {
1240 if ((bombs
[j
].state
== S_ACTIVE
) &&
1241 ((bombs
[i
].x
== bombs
[j
].x
) || (bombs
[i
].y
== bombs
[j
].y
))) {
1247 /* Skip this one, continue with next bomb */
1248 /* printf("Bomb %d duplet of %d\n", i, j); */
1252 /* Passed, set type */
1253 bombs
[i
].type
= rb
->rand() % 3;
1255 if (bombs
[i
].type
== 0) {
1256 bombs
[i
].frames
= 3;
1257 } else if (bombs
[i
].type
== 1) {
1258 bombs
[i
].frames
= 4;
1260 bombs
[i
].frames
= 6;
1264 bombs
[i
].state
= S_ACTIVE
;
1271 /* Erase old position */
1272 rb
->lcd_fillrect(bombs
[i
].x
, bombs
[i
].y
, BOMB_WIDTH
, BOMB_HEIGHT
);
1275 bombs
[i
].y
+= BOMB_SPEED
;
1277 /* Check if bottom hit */
1278 if (bombs
[i
].y
+ BOMB_HEIGHT
>= PLAYFIELD_Y
) {
1279 bombs
[i
].y
= PLAYFIELD_Y
- FIRE_HEIGHT
+ 1;
1280 fire_alpha(bombs
[i
].x
, bombs
[i
].y
, LCD_WHITE
);
1281 bombs
[i
].state
= S_EXPLODE
;
1282 bombs
[i
].target
= TARGET_BOTTOM
;
1286 /* Check for green (ship or shield) */
1287 for (j
= BOMB_HEIGHT
; j
>= BOMB_HEIGHT
- BOMB_SPEED
; j
--) {
1288 bombs
[i
].target
= 0;
1289 if(get_pixel(bombs
[i
].x
+ BOMB_WIDTH
/ 2, bombs
[i
].y
+ j
) == screen_green
) {
1290 /* Move to hit pixel */
1291 bombs
[i
].x
+= BOMB_WIDTH
/ 2;
1294 /* Check if ship is hit */
1295 if (bombs
[i
].y
> SHIELD_Y
+ SHIELD_HEIGHT
&& bombs
[i
].y
< PLAYFIELD_Y
) {
1297 /* TODO: play ship hit sound */
1301 ship_frame_counter
= 0;
1302 bombs
[i
].state
= S_EXPLODE
* 4;
1303 bombs
[i
].target
= TARGET_SHIP
;
1304 rb
->lcd_bitmap_part(invadrox_ships
, 0, 1 * SHIP_HEIGHT
,
1305 STRIDE( SCREEN_MAIN
,
1306 BMPWIDTH_invadrox_ships
,
1307 BMPHEIGHT_invadrox_ships
),
1309 SHIP_WIDTH
, SHIP_HEIGHT
);
1313 bombs
[i
].state
= S_EXPLODE
;
1314 bombs
[i
].target
= TARGET_SHIELD
;
1315 /* Center explosion around hit pixel in shield */
1316 bombs
[i
].y
-= FIRE_HEIGHT
/ 2;
1317 fire_alpha(bombs
[i
].x
, bombs
[i
].y
, SLIME_GREEN
);
1322 if (bombs
[i
].target
!= 0) {
1323 /* Hit ship or shield, continue */
1331 /* If we get here state should be < 0, exploding */
1333 if (bombs
[i
].state
== S_IDLE
) {
1335 /* Erase explosion */
1336 rb
->lcd_fillrect(ship_x
, SHIP_Y
, SHIP_WIDTH
, SHIP_HEIGHT
);
1337 rb
->lcd_update_rect(ship_x
, SHIP_Y
, SHIP_WIDTH
, SHIP_HEIGHT
);
1340 ship_x
= PLAYFIELD_X
+ 2 * LIVES_X
;
1347 /* Sleep 1s to give player time to examine lives left */
1350 /* Erase explosion (even if ship hit, might be another bomb) */
1351 fire_alpha(bombs
[i
].x
, bombs
[i
].y
, LCD_BLACK
);
1359 inline void move_ship(void)
1361 ship_dir
+= ship_acc
;
1362 if (ship_dir
> max_ship_speed
) {
1363 ship_dir
= max_ship_speed
;
1365 if (ship_dir
< -max_ship_speed
) {
1366 ship_dir
= -max_ship_speed
;
1369 if (ship_x
< SHIP_MIN_X
) {
1370 ship_x
= SHIP_MIN_X
;
1372 if (ship_x
> SHIP_MAX_X
) {
1373 ship_x
= SHIP_MAX_X
;
1380 /* Unidentified Flying Object */
1383 static int ufo_speed
;
1387 switch (ufo_state
) {
1391 if (rb
->rand() % 500 == 0) {
1392 /* Uh-oh, it's time to launch a mystery UFO */
1394 /* TODO: Play UFO sound */
1396 if (rb
->rand() % 2) {
1397 ufo_speed
= UFO_SPEED
;
1398 ufo_x
= PLAYFIELD_X
;
1400 ufo_speed
= -UFO_SPEED
;
1401 ufo_x
= LCD_WIDTH
- PLAYFIELD_X
- UFO_WIDTH
;
1403 ufo_state
= S_ACTIVE
;
1404 /* UFO will be drawn next frame */
1410 rb
->lcd_fillrect(ufo_x
, UFO_Y
, UFO_WIDTH
, UFO_HEIGHT
);
1414 if (ufo_x
< PLAYFIELD_X
|| ufo_x
> LCD_WIDTH
- PLAYFIELD_X
- UFO_WIDTH
) {
1419 rb
->lcd_bitmap(invadrox_ufo
, ufo_x
, UFO_Y
, UFO_WIDTH
, UFO_HEIGHT
);
1424 if (counter
== S_IDLE
) {
1425 /* Erase mystery number */
1426 rb
->lcd_fillrect(ufo_x
, UFO_Y
, 3 * NUMBERS_WIDTH
+ 2 * NUM_SPACING
, FONT_HEIGHT
);
1434 if (ufo_state
== S_IDLE
) {
1435 /* Erase explosion */
1436 rb
->lcd_fillrect(ufo_x
, UFO_Y
- 1, UFO_EXPLODE_WIDTH
, UFO_EXPLODE_HEIGHT
);
1437 ufo_state
= S_SHOWSCORE
;
1438 counter
= S_EXPLODE
* 4;
1439 /* Draw mystery_score, sleep, increase score and continue */
1440 mystery_score
= 50 + (rb
->rand() % 6) * 50;
1441 if (mystery_score
< 100) {
1442 draw_number(ufo_x
, UFO_Y
, mystery_score
, 2);
1444 draw_number(ufo_x
, UFO_Y
, mystery_score
, 3);
1446 score
+= mystery_score
;
1454 void draw_background(void)
1457 rb
->lcd_bitmap(invadrox_background
, 0, 0, LCD_WIDTH
, LCD_HEIGHT
);
1462 void new_level(void)
1467 /* Give an extra life for each new level */
1468 if (lives
< MAX_LIVES
) {
1475 draw_number(HISCORENUM_X
, SCORENUM_Y
, hiscore
.score
, 4);
1479 level_finished
= false;
1483 /* Init alien positions and states */
1484 for (i
= 0; i
< 4 * ALIENS
; i
++) {
1485 aliens
[i
].x
= 0 + (i
% ALIENS
) * ((ALIEN_WIDTH
+ ALIEN_SPACING
) / ALIEN_SPEED
);
1486 aliens
[i
].y
= 2 * (i
/ ALIENS
);
1487 aliens
[i
].state
= ALIVE
;
1489 /* Last row, bombers */
1490 for (i
= 4 * ALIENS
; i
< 5 * ALIENS
; i
++) {
1491 aliens
[i
].x
= 0 + (i
% ALIENS
) * ((ALIEN_WIDTH
+ ALIEN_SPACING
) / ALIEN_SPEED
);
1492 aliens
[i
].y
= 2 * (i
/ ALIENS
);
1493 aliens
[i
].state
= BOMBER
;
1496 /* Init bombs to inactive (S_IDLE) */
1497 for (i
= 0; i
< MAX_BOMBS
; i
++) {
1498 bombs
[i
].state
= S_IDLE
;
1501 /* Start aliens closer to earth from level 2 */
1502 for (i
= 0; i
< 5 * ALIENS
; i
++) {
1504 aliens
[i
].y
+= level
- 1;
1510 /* Max concurrent bombs */
1519 /* Increase speed */
1528 /* Increase speed more */
1538 for (i
= 1; i
<= 4; i
++) {
1539 rb
->lcd_bitmap(invadrox_shield
,
1540 PLAYFIELD_X
+ i
* PLAYFIELD_WIDTH
/ 5 - SHIELD_WIDTH
/ 2,
1541 SHIELD_Y
, SHIELD_WIDTH
, SHIELD_HEIGHT
);
1545 rb
->lcd_set_foreground(SLIME_GREEN
);
1546 rb
->lcd_hline(PLAYFIELD_X
, LCD_WIDTH
- PLAYFIELD_X
, PLAYFIELD_Y
);
1547 /* Restore foreground to black (for fast erase later). */
1548 rb
->lcd_set_foreground(LCD_BLACK
);
1550 ship_x
= PLAYFIELD_X
+ 2 * LIVES_X
;
1552 old_ship_x
= ship_x
;
1559 /* Start moving the bottom row left to right */
1560 curr_alien
= 4 * ALIENS
;
1561 aliens_paralyzed
= 0;
1562 aliens_right
= true;
1563 aliens_down
= false;
1564 hit_left_border
= false;
1565 hit_right_border
= false;
1566 /* TODO: Change max_ship_speed to 3 at higher levels */
1575 void init_invadrox(void)
1579 /* Seed random number generator with a "random" number */
1580 rb
->srand(rb
->get_time()->tm_sec
+ rb
->get_time()->tm_min
* 60);
1582 /* Precalculate start of each scanline */
1583 for (i
= 0; i
< LCD_HEIGHT
; i
++) {
1584 #if (LCD_DEPTH >= 8)
1585 ytab
[i
] = i
* LCD_WIDTH
;
1586 #elif (LCD_DEPTH == 2) && (LCD_PIXELFORMAT == HORIZONTAL_PACKING)
1587 ytab
[i
] = i
* (LCD_WIDTH
/ 4);
1588 #elif (LCD_DEPTH == 2) && (LCD_PIXELFORMAT == VERTICAL_PACKING)
1589 ytab
[i
] = (i
/ 4) * LCD_WIDTH
;
1591 #error pixelformat not implemented yet
1595 rb
->lcd_set_background(LCD_BLACK
);
1596 rb
->lcd_set_foreground(LCD_BLACK
);
1598 if (highscore_load(HISCOREFILE
, &hiscore
, 1) < 0) {
1599 /* Init hiscore to 0 */
1600 rb
->strlcpy(hiscore
.name
, "Invader", sizeof(hiscore
.name
));
1605 /* Init alien types in aliens array */
1606 for (i
= 0; i
< 1 * ALIENS
; i
++) {
1607 aliens
[i
].type
= 0; /* Kang */
1609 for (; i
< 3 * ALIENS
; i
++) {
1610 aliens
[i
].type
= 1; /* Kodos */
1612 for (; i
< 5 * ALIENS
; i
++) {
1613 aliens
[i
].type
= 2; /* Serak */
1617 /* Save screen white color */
1618 rb
->lcd_set_foreground(LCD_WHITE
);
1619 rb
->lcd_drawpixel(0, 0);
1620 rb
->lcd_update_rect(0, 0, 1, 1);
1621 screen_white
= get_pixel(0, 0);
1623 /* Save screen green color */
1624 rb
->lcd_set_foreground(SLIME_GREEN
);
1625 rb
->lcd_drawpixel(0, 0);
1626 rb
->lcd_update_rect(0, 0, 1, 1);
1627 screen_green
= get_pixel(0, 0);
1629 /* Restore black foreground */
1630 rb
->lcd_set_foreground(LCD_BLACK
);
1634 /* Flash score at start */
1635 for (i
= 0; i
< 5; i
++) {
1636 rb
->lcd_fillrect(SCORENUM_X
, SCORENUM_Y
,
1637 4 * NUMBERS_WIDTH
+ 3 * NUM_SPACING
,
1639 rb
->lcd_update_rect(SCORENUM_X
, SCORENUM_Y
,
1640 4 * NUMBERS_WIDTH
+ 3 * NUM_SPACING
,
1643 draw_number(SCORENUM_X
, SCORENUM_Y
, score
, 4);
1649 inline bool handle_buttons(void)
1651 static unsigned int oldbuttonstate
= 0;
1653 unsigned int released
, pressed
, newbuttonstate
;
1656 /* Don't allow ship movement during explosion */
1659 newbuttonstate
= rb
->button_status();
1661 if(newbuttonstate
== oldbuttonstate
) {
1662 if (newbuttonstate
== 0) {
1663 /* No button pressed. Stop ship. */
1675 released
= ~newbuttonstate
& oldbuttonstate
;
1676 pressed
= newbuttonstate
& ~oldbuttonstate
;
1677 oldbuttonstate
= newbuttonstate
;
1679 if (pressed
& ACTION_LEFT
) {
1680 if (ship_acc
> -1) {
1684 if (pressed
& ACTION_RIGHT
) {
1689 if (pressed
& ACTION_FIRE
) {
1690 if (fire
== S_IDLE
) {
1692 fire_x
= ship_x
+ SHIP_WIDTH
/ 2;
1693 fire_y
= SHIP_Y
- SHOT_HEIGHT
;
1695 /* TODO: play fire sound */
1698 if (pressed
& ACTION_QUIT
) {
1699 rb
->splash(HZ
* 1, "Quit");
1704 if ((released
& ACTION_LEFT
)) {
1709 if ((released
& ACTION_RIGHT
)) {
1710 if (ship_acc
> -1) {
1718 /* Quit if USB is connected */
1719 if (rb
->button_get(false) == SYS_USB_CONNECTED
) {
1727 void game_loop(void)
1731 /* Print dimensions (just for debugging) */
1732 DBG("%03dx%03dx%02d\n", LCD_WIDTH
, LCD_HEIGHT
, LCD_DEPTH
);
1738 /* Convert CYCLETIME (in ms) to HZ */
1739 end
= *rb
->current_tick
+ (CYCLETIME
* HZ
) / 1000;
1741 if (handle_buttons()) {
1749 /* Check if level is finished (marked by move_fire) */
1750 if (level_finished
) {
1751 /* TODO: Play level finished sound */
1758 if (!aliens_paralyzed
&& !ship_hit
) {
1759 for (i
= 0; i
< gamespeed
; i
++) {
1760 if (!move_aliens()) {
1768 /* Move alien bombs */
1774 /* Update "playfield" rect */
1775 rb
->lcd_update_rect(PLAYFIELD_X
, SCORENUM_Y
+ FONT_HEIGHT
,
1777 PLAYFIELD_Y
+ 1 - SCORENUM_Y
- FONT_HEIGHT
);
1779 /* Wait until next frame */
1780 DBG("%ld (%d)\n", end
- *rb
->current_tick
, (CYCLETIME
* HZ
) / 1000);
1781 if (TIME_BEFORE(*rb
->current_tick
, end
)) {
1782 rb
->sleep(end
- *rb
->current_tick
);
1791 /* this is the plugin entry point */
1792 enum plugin_status
plugin_start(UNUSED
const void* parameter
)
1794 rb
->lcd_setfont(FONT_SYSFIXED
);
1795 /* Turn off backlight timeout */
1796 backlight_force_on(); /* backlight control in lib/helper.c */
1798 /* now go ahead and have fun! */
1802 /* TODO: Play game over sound */
1803 rb
->splash(HZ
* 2, "Game Over");
1804 if (score
> hiscore
.score
) {
1805 /* Save new hiscore */
1806 highscore_update(score
, level
, "Invader", &hiscore
, 1);
1807 highscore_save(HISCOREFILE
, &hiscore
, 1);
1810 /* Restore user's original backlight setting */
1811 rb
->lcd_setfont(FONT_UI
);
1812 /* Turn on backlight timeout (revert to settings) */
1813 backlight_use_settings(); /* backlight control in lib/helper.c */
1821 * GNU Emacs settings: Kernighan & Richie coding style with
1822 * 4 spaces indent and no tabs.
1824 * c-file-style: "k&r"
1826 * indent-tabs-mode: nil