HD300 - plugins keymaps
[kugel-rb.git] / apps / plugins / chopper.c
blobfce7232d23675d2ddbb0b7be0cc124b7f79442f8
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"
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
118 #define ACTION2 BUTTON_PLUS
120 #elif CONFIG_KEYPAD == IAUDIO67_PAD
121 #define QUIT BUTTON_POWER
122 #define ACTION BUTTON_PLAY
123 #define ACTION2 BUTTON_STOP
124 #define ACTIONTEXT "PLAY"
126 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
127 #define QUIT BUTTON_BACK
128 #define ACTION BUTTON_UP
129 #define ACTION2 BUTTON_MENU
130 #define ACTIONTEXT "UP"
132 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
133 #define QUIT BUTTON_POWER
134 #define ACTION BUTTON_MENU
135 #define ACTION2 BUTTON_SELECT
136 #define ACTIONTEXT "MENU"
138 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
139 #define QUIT BUTTON_POWER
140 #define ACTION BUTTON_MENU
141 #define ACTION2 BUTTON_PLAY
142 #define ACTIONTEXT "MENU"
144 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
145 #define QUIT BUTTON_POWER
146 #define ACTION BUTTON_MENU
147 #define ACTION2 BUTTON_PLAY
148 #define ACTIONTEXT "MENU"
150 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
151 CONFIG_KEYPAD == ONDAVX777_PAD || \
152 CONFIG_KEYPAD == MROBE500_PAD
153 #define QUIT BUTTON_POWER
155 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
156 #define QUIT BUTTON_LEFT
157 #define ACTION BUTTON_RIGHT
158 #define ACTIONTEXT "RIGHT"
160 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
161 #define QUIT BUTTON_REC
162 #define ACTION BUTTON_PLAY
163 #define ACTION2 BUTTON_UP
164 #define ACTIONTEXT "PLAY"
166 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
167 #define QUIT (BUTTON_REC|BUTTON_PLAY)
168 #define ACTION BUTTON_FUNC
169 #define ACTIONTEXT "FUNC"
171 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
172 #define QUIT BUTTON_REC
173 #define ACTION BUTTON_ENTER
174 #define ACTIONTEXT "ENTER"
176 #else
177 #error No keymap defined!
178 #endif
180 #ifdef HAVE_TOUCHSCREEN
181 #ifndef QUIT
182 #define QUIT BUTTON_TOPLEFT
183 #endif
184 #ifndef ACTION
185 #define ACTION BUTTON_BOTTOMLEFT
186 #endif
187 #ifndef ACTION2
188 #define ACTION2 BUTTON_BOTTOMRIGHT
189 #endif
190 #ifndef ACTIONTEXT
191 #define ACTIONTEXT "BOTTOMRIGHT"
192 #endif
193 #endif
195 #define NUMBER_OF_BLOCKS 8
196 #define NUMBER_OF_PARTICLES 3
197 #define MAX_TERRAIN_NODES 15
199 #define LEVEL_MODE_NORMAL 0
200 #define LEVEL_MODE_STEEP 1
202 #if LCD_HEIGHT <= 64
203 #define CYCLES 100
204 static inline int SCALE(int x)
206 return x == 1 ? x : x >> 1;
208 #define SIZE 2
209 #else
210 #define CYCLES 60
211 #define SCALE(x) (x)
212 #define SIZE 1
213 #endif
215 /* in 10 milisecond (ticks) */
216 #define CYCLETIME ((CYCLES*HZ)/1000)
218 /*Chopper's local variables to track the terrain position etc*/
219 static int chopCounter;
220 static int iRotorOffset;
221 static int iScreenX;
222 static int iScreenY;
223 static int iPlayerPosX;
224 static int iPlayerPosY;
225 static int iCameraPosX;
226 static int iPlayerSpeedX;
227 static int iPlayerSpeedY;
228 static int iLastBlockPlacedPosX;
229 static int iGravityTimerCountdown;
230 static int iPlayerAlive;
231 static int iLevelMode, iCurrLevelMode;
232 static int blockh,blockw;
233 static int highscore;
234 static int score;
236 #define CFG_FILE "chopper.cfg"
237 #define MAX_POINTS 50000
238 static struct configdata config[] =
240 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
243 struct CBlock
245 int iWorldX;
246 int iWorldY;
248 int iSizeX;
249 int iSizeY;
251 int bIsActive;
254 struct CParticle
256 int iWorldX;
257 int iWorldY;
259 int iSpeedX;
260 int iSpeedY;
262 int bIsActive;
265 struct CTerrainNode
267 int x;
268 int y;
271 struct CTerrain
273 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
274 int iNodesCount;
275 int iLastNodePlacedPosX;
278 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
279 struct CParticle mParticles[NUMBER_OF_PARTICLES];
281 struct CTerrain mGround;
282 struct CTerrain mRoof;
284 /*Function declarations*/
285 static void chopDrawParticle(struct CParticle *mParticle);
286 static void chopDrawBlock(struct CBlock *mBlock);
287 static void chopRenderTerrain(struct CTerrain *ter, bool isground);
288 void chopper_load(bool newgame);
289 void cleanup_chopper(void);
291 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
294 #if LCD_DEPTH > 2
295 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
296 #elif LCD_DEPTH == 2
297 rb->lcd_set_foreground(LCD_DARKGRAY);
298 #endif
299 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
300 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
302 #if LCD_DEPTH > 2
303 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
304 #elif LCD_DEPTH == 2
305 rb->lcd_set_foreground(LCD_DARKGRAY);
306 #endif
307 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
308 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
310 #if LCD_DEPTH > 2
311 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
312 #elif LCD_DEPTH == 2
313 rb->lcd_set_foreground(LCD_BLACK);
314 #endif
315 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
316 SCALE(y-iRotorOffset));
318 #if LCD_DEPTH > 2
319 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
320 #elif LCD_DEPTH == 2
321 rb->lcd_set_foreground(LCD_BLACK);
322 #endif
323 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
327 static void chopClearTerrain(struct CTerrain *ter)
329 ter->iNodesCount = 0;
333 int iR(int low,int high)
335 return low+rb->rand()%(high-low+1);
338 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
339 int xOffset,int yOffset)
341 int i=0;
343 while(i < src->iNodesCount)
345 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
346 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
348 i++;
351 dest->iNodesCount = src->iNodesCount;
352 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
356 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
358 int i=0;
360 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
362 /* DEBUGF("ERROR: Not enough nodes!\n"); */
363 return;
366 ter->iNodesCount++;
368 i = ter->iNodesCount - 1;
370 ter->mNodes[i].x = x;
371 ter->mNodes[i].y= y;
373 ter->iLastNodePlacedPosX = x;
377 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
379 int i=nodeIndex;
381 while( i < ter->iNodesCount )
383 ter->mNodes[i - 1] = ter->mNodes[i];
384 i++;
387 ter->iNodesCount--;
392 int chopUpdateTerrainRecycling(struct CTerrain *ter)
394 int i=1;
395 int ret = 0;
396 int iNewNodePos,g,v;
397 while(i < ter->iNodesCount)
400 if( iCameraPosX > ter->mNodes[i].x)
403 chopTerrainNodeDeleteAndShift(ter,i);
405 iNewNodePos = ter->iLastNodePlacedPosX + 50;
406 g = iScreenY - 10;
408 v = 3*iPlayerSpeedX;
409 if(v>50)
410 v=50;
411 if(iCurrLevelMode == LEVEL_MODE_STEEP)
412 v*=5;
414 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
415 ret=1;
419 i++;
423 return 1;
426 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
429 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
430 float c,d;
432 int i=0;
433 for(i=1;i<MAX_TERRAIN_NODES;i++)
435 if(ter->mNodes[i].x > pX)
437 iNodeIndexOne = i - 1;
438 break;
443 iNodeIndexTwo = iNodeIndexOne + 1;
444 terY1 = ter->mNodes[iNodeIndexOne].y;
445 terY2 = ter->mNodes[iNodeIndexTwo].y;
447 terX1 = 0;
448 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
450 pX-= ter->mNodes[iNodeIndexOne].x;
452 a = terY2 - terY1;
453 b = terX2;
454 c = pX;
455 d = (c/b) * a;
457 h = d + terY1;
459 return h;
463 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
465 int h = chopTerrainHeightAtPoint(ter, pX);
467 if(iTestType == 0)
468 return (pY > h);
469 else
470 return (pY < h);
473 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
475 int i=0;
477 if(indexOverride < 0)
479 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
480 i++;
481 if(i==NUMBER_OF_BLOCKS)
483 DEBUGF("No blocks!\n");
484 return;
487 else
488 i = indexOverride;
490 mBlocks[i].bIsActive = 1;
491 mBlocks[i].iWorldX = x;
492 mBlocks[i].iWorldY = y;
493 mBlocks[i].iSizeX = sx;
494 mBlocks[i].iSizeY = sy;
496 iLastBlockPlacedPosX = x;
499 static void chopAddParticle(int x,int y,int sx,int sy)
501 int i=0;
503 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
504 i++;
506 if(i==NUMBER_OF_PARTICLES)
507 return;
509 mParticles[i].bIsActive = 1;
510 mParticles[i].iWorldX = x;
511 mParticles[i].iWorldY = y;
512 mParticles[i].iSpeedX = sx;
513 mParticles[i].iSpeedY = sy;
516 static void chopGenerateBlockIfNeeded(void)
518 int i=0;
519 int DistSpeedX = iPlayerSpeedX * 5;
520 if(DistSpeedX<200) DistSpeedX = 200;
522 while(i < NUMBER_OF_BLOCKS)
524 if(!mBlocks[i].bIsActive)
526 int iX,iY,sX,sY;
528 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
529 sX = blockw;
531 iY = iR(0,iScreenY);
532 sY = blockh + iR(1,blockh/3);
534 chopAddBlock(iX,iY,sX,sY,i);
537 i++;
542 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
544 int px = iPlayerPosX;
545 int py = iPlayerPosY;
547 int x = mBlock->iWorldX-17;
548 int y = mBlock->iWorldY-11;
550 int x2 = x + mBlock->iSizeX+17;
551 int y2 = y + mBlock->iSizeY+11;
553 if(px>x && px<x2)
555 if(py>y && py<y2)
557 return 1;
561 return 0;
564 static int chopBlockOffscreen(struct CBlock *mBlock)
566 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
567 return 1;
568 else
569 return 0;
572 static int chopParticleOffscreen(struct CParticle *mParticle)
574 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
575 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
576 iScreenX)
578 return 1;
580 else
581 return 0;
584 static void chopKillPlayer(void)
586 int i;
588 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
589 mParticles[i].bIsActive = 0;
590 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
591 iR(-2,2), iR(-2,2));
594 iPlayerAlive--;
596 if (iPlayerAlive == 0) {
597 rb->splash(HZ, "Game Over");
599 if (score > highscore) {
600 char scoretext[30];
601 highscore = score;
602 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
603 highscore);
604 rb->splash(HZ*2, scoretext);
606 } else
607 chopper_load(false);
610 static void chopDrawTheWorld(void)
612 int i=0;
614 while(i < NUMBER_OF_BLOCKS)
616 if(mBlocks[i].bIsActive)
618 if(chopBlockOffscreen(&mBlocks[i]) == 1)
619 mBlocks[i].bIsActive = 0;
620 else
621 chopDrawBlock(&mBlocks[i]);
624 i++;
627 i=0;
629 while(i < NUMBER_OF_PARTICLES)
631 if(mParticles[i].bIsActive)
633 if(chopParticleOffscreen(&mParticles[i]) == 1)
634 mParticles[i].bIsActive = 0;
635 else
636 chopDrawParticle(&mParticles[i]);
639 i++;
642 chopRenderTerrain(&mGround, true);
643 chopRenderTerrain(&mRoof, false);
647 static void chopDrawParticle(struct CParticle *mParticle)
650 int iPosX = (mParticle->iWorldX - iCameraPosX);
651 int iPosY = (mParticle->iWorldY);
652 #if LCD_DEPTH > 2
653 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
654 #elif LCD_DEPTH == 2
655 rb->lcd_set_foreground(LCD_LIGHTGRAY);
656 #endif
657 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
661 static void chopDrawScene(void)
663 char s[30];
664 int w;
665 #if LCD_DEPTH > 2
666 rb->lcd_set_background(LCD_BLACK);
667 #elif LCD_DEPTH == 2
668 rb->lcd_set_background(LCD_WHITE);
669 #endif
670 rb->lcd_clear_display();
671 chopDrawTheWorld();
672 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
674 score = -20 + iPlayerPosX/3;
676 #if LCD_DEPTH == 1
677 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
678 #else
679 rb->lcd_set_drawmode(DRMODE_FG);
680 #endif
682 #if LCD_DEPTH > 2
683 rb->lcd_set_foreground(LCD_BLACK);
684 #elif LCD_DEPTH == 2
685 rb->lcd_set_foreground(LCD_WHITE);
686 #endif
688 #if LCD_WIDTH <= 128
689 rb->snprintf(s, sizeof(s), "Dist: %d", score);
690 #else
691 rb->snprintf(s, sizeof(s), "Distance: %d", score);
692 #endif
693 rb->lcd_getstringsize(s, &w, NULL);
694 rb->lcd_putsxy(2, 2, s);
695 if (score < highscore)
697 int w2;
698 #if LCD_WIDTH <= 128
699 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
700 #else
701 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
702 #endif
703 rb->lcd_getstringsize(s, &w2, NULL);
704 if (LCD_WIDTH - 2 - w2 > w + 2)
705 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
707 rb->lcd_set_drawmode(DRMODE_SOLID);
709 rb->lcd_update();
712 static bool _ingame;
713 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
715 if(action == ACTION_REQUEST_MENUITEM
716 && !_ingame && ((intptr_t)this_item)==0)
717 return ACTION_EXIT_MENUITEM;
718 return action;
720 static int chopMenu(int menunum)
722 int result = 0;
723 int res = 0;
724 bool menu_quit = false;
726 static const struct opt_items levels[2] = {
727 { "Normal", -1 },
728 { "Steep", -1 },
731 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
732 "Resume Game","Start New Game",
733 "Level","Playback Control","Quit");
734 _ingame = (menunum!=0);
736 #ifdef HAVE_LCD_COLOR
737 rb->lcd_set_foreground(LCD_WHITE);
738 rb->lcd_set_background(LCD_BLACK);
739 #elif LCD_DEPTH == 2
740 rb->lcd_set_foreground(LCD_BLACK);
741 rb->lcd_set_background(LCD_WHITE);
742 #endif
744 rb->lcd_clear_display();
745 rb->button_clear_queue();
747 while (!menu_quit) {
748 switch(rb->do_menu(&menu, &result, NULL, false))
750 case 0: /* Resume Game */
751 menu_quit=true;
752 res = -1;
753 break;
754 case 1: /* Start New Game */
755 menu_quit=true;
756 chopper_load(true);
757 res = -1;
758 break;
759 case 2:
760 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
761 break;
762 case 3:
763 playback_control(NULL);
764 break;
765 case 4:
766 menu_quit=true;
767 res = PLUGIN_OK;
768 break;
769 case MENU_ATTACHED_USB:
770 menu_quit=true;
771 res = PLUGIN_USB_CONNECTED;
772 break;
775 rb->lcd_clear_display();
776 return res;
779 static int chopGameLoop(void)
781 int move_button, ret;
782 bool exit=false;
783 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
785 if (chopUpdateTerrainRecycling(&mGround) == 1)
786 /* mirror the sky if we've changed the ground */
787 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
789 ret = chopMenu(0);
790 if (ret != -1)
791 return PLUGIN_OK;
793 chopDrawScene();
795 while (!exit) {
797 end = *rb->current_tick + CYCLETIME;
799 if(chopUpdateTerrainRecycling(&mGround) == 1)
800 /* mirror the sky if we've changed the ground */
801 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
803 iRotorOffset = iR(-1,1);
805 /* We need to have this here so particles move when we're dead */
807 for (i=0; i < NUMBER_OF_PARTICLES; i++)
808 if(mParticles[i].bIsActive == 1)
810 mParticles[i].iWorldX += mParticles[i].iSpeedX;
811 mParticles[i].iWorldY += mParticles[i].iSpeedY;
814 /* Redraw the main window: */
815 chopDrawScene();
818 iGravityTimerCountdown--;
820 if(iGravityTimerCountdown <= 0)
822 iGravityTimerCountdown = 3;
823 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
826 if(iCurrLevelMode == LEVEL_MODE_NORMAL)
827 chopGenerateBlockIfNeeded();
830 move_button=rb->button_status();
831 if (rb->button_get(false) == QUIT) {
832 ret = chopMenu(1);
833 if (ret != -1)
834 return PLUGIN_OK;
835 bdelay = 0;
836 last_button = BUTTON_NONE;
837 move_button = BUTTON_NONE;
840 switch (move_button) {
841 case ACTION:
842 #ifdef ACTION2
843 case ACTION2:
844 #endif
845 if (last_button != ACTION
846 #ifdef ACTION2
847 && last_button != ACTION2
848 #endif
850 bdelay = -2;
851 if (bdelay == 0)
852 iPlayerSpeedY = -3;
853 break;
855 default:
856 if (last_button == ACTION
857 #ifdef ACTION2
858 || last_button == ACTION2
859 #endif
861 bdelay = 3;
862 if (bdelay == 0)
863 iPlayerSpeedY = 4;
865 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
866 return PLUGIN_USB_CONNECTED;
867 break;
869 last_button = move_button;
871 if (bdelay < 0) {
872 iPlayerSpeedY = bdelay;
873 bdelay++;
874 } else if (bdelay > 0) {
875 iPlayerSpeedY = bdelay;
876 bdelay--;
879 iCameraPosX = iPlayerPosX - 25;
880 iPlayerPosX += iPlayerSpeedX;
881 iPlayerPosY += iPlayerSpeedY;
883 chopCounter++;
884 /* increase speed as we go along */
885 if (chopCounter == 100){
886 iPlayerSpeedX++;
887 chopCounter=0;
890 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
891 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
892 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
894 chopKillPlayer();
895 chopDrawScene();
896 ret = chopMenu(0);
897 if (ret != -1)
898 return ret;
901 for (i=0; i < NUMBER_OF_BLOCKS; i++)
902 if(mBlocks[i].bIsActive == 1)
903 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
904 chopKillPlayer();
905 chopDrawScene();
906 ret = chopMenu(0);
907 if (ret != -1)
908 return ret;
911 if (TIME_BEFORE(*rb->current_tick, end))
912 rb->sleep(end - *rb->current_tick); /* wait until time is over */
913 else
914 rb->yield();
917 return PLUGIN_OK;
920 static void chopDrawBlock(struct CBlock *mBlock)
922 int iPosX = (mBlock->iWorldX - iCameraPosX);
923 int iPosY = (mBlock->iWorldY);
924 #if LCD_DEPTH > 2
925 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
926 #elif LCD_DEPTH == 2
927 rb->lcd_set_foreground(LCD_BLACK);
928 #endif
929 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
930 SCALE(mBlock->iSizeY));
934 static void chopRenderTerrain(struct CTerrain *ter, bool isground)
937 int i = 1;
939 int oldx = 0;
941 while(i < ter->iNodesCount && oldx < iScreenX)
944 int x = ter->mNodes[i-1].x - iCameraPosX;
945 int y = ter->mNodes[i-1].y;
947 int x2 = ter->mNodes[i].x - iCameraPosX;
948 int y2 = ter->mNodes[i].y;
950 int ax, ay;
952 if ((y < y2) != isground)
954 ax = x2;
955 ay = y;
957 else
959 ax = x;
960 ay = y2;
962 #if LCD_DEPTH > 2
963 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
964 #elif LCD_DEPTH == 2
965 rb->lcd_set_foreground(LCD_DARKGRAY);
966 #endif
968 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
970 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
971 SCALE(ax), SCALE(ay));
973 if (isground)
975 y = ay;
976 y2 = (LCD_HEIGHT*SIZE);
978 else
980 y = 0;
981 y2 = ay;
983 if (y2-y > 0)
984 rb->lcd_fillrect(SCALE(x), SCALE(y), SCALE(x2-x)+1, SCALE(y2-y)+1);
986 oldx = x;
987 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 iCurrLevelMode = iLevelMode;
1004 score = 0;
1006 iRotorOffset = 0;
1007 iPlayerPosX = 60;
1008 iPlayerPosY = (iScreenY * 4) / 10;
1009 iLastBlockPlacedPosX = 0;
1010 iGravityTimerCountdown = 2;
1011 chopCounter = 0;
1012 iPlayerSpeedX = 3;
1013 iPlayerSpeedY = 0;
1014 iCameraPosX = 30;
1016 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1017 mParticles[i].bIsActive = 0;
1019 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1020 mBlocks[i].bIsActive = 0;
1022 g = iScreenY - 10;
1023 chopClearTerrain(&mGround);
1025 for (i=0; i < MAX_TERRAIN_NODES; i++)
1026 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1028 if (chopUpdateTerrainRecycling(&mGround) == 1)
1029 /* mirror the sky if we've changed the ground */
1030 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1032 if (iCurrLevelMode == LEVEL_MODE_NORMAL)
1033 /* make it a bit more exciting, cause it's easy terrain... */
1034 iPlayerSpeedX *= 2;
1037 /* this is the plugin entry point */
1038 enum plugin_status plugin_start(const void* parameter)
1040 (void)parameter;
1041 int ret;
1043 rb->lcd_setfont(FONT_SYSFIXED);
1044 #if LCD_DEPTH > 1
1045 rb->lcd_set_backdrop(NULL);
1046 #endif
1047 #ifdef HAVE_LCD_COLOR
1048 rb->lcd_set_background(LCD_BLACK);
1049 rb->lcd_set_foreground(LCD_WHITE);
1050 #endif
1052 /* Turn off backlight timeout */
1053 backlight_force_on(); /* backlight control in lib/helper.c */
1055 rb->srand( *rb->current_tick );
1057 configfile_load(CFG_FILE, config, 1, 0);
1059 chopper_load(true);
1060 ret = chopGameLoop();
1062 configfile_save(CFG_FILE, config, 1, 0);
1064 rb->lcd_setfont(FONT_UI);
1065 /* Turn on backlight timeout (revert to settings) */
1066 backlight_use_settings(); /* backlight control in lib/helper.c */
1068 return ret;