Optimize chopper a bit by making a often used macro an inline function(which means...
[kugel-rb.git] / apps / plugins / chopper.c
blob2c1132aacdb1b0a41b33167e96f9b1ed4d0ef606
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 static inline int SCALE(int x)
170 return x == 1 ? x : x >> 1;
172 #define SIZE 2
173 #else
174 #define CYCLETIME 60
175 #define SCALE(x) (x)
176 #define SIZE 1
177 #endif
179 /*Chopper's local variables to track the terrain position etc*/
180 static int chopCounter;
181 static int iRotorOffset;
182 static int iScreenX;
183 static int iScreenY;
184 static int iPlayerPosX;
185 static int iPlayerPosY;
186 static int iCameraPosX;
187 static int iPlayerSpeedX;
188 static int iPlayerSpeedY;
189 static int iLastBlockPlacedPosX;
190 static int iGravityTimerCountdown;
191 static int iPlayerAlive;
192 static int iLevelMode;
193 static int blockh,blockw;
194 static int highscore;
195 static int score;
197 #define CFG_FILE "chopper.cfg"
198 #define MAX_POINTS 50000
199 static struct configdata config[] =
201 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
204 struct CBlock
206 int iWorldX;
207 int iWorldY;
209 int iSizeX;
210 int iSizeY;
212 int bIsActive;
215 struct CParticle
217 int iWorldX;
218 int iWorldY;
220 int iSpeedX;
221 int iSpeedY;
223 int bIsActive;
226 struct CTerrainNode
228 int x;
229 int y;
232 struct CTerrain
234 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
235 int iNodesCount;
236 int iLastNodePlacedPosX;
239 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
240 struct CParticle mParticles[NUMBER_OF_PARTICLES];
242 struct CTerrain mGround;
243 struct CTerrain mRoof;
245 /*Function declarations*/
246 static void chopDrawParticle(struct CParticle *mParticle);
247 static void chopDrawBlock(struct CBlock *mBlock);
248 static void chopRenderTerrain(struct CTerrain *ter);
249 void chopper_load(bool newgame);
250 void cleanup_chopper(void);
252 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
255 #if LCD_DEPTH > 2
256 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
257 #elif LCD_DEPTH == 2
258 rb->lcd_set_foreground(LCD_DARKGRAY);
259 #endif
260 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
261 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
263 #if LCD_DEPTH > 2
264 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
265 #elif LCD_DEPTH == 2
266 rb->lcd_set_foreground(LCD_DARKGRAY);
267 #endif
268 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
269 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
271 #if LCD_DEPTH > 2
272 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
273 #elif LCD_DEPTH == 2
274 rb->lcd_set_foreground(LCD_BLACK);
275 #endif
276 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
277 SCALE(y-iRotorOffset));
279 #if LCD_DEPTH > 2
280 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
281 #elif LCD_DEPTH == 2
282 rb->lcd_set_foreground(LCD_BLACK);
283 #endif
284 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
288 static void chopClearTerrain(struct CTerrain *ter)
290 ter->iNodesCount = 0;
294 int iR(int low,int high)
296 return low+rb->rand()%(high-low+1);
299 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
300 int xOffset,int yOffset)
302 int i=0;
304 while(i < src->iNodesCount)
306 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
307 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
309 i++;
312 dest->iNodesCount = src->iNodesCount;
313 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
317 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
319 int i=0;
321 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
323 /* DEBUGF("ERROR: Not enough nodes!\n"); */
324 return;
327 ter->iNodesCount++;
329 i = ter->iNodesCount - 1;
331 ter->mNodes[i].x = x;
332 ter->mNodes[i].y= y;
334 ter->iLastNodePlacedPosX = x;
338 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
340 int i=nodeIndex;
342 while( i < ter->iNodesCount )
344 ter->mNodes[i - 1] = ter->mNodes[i];
345 i++;
348 ter->iNodesCount--;
353 int chopUpdateTerrainRecycling(struct CTerrain *ter)
355 int i=1;
356 int ret = 0;
357 int iNewNodePos,g,v;
358 while(i < ter->iNodesCount)
361 if( iCameraPosX > ter->mNodes[i].x)
364 chopTerrainNodeDeleteAndShift(ter,i);
366 iNewNodePos = ter->iLastNodePlacedPosX + 50;
367 g = iScreenY - 10;
369 v = 3*iPlayerSpeedX;
370 if(v>50)
371 v=50;
372 if(iLevelMode == LEVEL_MODE_STEEP)
373 v*=5;
375 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
376 ret=1;
380 i++;
384 return 1;
387 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
390 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
391 float c,d;
393 int i=0;
394 for(i=1;i<MAX_TERRAIN_NODES;i++)
396 if(ter->mNodes[i].x > pX)
398 iNodeIndexOne = i - 1;
399 break;
404 iNodeIndexTwo = iNodeIndexOne + 1;
405 terY1 = ter->mNodes[iNodeIndexOne].y;
406 terY2 = ter->mNodes[iNodeIndexTwo].y;
408 terX1 = 0;
409 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
411 pX-= ter->mNodes[iNodeIndexOne].x;
413 a = terY2 - terY1;
414 b = terX2;
415 c = pX;
416 d = (c/b) * a;
418 h = d + terY1;
420 return h;
424 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
426 int h = chopTerrainHeightAtPoint(ter, pX);
428 if(iTestType == 0)
429 return (pY > h);
430 else
431 return (pY < h);
434 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
436 int i=0;
438 if(indexOverride < 0)
440 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
441 i++;
442 if(i==NUMBER_OF_BLOCKS)
444 DEBUGF("No blocks!\n");
445 return;
448 else
449 i = indexOverride;
451 mBlocks[i].bIsActive = 1;
452 mBlocks[i].iWorldX = x;
453 mBlocks[i].iWorldY = y;
454 mBlocks[i].iSizeX = sx;
455 mBlocks[i].iSizeY = sy;
457 iLastBlockPlacedPosX = x;
460 static void chopAddParticle(int x,int y,int sx,int sy)
462 int i=0;
464 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
465 i++;
467 if(i==NUMBER_OF_PARTICLES)
468 return;
470 mParticles[i].bIsActive = 1;
471 mParticles[i].iWorldX = x;
472 mParticles[i].iWorldY = y;
473 mParticles[i].iSpeedX = sx;
474 mParticles[i].iSpeedY = sy;
477 static void chopGenerateBlockIfNeeded(void)
479 int i=0;
480 int DistSpeedX = iPlayerSpeedX * 5;
481 if(DistSpeedX<200) DistSpeedX = 200;
483 while(i < NUMBER_OF_BLOCKS)
485 if(!mBlocks[i].bIsActive)
487 int iX,iY,sX,sY;
489 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
490 sX = blockw;
492 iY = iR(0,iScreenY);
493 sY = blockh + iR(1,blockh/3);
495 chopAddBlock(iX,iY,sX,sY,i);
498 i++;
503 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
505 int px = iPlayerPosX;
506 int py = iPlayerPosY;
508 int x = mBlock->iWorldX-17;
509 int y = mBlock->iWorldY-11;
511 int x2 = x + mBlock->iSizeX+17;
512 int y2 = y + mBlock->iSizeY+11;
514 if(px>x && px<x2)
516 if(py>y && py<y2)
518 return 1;
522 return 0;
525 static int chopBlockOffscreen(struct CBlock *mBlock)
527 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
528 return 1;
529 else
530 return 0;
533 static int chopParticleOffscreen(struct CParticle *mParticle)
535 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
536 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
537 iScreenX)
539 return 1;
541 else
542 return 0;
545 static void chopKillPlayer(void)
547 int i, button;
549 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
550 mParticles[i].bIsActive = 0;
551 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
552 iR(-2,2), iR(-2,2));
555 iPlayerAlive--;
557 if (iPlayerAlive == 0) {
558 rb->lcd_set_drawmode(DRMODE_FG);
559 #if LCD_DEPTH >= 2
560 rb->lcd_set_foreground(LCD_LIGHTGRAY);
561 #endif
562 rb->splash(HZ, "Game Over");
564 if (score > highscore) {
565 char scoretext[30];
566 highscore = score;
567 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
568 highscore);
569 rb->splash(HZ*2, scoretext);
572 rb->splash(HZ/4, "Press " ACTIONTEXT " to continue");
573 rb->lcd_update();
575 rb->lcd_set_drawmode(DRMODE_SOLID);
577 while (true) {
578 button = rb->button_get(true);
579 if (button == ACTION
580 #ifdef ACTION2
581 || button == ACTION2
582 #endif
584 while (true) {
585 button = rb->button_get(true);
586 if (button == (ACTION | BUTTON_REL)
587 #ifdef ACTION2
588 || button == (ACTION2 | BUTTON_REL)
589 #endif
591 chopper_load(true);
592 return;
598 } else
599 chopper_load(false);
603 static void chopDrawTheWorld(void)
605 int i=0;
607 while(i < NUMBER_OF_BLOCKS)
609 if(mBlocks[i].bIsActive)
611 if(chopBlockOffscreen(&mBlocks[i]) == 1)
612 mBlocks[i].bIsActive = 0;
613 else
614 chopDrawBlock(&mBlocks[i]);
617 i++;
620 i=0;
622 while(i < NUMBER_OF_PARTICLES)
624 if(mParticles[i].bIsActive)
626 if(chopParticleOffscreen(&mParticles[i]) == 1)
627 mParticles[i].bIsActive = 0;
628 else
629 chopDrawParticle(&mParticles[i]);
632 i++;
635 chopRenderTerrain(&mGround);
636 chopRenderTerrain(&mRoof);
640 static void chopDrawParticle(struct CParticle *mParticle)
643 int iPosX = (mParticle->iWorldX - iCameraPosX);
644 int iPosY = (mParticle->iWorldY);
645 #if LCD_DEPTH > 2
646 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
647 #elif LCD_DEPTH == 2
648 rb->lcd_set_foreground(LCD_LIGHTGRAY);
649 #endif
650 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
654 static void chopDrawScene(void)
656 char s[30];
657 int w;
658 #if LCD_DEPTH > 2
659 rb->lcd_set_background(LCD_BLACK);
660 #elif LCD_DEPTH == 2
661 rb->lcd_set_background(LCD_WHITE);
662 #endif
663 chopDrawTheWorld();
664 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
666 score = -20 + iPlayerPosX/3;
668 #if LCD_DEPTH == 1
669 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
670 #else
671 rb->lcd_set_drawmode(DRMODE_FG);
672 #endif
674 #if LCD_DEPTH > 2
675 rb->lcd_set_foreground(LCD_BLACK);
676 #elif LCD_DEPTH == 2
677 rb->lcd_set_foreground(LCD_WHITE);
678 #endif
680 #if LCD_WIDTH <= 128
681 rb->snprintf(s, sizeof(s), "Dist: %d", score);
682 #else
683 rb->snprintf(s, sizeof(s), "Distance: %d", score);
684 #endif
685 rb->lcd_getstringsize(s, &w, NULL);
686 rb->lcd_putsxy(2, 2, s);
687 if (score < highscore)
689 int w2;
690 #if LCD_WIDTH <= 128
691 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
692 #else
693 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
694 #endif
695 rb->lcd_getstringsize(s, &w2, NULL);
696 if (LCD_WIDTH - 2 - w2 > w + 2)
697 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
699 rb->lcd_set_drawmode(DRMODE_SOLID);
701 rb->lcd_update();
704 static bool _ingame;
705 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
707 if(action == ACTION_REQUEST_MENUITEM
708 && !_ingame && ((intptr_t)this_item)==0)
709 return ACTION_EXIT_MENUITEM;
710 return action;
712 static int chopMenu(int menunum)
714 int result = 0;
715 int res = 0;
716 bool menu_quit = false;
718 static const struct opt_items levels[2] = {
719 { "Normal", -1 },
720 { "Steep", -1 },
723 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
724 "Resume Game","Start New Game",
725 "Level","Playback Control","Quit");
726 _ingame = (menunum!=0);
728 #ifdef HAVE_LCD_COLOR
729 rb->lcd_set_foreground(LCD_WHITE);
730 rb->lcd_set_background(LCD_BLACK);
731 #elif LCD_DEPTH == 2
732 rb->lcd_set_foreground(LCD_BLACK);
733 rb->lcd_set_background(LCD_WHITE);
734 #endif
736 rb->lcd_clear_display();
738 while (!menu_quit) {
739 switch(rb->do_menu(&menu, &result, NULL, false))
741 case 0: /* Resume Game */
742 menu_quit=true;
743 res = -1;
744 break;
745 case 1: /* Start New Game */
746 menu_quit=true;
747 chopper_load(true);
748 res = -1;
749 break;
750 case 2:
751 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
752 break;
753 case 3:
754 playback_control(NULL);
755 break;
756 case 4:
757 menu_quit=true;
758 res = PLUGIN_OK;
759 break;
760 case MENU_ATTACHED_USB:
761 menu_quit=true;
762 res = PLUGIN_USB_CONNECTED;
763 break;
766 rb->lcd_clear_display();
767 return res;
770 static int chopGameLoop(void)
772 int move_button, ret;
773 bool exit=false;
774 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
776 if (chopUpdateTerrainRecycling(&mGround) == 1)
777 /* mirror the sky if we've changed the ground */
778 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
780 ret = chopMenu(0);
781 if (ret != -1)
782 return PLUGIN_OK;
784 chopDrawScene();
786 while (!exit) {
788 end = *rb->current_tick + (CYCLETIME * HZ) / 1000;
790 if(chopUpdateTerrainRecycling(&mGround) == 1)
791 /* mirror the sky if we've changed the ground */
792 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
794 iRotorOffset = iR(-1,1);
796 /* We need to have this here so particles move when we're dead */
798 for (i=0; i < NUMBER_OF_PARTICLES; i++)
799 if(mParticles[i].bIsActive == 1)
801 mParticles[i].iWorldX += mParticles[i].iSpeedX;
802 mParticles[i].iWorldY += mParticles[i].iSpeedY;
805 rb->lcd_clear_display();
806 /* Redraw the main window: */
807 chopDrawScene();
810 iGravityTimerCountdown--;
812 if(iGravityTimerCountdown <= 0)
814 iGravityTimerCountdown = 3;
815 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
818 if(iLevelMode == LEVEL_MODE_NORMAL)
819 chopGenerateBlockIfNeeded();
822 move_button=rb->button_status();
823 if (rb->button_get(false) == QUIT) {
824 ret = chopMenu(1);
825 if (ret != -1)
826 return PLUGIN_OK;
827 bdelay = 0;
828 last_button = BUTTON_NONE;
829 move_button = BUTTON_NONE;
832 switch (move_button) {
833 case ACTION:
834 #ifdef ACTION2
835 case ACTION2:
836 #endif
837 if (last_button != ACTION
838 #ifdef ACTION2
839 && last_button != ACTION2
840 #endif
842 bdelay = -2;
843 if (bdelay == 0)
844 iPlayerSpeedY = -3;
845 break;
847 default:
848 if (last_button == ACTION
849 #ifdef ACTION2
850 || last_button == ACTION2
851 #endif
853 bdelay = 3;
854 if (bdelay == 0)
855 iPlayerSpeedY = 4;
857 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
858 return PLUGIN_USB_CONNECTED;
859 break;
861 last_button = move_button;
863 if (bdelay < 0) {
864 iPlayerSpeedY = bdelay;
865 bdelay++;
866 } else if (bdelay > 0) {
867 iPlayerSpeedY = bdelay;
868 bdelay--;
871 iCameraPosX = iPlayerPosX - 25;
872 iPlayerPosX += iPlayerSpeedX;
873 iPlayerPosY += iPlayerSpeedY;
875 chopCounter++;
876 /* increase speed as we go along */
877 if (chopCounter == 100){
878 iPlayerSpeedX++;
879 chopCounter=0;
882 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
883 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
884 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
886 chopKillPlayer();
887 chopDrawScene();
888 ret = chopMenu(0);
889 if (ret != -1)
890 return ret;
893 for (i=0; i < NUMBER_OF_BLOCKS; i++)
894 if(mBlocks[i].bIsActive == 1)
895 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
896 chopKillPlayer();
897 chopDrawScene();
898 ret = chopMenu(0);
899 if (ret != -1)
900 return ret;
903 if (end > *rb->current_tick)
904 rb->sleep(end-*rb->current_tick);
905 else
906 rb->yield();
909 return PLUGIN_OK;
912 static void chopDrawBlock(struct CBlock *mBlock)
914 int iPosX = (mBlock->iWorldX - iCameraPosX);
915 int iPosY = (mBlock->iWorldY);
916 #if LCD_DEPTH > 2
917 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
918 #elif LCD_DEPTH == 2
919 rb->lcd_set_foreground(LCD_BLACK);
920 #endif
921 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
922 SCALE(mBlock->iSizeY));
926 static void chopRenderTerrain(struct CTerrain *ter)
929 int i=1;
931 int oldx=0;
933 int ay=0;
934 if(ter->mNodes[0].y < (LCD_HEIGHT*SIZE)/2)
935 ay=0;
936 else
937 ay=(LCD_HEIGHT*SIZE);
939 while(i < ter->iNodesCount && oldx < iScreenX)
942 int x = ter->mNodes[i-1].x - iCameraPosX;
943 int y = ter->mNodes[i-1].y;
945 int x2 = ter->mNodes[i].x - iCameraPosX;
946 int y2 = ter->mNodes[i].y;
947 #if LCD_DEPTH > 2
948 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
949 #elif LCD_DEPTH == 2
950 rb->lcd_set_foreground(LCD_DARKGRAY);
951 #endif
953 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
955 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
956 SCALE(x2), SCALE(ay));
957 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x2), SCALE(y2),
958 SCALE(x2), SCALE(ay));
960 if (ay == 0)
961 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
962 SCALE(x2), SCALE(y2 / 2));
963 else
964 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
965 SCALE(x2), SCALE((LCD_HEIGHT*SIZE) -
966 ((LCD_HEIGHT*SIZE) - y2) / 2));
968 oldx = x;
969 i++;
975 void chopper_load(bool newgame)
978 int i;
979 int g;
981 if (newgame) {
982 iScreenX = LCD_WIDTH * SIZE;
983 iScreenY = LCD_HEIGHT * SIZE;
984 blockh = iScreenY / 5;
985 blockw = iScreenX / 20;
986 iPlayerAlive = 1;
987 score = 0;
989 iRotorOffset = 0;
990 iPlayerPosX = 60;
991 iPlayerPosY = (iScreenY * 4) / 10;
992 iLastBlockPlacedPosX = 0;
993 iGravityTimerCountdown = 2;
994 chopCounter = 0;
995 iPlayerSpeedX = 3;
996 iPlayerSpeedY = 0;
997 iCameraPosX = 30;
999 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1000 mParticles[i].bIsActive = 0;
1002 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1003 mBlocks[i].bIsActive = 0;
1005 g = iScreenY - 10;
1006 chopClearTerrain(&mGround);
1008 for (i=0; i < MAX_TERRAIN_NODES; i++)
1009 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1011 if (chopUpdateTerrainRecycling(&mGround) == 1)
1012 /* mirror the sky if we've changed the ground */
1013 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1015 iLevelMode = LEVEL_MODE_NORMAL;
1016 if (iLevelMode == LEVEL_MODE_NORMAL)
1017 /* make it a bit more exciting, cause it's easy terrain... */
1018 iPlayerSpeedX *= 2;
1021 /* this is the plugin entry point */
1022 enum plugin_status plugin_start(const void* parameter)
1024 (void)parameter;
1025 int ret;
1027 rb->lcd_setfont(FONT_SYSFIXED);
1028 #if LCD_DEPTH > 1
1029 rb->lcd_set_backdrop(NULL);
1030 #endif
1031 #ifdef HAVE_LCD_COLOR
1032 rb->lcd_set_background(LCD_BLACK);
1033 rb->lcd_set_foreground(LCD_WHITE);
1034 #endif
1036 /* Turn off backlight timeout */
1037 backlight_force_on(); /* backlight control in lib/helper.c */
1039 rb->srand( *rb->current_tick );
1041 configfile_load(CFG_FILE, config, 1, 0);
1043 chopper_load(true);
1044 ret = chopGameLoop();
1046 configfile_save(CFG_FILE, config, 1, 0);
1048 rb->lcd_setfont(FONT_UI);
1049 /* Turn on backlight timeout (revert to settings) */
1050 backlight_use_settings(); /* backlight control in lib/helper.c */
1052 return ret;