1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Originally by Joshua Oreman, improved by Prashant Varanasi
11 * Ported to Rockbox by Ben Basha (Paprica)
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
23 #include "configfile.h"
30 - Make original speed and further increases in speed depend more on screen size
31 - attempt to make the tunnels get narrower as the game goes on
32 - make the chopper look better, maybe a picture, and scale according
34 - use textures for the color screens for background and terrain,
35 eg stars on background
36 - allow choice of different levels [later: different screen themes]
37 - better high score handling, improved screen etc.
40 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
42 #define QUIT BUTTON_OFF
43 #define ACTION BUTTON_UP
44 #define ACTION2 BUTTON_SELECT
45 #define ACTIONTEXT "SELECT"
47 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
48 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
49 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
51 #define QUIT BUTTON_MENU
52 #define ACTION BUTTON_SELECT
53 #define ACTIONTEXT "SELECT"
55 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD /* grayscale at the moment */
57 #define QUIT BUTTON_POWER
58 #define ACTION BUTTON_UP
59 #define ACTION2 BUTTON_SELECT
60 #define ACTIONTEXT "SELECT"
62 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
63 #define QUIT BUTTON_POWER
64 #define ACTION BUTTON_RIGHT
65 #define ACTIONTEXT "RIGHT"
67 #elif CONFIG_KEYPAD == SANSA_E200_PAD
68 #define QUIT BUTTON_POWER
69 #define ACTION BUTTON_SELECT
70 #define ACTIONTEXT "SELECT"
72 #elif CONFIG_KEYPAD == GIGABEAT_PAD
73 #define QUIT BUTTON_MENU
74 #define ACTION BUTTON_SELECT
75 #define ACTIONTEXT "SELECT"
77 #elif CONFIG_KEYPAD == RECORDER_PAD
78 #define QUIT BUTTON_OFF
79 #define ACTION BUTTON_PLAY
80 #define ACTIONTEXT "PLAY"
82 #elif CONFIG_KEYPAD == ONDIO_PAD
83 #define QUIT BUTTON_OFF
84 #define ACTION BUTTON_UP
85 #define ACTION2 BUTTON_MENU
86 #define ACTIONTEXT "UP"
89 #error Unsupported keypad
92 static struct plugin_api
* rb
;
94 #define NUMBER_OF_BLOCKS 8
95 #define NUMBER_OF_PARTICLES 3
96 #define MAX_TERRAIN_NODES 15
98 #define LEVEL_MODE_NORMAL 0
99 #define LEVEL_MODE_STEEP 1
102 #define CYCLETIME 100
103 #define SCALE(x) ((x)==1 ? (x) : ((x) >> 1))
111 /*Chopper's local variables to track the terrain position etc*/
112 static int chopCounter
;
113 static int iRotorOffset
;
116 static int iPlayerPosX
;
117 static int iPlayerPosY
;
118 static int iCameraPosX
;
119 static int iPlayerSpeedX
;
120 static int iPlayerSpeedY
;
121 static int iLastBlockPlacedPosX
;
122 static int iGravityTimerCountdown
;
123 static int iPlayerAlive
;
124 static int iLevelMode
;
125 static int blockh
,blockw
;
126 static int highscore
;
129 #define CFG_FILE "chopper.cfg"
130 #define MAX_POINTS 50000
131 static struct configdata config
[] =
133 {TYPE_INT
, 0, MAX_POINTS
, &highscore
, "highscore", NULL
, NULL
}
166 struct CTerrainNode mNodes
[MAX_TERRAIN_NODES
];
168 int iLastNodePlacedPosX
;
171 struct CBlock mBlocks
[NUMBER_OF_BLOCKS
];
172 struct CParticle mParticles
[NUMBER_OF_PARTICLES
];
174 struct CTerrain mGround
;
175 struct CTerrain mRoof
;
177 /*Function declarations*/
178 static void chopDrawParticle(struct CParticle
*mParticle
);
179 static void chopDrawBlock(struct CBlock
*mBlock
);
180 static void chopRenderTerrain(struct CTerrain
*ter
);
181 void chopper_load(bool newgame
);
182 void cleanup_chopper(void);
184 static void chopDrawPlayer(int x
,int y
) /* These are SCREEN coords, not world!*/
188 rb
->lcd_set_foreground(LCD_RGBPACK(50,50,200));
190 rb
->lcd_set_foreground(LCD_DARKGRAY
);
192 rb
->lcd_fillrect(SCALE(x
+6), SCALE(y
+2), SCALE(12), SCALE(9));
193 rb
->lcd_fillrect(SCALE(x
-3), SCALE(y
+6), SCALE(20), SCALE(3));
196 rb
->lcd_set_foreground(LCD_RGBPACK(50,50,50));
198 rb
->lcd_set_foreground(LCD_DARKGRAY
);
200 rb
->lcd_fillrect(SCALE(x
+10), SCALE(y
), SCALE(2), SCALE(3));
201 rb
->lcd_fillrect(SCALE(x
+10), SCALE(y
), SCALE(1), SCALE(3));
204 rb
->lcd_set_foreground(LCD_RGBPACK(40,40,100));
206 rb
->lcd_set_foreground(LCD_BLACK
);
208 rb
->lcd_drawline(SCALE(x
), SCALE(y
+iRotorOffset
), SCALE(x
+20),
209 SCALE(y
-iRotorOffset
));
212 rb
->lcd_set_foreground(LCD_RGBPACK(20,20,50));
214 rb
->lcd_set_foreground(LCD_BLACK
);
216 rb
->lcd_fillrect(SCALE(x
- 2), SCALE(y
+ 5), SCALE(2), SCALE(5));
220 static void chopClearTerrain(struct CTerrain
*ter
)
222 ter
->iNodesCount
= 0;
226 int iR(int low
,int high
)
228 return low
+rb
->rand()%(high
-low
+1);
231 static void chopCopyTerrain(struct CTerrain
*src
, struct CTerrain
*dest
,
232 int xOffset
,int yOffset
)
236 while(i
< src
->iNodesCount
)
238 dest
->mNodes
[i
].x
= src
->mNodes
[i
].x
+ xOffset
;
239 dest
->mNodes
[i
].y
= src
->mNodes
[i
].y
+ yOffset
;
244 dest
->iNodesCount
= src
->iNodesCount
;
245 dest
->iLastNodePlacedPosX
= src
->iLastNodePlacedPosX
;
249 static void chopAddTerrainNode(struct CTerrain
*ter
, int x
, int y
)
253 if(ter
->iNodesCount
+ 1 >= MAX_TERRAIN_NODES
)
255 /* DEBUGF("ERROR: Not enough nodes!\n"); */
261 i
= ter
->iNodesCount
- 1;
263 ter
->mNodes
[i
].x
= x
;
266 ter
->iLastNodePlacedPosX
= x
;
270 static void chopTerrainNodeDeleteAndShift(struct CTerrain
*ter
,int nodeIndex
)
274 while( i
< ter
->iNodesCount
)
276 ter
->mNodes
[i
- 1] = ter
->mNodes
[i
];
285 int chopUpdateTerrainRecycling(struct CTerrain
*ter
)
290 while(i
< ter
->iNodesCount
)
293 if( iCameraPosX
> ter
->mNodes
[i
].x
)
296 chopTerrainNodeDeleteAndShift(ter
,i
);
298 iNewNodePos
= ter
->iLastNodePlacedPosX
+ 50;
304 if(iLevelMode
== LEVEL_MODE_STEEP
)
307 chopAddTerrainNode(ter
,iNewNodePos
,g
- iR(-v
,v
));
319 int chopTerrainHeightAtPoint(struct CTerrain
*ter
, int pX
)
322 int iNodeIndexOne
=0,iNodeIndexTwo
=0, h
, terY1
, terY2
, terX1
, terX2
, a
, b
;
326 for(i
=1;i
<MAX_TERRAIN_NODES
;i
++)
328 if(ter
->mNodes
[i
].x
> pX
)
330 iNodeIndexOne
= i
- 1;
336 iNodeIndexTwo
= iNodeIndexOne
+ 1;
337 terY1
= ter
->mNodes
[iNodeIndexOne
].y
;
338 terY2
= ter
->mNodes
[iNodeIndexTwo
].y
;
341 terX2
= ter
->mNodes
[iNodeIndexTwo
].x
- ter
->mNodes
[iNodeIndexOne
].x
;
343 pX
-= ter
->mNodes
[iNodeIndexOne
].x
;
356 int chopPointInTerrain(struct CTerrain
*ter
, int pX
, int pY
, int iTestType
)
358 int h
= chopTerrainHeightAtPoint(ter
, pX
);
366 static void chopAddBlock(int x
,int y
,int sx
,int sy
, int indexOverride
)
370 if(indexOverride
< 0)
372 while(mBlocks
[i
].bIsActive
&& i
< NUMBER_OF_BLOCKS
)
374 if(i
==NUMBER_OF_BLOCKS
)
376 DEBUGF("No blocks!\n");
383 mBlocks
[i
].bIsActive
= 1;
384 mBlocks
[i
].iWorldX
= x
;
385 mBlocks
[i
].iWorldY
= y
;
386 mBlocks
[i
].iSizeX
= sx
;
387 mBlocks
[i
].iSizeY
= sy
;
389 iLastBlockPlacedPosX
= x
;
392 static void chopAddParticle(int x
,int y
,int sx
,int sy
)
396 while(mParticles
[i
].bIsActive
&& i
< NUMBER_OF_PARTICLES
)
399 if(i
==NUMBER_OF_PARTICLES
)
402 mParticles
[i
].bIsActive
= 1;
403 mParticles
[i
].iWorldX
= x
;
404 mParticles
[i
].iWorldY
= y
;
405 mParticles
[i
].iSpeedX
= sx
;
406 mParticles
[i
].iSpeedY
= sy
;
409 static void chopGenerateBlockIfNeeded(void)
412 int DistSpeedX
= iPlayerSpeedX
* 5;
413 if(DistSpeedX
<200) DistSpeedX
= 200;
415 while(i
< NUMBER_OF_BLOCKS
)
417 if(!mBlocks
[i
].bIsActive
)
421 iX
= iLastBlockPlacedPosX
+ (350-DistSpeedX
);
425 sY
= blockh
+ iR(1,blockh
/3);
427 chopAddBlock(iX
,iY
,sX
,sY
,i
);
435 static int chopBlockCollideWithPlayer(struct CBlock
*mBlock
)
437 int px
= iPlayerPosX
;
438 int py
= iPlayerPosY
;
440 int x
= mBlock
->iWorldX
-17;
441 int y
= mBlock
->iWorldY
-11;
443 int x2
= x
+ mBlock
->iSizeX
+17;
444 int y2
= y
+ mBlock
->iSizeY
+11;
457 static int chopBlockOffscreen(struct CBlock
*mBlock
)
459 if(mBlock
->iWorldX
+ mBlock
->iSizeX
< iCameraPosX
)
465 static int chopParticleOffscreen(struct CParticle
*mParticle
)
467 if (mParticle
->iWorldX
< iCameraPosX
|| mParticle
->iWorldY
< 0 ||
468 mParticle
->iWorldY
> iScreenY
|| mParticle
->iWorldX
> iCameraPosX
+
477 static void chopKillPlayer(void)
481 for (i
= 0; i
< NUMBER_OF_PARTICLES
; i
++) {
482 mParticles
[i
].bIsActive
= 0;
483 chopAddParticle(iPlayerPosX
+ iR(0,20), iPlayerPosY
+ iR(0,20),
489 if (iPlayerAlive
== 0) {
490 rb
->lcd_set_drawmode(DRMODE_FG
);
492 rb
->lcd_set_foreground(LCD_LIGHTGRAY
);
494 rb
->splash(HZ
, "Game Over");
496 if (score
> highscore
) {
499 rb
->snprintf(scoretext
, sizeof(scoretext
), "New High Score: %d",
501 rb
->splash(HZ
*2, scoretext
);
504 rb
->splash(HZ
/4, "Press " ACTIONTEXT
" to continue");
507 rb
->lcd_set_drawmode(DRMODE_SOLID
);
510 button
= rb
->button_get(true);
517 button
= rb
->button_get(true);
518 if (button
== (ACTION
| BUTTON_REL
)
520 || button
== (ACTION2
| BUTTON_REL
)
535 static void chopDrawTheWorld(void)
539 while(i
< NUMBER_OF_BLOCKS
)
541 if(mBlocks
[i
].bIsActive
)
543 if(chopBlockOffscreen(&mBlocks
[i
]) == 1)
544 mBlocks
[i
].bIsActive
= 0;
546 chopDrawBlock(&mBlocks
[i
]);
554 while(i
< NUMBER_OF_PARTICLES
)
556 if(mParticles
[i
].bIsActive
)
558 if(chopParticleOffscreen(&mParticles
[i
]) == 1)
559 mParticles
[i
].bIsActive
= 0;
561 chopDrawParticle(&mParticles
[i
]);
567 chopRenderTerrain(&mGround
);
568 chopRenderTerrain(&mRoof
);
572 static void chopDrawParticle(struct CParticle
*mParticle
)
575 int iPosX
= (mParticle
->iWorldX
- iCameraPosX
);
576 int iPosY
= (mParticle
->iWorldY
);
578 rb
->lcd_set_foreground(LCD_RGBPACK(192,192,192));
580 rb
->lcd_set_foreground(LCD_LIGHTGRAY
);
582 rb
->lcd_fillrect(SCALE(iPosX
), SCALE(iPosY
), SCALE(3), SCALE(3));
586 static void chopDrawScene(void)
591 rb
->lcd_set_background(LCD_BLACK
);
593 rb
->lcd_set_background(LCD_WHITE
);
595 rb
->lcd_clear_display();
598 chopDrawPlayer(iPlayerPosX
- iCameraPosX
, iPlayerPosY
);
600 score
= -20 + iPlayerPosX
/3;
603 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
605 rb
->lcd_set_drawmode(DRMODE_FG
);
609 rb
->lcd_set_foreground(LCD_BLACK
);
611 rb
->lcd_set_foreground(LCD_WHITE
);
615 rb
->snprintf(s
, sizeof(s
), "Dist: %d", score
);
617 rb
->snprintf(s
, sizeof(s
), "Distance: %d", score
);
619 rb
->lcd_getstringsize(s
, &w
, NULL
);
620 rb
->lcd_putsxy(2, 2, s
);
621 if (score
< highscore
)
625 rb
->snprintf(s
, sizeof(s
), "Hi: %d", highscore
);
627 rb
->snprintf(s
, sizeof(s
), "Best: %d", highscore
);
629 rb
->lcd_getstringsize(s
, &w2
, NULL
);
630 if (LCD_WIDTH
- 2 - w2
> w
+ 2)
631 rb
->lcd_putsxy(LCD_WIDTH
- 2 - w2
, 2, s
);
633 rb
->lcd_set_drawmode(DRMODE_SOLID
);
638 static int chopMenu(int menunum
)
640 int result
= (menunum
==0)?0:1;
642 bool menu_quit
= false;
644 static const struct opt_items levels
[2] = {
649 MENUITEM_STRINGLIST(menu
,"Chopper Menu",NULL
,"Start New Game","Resume Game",
652 #ifdef HAVE_LCD_COLOR
653 rb
->lcd_set_foreground(LCD_WHITE
);
654 rb
->lcd_set_background(LCD_BLACK
);
656 rb
->lcd_set_foreground(LCD_BLACK
);
657 rb
->lcd_set_background(LCD_WHITE
);
660 rb
->lcd_clear_display();
663 switch(rb
->do_menu(&menu
, &result
))
665 case 0: /* Start New Game */
670 case 1: /* Resume Game */
674 } else if(menunum
==0){
675 rb
->splash(HZ
, "No game to resume");
679 rb
->set_option("Level", &iLevelMode
, INT
, levels
, 2, NULL
);
685 case MENU_ATTACHED_USB
:
687 res
= PLUGIN_USB_CONNECTED
;
691 rb
->lcd_clear_display();
695 static int chopGameLoop(void)
697 int move_button
, ret
;
699 int end
, i
=0, bdelay
=0, last_button
=BUTTON_NONE
;
701 if (chopUpdateTerrainRecycling(&mGround
) == 1)
702 /* mirror the sky if we've changed the ground */
703 chopCopyTerrain(&mGround
, &mRoof
, 0, - ( (iScreenY
* 3) / 4));
713 end
= *rb
->current_tick
+ (CYCLETIME
* HZ
) / 1000;
715 if(chopUpdateTerrainRecycling(&mGround
) == 1)
716 /* mirror the sky if we've changed the ground */
717 chopCopyTerrain(&mGround
, &mRoof
, 0, - ( (iScreenY
* 3) / 4));
719 iRotorOffset
= iR(-1,1);
721 /* We need to have this here so particles move when we're dead */
723 for (i
=0; i
< NUMBER_OF_PARTICLES
; i
++)
724 if(mParticles
[i
].bIsActive
== 1)
726 mParticles
[i
].iWorldX
+= mParticles
[i
].iSpeedX
;
727 mParticles
[i
].iWorldY
+= mParticles
[i
].iSpeedY
;
730 /* Redraw the main window: */
734 iGravityTimerCountdown
--;
736 if(iGravityTimerCountdown
<= 0)
738 iGravityTimerCountdown
= 3;
739 chopAddParticle(iPlayerPosX
, iPlayerPosY
+5, 0, 0);
742 if(iLevelMode
== LEVEL_MODE_NORMAL
)
743 chopGenerateBlockIfNeeded();
746 move_button
=rb
->button_status();
747 if (rb
->button_get(false) == QUIT
) {
752 last_button
= BUTTON_NONE
;
753 move_button
= BUTTON_NONE
;
756 switch (move_button
) {
761 if (last_button
!= ACTION
763 && last_button
!= ACTION2
772 if (last_button
== ACTION
774 || last_button
== ACTION2
781 if (rb
->default_event_handler(move_button
) == SYS_USB_CONNECTED
)
782 return PLUGIN_USB_CONNECTED
;
785 last_button
= move_button
;
788 iPlayerSpeedY
= bdelay
;
790 } else if (bdelay
> 0) {
791 iPlayerSpeedY
= bdelay
;
795 iCameraPosX
= iPlayerPosX
- 25;
796 iPlayerPosX
+= iPlayerSpeedX
;
797 iPlayerPosY
+= iPlayerSpeedY
;
800 /* increase speed as we go along */
801 if (chopCounter
== 100){
806 if (iPlayerPosY
> iScreenY
-10 || iPlayerPosY
< -5 ||
807 chopPointInTerrain(&mGround
, iPlayerPosX
, iPlayerPosY
+ 10, 0) ||
808 chopPointInTerrain(&mRoof
, iPlayerPosX
,iPlayerPosY
, 1))
817 for (i
=0; i
< NUMBER_OF_BLOCKS
; i
++)
818 if(mBlocks
[i
].bIsActive
== 1)
819 if(chopBlockCollideWithPlayer(&mBlocks
[i
])) {
827 if (end
> *rb
->current_tick
)
828 rb
->sleep(end
-*rb
->current_tick
);
836 static void chopDrawBlock(struct CBlock
*mBlock
)
838 int iPosX
= (mBlock
->iWorldX
- iCameraPosX
);
839 int iPosY
= (mBlock
->iWorldY
);
841 rb
->lcd_set_foreground(LCD_RGBPACK(100,255,100));
843 rb
->lcd_set_foreground(LCD_BLACK
);
845 rb
->lcd_fillrect(SCALE(iPosX
), SCALE(iPosY
), SCALE(mBlock
->iSizeX
),
846 SCALE(mBlock
->iSizeY
));
850 static void chopRenderTerrain(struct CTerrain
*ter
)
858 if(ter
->mNodes
[0].y
< (LCD_HEIGHT
*SIZE
)/2)
861 ay
=(LCD_HEIGHT
*SIZE
);
863 while(i
< ter
->iNodesCount
&& oldx
< iScreenX
)
866 int x
= ter
->mNodes
[i
-1].x
- iCameraPosX
;
867 int y
= ter
->mNodes
[i
-1].y
;
869 int x2
= ter
->mNodes
[i
].x
- iCameraPosX
;
870 int y2
= ter
->mNodes
[i
].y
;
872 rb
->lcd_set_foreground(LCD_RGBPACK(100,255,100));
874 rb
->lcd_set_foreground(LCD_DARKGRAY
);
877 rb
->lcd_drawline(SCALE(x
), SCALE(y
), SCALE(x2
), SCALE(y2
));
879 xlcd_filltriangle(SCALE(x
), SCALE(y
), SCALE(x2
), SCALE(y2
),
880 SCALE(x2
), SCALE(ay
));
881 xlcd_filltriangle(SCALE(x
), SCALE(ay
), SCALE(x2
), SCALE(y2
),
882 SCALE(x2
), SCALE(ay
));
885 xlcd_filltriangle(SCALE(x
), SCALE(ay
), SCALE(x
), SCALE(y
),
886 SCALE(x2
), SCALE(y2
/ 2));
888 xlcd_filltriangle(SCALE(x
), SCALE(ay
), SCALE(x
), SCALE(y
),
889 SCALE(x2
), SCALE((LCD_HEIGHT
*SIZE
) -
890 ((LCD_HEIGHT
*SIZE
) - y2
) / 2));
899 void chopper_load(bool newgame
)
906 iScreenX
= LCD_WIDTH
* SIZE
;
907 iScreenY
= LCD_HEIGHT
* SIZE
;
908 blockh
= iScreenY
/ 5;
909 blockw
= iScreenX
/ 20;
915 iPlayerPosY
= (iScreenY
* 4) / 10;
916 iLastBlockPlacedPosX
= 0;
917 iGravityTimerCountdown
= 2;
923 for (i
=0; i
< NUMBER_OF_PARTICLES
; i
++)
924 mParticles
[i
].bIsActive
= 0;
926 for (i
=0; i
< NUMBER_OF_BLOCKS
; i
++)
927 mBlocks
[i
].bIsActive
= 0;
930 chopClearTerrain(&mGround
);
932 for (i
=0; i
< MAX_TERRAIN_NODES
; i
++)
933 chopAddTerrainNode(&mGround
,i
* 30,g
- iR(0,20));
935 if (chopUpdateTerrainRecycling(&mGround
) == 1)
936 /* mirror the sky if we've changed the ground */
937 chopCopyTerrain(&mGround
, &mRoof
, 0, - ( (iScreenY
* 3) / 4));
939 iLevelMode
= LEVEL_MODE_NORMAL
;
940 if (iLevelMode
== LEVEL_MODE_NORMAL
)
941 /* make it a bit more exciting, cause it's easy terrain... */
945 /* this is the plugin entry point */
946 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
952 rb
->lcd_setfont(FONT_SYSFIXED
);
954 rb
->lcd_set_backdrop(NULL
);
956 #ifdef HAVE_LCD_COLOR
957 rb
->lcd_set_background(LCD_BLACK
);
958 rb
->lcd_set_foreground(LCD_WHITE
);
961 /* Turn off backlight timeout */
962 backlight_force_on(rb
); /* backlight control in lib/helper.c */
964 rb
->srand( *rb
->current_tick
);
968 configfile_load(CFG_FILE
, config
, 1, 0);
971 ret
= chopGameLoop();
973 configfile_save(CFG_FILE
, config
, 1, 0);
975 rb
->lcd_setfont(FONT_UI
);
976 /* Turn on backlight timeout (revert to settings) */
977 backlight_use_settings(rb
); /* backlight control in lib/helper.c */