Fuze: Change pause/exit button in chopper to short home as it's nearly impossible...
[kugel-rb.git] / apps / plugins / chopper.c
blob66ce3092754c9aab05a70b9022d13cac06e568e7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Originally by Joshua Oreman, improved by Prashant Varanasi
11 * Ported to Rockbox by Ben Basha (Paprica)
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "plugin.h"
24 #include "lib/xlcd.h"
25 #include "lib/configfile.h"
26 #include "lib/helper.h"
27 #include "lib/playback_control.h"
29 PLUGIN_HEADER
32 Still To do:
33 - Make original speed and further increases in speed depend more on screen size
34 - attempt to make the tunnels get narrower as the game goes on
35 - make the chopper look better, maybe a picture, and scale according
36 to screen size
37 - use textures for the color screens for background and terrain,
38 eg stars on background
39 - allow choice of different levels [later: different screen themes]
40 - better high score handling, improved screen etc.
43 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
45 #define QUIT BUTTON_OFF
46 #define ACTION BUTTON_UP
47 #define ACTION2 BUTTON_SELECT
48 #define ACTIONTEXT "SELECT"
50 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
52 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
54 #define QUIT BUTTON_MENU
55 #define ACTION BUTTON_SELECT
56 #define ACTIONTEXT "SELECT"
58 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD /* grayscale at the moment */
60 #define QUIT BUTTON_POWER
61 #define ACTION BUTTON_UP
62 #define ACTION2 BUTTON_SELECT
63 #define ACTIONTEXT "SELECT"
65 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
66 #define QUIT BUTTON_POWER
67 #define ACTION BUTTON_RIGHT
68 #define ACTIONTEXT "RIGHT"
70 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
71 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
72 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
73 (CONFIG_KEYPAD == SANSA_M200_PAD)
74 #define QUIT BUTTON_POWER
75 #define ACTION BUTTON_SELECT
76 #define ACTIONTEXT "SELECT"
78 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
79 #define QUIT BUTTON_HOME
80 #define ACTION BUTTON_SELECT
81 #define ACTIONTEXT "SELECT"
83 #elif CONFIG_KEYPAD == GIGABEAT_PAD
84 #define QUIT BUTTON_MENU
85 #define ACTION BUTTON_SELECT
86 #define ACTIONTEXT "SELECT"
88 #elif CONFIG_KEYPAD == RECORDER_PAD
89 #define QUIT BUTTON_OFF
90 #define ACTION BUTTON_PLAY
91 #define ACTIONTEXT "PLAY"
93 #elif CONFIG_KEYPAD == ONDIO_PAD
94 #define QUIT BUTTON_OFF
95 #define ACTION BUTTON_UP
96 #define ACTION2 BUTTON_MENU
97 #define ACTIONTEXT "UP"
99 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
100 #define QUIT BUTTON_BACK
101 #define ACTION BUTTON_SELECT
102 #define ACTION2 BUTTON_MENU
103 #define ACTIONTEXT "SELECT"
105 #elif CONFIG_KEYPAD == MROBE100_PAD
106 #define QUIT BUTTON_POWER
107 #define ACTION BUTTON_SELECT
108 #define ACTIONTEXT "SELECT"
110 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
111 #define QUIT BUTTON_RC_REC
112 #define ACTION BUTTON_RC_PLAY
113 #define ACTION2 BUTTON_RC_MODE
114 #define ACTIONTEXT "PLAY"
116 #elif CONFIG_KEYPAD == COWON_D2_PAD
117 #define QUIT BUTTON_POWER
119 #elif CONFIG_KEYPAD == IAUDIO67_PAD
120 #define QUIT BUTTON_POWER
121 #define ACTION BUTTON_PLAY
122 #define ACTION2 BUTTON_STOP
123 #define ACTIONTEXT "PLAY"
125 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
126 #define QUIT BUTTON_BACK
127 #define ACTION BUTTON_UP
128 #define ACTION2 BUTTON_MENU
129 #define ACTIONTEXT "UP"
131 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
132 #define QUIT BUTTON_POWER
133 #define ACTION BUTTON_MENU
134 #define ACTION2 BUTTON_SELECT
135 #define ACTIONTEXT "MENU"
137 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
138 #define QUIT BUTTON_POWER
139 #define ACTION BUTTON_MENU
140 #define ACTION2 BUTTON_PLAY
141 #define ACTIONTEXT "MENU"
143 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
144 CONFIG_KEYPAD == ONDAVX777_PAD || \
145 CONFIG_KEYPAD == MROBE500_PAD
146 #define QUIT BUTTON_POWER
148 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
149 #define QUIT BUTTON_LEFT
150 #define ACTION BUTTON_RIGHT
151 #define ACTIONTEXT "RIGHT"
153 #else
154 #error No keymap defined!
155 #endif
157 #ifdef HAVE_TOUCHSCREEN
158 #ifndef QUIT
159 #define QUIT BUTTON_TOPLEFT
160 #endif
161 #ifndef ACTION
162 #define ACTION BUTTON_BOTTOMLEFT
163 #endif
164 #ifndef ACTION2
165 #define ACTION2 BUTTON_BOTTOMRIGHT
166 #endif
167 #ifndef ACTIONTEXT
168 #define ACTIONTEXT "BOTTOMRIGHT"
169 #endif
170 #endif
172 #define NUMBER_OF_BLOCKS 8
173 #define NUMBER_OF_PARTICLES 3
174 #define MAX_TERRAIN_NODES 15
176 #define LEVEL_MODE_NORMAL 0
177 #define LEVEL_MODE_STEEP 1
179 #if LCD_HEIGHT <= 64
180 #define CYCLES 100
181 static inline int SCALE(int x)
183 return x == 1 ? x : x >> 1;
185 #define SIZE 2
186 #else
187 #define CYCLES 60
188 #define SCALE(x) (x)
189 #define SIZE 1
190 #endif
192 /* in 10 milisecond (ticks) */
193 #define CYCLETIME ((CYCLES*HZ)/1000)
195 /*Chopper's local variables to track the terrain position etc*/
196 static int chopCounter;
197 static int iRotorOffset;
198 static int iScreenX;
199 static int iScreenY;
200 static int iPlayerPosX;
201 static int iPlayerPosY;
202 static int iCameraPosX;
203 static int iPlayerSpeedX;
204 static int iPlayerSpeedY;
205 static int iLastBlockPlacedPosX;
206 static int iGravityTimerCountdown;
207 static int iPlayerAlive;
208 static int iLevelMode;
209 static int blockh,blockw;
210 static int highscore;
211 static int score;
213 #define CFG_FILE "chopper.cfg"
214 #define MAX_POINTS 50000
215 static struct configdata config[] =
217 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
220 struct CBlock
222 int iWorldX;
223 int iWorldY;
225 int iSizeX;
226 int iSizeY;
228 int bIsActive;
231 struct CParticle
233 int iWorldX;
234 int iWorldY;
236 int iSpeedX;
237 int iSpeedY;
239 int bIsActive;
242 struct CTerrainNode
244 int x;
245 int y;
248 struct CTerrain
250 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
251 int iNodesCount;
252 int iLastNodePlacedPosX;
255 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
256 struct CParticle mParticles[NUMBER_OF_PARTICLES];
258 struct CTerrain mGround;
259 struct CTerrain mRoof;
261 /*Function declarations*/
262 static void chopDrawParticle(struct CParticle *mParticle);
263 static void chopDrawBlock(struct CBlock *mBlock);
264 static void chopRenderTerrain(struct CTerrain *ter);
265 void chopper_load(bool newgame);
266 void cleanup_chopper(void);
268 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
271 #if LCD_DEPTH > 2
272 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
273 #elif LCD_DEPTH == 2
274 rb->lcd_set_foreground(LCD_DARKGRAY);
275 #endif
276 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
277 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
279 #if LCD_DEPTH > 2
280 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
281 #elif LCD_DEPTH == 2
282 rb->lcd_set_foreground(LCD_DARKGRAY);
283 #endif
284 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
285 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
287 #if LCD_DEPTH > 2
288 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
289 #elif LCD_DEPTH == 2
290 rb->lcd_set_foreground(LCD_BLACK);
291 #endif
292 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
293 SCALE(y-iRotorOffset));
295 #if LCD_DEPTH > 2
296 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
297 #elif LCD_DEPTH == 2
298 rb->lcd_set_foreground(LCD_BLACK);
299 #endif
300 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
304 static void chopClearTerrain(struct CTerrain *ter)
306 ter->iNodesCount = 0;
310 int iR(int low,int high)
312 return low+rb->rand()%(high-low+1);
315 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
316 int xOffset,int yOffset)
318 int i=0;
320 while(i < src->iNodesCount)
322 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
323 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
325 i++;
328 dest->iNodesCount = src->iNodesCount;
329 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
333 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
335 int i=0;
337 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
339 /* DEBUGF("ERROR: Not enough nodes!\n"); */
340 return;
343 ter->iNodesCount++;
345 i = ter->iNodesCount - 1;
347 ter->mNodes[i].x = x;
348 ter->mNodes[i].y= y;
350 ter->iLastNodePlacedPosX = x;
354 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
356 int i=nodeIndex;
358 while( i < ter->iNodesCount )
360 ter->mNodes[i - 1] = ter->mNodes[i];
361 i++;
364 ter->iNodesCount--;
369 int chopUpdateTerrainRecycling(struct CTerrain *ter)
371 int i=1;
372 int ret = 0;
373 int iNewNodePos,g,v;
374 while(i < ter->iNodesCount)
377 if( iCameraPosX > ter->mNodes[i].x)
380 chopTerrainNodeDeleteAndShift(ter,i);
382 iNewNodePos = ter->iLastNodePlacedPosX + 50;
383 g = iScreenY - 10;
385 v = 3*iPlayerSpeedX;
386 if(v>50)
387 v=50;
388 if(iLevelMode == LEVEL_MODE_STEEP)
389 v*=5;
391 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
392 ret=1;
396 i++;
400 return 1;
403 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
406 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
407 float c,d;
409 int i=0;
410 for(i=1;i<MAX_TERRAIN_NODES;i++)
412 if(ter->mNodes[i].x > pX)
414 iNodeIndexOne = i - 1;
415 break;
420 iNodeIndexTwo = iNodeIndexOne + 1;
421 terY1 = ter->mNodes[iNodeIndexOne].y;
422 terY2 = ter->mNodes[iNodeIndexTwo].y;
424 terX1 = 0;
425 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
427 pX-= ter->mNodes[iNodeIndexOne].x;
429 a = terY2 - terY1;
430 b = terX2;
431 c = pX;
432 d = (c/b) * a;
434 h = d + terY1;
436 return h;
440 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
442 int h = chopTerrainHeightAtPoint(ter, pX);
444 if(iTestType == 0)
445 return (pY > h);
446 else
447 return (pY < h);
450 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
452 int i=0;
454 if(indexOverride < 0)
456 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
457 i++;
458 if(i==NUMBER_OF_BLOCKS)
460 DEBUGF("No blocks!\n");
461 return;
464 else
465 i = indexOverride;
467 mBlocks[i].bIsActive = 1;
468 mBlocks[i].iWorldX = x;
469 mBlocks[i].iWorldY = y;
470 mBlocks[i].iSizeX = sx;
471 mBlocks[i].iSizeY = sy;
473 iLastBlockPlacedPosX = x;
476 static void chopAddParticle(int x,int y,int sx,int sy)
478 int i=0;
480 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
481 i++;
483 if(i==NUMBER_OF_PARTICLES)
484 return;
486 mParticles[i].bIsActive = 1;
487 mParticles[i].iWorldX = x;
488 mParticles[i].iWorldY = y;
489 mParticles[i].iSpeedX = sx;
490 mParticles[i].iSpeedY = sy;
493 static void chopGenerateBlockIfNeeded(void)
495 int i=0;
496 int DistSpeedX = iPlayerSpeedX * 5;
497 if(DistSpeedX<200) DistSpeedX = 200;
499 while(i < NUMBER_OF_BLOCKS)
501 if(!mBlocks[i].bIsActive)
503 int iX,iY,sX,sY;
505 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
506 sX = blockw;
508 iY = iR(0,iScreenY);
509 sY = blockh + iR(1,blockh/3);
511 chopAddBlock(iX,iY,sX,sY,i);
514 i++;
519 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
521 int px = iPlayerPosX;
522 int py = iPlayerPosY;
524 int x = mBlock->iWorldX-17;
525 int y = mBlock->iWorldY-11;
527 int x2 = x + mBlock->iSizeX+17;
528 int y2 = y + mBlock->iSizeY+11;
530 if(px>x && px<x2)
532 if(py>y && py<y2)
534 return 1;
538 return 0;
541 static int chopBlockOffscreen(struct CBlock *mBlock)
543 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
544 return 1;
545 else
546 return 0;
549 static int chopParticleOffscreen(struct CParticle *mParticle)
551 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
552 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
553 iScreenX)
555 return 1;
557 else
558 return 0;
561 static void chopKillPlayer(void)
563 int i, button;
565 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
566 mParticles[i].bIsActive = 0;
567 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
568 iR(-2,2), iR(-2,2));
571 iPlayerAlive--;
573 if (iPlayerAlive == 0) {
574 rb->lcd_set_drawmode(DRMODE_FG);
575 #if LCD_DEPTH >= 2
576 rb->lcd_set_foreground(LCD_LIGHTGRAY);
577 #endif
578 rb->splash(HZ, "Game Over");
580 if (score > highscore) {
581 char scoretext[30];
582 highscore = score;
583 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
584 highscore);
585 rb->splash(HZ*2, scoretext);
588 rb->splash(HZ/4, "Press " ACTIONTEXT " to continue");
589 rb->lcd_update();
591 rb->lcd_set_drawmode(DRMODE_SOLID);
593 while (true) {
594 button = rb->button_get(true);
595 if (button == ACTION
596 #ifdef ACTION2
597 || button == ACTION2
598 #endif
600 while (true) {
601 button = rb->button_get(true);
602 if (button == (ACTION | BUTTON_REL)
603 #ifdef ACTION2
604 || button == (ACTION2 | BUTTON_REL)
605 #endif
607 chopper_load(true);
608 return;
614 } else
615 chopper_load(false);
619 static void chopDrawTheWorld(void)
621 int i=0;
623 while(i < NUMBER_OF_BLOCKS)
625 if(mBlocks[i].bIsActive)
627 if(chopBlockOffscreen(&mBlocks[i]) == 1)
628 mBlocks[i].bIsActive = 0;
629 else
630 chopDrawBlock(&mBlocks[i]);
633 i++;
636 i=0;
638 while(i < NUMBER_OF_PARTICLES)
640 if(mParticles[i].bIsActive)
642 if(chopParticleOffscreen(&mParticles[i]) == 1)
643 mParticles[i].bIsActive = 0;
644 else
645 chopDrawParticle(&mParticles[i]);
648 i++;
651 chopRenderTerrain(&mGround);
652 chopRenderTerrain(&mRoof);
656 static void chopDrawParticle(struct CParticle *mParticle)
659 int iPosX = (mParticle->iWorldX - iCameraPosX);
660 int iPosY = (mParticle->iWorldY);
661 #if LCD_DEPTH > 2
662 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
663 #elif LCD_DEPTH == 2
664 rb->lcd_set_foreground(LCD_LIGHTGRAY);
665 #endif
666 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
670 static void chopDrawScene(void)
672 char s[30];
673 int w;
674 #if LCD_DEPTH > 2
675 rb->lcd_set_background(LCD_BLACK);
676 #elif LCD_DEPTH == 2
677 rb->lcd_set_background(LCD_WHITE);
678 #endif
679 chopDrawTheWorld();
680 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
682 score = -20 + iPlayerPosX/3;
684 #if LCD_DEPTH == 1
685 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
686 #else
687 rb->lcd_set_drawmode(DRMODE_FG);
688 #endif
690 #if LCD_DEPTH > 2
691 rb->lcd_set_foreground(LCD_BLACK);
692 #elif LCD_DEPTH == 2
693 rb->lcd_set_foreground(LCD_WHITE);
694 #endif
696 #if LCD_WIDTH <= 128
697 rb->snprintf(s, sizeof(s), "Dist: %d", score);
698 #else
699 rb->snprintf(s, sizeof(s), "Distance: %d", score);
700 #endif
701 rb->lcd_getstringsize(s, &w, NULL);
702 rb->lcd_putsxy(2, 2, s);
703 if (score < highscore)
705 int w2;
706 #if LCD_WIDTH <= 128
707 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
708 #else
709 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
710 #endif
711 rb->lcd_getstringsize(s, &w2, NULL);
712 if (LCD_WIDTH - 2 - w2 > w + 2)
713 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
715 rb->lcd_set_drawmode(DRMODE_SOLID);
717 rb->lcd_update();
720 static bool _ingame;
721 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
723 if(action == ACTION_REQUEST_MENUITEM
724 && !_ingame && ((intptr_t)this_item)==0)
725 return ACTION_EXIT_MENUITEM;
726 return action;
728 static int chopMenu(int menunum)
730 int result = 0;
731 int res = 0;
732 bool menu_quit = false;
734 static const struct opt_items levels[2] = {
735 { "Normal", -1 },
736 { "Steep", -1 },
739 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
740 "Resume Game","Start New Game",
741 "Level","Playback Control","Quit");
742 _ingame = (menunum!=0);
744 #ifdef HAVE_LCD_COLOR
745 rb->lcd_set_foreground(LCD_WHITE);
746 rb->lcd_set_background(LCD_BLACK);
747 #elif LCD_DEPTH == 2
748 rb->lcd_set_foreground(LCD_BLACK);
749 rb->lcd_set_background(LCD_WHITE);
750 #endif
752 rb->lcd_clear_display();
754 while (!menu_quit) {
755 switch(rb->do_menu(&menu, &result, NULL, false))
757 case 0: /* Resume Game */
758 menu_quit=true;
759 res = -1;
760 break;
761 case 1: /* Start New Game */
762 menu_quit=true;
763 chopper_load(true);
764 res = -1;
765 break;
766 case 2:
767 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
768 break;
769 case 3:
770 playback_control(NULL);
771 break;
772 case 4:
773 menu_quit=true;
774 res = PLUGIN_OK;
775 break;
776 case MENU_ATTACHED_USB:
777 menu_quit=true;
778 res = PLUGIN_USB_CONNECTED;
779 break;
782 rb->lcd_clear_display();
783 return res;
786 static int chopGameLoop(void)
788 int move_button, ret;
789 bool exit=false;
790 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
792 if (chopUpdateTerrainRecycling(&mGround) == 1)
793 /* mirror the sky if we've changed the ground */
794 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
796 ret = chopMenu(0);
797 if (ret != -1)
798 return PLUGIN_OK;
800 chopDrawScene();
802 while (!exit) {
804 end = *rb->current_tick + CYCLETIME;
806 if(chopUpdateTerrainRecycling(&mGround) == 1)
807 /* mirror the sky if we've changed the ground */
808 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
810 iRotorOffset = iR(-1,1);
812 /* We need to have this here so particles move when we're dead */
814 for (i=0; i < NUMBER_OF_PARTICLES; i++)
815 if(mParticles[i].bIsActive == 1)
817 mParticles[i].iWorldX += mParticles[i].iSpeedX;
818 mParticles[i].iWorldY += mParticles[i].iSpeedY;
821 rb->lcd_clear_display();
822 /* Redraw the main window: */
823 chopDrawScene();
826 iGravityTimerCountdown--;
828 if(iGravityTimerCountdown <= 0)
830 iGravityTimerCountdown = 3;
831 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
834 if(iLevelMode == LEVEL_MODE_NORMAL)
835 chopGenerateBlockIfNeeded();
838 move_button=rb->button_status();
839 if (rb->button_get(false) == QUIT) {
840 ret = chopMenu(1);
841 if (ret != -1)
842 return PLUGIN_OK;
843 bdelay = 0;
844 last_button = BUTTON_NONE;
845 move_button = BUTTON_NONE;
848 switch (move_button) {
849 case ACTION:
850 #ifdef ACTION2
851 case ACTION2:
852 #endif
853 if (last_button != ACTION
854 #ifdef ACTION2
855 && last_button != ACTION2
856 #endif
858 bdelay = -2;
859 if (bdelay == 0)
860 iPlayerSpeedY = -3;
861 break;
863 default:
864 if (last_button == ACTION
865 #ifdef ACTION2
866 || last_button == ACTION2
867 #endif
869 bdelay = 3;
870 if (bdelay == 0)
871 iPlayerSpeedY = 4;
873 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
874 return PLUGIN_USB_CONNECTED;
875 break;
877 last_button = move_button;
879 if (bdelay < 0) {
880 iPlayerSpeedY = bdelay;
881 bdelay++;
882 } else if (bdelay > 0) {
883 iPlayerSpeedY = bdelay;
884 bdelay--;
887 iCameraPosX = iPlayerPosX - 25;
888 iPlayerPosX += iPlayerSpeedX;
889 iPlayerPosY += iPlayerSpeedY;
891 chopCounter++;
892 /* increase speed as we go along */
893 if (chopCounter == 100){
894 iPlayerSpeedX++;
895 chopCounter=0;
898 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
899 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
900 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
902 chopKillPlayer();
903 chopDrawScene();
904 ret = chopMenu(0);
905 if (ret != -1)
906 return ret;
909 for (i=0; i < NUMBER_OF_BLOCKS; i++)
910 if(mBlocks[i].bIsActive == 1)
911 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
912 chopKillPlayer();
913 chopDrawScene();
914 ret = chopMenu(0);
915 if (ret != -1)
916 return ret;
919 if (TIME_BEFORE(*rb->current_tick, end))
920 rb->sleep(end - *rb->current_tick); /* wait until time is over */
921 else
922 rb->yield();
925 return PLUGIN_OK;
928 static void chopDrawBlock(struct CBlock *mBlock)
930 int iPosX = (mBlock->iWorldX - iCameraPosX);
931 int iPosY = (mBlock->iWorldY);
932 #if LCD_DEPTH > 2
933 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
934 #elif LCD_DEPTH == 2
935 rb->lcd_set_foreground(LCD_BLACK);
936 #endif
937 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
938 SCALE(mBlock->iSizeY));
942 static void chopRenderTerrain(struct CTerrain *ter)
945 int i=1;
947 int oldx=0;
949 int ay=0;
950 if(ter->mNodes[0].y < (LCD_HEIGHT*SIZE)/2)
951 ay=0;
952 else
953 ay=(LCD_HEIGHT*SIZE);
955 while(i < ter->iNodesCount && oldx < iScreenX)
958 int x = ter->mNodes[i-1].x - iCameraPosX;
959 int y = ter->mNodes[i-1].y;
961 int x2 = ter->mNodes[i].x - iCameraPosX;
962 int y2 = ter->mNodes[i].y;
963 #if LCD_DEPTH > 2
964 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
965 #elif LCD_DEPTH == 2
966 rb->lcd_set_foreground(LCD_DARKGRAY);
967 #endif
969 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
971 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
972 SCALE(x2), SCALE(ay));
973 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x2), SCALE(y2),
974 SCALE(x2), SCALE(ay));
976 if (ay == 0)
977 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
978 SCALE(x2), SCALE(y2 / 2));
979 else
980 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
981 SCALE(x2), SCALE((LCD_HEIGHT*SIZE) -
982 ((LCD_HEIGHT*SIZE) - y2) / 2));
984 oldx = x;
985 i++;
991 void chopper_load(bool newgame)
994 int i;
995 int g;
997 if (newgame) {
998 iScreenX = LCD_WIDTH * SIZE;
999 iScreenY = LCD_HEIGHT * SIZE;
1000 blockh = iScreenY / 5;
1001 blockw = iScreenX / 20;
1002 iPlayerAlive = 1;
1003 score = 0;
1005 iRotorOffset = 0;
1006 iPlayerPosX = 60;
1007 iPlayerPosY = (iScreenY * 4) / 10;
1008 iLastBlockPlacedPosX = 0;
1009 iGravityTimerCountdown = 2;
1010 chopCounter = 0;
1011 iPlayerSpeedX = 3;
1012 iPlayerSpeedY = 0;
1013 iCameraPosX = 30;
1015 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1016 mParticles[i].bIsActive = 0;
1018 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1019 mBlocks[i].bIsActive = 0;
1021 g = iScreenY - 10;
1022 chopClearTerrain(&mGround);
1024 for (i=0; i < MAX_TERRAIN_NODES; i++)
1025 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1027 if (chopUpdateTerrainRecycling(&mGround) == 1)
1028 /* mirror the sky if we've changed the ground */
1029 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1031 if (iLevelMode == LEVEL_MODE_NORMAL)
1032 /* make it a bit more exciting, cause it's easy terrain... */
1033 iPlayerSpeedX *= 2;
1036 /* this is the plugin entry point */
1037 enum plugin_status plugin_start(const void* parameter)
1039 (void)parameter;
1040 int ret;
1042 rb->lcd_setfont(FONT_SYSFIXED);
1043 #if LCD_DEPTH > 1
1044 rb->lcd_set_backdrop(NULL);
1045 #endif
1046 #ifdef HAVE_LCD_COLOR
1047 rb->lcd_set_background(LCD_BLACK);
1048 rb->lcd_set_foreground(LCD_WHITE);
1049 #endif
1051 /* Turn off backlight timeout */
1052 backlight_force_on(); /* backlight control in lib/helper.c */
1054 rb->srand( *rb->current_tick );
1056 configfile_load(CFG_FILE, config, 1, 0);
1058 chopper_load(true);
1059 ret = chopGameLoop();
1061 configfile_save(CFG_FILE, config, 1, 0);
1063 rb->lcd_setfont(FONT_UI);
1064 /* Turn on backlight timeout (revert to settings) */
1065 backlight_use_settings(); /* backlight control in lib/helper.c */
1067 return ret;