Merge branch 'android-notification' into android-test-plugins
[kugel-rb.git] / apps / plugins / chopper.c
blob97be2c6a071cbc97edcd24855c3d3b81f3780ed1
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 #elif !defined(HAVE_TOUCHSCREEN)
178 #error No keymap defined!
179 #endif
181 #ifdef HAVE_TOUCHSCREEN
182 #ifndef QUIT
183 #define QUIT BUTTON_TOPLEFT
184 #endif
185 #ifndef ACTION
186 #define ACTION BUTTON_BOTTOMLEFT
187 #endif
188 #ifndef ACTION2
189 #define ACTION2 BUTTON_BOTTOMRIGHT
190 #endif
191 #ifndef ACTIONTEXT
192 #define ACTIONTEXT "BOTTOMRIGHT"
193 #endif
194 #endif
196 #define NUMBER_OF_BLOCKS 8
197 #define NUMBER_OF_PARTICLES 3
198 #define MAX_TERRAIN_NODES 15
200 #define LEVEL_MODE_NORMAL 0
201 #define LEVEL_MODE_STEEP 1
203 #if LCD_HEIGHT <= 64
204 #define CYCLES 100
205 static inline int SCALE(int x)
207 return x == 1 ? x : x >> 1;
209 #define SIZE 2
210 #else
211 #define CYCLES 60
212 #define SCALE(x) (x)
213 #define SIZE 1
214 #endif
216 /* in 10 milisecond (ticks) */
217 #define CYCLETIME ((CYCLES*HZ)/1000)
219 /*Chopper's local variables to track the terrain position etc*/
220 static int chopCounter;
221 static int iRotorOffset;
222 static int iScreenX;
223 static int iScreenY;
224 static int iPlayerPosX;
225 static int iPlayerPosY;
226 static int iCameraPosX;
227 static int iPlayerSpeedX;
228 static int iPlayerSpeedY;
229 static int iLastBlockPlacedPosX;
230 static int iGravityTimerCountdown;
231 static int iPlayerAlive;
232 static int iLevelMode, iCurrLevelMode;
233 static int blockh,blockw;
234 static int highscore;
235 static int score;
237 #define CFG_FILE "chopper.cfg"
238 #define MAX_POINTS 50000
239 static struct configdata config[] =
241 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
244 struct CBlock
246 int iWorldX;
247 int iWorldY;
249 int iSizeX;
250 int iSizeY;
252 int bIsActive;
255 struct CParticle
257 int iWorldX;
258 int iWorldY;
260 int iSpeedX;
261 int iSpeedY;
263 int bIsActive;
266 struct CTerrainNode
268 int x;
269 int y;
272 struct CTerrain
274 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
275 int iNodesCount;
276 int iLastNodePlacedPosX;
279 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
280 struct CParticle mParticles[NUMBER_OF_PARTICLES];
282 struct CTerrain mGround;
283 struct CTerrain mRoof;
285 /*Function declarations*/
286 static void chopDrawParticle(struct CParticle *mParticle);
287 static void chopDrawBlock(struct CBlock *mBlock);
288 static void chopRenderTerrain(struct CTerrain *ter, bool isground);
289 void chopper_load(bool newgame);
290 void cleanup_chopper(void);
292 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
295 #if LCD_DEPTH > 2
296 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
297 #elif LCD_DEPTH == 2
298 rb->lcd_set_foreground(LCD_DARKGRAY);
299 #endif
300 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
301 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
303 #if LCD_DEPTH > 2
304 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
305 #elif LCD_DEPTH == 2
306 rb->lcd_set_foreground(LCD_DARKGRAY);
307 #endif
308 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
309 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
311 #if LCD_DEPTH > 2
312 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
313 #elif LCD_DEPTH == 2
314 rb->lcd_set_foreground(LCD_BLACK);
315 #endif
316 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
317 SCALE(y-iRotorOffset));
319 #if LCD_DEPTH > 2
320 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
321 #elif LCD_DEPTH == 2
322 rb->lcd_set_foreground(LCD_BLACK);
323 #endif
324 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
328 static void chopClearTerrain(struct CTerrain *ter)
330 ter->iNodesCount = 0;
334 int iR(int low,int high)
336 return low+rb->rand()%(high-low+1);
339 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
340 int xOffset,int yOffset)
342 int i=0;
344 while(i < src->iNodesCount)
346 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
347 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
349 i++;
352 dest->iNodesCount = src->iNodesCount;
353 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
357 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
359 int i=0;
361 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
363 /* DEBUGF("ERROR: Not enough nodes!\n"); */
364 return;
367 ter->iNodesCount++;
369 i = ter->iNodesCount - 1;
371 ter->mNodes[i].x = x;
372 ter->mNodes[i].y= y;
374 ter->iLastNodePlacedPosX = x;
378 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
380 int i=nodeIndex;
382 while( i < ter->iNodesCount )
384 ter->mNodes[i - 1] = ter->mNodes[i];
385 i++;
388 ter->iNodesCount--;
393 int chopUpdateTerrainRecycling(struct CTerrain *ter)
395 int i=1;
396 int ret = 0;
397 int iNewNodePos,g,v;
398 while(i < ter->iNodesCount)
401 if( iCameraPosX > ter->mNodes[i].x)
404 chopTerrainNodeDeleteAndShift(ter,i);
406 iNewNodePos = ter->iLastNodePlacedPosX + 50;
407 g = iScreenY - 10;
409 v = 3*iPlayerSpeedX;
410 if(v>50)
411 v=50;
412 if(iCurrLevelMode == LEVEL_MODE_STEEP)
413 v*=5;
415 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
416 ret=1;
420 i++;
424 return 1;
427 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
430 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
431 float c,d;
433 int i=0;
434 for(i=1;i<MAX_TERRAIN_NODES;i++)
436 if(ter->mNodes[i].x > pX)
438 iNodeIndexOne = i - 1;
439 break;
444 iNodeIndexTwo = iNodeIndexOne + 1;
445 terY1 = ter->mNodes[iNodeIndexOne].y;
446 terY2 = ter->mNodes[iNodeIndexTwo].y;
448 terX1 = 0;
449 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
451 pX-= ter->mNodes[iNodeIndexOne].x;
453 a = terY2 - terY1;
454 b = terX2;
455 c = pX;
456 d = (c/b) * a;
458 h = d + terY1;
460 return h;
464 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
466 int h = chopTerrainHeightAtPoint(ter, pX);
468 if(iTestType == 0)
469 return (pY > h);
470 else
471 return (pY < h);
474 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
476 int i=0;
478 if(indexOverride < 0)
480 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
481 i++;
482 if(i==NUMBER_OF_BLOCKS)
484 DEBUGF("No blocks!\n");
485 return;
488 else
489 i = indexOverride;
491 mBlocks[i].bIsActive = 1;
492 mBlocks[i].iWorldX = x;
493 mBlocks[i].iWorldY = y;
494 mBlocks[i].iSizeX = sx;
495 mBlocks[i].iSizeY = sy;
497 iLastBlockPlacedPosX = x;
500 static void chopAddParticle(int x,int y,int sx,int sy)
502 int i=0;
504 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
505 i++;
507 if(i==NUMBER_OF_PARTICLES)
508 return;
510 mParticles[i].bIsActive = 1;
511 mParticles[i].iWorldX = x;
512 mParticles[i].iWorldY = y;
513 mParticles[i].iSpeedX = sx;
514 mParticles[i].iSpeedY = sy;
517 static void chopGenerateBlockIfNeeded(void)
519 int i=0;
520 int DistSpeedX = iPlayerSpeedX * 5;
521 if(DistSpeedX<200) DistSpeedX = 200;
523 while(i < NUMBER_OF_BLOCKS)
525 if(!mBlocks[i].bIsActive)
527 int iX,iY,sX,sY;
529 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
530 sX = blockw;
532 iY = iR(0,iScreenY);
533 sY = blockh + iR(1,blockh/3);
535 chopAddBlock(iX,iY,sX,sY,i);
538 i++;
543 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
545 int px = iPlayerPosX;
546 int py = iPlayerPosY;
548 int x = mBlock->iWorldX-17;
549 int y = mBlock->iWorldY-11;
551 int x2 = x + mBlock->iSizeX+17;
552 int y2 = y + mBlock->iSizeY+11;
554 if(px>x && px<x2)
556 if(py>y && py<y2)
558 return 1;
562 return 0;
565 static int chopBlockOffscreen(struct CBlock *mBlock)
567 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
568 return 1;
569 else
570 return 0;
573 static int chopParticleOffscreen(struct CParticle *mParticle)
575 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
576 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
577 iScreenX)
579 return 1;
581 else
582 return 0;
585 static void chopKillPlayer(void)
587 int i;
589 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
590 mParticles[i].bIsActive = 0;
591 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
592 iR(-2,2), iR(-2,2));
595 iPlayerAlive--;
597 if (iPlayerAlive == 0) {
598 rb->splash(HZ, "Game Over");
600 if (score > highscore) {
601 char scoretext[30];
602 highscore = score;
603 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
604 highscore);
605 rb->splash(HZ*2, scoretext);
607 } else
608 chopper_load(false);
611 static void chopDrawTheWorld(void)
613 int i=0;
615 while(i < NUMBER_OF_BLOCKS)
617 if(mBlocks[i].bIsActive)
619 if(chopBlockOffscreen(&mBlocks[i]) == 1)
620 mBlocks[i].bIsActive = 0;
621 else
622 chopDrawBlock(&mBlocks[i]);
625 i++;
628 i=0;
630 while(i < NUMBER_OF_PARTICLES)
632 if(mParticles[i].bIsActive)
634 if(chopParticleOffscreen(&mParticles[i]) == 1)
635 mParticles[i].bIsActive = 0;
636 else
637 chopDrawParticle(&mParticles[i]);
640 i++;
643 chopRenderTerrain(&mGround, true);
644 chopRenderTerrain(&mRoof, false);
648 static void chopDrawParticle(struct CParticle *mParticle)
651 int iPosX = (mParticle->iWorldX - iCameraPosX);
652 int iPosY = (mParticle->iWorldY);
653 #if LCD_DEPTH > 2
654 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
655 #elif LCD_DEPTH == 2
656 rb->lcd_set_foreground(LCD_LIGHTGRAY);
657 #endif
658 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
662 static void chopDrawScene(void)
664 char s[30];
665 int w;
666 #if LCD_DEPTH > 2
667 rb->lcd_set_background(LCD_BLACK);
668 #elif LCD_DEPTH == 2
669 rb->lcd_set_background(LCD_WHITE);
670 #endif
671 rb->lcd_clear_display();
672 chopDrawTheWorld();
673 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
675 score = -20 + iPlayerPosX/3;
677 #if LCD_DEPTH == 1
678 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
679 #else
680 rb->lcd_set_drawmode(DRMODE_FG);
681 #endif
683 #if LCD_DEPTH > 2
684 rb->lcd_set_foreground(LCD_BLACK);
685 #elif LCD_DEPTH == 2
686 rb->lcd_set_foreground(LCD_WHITE);
687 #endif
689 #if LCD_WIDTH <= 128
690 rb->snprintf(s, sizeof(s), "Dist: %d", score);
691 #else
692 rb->snprintf(s, sizeof(s), "Distance: %d", score);
693 #endif
694 rb->lcd_getstringsize(s, &w, NULL);
695 rb->lcd_putsxy(2, 2, s);
696 if (score < highscore)
698 int w2;
699 #if LCD_WIDTH <= 128
700 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
701 #else
702 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
703 #endif
704 rb->lcd_getstringsize(s, &w2, NULL);
705 if (LCD_WIDTH - 2 - w2 > w + 2)
706 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
708 rb->lcd_set_drawmode(DRMODE_SOLID);
710 rb->lcd_update();
713 static bool _ingame;
714 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
716 if(action == ACTION_REQUEST_MENUITEM
717 && !_ingame && ((intptr_t)this_item)==0)
718 return ACTION_EXIT_MENUITEM;
719 return action;
721 static int chopMenu(int menunum)
723 int result = 0;
724 int res = 0;
725 bool menu_quit = false;
727 static const struct opt_items levels[2] = {
728 { "Normal", -1 },
729 { "Steep", -1 },
732 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
733 "Resume Game","Start New Game",
734 "Level","Playback Control","Quit");
735 _ingame = (menunum!=0);
737 #ifdef HAVE_LCD_COLOR
738 rb->lcd_set_foreground(LCD_WHITE);
739 rb->lcd_set_background(LCD_BLACK);
740 #elif LCD_DEPTH == 2
741 rb->lcd_set_foreground(LCD_BLACK);
742 rb->lcd_set_background(LCD_WHITE);
743 #endif
745 rb->lcd_clear_display();
746 rb->button_clear_queue();
748 while (!menu_quit) {
749 switch(rb->do_menu(&menu, &result, NULL, false))
751 case 0: /* Resume Game */
752 menu_quit=true;
753 res = -1;
754 break;
755 case 1: /* Start New Game */
756 menu_quit=true;
757 chopper_load(true);
758 res = -1;
759 break;
760 case 2:
761 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
762 break;
763 case 3:
764 playback_control(NULL);
765 break;
766 case 4:
767 menu_quit=true;
768 res = PLUGIN_OK;
769 break;
770 case MENU_ATTACHED_USB:
771 menu_quit=true;
772 res = PLUGIN_USB_CONNECTED;
773 break;
776 rb->lcd_clear_display();
777 return res;
780 static int chopGameLoop(void)
782 int move_button, ret;
783 bool exit=false;
784 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
786 if (chopUpdateTerrainRecycling(&mGround) == 1)
787 /* mirror the sky if we've changed the ground */
788 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
790 ret = chopMenu(0);
791 if (ret != -1)
792 return PLUGIN_OK;
794 chopDrawScene();
796 while (!exit) {
798 end = *rb->current_tick + CYCLETIME;
800 if(chopUpdateTerrainRecycling(&mGround) == 1)
801 /* mirror the sky if we've changed the ground */
802 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
804 iRotorOffset = iR(-1,1);
806 /* We need to have this here so particles move when we're dead */
808 for (i=0; i < NUMBER_OF_PARTICLES; i++)
809 if(mParticles[i].bIsActive == 1)
811 mParticles[i].iWorldX += mParticles[i].iSpeedX;
812 mParticles[i].iWorldY += mParticles[i].iSpeedY;
815 /* Redraw the main window: */
816 chopDrawScene();
819 iGravityTimerCountdown--;
821 if(iGravityTimerCountdown <= 0)
823 iGravityTimerCountdown = 3;
824 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
827 if(iCurrLevelMode == LEVEL_MODE_NORMAL)
828 chopGenerateBlockIfNeeded();
831 move_button=rb->button_status();
832 if (rb->button_get(false) == QUIT) {
833 ret = chopMenu(1);
834 if (ret != -1)
835 return PLUGIN_OK;
836 bdelay = 0;
837 last_button = BUTTON_NONE;
838 move_button = BUTTON_NONE;
841 switch (move_button) {
842 case ACTION:
843 #ifdef ACTION2
844 case ACTION2:
845 #endif
846 if (last_button != ACTION
847 #ifdef ACTION2
848 && last_button != ACTION2
849 #endif
851 bdelay = -2;
852 if (bdelay == 0)
853 iPlayerSpeedY = -3;
854 break;
856 default:
857 if (last_button == ACTION
858 #ifdef ACTION2
859 || last_button == ACTION2
860 #endif
862 bdelay = 3;
863 if (bdelay == 0)
864 iPlayerSpeedY = 4;
866 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
867 return PLUGIN_USB_CONNECTED;
868 break;
870 last_button = move_button;
872 if (bdelay < 0) {
873 iPlayerSpeedY = bdelay;
874 bdelay++;
875 } else if (bdelay > 0) {
876 iPlayerSpeedY = bdelay;
877 bdelay--;
880 iCameraPosX = iPlayerPosX - 25;
881 iPlayerPosX += iPlayerSpeedX;
882 iPlayerPosY += iPlayerSpeedY;
884 chopCounter++;
885 /* increase speed as we go along */
886 if (chopCounter == 100){
887 iPlayerSpeedX++;
888 chopCounter=0;
891 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
892 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
893 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
895 chopKillPlayer();
896 chopDrawScene();
897 ret = chopMenu(0);
898 if (ret != -1)
899 return ret;
902 for (i=0; i < NUMBER_OF_BLOCKS; i++)
903 if(mBlocks[i].bIsActive == 1)
904 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
905 chopKillPlayer();
906 chopDrawScene();
907 ret = chopMenu(0);
908 if (ret != -1)
909 return ret;
912 if (TIME_BEFORE(*rb->current_tick, end))
913 rb->sleep(end - *rb->current_tick); /* wait until time is over */
914 else
915 rb->yield();
918 return PLUGIN_OK;
921 static void chopDrawBlock(struct CBlock *mBlock)
923 int iPosX = (mBlock->iWorldX - iCameraPosX);
924 int iPosY = (mBlock->iWorldY);
925 #if LCD_DEPTH > 2
926 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
927 #elif LCD_DEPTH == 2
928 rb->lcd_set_foreground(LCD_BLACK);
929 #endif
930 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
931 SCALE(mBlock->iSizeY));
935 static void chopRenderTerrain(struct CTerrain *ter, bool isground)
938 int i = 1;
940 int oldx = 0;
942 while(i < ter->iNodesCount && oldx < iScreenX)
945 int x = ter->mNodes[i-1].x - iCameraPosX;
946 int y = ter->mNodes[i-1].y;
948 int x2 = ter->mNodes[i].x - iCameraPosX;
949 int y2 = ter->mNodes[i].y;
951 int ax, ay;
953 if ((y < y2) != isground)
955 ax = x2;
956 ay = y;
958 else
960 ax = x;
961 ay = y2;
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(ax), SCALE(ay));
974 if (isground)
976 y = ay;
977 y2 = (LCD_HEIGHT*SIZE);
979 else
981 y = 0;
982 y2 = ay;
984 if (y2-y > 0)
985 rb->lcd_fillrect(SCALE(x), SCALE(y), SCALE(x2-x)+1, SCALE(y2-y)+1);
987 oldx = x;
988 i++;
992 void chopper_load(bool newgame)
995 int i;
996 int g;
998 if (newgame) {
999 iScreenX = LCD_WIDTH * SIZE;
1000 iScreenY = LCD_HEIGHT * SIZE;
1001 blockh = iScreenY / 5;
1002 blockw = iScreenX / 20;
1003 iPlayerAlive = 1;
1004 iCurrLevelMode = iLevelMode;
1005 score = 0;
1007 iRotorOffset = 0;
1008 iPlayerPosX = 60;
1009 iPlayerPosY = (iScreenY * 4) / 10;
1010 iLastBlockPlacedPosX = 0;
1011 iGravityTimerCountdown = 2;
1012 chopCounter = 0;
1013 iPlayerSpeedX = 3;
1014 iPlayerSpeedY = 0;
1015 iCameraPosX = 30;
1017 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1018 mParticles[i].bIsActive = 0;
1020 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1021 mBlocks[i].bIsActive = 0;
1023 g = iScreenY - 10;
1024 chopClearTerrain(&mGround);
1026 for (i=0; i < MAX_TERRAIN_NODES; i++)
1027 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1029 if (chopUpdateTerrainRecycling(&mGround) == 1)
1030 /* mirror the sky if we've changed the ground */
1031 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1033 if (iCurrLevelMode == LEVEL_MODE_NORMAL)
1034 /* make it a bit more exciting, cause it's easy terrain... */
1035 iPlayerSpeedX *= 2;
1038 /* this is the plugin entry point */
1039 enum plugin_status plugin_start(const void* parameter)
1041 (void)parameter;
1042 int ret;
1044 rb->lcd_setfont(FONT_SYSFIXED);
1045 #if LCD_DEPTH > 1
1046 rb->lcd_set_backdrop(NULL);
1047 #endif
1048 #ifdef HAVE_LCD_COLOR
1049 rb->lcd_set_background(LCD_BLACK);
1050 rb->lcd_set_foreground(LCD_WHITE);
1051 #endif
1053 /* Turn off backlight timeout */
1054 backlight_force_on(); /* backlight control in lib/helper.c */
1056 rb->srand( *rb->current_tick );
1058 configfile_load(CFG_FILE, config, 1, 0);
1060 chopper_load(true);
1061 ret = chopGameLoop();
1063 configfile_save(CFG_FILE, config, 1, 0);
1065 rb->lcd_setfont(FONT_UI);
1066 /* Turn on backlight timeout (revert to settings) */
1067 backlight_use_settings(); /* backlight control in lib/helper.c */
1069 return ret;