Consolidate some #ifdef code.
[kugel-rb.git] / apps / plugins / chopper.c
blob1ff0a20559b0696664b45c72a3eb53ed737c7a14
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 || \
138 CONFIG_KEYPAD == ONDAVX777_PAD || \
139 CONFIG_KEYPAD == MROBE500_PAD
140 #define QUIT BUTTON_POWER
142 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
143 #define QUIT BUTTON_LEFT
144 #define ACTION BUTTON_RIGHT
145 #define ACTIONTEXT "RIGHT"
147 #else
148 #error No keymap defined!
149 #endif
151 #ifdef HAVE_TOUCHSCREEN
152 #ifndef QUIT
153 #define QUIT BUTTON_TOPLEFT
154 #endif
155 #ifndef ACTION
156 #define ACTION BUTTON_BOTTOMLEFT
157 #endif
158 #ifndef ACTION2
159 #define ACTION2 BUTTON_BOTTOMRIGHT
160 #endif
161 #ifndef ACTIONTEXT
162 #define ACTIONTEXT "BOTTOMRIGHT"
163 #endif
164 #endif
166 #define NUMBER_OF_BLOCKS 8
167 #define NUMBER_OF_PARTICLES 3
168 #define MAX_TERRAIN_NODES 15
170 #define LEVEL_MODE_NORMAL 0
171 #define LEVEL_MODE_STEEP 1
173 #if LCD_HEIGHT <= 64
174 #define CYCLES 100
175 static inline int SCALE(int x)
177 return x == 1 ? x : x >> 1;
179 #define SIZE 2
180 #else
181 #define CYCLES 60
182 #define SCALE(x) (x)
183 #define SIZE 1
184 #endif
186 /* in 10 milisecond (ticks) */
187 #define CYCLETIME ((CYCLES*HZ)/1000)
189 /*Chopper's local variables to track the terrain position etc*/
190 static int chopCounter;
191 static int iRotorOffset;
192 static int iScreenX;
193 static int iScreenY;
194 static int iPlayerPosX;
195 static int iPlayerPosY;
196 static int iCameraPosX;
197 static int iPlayerSpeedX;
198 static int iPlayerSpeedY;
199 static int iLastBlockPlacedPosX;
200 static int iGravityTimerCountdown;
201 static int iPlayerAlive;
202 static int iLevelMode;
203 static int blockh,blockw;
204 static int highscore;
205 static int score;
207 #define CFG_FILE "chopper.cfg"
208 #define MAX_POINTS 50000
209 static struct configdata config[] =
211 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
214 struct CBlock
216 int iWorldX;
217 int iWorldY;
219 int iSizeX;
220 int iSizeY;
222 int bIsActive;
225 struct CParticle
227 int iWorldX;
228 int iWorldY;
230 int iSpeedX;
231 int iSpeedY;
233 int bIsActive;
236 struct CTerrainNode
238 int x;
239 int y;
242 struct CTerrain
244 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
245 int iNodesCount;
246 int iLastNodePlacedPosX;
249 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
250 struct CParticle mParticles[NUMBER_OF_PARTICLES];
252 struct CTerrain mGround;
253 struct CTerrain mRoof;
255 /*Function declarations*/
256 static void chopDrawParticle(struct CParticle *mParticle);
257 static void chopDrawBlock(struct CBlock *mBlock);
258 static void chopRenderTerrain(struct CTerrain *ter);
259 void chopper_load(bool newgame);
260 void cleanup_chopper(void);
262 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
265 #if LCD_DEPTH > 2
266 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
267 #elif LCD_DEPTH == 2
268 rb->lcd_set_foreground(LCD_DARKGRAY);
269 #endif
270 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
271 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
273 #if LCD_DEPTH > 2
274 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
275 #elif LCD_DEPTH == 2
276 rb->lcd_set_foreground(LCD_DARKGRAY);
277 #endif
278 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
279 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
281 #if LCD_DEPTH > 2
282 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
283 #elif LCD_DEPTH == 2
284 rb->lcd_set_foreground(LCD_BLACK);
285 #endif
286 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
287 SCALE(y-iRotorOffset));
289 #if LCD_DEPTH > 2
290 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
291 #elif LCD_DEPTH == 2
292 rb->lcd_set_foreground(LCD_BLACK);
293 #endif
294 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
298 static void chopClearTerrain(struct CTerrain *ter)
300 ter->iNodesCount = 0;
304 int iR(int low,int high)
306 return low+rb->rand()%(high-low+1);
309 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
310 int xOffset,int yOffset)
312 int i=0;
314 while(i < src->iNodesCount)
316 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
317 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
319 i++;
322 dest->iNodesCount = src->iNodesCount;
323 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
327 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
329 int i=0;
331 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
333 /* DEBUGF("ERROR: Not enough nodes!\n"); */
334 return;
337 ter->iNodesCount++;
339 i = ter->iNodesCount - 1;
341 ter->mNodes[i].x = x;
342 ter->mNodes[i].y= y;
344 ter->iLastNodePlacedPosX = x;
348 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
350 int i=nodeIndex;
352 while( i < ter->iNodesCount )
354 ter->mNodes[i - 1] = ter->mNodes[i];
355 i++;
358 ter->iNodesCount--;
363 int chopUpdateTerrainRecycling(struct CTerrain *ter)
365 int i=1;
366 int ret = 0;
367 int iNewNodePos,g,v;
368 while(i < ter->iNodesCount)
371 if( iCameraPosX > ter->mNodes[i].x)
374 chopTerrainNodeDeleteAndShift(ter,i);
376 iNewNodePos = ter->iLastNodePlacedPosX + 50;
377 g = iScreenY - 10;
379 v = 3*iPlayerSpeedX;
380 if(v>50)
381 v=50;
382 if(iLevelMode == LEVEL_MODE_STEEP)
383 v*=5;
385 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
386 ret=1;
390 i++;
394 return 1;
397 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
400 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
401 float c,d;
403 int i=0;
404 for(i=1;i<MAX_TERRAIN_NODES;i++)
406 if(ter->mNodes[i].x > pX)
408 iNodeIndexOne = i - 1;
409 break;
414 iNodeIndexTwo = iNodeIndexOne + 1;
415 terY1 = ter->mNodes[iNodeIndexOne].y;
416 terY2 = ter->mNodes[iNodeIndexTwo].y;
418 terX1 = 0;
419 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
421 pX-= ter->mNodes[iNodeIndexOne].x;
423 a = terY2 - terY1;
424 b = terX2;
425 c = pX;
426 d = (c/b) * a;
428 h = d + terY1;
430 return h;
434 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
436 int h = chopTerrainHeightAtPoint(ter, pX);
438 if(iTestType == 0)
439 return (pY > h);
440 else
441 return (pY < h);
444 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
446 int i=0;
448 if(indexOverride < 0)
450 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
451 i++;
452 if(i==NUMBER_OF_BLOCKS)
454 DEBUGF("No blocks!\n");
455 return;
458 else
459 i = indexOverride;
461 mBlocks[i].bIsActive = 1;
462 mBlocks[i].iWorldX = x;
463 mBlocks[i].iWorldY = y;
464 mBlocks[i].iSizeX = sx;
465 mBlocks[i].iSizeY = sy;
467 iLastBlockPlacedPosX = x;
470 static void chopAddParticle(int x,int y,int sx,int sy)
472 int i=0;
474 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
475 i++;
477 if(i==NUMBER_OF_PARTICLES)
478 return;
480 mParticles[i].bIsActive = 1;
481 mParticles[i].iWorldX = x;
482 mParticles[i].iWorldY = y;
483 mParticles[i].iSpeedX = sx;
484 mParticles[i].iSpeedY = sy;
487 static void chopGenerateBlockIfNeeded(void)
489 int i=0;
490 int DistSpeedX = iPlayerSpeedX * 5;
491 if(DistSpeedX<200) DistSpeedX = 200;
493 while(i < NUMBER_OF_BLOCKS)
495 if(!mBlocks[i].bIsActive)
497 int iX,iY,sX,sY;
499 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
500 sX = blockw;
502 iY = iR(0,iScreenY);
503 sY = blockh + iR(1,blockh/3);
505 chopAddBlock(iX,iY,sX,sY,i);
508 i++;
513 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
515 int px = iPlayerPosX;
516 int py = iPlayerPosY;
518 int x = mBlock->iWorldX-17;
519 int y = mBlock->iWorldY-11;
521 int x2 = x + mBlock->iSizeX+17;
522 int y2 = y + mBlock->iSizeY+11;
524 if(px>x && px<x2)
526 if(py>y && py<y2)
528 return 1;
532 return 0;
535 static int chopBlockOffscreen(struct CBlock *mBlock)
537 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
538 return 1;
539 else
540 return 0;
543 static int chopParticleOffscreen(struct CParticle *mParticle)
545 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
546 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
547 iScreenX)
549 return 1;
551 else
552 return 0;
555 static void chopKillPlayer(void)
557 int i, button;
559 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
560 mParticles[i].bIsActive = 0;
561 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
562 iR(-2,2), iR(-2,2));
565 iPlayerAlive--;
567 if (iPlayerAlive == 0) {
568 rb->lcd_set_drawmode(DRMODE_FG);
569 #if LCD_DEPTH >= 2
570 rb->lcd_set_foreground(LCD_LIGHTGRAY);
571 #endif
572 rb->splash(HZ, "Game Over");
574 if (score > highscore) {
575 char scoretext[30];
576 highscore = score;
577 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
578 highscore);
579 rb->splash(HZ*2, scoretext);
582 rb->splash(HZ/4, "Press " ACTIONTEXT " to continue");
583 rb->lcd_update();
585 rb->lcd_set_drawmode(DRMODE_SOLID);
587 while (true) {
588 button = rb->button_get(true);
589 if (button == ACTION
590 #ifdef ACTION2
591 || button == ACTION2
592 #endif
594 while (true) {
595 button = rb->button_get(true);
596 if (button == (ACTION | BUTTON_REL)
597 #ifdef ACTION2
598 || button == (ACTION2 | BUTTON_REL)
599 #endif
601 chopper_load(true);
602 return;
608 } else
609 chopper_load(false);
613 static void chopDrawTheWorld(void)
615 int i=0;
617 while(i < NUMBER_OF_BLOCKS)
619 if(mBlocks[i].bIsActive)
621 if(chopBlockOffscreen(&mBlocks[i]) == 1)
622 mBlocks[i].bIsActive = 0;
623 else
624 chopDrawBlock(&mBlocks[i]);
627 i++;
630 i=0;
632 while(i < NUMBER_OF_PARTICLES)
634 if(mParticles[i].bIsActive)
636 if(chopParticleOffscreen(&mParticles[i]) == 1)
637 mParticles[i].bIsActive = 0;
638 else
639 chopDrawParticle(&mParticles[i]);
642 i++;
645 chopRenderTerrain(&mGround);
646 chopRenderTerrain(&mRoof);
650 static void chopDrawParticle(struct CParticle *mParticle)
653 int iPosX = (mParticle->iWorldX - iCameraPosX);
654 int iPosY = (mParticle->iWorldY);
655 #if LCD_DEPTH > 2
656 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
657 #elif LCD_DEPTH == 2
658 rb->lcd_set_foreground(LCD_LIGHTGRAY);
659 #endif
660 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
664 static void chopDrawScene(void)
666 char s[30];
667 int w;
668 #if LCD_DEPTH > 2
669 rb->lcd_set_background(LCD_BLACK);
670 #elif LCD_DEPTH == 2
671 rb->lcd_set_background(LCD_WHITE);
672 #endif
673 chopDrawTheWorld();
674 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
676 score = -20 + iPlayerPosX/3;
678 #if LCD_DEPTH == 1
679 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
680 #else
681 rb->lcd_set_drawmode(DRMODE_FG);
682 #endif
684 #if LCD_DEPTH > 2
685 rb->lcd_set_foreground(LCD_BLACK);
686 #elif LCD_DEPTH == 2
687 rb->lcd_set_foreground(LCD_WHITE);
688 #endif
690 #if LCD_WIDTH <= 128
691 rb->snprintf(s, sizeof(s), "Dist: %d", score);
692 #else
693 rb->snprintf(s, sizeof(s), "Distance: %d", score);
694 #endif
695 rb->lcd_getstringsize(s, &w, NULL);
696 rb->lcd_putsxy(2, 2, s);
697 if (score < highscore)
699 int w2;
700 #if LCD_WIDTH <= 128
701 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
702 #else
703 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
704 #endif
705 rb->lcd_getstringsize(s, &w2, NULL);
706 if (LCD_WIDTH - 2 - w2 > w + 2)
707 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
709 rb->lcd_set_drawmode(DRMODE_SOLID);
711 rb->lcd_update();
714 static bool _ingame;
715 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
717 if(action == ACTION_REQUEST_MENUITEM
718 && !_ingame && ((intptr_t)this_item)==0)
719 return ACTION_EXIT_MENUITEM;
720 return action;
722 static int chopMenu(int menunum)
724 int result = 0;
725 int res = 0;
726 bool menu_quit = false;
728 static const struct opt_items levels[2] = {
729 { "Normal", -1 },
730 { "Steep", -1 },
733 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
734 "Resume Game","Start New Game",
735 "Level","Playback Control","Quit");
736 _ingame = (menunum!=0);
738 #ifdef HAVE_LCD_COLOR
739 rb->lcd_set_foreground(LCD_WHITE);
740 rb->lcd_set_background(LCD_BLACK);
741 #elif LCD_DEPTH == 2
742 rb->lcd_set_foreground(LCD_BLACK);
743 rb->lcd_set_background(LCD_WHITE);
744 #endif
746 rb->lcd_clear_display();
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 rb->lcd_clear_display();
816 /* Redraw the main window: */
817 chopDrawScene();
820 iGravityTimerCountdown--;
822 if(iGravityTimerCountdown <= 0)
824 iGravityTimerCountdown = 3;
825 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
828 if(iLevelMode == LEVEL_MODE_NORMAL)
829 chopGenerateBlockIfNeeded();
832 move_button=rb->button_status();
833 if (rb->button_get(false) == QUIT) {
834 ret = chopMenu(1);
835 if (ret != -1)
836 return PLUGIN_OK;
837 bdelay = 0;
838 last_button = BUTTON_NONE;
839 move_button = BUTTON_NONE;
842 switch (move_button) {
843 case ACTION:
844 #ifdef ACTION2
845 case ACTION2:
846 #endif
847 if (last_button != ACTION
848 #ifdef ACTION2
849 && last_button != ACTION2
850 #endif
852 bdelay = -2;
853 if (bdelay == 0)
854 iPlayerSpeedY = -3;
855 break;
857 default:
858 if (last_button == ACTION
859 #ifdef ACTION2
860 || last_button == ACTION2
861 #endif
863 bdelay = 3;
864 if (bdelay == 0)
865 iPlayerSpeedY = 4;
867 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
868 return PLUGIN_USB_CONNECTED;
869 break;
871 last_button = move_button;
873 if (bdelay < 0) {
874 iPlayerSpeedY = bdelay;
875 bdelay++;
876 } else if (bdelay > 0) {
877 iPlayerSpeedY = bdelay;
878 bdelay--;
881 iCameraPosX = iPlayerPosX - 25;
882 iPlayerPosX += iPlayerSpeedX;
883 iPlayerPosY += iPlayerSpeedY;
885 chopCounter++;
886 /* increase speed as we go along */
887 if (chopCounter == 100){
888 iPlayerSpeedX++;
889 chopCounter=0;
892 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
893 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
894 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
896 chopKillPlayer();
897 chopDrawScene();
898 ret = chopMenu(0);
899 if (ret != -1)
900 return ret;
903 for (i=0; i < NUMBER_OF_BLOCKS; i++)
904 if(mBlocks[i].bIsActive == 1)
905 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
906 chopKillPlayer();
907 chopDrawScene();
908 ret = chopMenu(0);
909 if (ret != -1)
910 return ret;
913 if (TIME_BEFORE(*rb->current_tick, end))
914 rb->sleep(end - *rb->current_tick); /* wait until time is over */
915 else
916 rb->yield();
919 return PLUGIN_OK;
922 static void chopDrawBlock(struct CBlock *mBlock)
924 int iPosX = (mBlock->iWorldX - iCameraPosX);
925 int iPosY = (mBlock->iWorldY);
926 #if LCD_DEPTH > 2
927 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
928 #elif LCD_DEPTH == 2
929 rb->lcd_set_foreground(LCD_BLACK);
930 #endif
931 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
932 SCALE(mBlock->iSizeY));
936 static void chopRenderTerrain(struct CTerrain *ter)
939 int i=1;
941 int oldx=0;
943 int ay=0;
944 if(ter->mNodes[0].y < (LCD_HEIGHT*SIZE)/2)
945 ay=0;
946 else
947 ay=(LCD_HEIGHT*SIZE);
949 while(i < ter->iNodesCount && oldx < iScreenX)
952 int x = ter->mNodes[i-1].x - iCameraPosX;
953 int y = ter->mNodes[i-1].y;
955 int x2 = ter->mNodes[i].x - iCameraPosX;
956 int y2 = ter->mNodes[i].y;
957 #if LCD_DEPTH > 2
958 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
959 #elif LCD_DEPTH == 2
960 rb->lcd_set_foreground(LCD_DARKGRAY);
961 #endif
963 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
965 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
966 SCALE(x2), SCALE(ay));
967 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x2), SCALE(y2),
968 SCALE(x2), SCALE(ay));
970 if (ay == 0)
971 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
972 SCALE(x2), SCALE(y2 / 2));
973 else
974 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
975 SCALE(x2), SCALE((LCD_HEIGHT*SIZE) -
976 ((LCD_HEIGHT*SIZE) - y2) / 2));
978 oldx = x;
979 i++;
985 void chopper_load(bool newgame)
988 int i;
989 int g;
991 if (newgame) {
992 iScreenX = LCD_WIDTH * SIZE;
993 iScreenY = LCD_HEIGHT * SIZE;
994 blockh = iScreenY / 5;
995 blockw = iScreenX / 20;
996 iPlayerAlive = 1;
997 score = 0;
999 iRotorOffset = 0;
1000 iPlayerPosX = 60;
1001 iPlayerPosY = (iScreenY * 4) / 10;
1002 iLastBlockPlacedPosX = 0;
1003 iGravityTimerCountdown = 2;
1004 chopCounter = 0;
1005 iPlayerSpeedX = 3;
1006 iPlayerSpeedY = 0;
1007 iCameraPosX = 30;
1009 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1010 mParticles[i].bIsActive = 0;
1012 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1013 mBlocks[i].bIsActive = 0;
1015 g = iScreenY - 10;
1016 chopClearTerrain(&mGround);
1018 for (i=0; i < MAX_TERRAIN_NODES; i++)
1019 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1021 if (chopUpdateTerrainRecycling(&mGround) == 1)
1022 /* mirror the sky if we've changed the ground */
1023 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1025 if (iLevelMode == LEVEL_MODE_NORMAL)
1026 /* make it a bit more exciting, cause it's easy terrain... */
1027 iPlayerSpeedX *= 2;
1030 /* this is the plugin entry point */
1031 enum plugin_status plugin_start(const void* parameter)
1033 (void)parameter;
1034 int ret;
1036 rb->lcd_setfont(FONT_SYSFIXED);
1037 #if LCD_DEPTH > 1
1038 rb->lcd_set_backdrop(NULL);
1039 #endif
1040 #ifdef HAVE_LCD_COLOR
1041 rb->lcd_set_background(LCD_BLACK);
1042 rb->lcd_set_foreground(LCD_WHITE);
1043 #endif
1045 /* Turn off backlight timeout */
1046 backlight_force_on(); /* backlight control in lib/helper.c */
1048 rb->srand( *rb->current_tick );
1050 configfile_load(CFG_FILE, config, 1, 0);
1052 chopper_load(true);
1053 ret = chopGameLoop();
1055 configfile_save(CFG_FILE, config, 1, 0);
1057 rb->lcd_setfont(FONT_UI);
1058 /* Turn on backlight timeout (revert to settings) */
1059 backlight_use_settings(); /* backlight control in lib/helper.c */
1061 return ret;