Fix red in bootloaders
[maemo-rb.git] / apps / plugins / chopper.c
blob54a1ae0bfe38fc92b283ec01633118eb3f7d99fd
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 #else
141 #error No keymap defined!
142 #endif
144 #ifdef HAVE_TOUCHSCREEN
145 #ifndef QUIT
146 #define QUIT BUTTON_TOPLEFT
147 #endif
148 #ifndef ACTION
149 #define ACTION BUTTON_BOTTOMLEFT
150 #endif
151 #ifndef ACTION2
152 #define ACTION2 BUTTON_BOTTOMRIGHT
153 #endif
154 #ifndef ACTIONTEXT
155 #define ACTIONTEXT "BOTTOMRIGHT"
156 #endif
157 #endif
159 #define NUMBER_OF_BLOCKS 8
160 #define NUMBER_OF_PARTICLES 3
161 #define MAX_TERRAIN_NODES 15
163 #define LEVEL_MODE_NORMAL 0
164 #define LEVEL_MODE_STEEP 1
166 #if LCD_HEIGHT <= 64
167 #define CYCLETIME 100
168 #define SCALE(x) ((x)==1 ? (x) : ((x) >> 1))
169 #define SIZE 2
170 #else
171 #define CYCLETIME 60
172 #define SCALE(x) (x)
173 #define SIZE 1
174 #endif
176 /*Chopper's local variables to track the terrain position etc*/
177 static int chopCounter;
178 static int iRotorOffset;
179 static int iScreenX;
180 static int iScreenY;
181 static int iPlayerPosX;
182 static int iPlayerPosY;
183 static int iCameraPosX;
184 static int iPlayerSpeedX;
185 static int iPlayerSpeedY;
186 static int iLastBlockPlacedPosX;
187 static int iGravityTimerCountdown;
188 static int iPlayerAlive;
189 static int iLevelMode;
190 static int blockh,blockw;
191 static int highscore;
192 static int score;
194 #define CFG_FILE "chopper.cfg"
195 #define MAX_POINTS 50000
196 static struct configdata config[] =
198 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
201 struct CBlock
203 int iWorldX;
204 int iWorldY;
206 int iSizeX;
207 int iSizeY;
209 int bIsActive;
212 struct CParticle
214 int iWorldX;
215 int iWorldY;
217 int iSpeedX;
218 int iSpeedY;
220 int bIsActive;
223 struct CTerrainNode
225 int x;
226 int y;
229 struct CTerrain
231 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
232 int iNodesCount;
233 int iLastNodePlacedPosX;
236 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
237 struct CParticle mParticles[NUMBER_OF_PARTICLES];
239 struct CTerrain mGround;
240 struct CTerrain mRoof;
242 /*Function declarations*/
243 static void chopDrawParticle(struct CParticle *mParticle);
244 static void chopDrawBlock(struct CBlock *mBlock);
245 static void chopRenderTerrain(struct CTerrain *ter);
246 void chopper_load(bool newgame);
247 void cleanup_chopper(void);
249 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
252 #if LCD_DEPTH > 2
253 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
254 #elif LCD_DEPTH == 2
255 rb->lcd_set_foreground(LCD_DARKGRAY);
256 #endif
257 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
258 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
260 #if LCD_DEPTH > 2
261 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
262 #elif LCD_DEPTH == 2
263 rb->lcd_set_foreground(LCD_DARKGRAY);
264 #endif
265 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
266 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
268 #if LCD_DEPTH > 2
269 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
270 #elif LCD_DEPTH == 2
271 rb->lcd_set_foreground(LCD_BLACK);
272 #endif
273 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
274 SCALE(y-iRotorOffset));
276 #if LCD_DEPTH > 2
277 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
278 #elif LCD_DEPTH == 2
279 rb->lcd_set_foreground(LCD_BLACK);
280 #endif
281 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
285 static void chopClearTerrain(struct CTerrain *ter)
287 ter->iNodesCount = 0;
291 int iR(int low,int high)
293 return low+rb->rand()%(high-low+1);
296 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
297 int xOffset,int yOffset)
299 int i=0;
301 while(i < src->iNodesCount)
303 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
304 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
306 i++;
309 dest->iNodesCount = src->iNodesCount;
310 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
314 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
316 int i=0;
318 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
320 /* DEBUGF("ERROR: Not enough nodes!\n"); */
321 return;
324 ter->iNodesCount++;
326 i = ter->iNodesCount - 1;
328 ter->mNodes[i].x = x;
329 ter->mNodes[i].y= y;
331 ter->iLastNodePlacedPosX = x;
335 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
337 int i=nodeIndex;
339 while( i < ter->iNodesCount )
341 ter->mNodes[i - 1] = ter->mNodes[i];
342 i++;
345 ter->iNodesCount--;
350 int chopUpdateTerrainRecycling(struct CTerrain *ter)
352 int i=1;
353 int ret = 0;
354 int iNewNodePos,g,v;
355 while(i < ter->iNodesCount)
358 if( iCameraPosX > ter->mNodes[i].x)
361 chopTerrainNodeDeleteAndShift(ter,i);
363 iNewNodePos = ter->iLastNodePlacedPosX + 50;
364 g = iScreenY - 10;
366 v = 3*iPlayerSpeedX;
367 if(v>50)
368 v=50;
369 if(iLevelMode == LEVEL_MODE_STEEP)
370 v*=5;
372 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
373 ret=1;
377 i++;
381 return 1;
384 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
387 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
388 float c,d;
390 int i=0;
391 for(i=1;i<MAX_TERRAIN_NODES;i++)
393 if(ter->mNodes[i].x > pX)
395 iNodeIndexOne = i - 1;
396 break;
401 iNodeIndexTwo = iNodeIndexOne + 1;
402 terY1 = ter->mNodes[iNodeIndexOne].y;
403 terY2 = ter->mNodes[iNodeIndexTwo].y;
405 terX1 = 0;
406 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
408 pX-= ter->mNodes[iNodeIndexOne].x;
410 a = terY2 - terY1;
411 b = terX2;
412 c = pX;
413 d = (c/b) * a;
415 h = d + terY1;
417 return h;
421 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
423 int h = chopTerrainHeightAtPoint(ter, pX);
425 if(iTestType == 0)
426 return (pY > h);
427 else
428 return (pY < h);
431 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
433 int i=0;
435 if(indexOverride < 0)
437 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
438 i++;
439 if(i==NUMBER_OF_BLOCKS)
441 DEBUGF("No blocks!\n");
442 return;
445 else
446 i = indexOverride;
448 mBlocks[i].bIsActive = 1;
449 mBlocks[i].iWorldX = x;
450 mBlocks[i].iWorldY = y;
451 mBlocks[i].iSizeX = sx;
452 mBlocks[i].iSizeY = sy;
454 iLastBlockPlacedPosX = x;
457 static void chopAddParticle(int x,int y,int sx,int sy)
459 int i=0;
461 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
462 i++;
464 if(i==NUMBER_OF_PARTICLES)
465 return;
467 mParticles[i].bIsActive = 1;
468 mParticles[i].iWorldX = x;
469 mParticles[i].iWorldY = y;
470 mParticles[i].iSpeedX = sx;
471 mParticles[i].iSpeedY = sy;
474 static void chopGenerateBlockIfNeeded(void)
476 int i=0;
477 int DistSpeedX = iPlayerSpeedX * 5;
478 if(DistSpeedX<200) DistSpeedX = 200;
480 while(i < NUMBER_OF_BLOCKS)
482 if(!mBlocks[i].bIsActive)
484 int iX,iY,sX,sY;
486 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
487 sX = blockw;
489 iY = iR(0,iScreenY);
490 sY = blockh + iR(1,blockh/3);
492 chopAddBlock(iX,iY,sX,sY,i);
495 i++;
500 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
502 int px = iPlayerPosX;
503 int py = iPlayerPosY;
505 int x = mBlock->iWorldX-17;
506 int y = mBlock->iWorldY-11;
508 int x2 = x + mBlock->iSizeX+17;
509 int y2 = y + mBlock->iSizeY+11;
511 if(px>x && px<x2)
513 if(py>y && py<y2)
515 return 1;
519 return 0;
522 static int chopBlockOffscreen(struct CBlock *mBlock)
524 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
525 return 1;
526 else
527 return 0;
530 static int chopParticleOffscreen(struct CParticle *mParticle)
532 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
533 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
534 iScreenX)
536 return 1;
538 else
539 return 0;
542 static void chopKillPlayer(void)
544 int i, button;
546 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
547 mParticles[i].bIsActive = 0;
548 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
549 iR(-2,2), iR(-2,2));
552 iPlayerAlive--;
554 if (iPlayerAlive == 0) {
555 rb->lcd_set_drawmode(DRMODE_FG);
556 #if LCD_DEPTH >= 2
557 rb->lcd_set_foreground(LCD_LIGHTGRAY);
558 #endif
559 rb->splash(HZ, "Game Over");
561 if (score > highscore) {
562 char scoretext[30];
563 highscore = score;
564 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
565 highscore);
566 rb->splash(HZ*2, scoretext);
569 rb->splash(HZ/4, "Press " ACTIONTEXT " to continue");
570 rb->lcd_update();
572 rb->lcd_set_drawmode(DRMODE_SOLID);
574 while (true) {
575 button = rb->button_get(true);
576 if (button == ACTION
577 #ifdef ACTION2
578 || button == ACTION2
579 #endif
581 while (true) {
582 button = rb->button_get(true);
583 if (button == (ACTION | BUTTON_REL)
584 #ifdef ACTION2
585 || button == (ACTION2 | BUTTON_REL)
586 #endif
588 chopper_load(true);
589 return;
595 } else
596 chopper_load(false);
600 static void chopDrawTheWorld(void)
602 int i=0;
604 while(i < NUMBER_OF_BLOCKS)
606 if(mBlocks[i].bIsActive)
608 if(chopBlockOffscreen(&mBlocks[i]) == 1)
609 mBlocks[i].bIsActive = 0;
610 else
611 chopDrawBlock(&mBlocks[i]);
614 i++;
617 i=0;
619 while(i < NUMBER_OF_PARTICLES)
621 if(mParticles[i].bIsActive)
623 if(chopParticleOffscreen(&mParticles[i]) == 1)
624 mParticles[i].bIsActive = 0;
625 else
626 chopDrawParticle(&mParticles[i]);
629 i++;
632 chopRenderTerrain(&mGround);
633 chopRenderTerrain(&mRoof);
637 static void chopDrawParticle(struct CParticle *mParticle)
640 int iPosX = (mParticle->iWorldX - iCameraPosX);
641 int iPosY = (mParticle->iWorldY);
642 #if LCD_DEPTH > 2
643 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
644 #elif LCD_DEPTH == 2
645 rb->lcd_set_foreground(LCD_LIGHTGRAY);
646 #endif
647 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
651 static void chopDrawScene(void)
653 char s[30];
654 int w;
655 #if LCD_DEPTH > 2
656 rb->lcd_set_background(LCD_BLACK);
657 #elif LCD_DEPTH == 2
658 rb->lcd_set_background(LCD_WHITE);
659 #endif
660 rb->lcd_clear_display();
662 chopDrawTheWorld();
663 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
665 score = -20 + iPlayerPosX/3;
667 #if LCD_DEPTH == 1
668 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
669 #else
670 rb->lcd_set_drawmode(DRMODE_FG);
671 #endif
673 #if LCD_DEPTH > 2
674 rb->lcd_set_foreground(LCD_BLACK);
675 #elif LCD_DEPTH == 2
676 rb->lcd_set_foreground(LCD_WHITE);
677 #endif
679 #if LCD_WIDTH <= 128
680 rb->snprintf(s, sizeof(s), "Dist: %d", score);
681 #else
682 rb->snprintf(s, sizeof(s), "Distance: %d", score);
683 #endif
684 rb->lcd_getstringsize(s, &w, NULL);
685 rb->lcd_putsxy(2, 2, s);
686 if (score < highscore)
688 int w2;
689 #if LCD_WIDTH <= 128
690 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
691 #else
692 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
693 #endif
694 rb->lcd_getstringsize(s, &w2, NULL);
695 if (LCD_WIDTH - 2 - w2 > w + 2)
696 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
698 rb->lcd_set_drawmode(DRMODE_SOLID);
700 rb->lcd_update();
703 static bool _ingame;
704 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
706 if(action == ACTION_REQUEST_MENUITEM
707 && !_ingame && ((intptr_t)this_item)==0)
708 return ACTION_EXIT_MENUITEM;
709 return action;
711 static int chopMenu(int menunum)
713 int result = 0;
714 int res = 0;
715 bool menu_quit = false;
717 static const struct opt_items levels[2] = {
718 { "Normal", -1 },
719 { "Steep", -1 },
722 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
723 "Resume Game","Start New Game",
724 "Level","Playback Control","Quit");
725 _ingame = (menunum!=0);
727 #ifdef HAVE_LCD_COLOR
728 rb->lcd_set_foreground(LCD_WHITE);
729 rb->lcd_set_background(LCD_BLACK);
730 #elif LCD_DEPTH == 2
731 rb->lcd_set_foreground(LCD_BLACK);
732 rb->lcd_set_background(LCD_WHITE);
733 #endif
735 rb->lcd_clear_display();
737 while (!menu_quit) {
738 switch(rb->do_menu(&menu, &result, NULL, false))
740 case 0: /* Resume Game */
741 menu_quit=true;
742 res = -1;
743 break;
744 case 1: /* Start New Game */
745 menu_quit=true;
746 chopper_load(true);
747 res = -1;
748 break;
749 case 2:
750 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
751 break;
752 case 3:
753 playback_control(NULL);
754 break;
755 case 4:
756 menu_quit=true;
757 res = PLUGIN_OK;
758 break;
759 case MENU_ATTACHED_USB:
760 menu_quit=true;
761 res = PLUGIN_USB_CONNECTED;
762 break;
765 rb->lcd_clear_display();
766 return res;
769 static int chopGameLoop(void)
771 int move_button, ret;
772 bool exit=false;
773 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
775 if (chopUpdateTerrainRecycling(&mGround) == 1)
776 /* mirror the sky if we've changed the ground */
777 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
779 ret = chopMenu(0);
780 if (ret != -1)
781 return PLUGIN_OK;
783 chopDrawScene();
785 while (!exit) {
787 end = *rb->current_tick + (CYCLETIME * HZ) / 1000;
789 if(chopUpdateTerrainRecycling(&mGround) == 1)
790 /* mirror the sky if we've changed the ground */
791 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
793 iRotorOffset = iR(-1,1);
795 /* We need to have this here so particles move when we're dead */
797 for (i=0; i < NUMBER_OF_PARTICLES; i++)
798 if(mParticles[i].bIsActive == 1)
800 mParticles[i].iWorldX += mParticles[i].iSpeedX;
801 mParticles[i].iWorldY += mParticles[i].iSpeedY;
804 /* Redraw the main window: */
805 chopDrawScene();
808 iGravityTimerCountdown--;
810 if(iGravityTimerCountdown <= 0)
812 iGravityTimerCountdown = 3;
813 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
816 if(iLevelMode == LEVEL_MODE_NORMAL)
817 chopGenerateBlockIfNeeded();
820 move_button=rb->button_status();
821 if (rb->button_get(false) == QUIT) {
822 ret = chopMenu(1);
823 if (ret != -1)
824 return PLUGIN_OK;
825 bdelay = 0;
826 last_button = BUTTON_NONE;
827 move_button = BUTTON_NONE;
830 switch (move_button) {
831 case ACTION:
832 #ifdef ACTION2
833 case ACTION2:
834 #endif
835 if (last_button != ACTION
836 #ifdef ACTION2
837 && last_button != ACTION2
838 #endif
840 bdelay = -2;
841 if (bdelay == 0)
842 iPlayerSpeedY = -3;
843 break;
845 default:
846 if (last_button == ACTION
847 #ifdef ACTION2
848 || last_button == ACTION2
849 #endif
851 bdelay = 3;
852 if (bdelay == 0)
853 iPlayerSpeedY = 4;
855 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
856 return PLUGIN_USB_CONNECTED;
857 break;
859 last_button = move_button;
861 if (bdelay < 0) {
862 iPlayerSpeedY = bdelay;
863 bdelay++;
864 } else if (bdelay > 0) {
865 iPlayerSpeedY = bdelay;
866 bdelay--;
869 iCameraPosX = iPlayerPosX - 25;
870 iPlayerPosX += iPlayerSpeedX;
871 iPlayerPosY += iPlayerSpeedY;
873 chopCounter++;
874 /* increase speed as we go along */
875 if (chopCounter == 100){
876 iPlayerSpeedX++;
877 chopCounter=0;
880 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
881 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
882 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
884 chopKillPlayer();
885 chopDrawScene();
886 ret = chopMenu(0);
887 if (ret != -1)
888 return ret;
891 for (i=0; i < NUMBER_OF_BLOCKS; i++)
892 if(mBlocks[i].bIsActive == 1)
893 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
894 chopKillPlayer();
895 chopDrawScene();
896 ret = chopMenu(0);
897 if (ret != -1)
898 return ret;
901 if (end > *rb->current_tick)
902 rb->sleep(end-*rb->current_tick);
903 else
904 rb->yield();
907 return PLUGIN_OK;
910 static void chopDrawBlock(struct CBlock *mBlock)
912 int iPosX = (mBlock->iWorldX - iCameraPosX);
913 int iPosY = (mBlock->iWorldY);
914 #if LCD_DEPTH > 2
915 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
916 #elif LCD_DEPTH == 2
917 rb->lcd_set_foreground(LCD_BLACK);
918 #endif
919 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
920 SCALE(mBlock->iSizeY));
924 static void chopRenderTerrain(struct CTerrain *ter)
927 int i=1;
929 int oldx=0;
931 int ay=0;
932 if(ter->mNodes[0].y < (LCD_HEIGHT*SIZE)/2)
933 ay=0;
934 else
935 ay=(LCD_HEIGHT*SIZE);
937 while(i < ter->iNodesCount && oldx < iScreenX)
940 int x = ter->mNodes[i-1].x - iCameraPosX;
941 int y = ter->mNodes[i-1].y;
943 int x2 = ter->mNodes[i].x - iCameraPosX;
944 int y2 = ter->mNodes[i].y;
945 #if LCD_DEPTH > 2
946 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
947 #elif LCD_DEPTH == 2
948 rb->lcd_set_foreground(LCD_DARKGRAY);
949 #endif
951 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
953 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
954 SCALE(x2), SCALE(ay));
955 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x2), SCALE(y2),
956 SCALE(x2), SCALE(ay));
958 if (ay == 0)
959 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
960 SCALE(x2), SCALE(y2 / 2));
961 else
962 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
963 SCALE(x2), SCALE((LCD_HEIGHT*SIZE) -
964 ((LCD_HEIGHT*SIZE) - y2) / 2));
966 oldx = x;
967 i++;
973 void chopper_load(bool newgame)
976 int i;
977 int g;
979 if (newgame) {
980 iScreenX = LCD_WIDTH * SIZE;
981 iScreenY = LCD_HEIGHT * SIZE;
982 blockh = iScreenY / 5;
983 blockw = iScreenX / 20;
984 iPlayerAlive = 1;
985 score = 0;
987 iRotorOffset = 0;
988 iPlayerPosX = 60;
989 iPlayerPosY = (iScreenY * 4) / 10;
990 iLastBlockPlacedPosX = 0;
991 iGravityTimerCountdown = 2;
992 chopCounter = 0;
993 iPlayerSpeedX = 3;
994 iPlayerSpeedY = 0;
995 iCameraPosX = 30;
997 for (i=0; i < NUMBER_OF_PARTICLES; i++)
998 mParticles[i].bIsActive = 0;
1000 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1001 mBlocks[i].bIsActive = 0;
1003 g = iScreenY - 10;
1004 chopClearTerrain(&mGround);
1006 for (i=0; i < MAX_TERRAIN_NODES; i++)
1007 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1009 if (chopUpdateTerrainRecycling(&mGround) == 1)
1010 /* mirror the sky if we've changed the ground */
1011 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1013 iLevelMode = LEVEL_MODE_NORMAL;
1014 if (iLevelMode == LEVEL_MODE_NORMAL)
1015 /* make it a bit more exciting, cause it's easy terrain... */
1016 iPlayerSpeedX *= 2;
1019 /* this is the plugin entry point */
1020 enum plugin_status plugin_start(const void* parameter)
1022 (void)parameter;
1023 int ret;
1025 rb->lcd_setfont(FONT_SYSFIXED);
1026 #if LCD_DEPTH > 1
1027 rb->lcd_set_backdrop(NULL);
1028 #endif
1029 #ifdef HAVE_LCD_COLOR
1030 rb->lcd_set_background(LCD_BLACK);
1031 rb->lcd_set_foreground(LCD_WHITE);
1032 #endif
1034 /* Turn off backlight timeout */
1035 backlight_force_on(); /* backlight control in lib/helper.c */
1037 rb->srand( *rb->current_tick );
1039 configfile_load(CFG_FILE, config, 1, 0);
1041 chopper_load(true);
1042 ret = chopGameLoop();
1044 configfile_save(CFG_FILE, config, 1, 0);
1046 rb->lcd_setfont(FONT_UI);
1047 /* Turn on backlight timeout (revert to settings) */
1048 backlight_use_settings(); /* backlight control in lib/helper.c */
1050 return ret;