Rearange menu of mpegplayer. Add new menu with "settings" and "quit", and remove...
[kugel-rb.git] / apps / plugins / chopper.c
blob4a39d2da54b4adbfbd0d519f2e76519938858f4f
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|BUTTON_REPEAT)
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 == COWOND2_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 == ONDAVX747_PAD || CONFIG_KEYPAD == MROBE500_PAD
138 #define QUIT BUTTON_POWER
140 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
141 #define QUIT BUTTON_LEFT
142 #define ACTION BUTTON_RIGHT
143 #define ACTIONTEXT "RIGHT"
145 #else
146 #error No keymap defined!
147 #endif
149 #ifdef HAVE_TOUCHSCREEN
150 #ifndef QUIT
151 #define QUIT BUTTON_TOPLEFT
152 #endif
153 #ifndef ACTION
154 #define ACTION BUTTON_BOTTOMLEFT
155 #endif
156 #ifndef ACTION2
157 #define ACTION2 BUTTON_BOTTOMRIGHT
158 #endif
159 #ifndef ACTIONTEXT
160 #define ACTIONTEXT "BOTTOMRIGHT"
161 #endif
162 #endif
164 #define NUMBER_OF_BLOCKS 8
165 #define NUMBER_OF_PARTICLES 3
166 #define MAX_TERRAIN_NODES 15
168 #define LEVEL_MODE_NORMAL 0
169 #define LEVEL_MODE_STEEP 1
171 #if LCD_HEIGHT <= 64
172 #define CYCLETIME 100
173 static inline int SCALE(int x)
175 return x == 1 ? x : x >> 1;
177 #define SIZE 2
178 #else
179 #define CYCLETIME 60
180 #define SCALE(x) (x)
181 #define SIZE 1
182 #endif
184 /*Chopper's local variables to track the terrain position etc*/
185 static int chopCounter;
186 static int iRotorOffset;
187 static int iScreenX;
188 static int iScreenY;
189 static int iPlayerPosX;
190 static int iPlayerPosY;
191 static int iCameraPosX;
192 static int iPlayerSpeedX;
193 static int iPlayerSpeedY;
194 static int iLastBlockPlacedPosX;
195 static int iGravityTimerCountdown;
196 static int iPlayerAlive;
197 static int iLevelMode;
198 static int blockh,blockw;
199 static int highscore;
200 static int score;
202 #define CFG_FILE "chopper.cfg"
203 #define MAX_POINTS 50000
204 static struct configdata config[] =
206 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
209 struct CBlock
211 int iWorldX;
212 int iWorldY;
214 int iSizeX;
215 int iSizeY;
217 int bIsActive;
220 struct CParticle
222 int iWorldX;
223 int iWorldY;
225 int iSpeedX;
226 int iSpeedY;
228 int bIsActive;
231 struct CTerrainNode
233 int x;
234 int y;
237 struct CTerrain
239 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
240 int iNodesCount;
241 int iLastNodePlacedPosX;
244 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
245 struct CParticle mParticles[NUMBER_OF_PARTICLES];
247 struct CTerrain mGround;
248 struct CTerrain mRoof;
250 /*Function declarations*/
251 static void chopDrawParticle(struct CParticle *mParticle);
252 static void chopDrawBlock(struct CBlock *mBlock);
253 static void chopRenderTerrain(struct CTerrain *ter);
254 void chopper_load(bool newgame);
255 void cleanup_chopper(void);
257 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
260 #if LCD_DEPTH > 2
261 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
262 #elif LCD_DEPTH == 2
263 rb->lcd_set_foreground(LCD_DARKGRAY);
264 #endif
265 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
266 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
268 #if LCD_DEPTH > 2
269 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
270 #elif LCD_DEPTH == 2
271 rb->lcd_set_foreground(LCD_DARKGRAY);
272 #endif
273 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
274 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
276 #if LCD_DEPTH > 2
277 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
278 #elif LCD_DEPTH == 2
279 rb->lcd_set_foreground(LCD_BLACK);
280 #endif
281 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
282 SCALE(y-iRotorOffset));
284 #if LCD_DEPTH > 2
285 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
286 #elif LCD_DEPTH == 2
287 rb->lcd_set_foreground(LCD_BLACK);
288 #endif
289 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
293 static void chopClearTerrain(struct CTerrain *ter)
295 ter->iNodesCount = 0;
299 int iR(int low,int high)
301 return low+rb->rand()%(high-low+1);
304 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
305 int xOffset,int yOffset)
307 int i=0;
309 while(i < src->iNodesCount)
311 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
312 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
314 i++;
317 dest->iNodesCount = src->iNodesCount;
318 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
322 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
324 int i=0;
326 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
328 /* DEBUGF("ERROR: Not enough nodes!\n"); */
329 return;
332 ter->iNodesCount++;
334 i = ter->iNodesCount - 1;
336 ter->mNodes[i].x = x;
337 ter->mNodes[i].y= y;
339 ter->iLastNodePlacedPosX = x;
343 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
345 int i=nodeIndex;
347 while( i < ter->iNodesCount )
349 ter->mNodes[i - 1] = ter->mNodes[i];
350 i++;
353 ter->iNodesCount--;
358 int chopUpdateTerrainRecycling(struct CTerrain *ter)
360 int i=1;
361 int ret = 0;
362 int iNewNodePos,g,v;
363 while(i < ter->iNodesCount)
366 if( iCameraPosX > ter->mNodes[i].x)
369 chopTerrainNodeDeleteAndShift(ter,i);
371 iNewNodePos = ter->iLastNodePlacedPosX + 50;
372 g = iScreenY - 10;
374 v = 3*iPlayerSpeedX;
375 if(v>50)
376 v=50;
377 if(iLevelMode == LEVEL_MODE_STEEP)
378 v*=5;
380 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
381 ret=1;
385 i++;
389 return 1;
392 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
395 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
396 float c,d;
398 int i=0;
399 for(i=1;i<MAX_TERRAIN_NODES;i++)
401 if(ter->mNodes[i].x > pX)
403 iNodeIndexOne = i - 1;
404 break;
409 iNodeIndexTwo = iNodeIndexOne + 1;
410 terY1 = ter->mNodes[iNodeIndexOne].y;
411 terY2 = ter->mNodes[iNodeIndexTwo].y;
413 terX1 = 0;
414 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
416 pX-= ter->mNodes[iNodeIndexOne].x;
418 a = terY2 - terY1;
419 b = terX2;
420 c = pX;
421 d = (c/b) * a;
423 h = d + terY1;
425 return h;
429 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
431 int h = chopTerrainHeightAtPoint(ter, pX);
433 if(iTestType == 0)
434 return (pY > h);
435 else
436 return (pY < h);
439 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
441 int i=0;
443 if(indexOverride < 0)
445 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
446 i++;
447 if(i==NUMBER_OF_BLOCKS)
449 DEBUGF("No blocks!\n");
450 return;
453 else
454 i = indexOverride;
456 mBlocks[i].bIsActive = 1;
457 mBlocks[i].iWorldX = x;
458 mBlocks[i].iWorldY = y;
459 mBlocks[i].iSizeX = sx;
460 mBlocks[i].iSizeY = sy;
462 iLastBlockPlacedPosX = x;
465 static void chopAddParticle(int x,int y,int sx,int sy)
467 int i=0;
469 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
470 i++;
472 if(i==NUMBER_OF_PARTICLES)
473 return;
475 mParticles[i].bIsActive = 1;
476 mParticles[i].iWorldX = x;
477 mParticles[i].iWorldY = y;
478 mParticles[i].iSpeedX = sx;
479 mParticles[i].iSpeedY = sy;
482 static void chopGenerateBlockIfNeeded(void)
484 int i=0;
485 int DistSpeedX = iPlayerSpeedX * 5;
486 if(DistSpeedX<200) DistSpeedX = 200;
488 while(i < NUMBER_OF_BLOCKS)
490 if(!mBlocks[i].bIsActive)
492 int iX,iY,sX,sY;
494 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
495 sX = blockw;
497 iY = iR(0,iScreenY);
498 sY = blockh + iR(1,blockh/3);
500 chopAddBlock(iX,iY,sX,sY,i);
503 i++;
508 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
510 int px = iPlayerPosX;
511 int py = iPlayerPosY;
513 int x = mBlock->iWorldX-17;
514 int y = mBlock->iWorldY-11;
516 int x2 = x + mBlock->iSizeX+17;
517 int y2 = y + mBlock->iSizeY+11;
519 if(px>x && px<x2)
521 if(py>y && py<y2)
523 return 1;
527 return 0;
530 static int chopBlockOffscreen(struct CBlock *mBlock)
532 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
533 return 1;
534 else
535 return 0;
538 static int chopParticleOffscreen(struct CParticle *mParticle)
540 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
541 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
542 iScreenX)
544 return 1;
546 else
547 return 0;
550 static void chopKillPlayer(void)
552 int i, button;
554 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
555 mParticles[i].bIsActive = 0;
556 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
557 iR(-2,2), iR(-2,2));
560 iPlayerAlive--;
562 if (iPlayerAlive == 0) {
563 rb->lcd_set_drawmode(DRMODE_FG);
564 #if LCD_DEPTH >= 2
565 rb->lcd_set_foreground(LCD_LIGHTGRAY);
566 #endif
567 rb->splash(HZ, "Game Over");
569 if (score > highscore) {
570 char scoretext[30];
571 highscore = score;
572 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
573 highscore);
574 rb->splash(HZ*2, scoretext);
577 rb->splash(HZ/4, "Press " ACTIONTEXT " to continue");
578 rb->lcd_update();
580 rb->lcd_set_drawmode(DRMODE_SOLID);
582 while (true) {
583 button = rb->button_get(true);
584 if (button == ACTION
585 #ifdef ACTION2
586 || button == ACTION2
587 #endif
589 while (true) {
590 button = rb->button_get(true);
591 if (button == (ACTION | BUTTON_REL)
592 #ifdef ACTION2
593 || button == (ACTION2 | BUTTON_REL)
594 #endif
596 chopper_load(true);
597 return;
603 } else
604 chopper_load(false);
608 static void chopDrawTheWorld(void)
610 int i=0;
612 while(i < NUMBER_OF_BLOCKS)
614 if(mBlocks[i].bIsActive)
616 if(chopBlockOffscreen(&mBlocks[i]) == 1)
617 mBlocks[i].bIsActive = 0;
618 else
619 chopDrawBlock(&mBlocks[i]);
622 i++;
625 i=0;
627 while(i < NUMBER_OF_PARTICLES)
629 if(mParticles[i].bIsActive)
631 if(chopParticleOffscreen(&mParticles[i]) == 1)
632 mParticles[i].bIsActive = 0;
633 else
634 chopDrawParticle(&mParticles[i]);
637 i++;
640 chopRenderTerrain(&mGround);
641 chopRenderTerrain(&mRoof);
645 static void chopDrawParticle(struct CParticle *mParticle)
648 int iPosX = (mParticle->iWorldX - iCameraPosX);
649 int iPosY = (mParticle->iWorldY);
650 #if LCD_DEPTH > 2
651 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
652 #elif LCD_DEPTH == 2
653 rb->lcd_set_foreground(LCD_LIGHTGRAY);
654 #endif
655 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
659 static void chopDrawScene(void)
661 char s[30];
662 int w;
663 #if LCD_DEPTH > 2
664 rb->lcd_set_background(LCD_BLACK);
665 #elif LCD_DEPTH == 2
666 rb->lcd_set_background(LCD_WHITE);
667 #endif
668 chopDrawTheWorld();
669 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
671 score = -20 + iPlayerPosX/3;
673 #if LCD_DEPTH == 1
674 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
675 #else
676 rb->lcd_set_drawmode(DRMODE_FG);
677 #endif
679 #if LCD_DEPTH > 2
680 rb->lcd_set_foreground(LCD_BLACK);
681 #elif LCD_DEPTH == 2
682 rb->lcd_set_foreground(LCD_WHITE);
683 #endif
685 #if LCD_WIDTH <= 128
686 rb->snprintf(s, sizeof(s), "Dist: %d", score);
687 #else
688 rb->snprintf(s, sizeof(s), "Distance: %d", score);
689 #endif
690 rb->lcd_getstringsize(s, &w, NULL);
691 rb->lcd_putsxy(2, 2, s);
692 if (score < highscore)
694 int w2;
695 #if LCD_WIDTH <= 128
696 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
697 #else
698 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
699 #endif
700 rb->lcd_getstringsize(s, &w2, NULL);
701 if (LCD_WIDTH - 2 - w2 > w + 2)
702 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
704 rb->lcd_set_drawmode(DRMODE_SOLID);
706 rb->lcd_update();
709 static bool _ingame;
710 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
712 if(action == ACTION_REQUEST_MENUITEM
713 && !_ingame && ((intptr_t)this_item)==0)
714 return ACTION_EXIT_MENUITEM;
715 return action;
717 static int chopMenu(int menunum)
719 int result = 0;
720 int res = 0;
721 bool menu_quit = false;
723 static const struct opt_items levels[2] = {
724 { "Normal", -1 },
725 { "Steep", -1 },
728 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
729 "Resume Game","Start New Game",
730 "Level","Playback Control","Quit");
731 _ingame = (menunum!=0);
733 #ifdef HAVE_LCD_COLOR
734 rb->lcd_set_foreground(LCD_WHITE);
735 rb->lcd_set_background(LCD_BLACK);
736 #elif LCD_DEPTH == 2
737 rb->lcd_set_foreground(LCD_BLACK);
738 rb->lcd_set_background(LCD_WHITE);
739 #endif
741 rb->lcd_clear_display();
743 while (!menu_quit) {
744 switch(rb->do_menu(&menu, &result, NULL, false))
746 case 0: /* Resume Game */
747 menu_quit=true;
748 res = -1;
749 break;
750 case 1: /* Start New Game */
751 menu_quit=true;
752 chopper_load(true);
753 res = -1;
754 break;
755 case 2:
756 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
757 break;
758 case 3:
759 playback_control(NULL);
760 break;
761 case 4:
762 menu_quit=true;
763 res = PLUGIN_OK;
764 break;
765 case MENU_ATTACHED_USB:
766 menu_quit=true;
767 res = PLUGIN_USB_CONNECTED;
768 break;
771 rb->lcd_clear_display();
772 return res;
775 static int chopGameLoop(void)
777 int move_button, ret;
778 bool exit=false;
779 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
781 if (chopUpdateTerrainRecycling(&mGround) == 1)
782 /* mirror the sky if we've changed the ground */
783 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
785 ret = chopMenu(0);
786 if (ret != -1)
787 return PLUGIN_OK;
789 chopDrawScene();
791 while (!exit) {
793 end = *rb->current_tick + (CYCLETIME * HZ) / 1000;
795 if(chopUpdateTerrainRecycling(&mGround) == 1)
796 /* mirror the sky if we've changed the ground */
797 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
799 iRotorOffset = iR(-1,1);
801 /* We need to have this here so particles move when we're dead */
803 for (i=0; i < NUMBER_OF_PARTICLES; i++)
804 if(mParticles[i].bIsActive == 1)
806 mParticles[i].iWorldX += mParticles[i].iSpeedX;
807 mParticles[i].iWorldY += mParticles[i].iSpeedY;
810 rb->lcd_clear_display();
811 /* Redraw the main window: */
812 chopDrawScene();
815 iGravityTimerCountdown--;
817 if(iGravityTimerCountdown <= 0)
819 iGravityTimerCountdown = 3;
820 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
823 if(iLevelMode == LEVEL_MODE_NORMAL)
824 chopGenerateBlockIfNeeded();
827 move_button=rb->button_status();
828 if (rb->button_get(false) == QUIT) {
829 ret = chopMenu(1);
830 if (ret != -1)
831 return PLUGIN_OK;
832 bdelay = 0;
833 last_button = BUTTON_NONE;
834 move_button = BUTTON_NONE;
837 switch (move_button) {
838 case ACTION:
839 #ifdef ACTION2
840 case ACTION2:
841 #endif
842 if (last_button != ACTION
843 #ifdef ACTION2
844 && last_button != ACTION2
845 #endif
847 bdelay = -2;
848 if (bdelay == 0)
849 iPlayerSpeedY = -3;
850 break;
852 default:
853 if (last_button == ACTION
854 #ifdef ACTION2
855 || last_button == ACTION2
856 #endif
858 bdelay = 3;
859 if (bdelay == 0)
860 iPlayerSpeedY = 4;
862 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
863 return PLUGIN_USB_CONNECTED;
864 break;
866 last_button = move_button;
868 if (bdelay < 0) {
869 iPlayerSpeedY = bdelay;
870 bdelay++;
871 } else if (bdelay > 0) {
872 iPlayerSpeedY = bdelay;
873 bdelay--;
876 iCameraPosX = iPlayerPosX - 25;
877 iPlayerPosX += iPlayerSpeedX;
878 iPlayerPosY += iPlayerSpeedY;
880 chopCounter++;
881 /* increase speed as we go along */
882 if (chopCounter == 100){
883 iPlayerSpeedX++;
884 chopCounter=0;
887 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
888 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
889 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
891 chopKillPlayer();
892 chopDrawScene();
893 ret = chopMenu(0);
894 if (ret != -1)
895 return ret;
898 for (i=0; i < NUMBER_OF_BLOCKS; i++)
899 if(mBlocks[i].bIsActive == 1)
900 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
901 chopKillPlayer();
902 chopDrawScene();
903 ret = chopMenu(0);
904 if (ret != -1)
905 return ret;
908 if (end > *rb->current_tick)
909 rb->sleep(end-*rb->current_tick);
910 else
911 rb->yield();
914 return PLUGIN_OK;
917 static void chopDrawBlock(struct CBlock *mBlock)
919 int iPosX = (mBlock->iWorldX - iCameraPosX);
920 int iPosY = (mBlock->iWorldY);
921 #if LCD_DEPTH > 2
922 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
923 #elif LCD_DEPTH == 2
924 rb->lcd_set_foreground(LCD_BLACK);
925 #endif
926 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
927 SCALE(mBlock->iSizeY));
931 static void chopRenderTerrain(struct CTerrain *ter)
934 int i=1;
936 int oldx=0;
938 int ay=0;
939 if(ter->mNodes[0].y < (LCD_HEIGHT*SIZE)/2)
940 ay=0;
941 else
942 ay=(LCD_HEIGHT*SIZE);
944 while(i < ter->iNodesCount && oldx < iScreenX)
947 int x = ter->mNodes[i-1].x - iCameraPosX;
948 int y = ter->mNodes[i-1].y;
950 int x2 = ter->mNodes[i].x - iCameraPosX;
951 int y2 = ter->mNodes[i].y;
952 #if LCD_DEPTH > 2
953 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
954 #elif LCD_DEPTH == 2
955 rb->lcd_set_foreground(LCD_DARKGRAY);
956 #endif
958 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
960 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
961 SCALE(x2), SCALE(ay));
962 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x2), SCALE(y2),
963 SCALE(x2), SCALE(ay));
965 if (ay == 0)
966 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
967 SCALE(x2), SCALE(y2 / 2));
968 else
969 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
970 SCALE(x2), SCALE((LCD_HEIGHT*SIZE) -
971 ((LCD_HEIGHT*SIZE) - y2) / 2));
973 oldx = x;
974 i++;
980 void chopper_load(bool newgame)
983 int i;
984 int g;
986 if (newgame) {
987 iScreenX = LCD_WIDTH * SIZE;
988 iScreenY = LCD_HEIGHT * SIZE;
989 blockh = iScreenY / 5;
990 blockw = iScreenX / 20;
991 iPlayerAlive = 1;
992 score = 0;
994 iRotorOffset = 0;
995 iPlayerPosX = 60;
996 iPlayerPosY = (iScreenY * 4) / 10;
997 iLastBlockPlacedPosX = 0;
998 iGravityTimerCountdown = 2;
999 chopCounter = 0;
1000 iPlayerSpeedX = 3;
1001 iPlayerSpeedY = 0;
1002 iCameraPosX = 30;
1004 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1005 mParticles[i].bIsActive = 0;
1007 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1008 mBlocks[i].bIsActive = 0;
1010 g = iScreenY - 10;
1011 chopClearTerrain(&mGround);
1013 for (i=0; i < MAX_TERRAIN_NODES; i++)
1014 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1016 if (chopUpdateTerrainRecycling(&mGround) == 1)
1017 /* mirror the sky if we've changed the ground */
1018 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1020 iLevelMode = LEVEL_MODE_NORMAL;
1021 if (iLevelMode == LEVEL_MODE_NORMAL)
1022 /* make it a bit more exciting, cause it's easy terrain... */
1023 iPlayerSpeedX *= 2;
1026 /* this is the plugin entry point */
1027 enum plugin_status plugin_start(const void* parameter)
1029 (void)parameter;
1030 int ret;
1032 rb->lcd_setfont(FONT_SYSFIXED);
1033 #if LCD_DEPTH > 1
1034 rb->lcd_set_backdrop(NULL);
1035 #endif
1036 #ifdef HAVE_LCD_COLOR
1037 rb->lcd_set_background(LCD_BLACK);
1038 rb->lcd_set_foreground(LCD_WHITE);
1039 #endif
1041 /* Turn off backlight timeout */
1042 backlight_force_on(); /* backlight control in lib/helper.c */
1044 rb->srand( *rb->current_tick );
1046 configfile_load(CFG_FILE, config, 1, 0);
1048 chopper_load(true);
1049 ret = chopGameLoop();
1051 configfile_save(CFG_FILE, config, 1, 0);
1053 rb->lcd_setfont(FONT_UI);
1054 /* Turn on backlight timeout (revert to settings) */
1055 backlight_use_settings(); /* backlight control in lib/helper.c */
1057 return ret;