We have a 3.9 release, update builds.pm
[maemo-rb.git] / apps / plugins / chopper.c
blobbb36294238fc10c27f28ff3463de5d3fc3ebf873
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 #error No keymap defined!
178 #endif
180 #ifdef HAVE_TOUCHSCREEN
181 #ifndef QUIT
182 #define QUIT BUTTON_TOPLEFT
183 #endif
184 #ifndef ACTION
185 #define ACTION BUTTON_BOTTOMLEFT
186 #endif
187 #ifndef ACTION2
188 #define ACTION2 BUTTON_BOTTOMRIGHT
189 #endif
190 #ifndef ACTIONTEXT
191 #define ACTIONTEXT "BOTTOMRIGHT"
192 #endif
193 #endif
195 #define NUMBER_OF_BLOCKS 8
196 #define NUMBER_OF_PARTICLES 3
197 #define MAX_TERRAIN_NODES 15
199 #define LEVEL_MODE_NORMAL 0
200 #define LEVEL_MODE_STEEP 1
202 #if LCD_HEIGHT <= 64
203 #define CYCLES 100
204 static inline int SCALE(int x)
206 return x == 1 ? x : x >> 1;
208 #define SIZE 2
209 #else
210 #define CYCLES 60
211 #define SCALE(x) (x)
212 #define SIZE 1
213 #endif
215 /* in 10 milisecond (ticks) */
216 #define CYCLETIME ((CYCLES*HZ)/1000)
218 /*Chopper's local variables to track the terrain position etc*/
219 static int chopCounter;
220 static int iRotorOffset;
221 static int iScreenX;
222 static int iScreenY;
223 static int iPlayerPosX;
224 static int iPlayerPosY;
225 static int iCameraPosX;
226 static int iPlayerSpeedX;
227 static int iPlayerSpeedY;
228 static int iLastBlockPlacedPosX;
229 static int iGravityTimerCountdown;
230 static int iPlayerAlive;
231 static int iLevelMode, iCurrLevelMode;
232 static int blockh,blockw;
233 static int highscore;
234 static int score;
236 #define CFG_FILE "chopper.cfg"
237 #define MAX_POINTS 50000
238 static struct configdata config[] =
240 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
243 struct CBlock
245 int iWorldX;
246 int iWorldY;
248 int iSizeX;
249 int iSizeY;
251 int bIsActive;
254 struct CParticle
256 int iWorldX;
257 int iWorldY;
259 int iSpeedX;
260 int iSpeedY;
262 int bIsActive;
265 struct CTerrainNode
267 int x;
268 int y;
271 struct CTerrain
273 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
274 int iNodesCount;
275 int iLastNodePlacedPosX;
278 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
279 struct CParticle mParticles[NUMBER_OF_PARTICLES];
281 struct CTerrain mGround;
282 struct CTerrain mRoof;
284 /*Function declarations*/
285 static void chopDrawParticle(struct CParticle *mParticle);
286 static void chopDrawBlock(struct CBlock *mBlock);
287 static void chopRenderTerrain(struct CTerrain *ter, bool isground);
288 void chopper_load(bool newgame);
289 void cleanup_chopper(void);
291 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
294 #if LCD_DEPTH > 2
295 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
296 #elif LCD_DEPTH == 2
297 rb->lcd_set_foreground(LCD_DARKGRAY);
298 #endif
299 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
300 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
302 #if LCD_DEPTH > 2
303 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
304 #elif LCD_DEPTH == 2
305 rb->lcd_set_foreground(LCD_DARKGRAY);
306 #endif
307 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
308 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
310 #if LCD_DEPTH > 2
311 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
312 #elif LCD_DEPTH == 2
313 rb->lcd_set_foreground(LCD_BLACK);
314 #endif
315 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
316 SCALE(y-iRotorOffset));
318 #if LCD_DEPTH > 2
319 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
320 #elif LCD_DEPTH == 2
321 rb->lcd_set_foreground(LCD_BLACK);
322 #endif
323 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
327 static void chopClearTerrain(struct CTerrain *ter)
329 ter->iNodesCount = 0;
333 int iR(int low,int high)
335 return low+rb->rand()%(high-low+1);
338 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
339 int xOffset,int yOffset)
341 int i=0;
343 while(i < src->iNodesCount)
345 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
346 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
348 i++;
351 dest->iNodesCount = src->iNodesCount;
352 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
356 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
358 int i=0;
360 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
362 /* DEBUGF("ERROR: Not enough nodes!\n"); */
363 return;
366 ter->iNodesCount++;
368 i = ter->iNodesCount - 1;
370 ter->mNodes[i].x = x;
371 ter->mNodes[i].y= y;
373 ter->iLastNodePlacedPosX = x;
377 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
379 int i=nodeIndex;
381 while( i < ter->iNodesCount )
383 ter->mNodes[i - 1] = ter->mNodes[i];
384 i++;
387 ter->iNodesCount--;
392 int chopUpdateTerrainRecycling(struct CTerrain *ter)
394 int i=1;
395 int iNewNodePos,g,v;
396 while(i < ter->iNodesCount)
399 if( iCameraPosX > ter->mNodes[i].x)
402 chopTerrainNodeDeleteAndShift(ter,i);
404 iNewNodePos = ter->iLastNodePlacedPosX + 50;
405 g = iScreenY - 10;
407 v = 3*iPlayerSpeedX;
408 if(v>50)
409 v=50;
410 if(iCurrLevelMode == LEVEL_MODE_STEEP)
411 v*=5;
413 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
416 i++;
420 return 1;
423 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
426 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX2, a, b;
427 float c,d;
429 int i=0;
430 for(i=1;i<MAX_TERRAIN_NODES;i++)
432 if(ter->mNodes[i].x > pX)
434 iNodeIndexOne = i - 1;
435 break;
440 iNodeIndexTwo = iNodeIndexOne + 1;
441 terY1 = ter->mNodes[iNodeIndexOne].y;
442 terY2 = ter->mNodes[iNodeIndexTwo].y;
444 /* terX1 = 0; */
445 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
447 pX-= ter->mNodes[iNodeIndexOne].x;
449 a = terY2 - terY1;
450 b = terX2;
451 c = pX;
452 d = (c/b) * a;
454 h = d + terY1;
456 return h;
460 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
462 int h = chopTerrainHeightAtPoint(ter, pX);
464 if(iTestType == 0)
465 return (pY > h);
466 else
467 return (pY < h);
470 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
472 int i=0;
474 if(indexOverride < 0)
476 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
477 i++;
478 if(i==NUMBER_OF_BLOCKS)
480 DEBUGF("No blocks!\n");
481 return;
484 else
485 i = indexOverride;
487 mBlocks[i].bIsActive = 1;
488 mBlocks[i].iWorldX = x;
489 mBlocks[i].iWorldY = y;
490 mBlocks[i].iSizeX = sx;
491 mBlocks[i].iSizeY = sy;
493 iLastBlockPlacedPosX = x;
496 static void chopAddParticle(int x,int y,int sx,int sy)
498 int i=0;
500 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
501 i++;
503 if(i==NUMBER_OF_PARTICLES)
504 return;
506 mParticles[i].bIsActive = 1;
507 mParticles[i].iWorldX = x;
508 mParticles[i].iWorldY = y;
509 mParticles[i].iSpeedX = sx;
510 mParticles[i].iSpeedY = sy;
513 static void chopGenerateBlockIfNeeded(void)
515 int i=0;
516 int DistSpeedX = iPlayerSpeedX * 5;
517 if(DistSpeedX<200) DistSpeedX = 200;
519 while(i < NUMBER_OF_BLOCKS)
521 if(!mBlocks[i].bIsActive)
523 int iX,iY,sX,sY;
525 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
526 sX = blockw;
528 iY = iR(0,iScreenY);
529 sY = blockh + iR(1,blockh/3);
531 chopAddBlock(iX,iY,sX,sY,i);
534 i++;
539 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
541 int px = iPlayerPosX;
542 int py = iPlayerPosY;
544 int x = mBlock->iWorldX-17;
545 int y = mBlock->iWorldY-11;
547 int x2 = x + mBlock->iSizeX+17;
548 int y2 = y + mBlock->iSizeY+11;
550 if(px>x && px<x2)
552 if(py>y && py<y2)
554 return 1;
558 return 0;
561 static int chopBlockOffscreen(struct CBlock *mBlock)
563 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
564 return 1;
565 else
566 return 0;
569 static int chopParticleOffscreen(struct CParticle *mParticle)
571 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
572 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
573 iScreenX)
575 return 1;
577 else
578 return 0;
581 static void chopKillPlayer(void)
583 int i;
585 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
586 mParticles[i].bIsActive = 0;
587 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
588 iR(-2,2), iR(-2,2));
591 iPlayerAlive--;
593 if (iPlayerAlive == 0) {
594 rb->splash(HZ, "Game Over");
596 if (score > highscore) {
597 char scoretext[30];
598 highscore = score;
599 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
600 highscore);
601 rb->splash(HZ*2, scoretext);
603 } else
604 chopper_load(false);
607 static void chopDrawTheWorld(void)
609 int i=0;
611 while(i < NUMBER_OF_BLOCKS)
613 if(mBlocks[i].bIsActive)
615 if(chopBlockOffscreen(&mBlocks[i]) == 1)
616 mBlocks[i].bIsActive = 0;
617 else
618 chopDrawBlock(&mBlocks[i]);
621 i++;
624 i=0;
626 while(i < NUMBER_OF_PARTICLES)
628 if(mParticles[i].bIsActive)
630 if(chopParticleOffscreen(&mParticles[i]) == 1)
631 mParticles[i].bIsActive = 0;
632 else
633 chopDrawParticle(&mParticles[i]);
636 i++;
639 chopRenderTerrain(&mGround, true);
640 chopRenderTerrain(&mRoof, false);
644 static void chopDrawParticle(struct CParticle *mParticle)
647 int iPosX = (mParticle->iWorldX - iCameraPosX);
648 int iPosY = (mParticle->iWorldY);
649 #if LCD_DEPTH > 2
650 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
651 #elif LCD_DEPTH == 2
652 rb->lcd_set_foreground(LCD_LIGHTGRAY);
653 #endif
654 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
658 static void chopDrawScene(void)
660 char s[30];
661 int w;
662 #if LCD_DEPTH > 2
663 rb->lcd_set_background(LCD_BLACK);
664 #elif LCD_DEPTH == 2
665 rb->lcd_set_background(LCD_WHITE);
666 #endif
667 rb->lcd_clear_display();
668 chopDrawTheWorld();
669 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
671 score = -20 + iPlayerPosX/3;
673 #if LCD_DEPTH == 1
674 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
675 #else
676 rb->lcd_set_drawmode(DRMODE_FG);
677 #endif
679 #if LCD_DEPTH > 2
680 rb->lcd_set_foreground(LCD_BLACK);
681 #elif LCD_DEPTH == 2
682 rb->lcd_set_foreground(LCD_WHITE);
683 #endif
685 #if LCD_WIDTH <= 128
686 rb->snprintf(s, sizeof(s), "Dist: %d", score);
687 #else
688 rb->snprintf(s, sizeof(s), "Distance: %d", score);
689 #endif
690 rb->lcd_getstringsize(s, &w, NULL);
691 rb->lcd_putsxy(2, 2, s);
692 if (score < highscore)
694 int w2;
695 #if LCD_WIDTH <= 128
696 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
697 #else
698 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
699 #endif
700 rb->lcd_getstringsize(s, &w2, NULL);
701 if (LCD_WIDTH - 2 - w2 > w + 2)
702 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
704 rb->lcd_set_drawmode(DRMODE_SOLID);
706 rb->lcd_update();
709 static bool _ingame;
710 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
712 if(action == ACTION_REQUEST_MENUITEM
713 && !_ingame && ((intptr_t)this_item)==0)
714 return ACTION_EXIT_MENUITEM;
715 return action;
717 static int chopMenu(int menunum)
719 int result = 0;
720 int res = 0;
721 bool menu_quit = false;
723 static const struct opt_items levels[2] = {
724 { "Normal", -1 },
725 { "Steep", -1 },
728 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
729 "Resume Game","Start New Game",
730 "Level","Playback Control","Quit");
731 _ingame = (menunum!=0);
733 #ifdef HAVE_LCD_COLOR
734 rb->lcd_set_foreground(LCD_WHITE);
735 rb->lcd_set_background(LCD_BLACK);
736 #elif LCD_DEPTH == 2
737 rb->lcd_set_foreground(LCD_BLACK);
738 rb->lcd_set_background(LCD_WHITE);
739 #endif
741 rb->lcd_clear_display();
742 rb->button_clear_queue();
744 while (!menu_quit) {
745 switch(rb->do_menu(&menu, &result, NULL, false))
747 case 0: /* Resume Game */
748 menu_quit=true;
749 res = -1;
750 break;
751 case 1: /* Start New Game */
752 menu_quit=true;
753 chopper_load(true);
754 res = -1;
755 break;
756 case 2:
757 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
758 break;
759 case 3:
760 playback_control(NULL);
761 break;
762 case 4:
763 menu_quit=true;
764 res = PLUGIN_OK;
765 break;
766 case MENU_ATTACHED_USB:
767 menu_quit=true;
768 res = PLUGIN_USB_CONNECTED;
769 break;
772 rb->lcd_clear_display();
773 return res;
776 static int chopGameLoop(void)
778 int move_button, ret;
779 bool exit=false;
780 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
782 if (chopUpdateTerrainRecycling(&mGround) == 1)
783 /* mirror the sky if we've changed the ground */
784 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
786 ret = chopMenu(0);
787 if (ret != -1)
788 return PLUGIN_OK;
790 chopDrawScene();
792 while (!exit) {
794 end = *rb->current_tick + CYCLETIME;
796 if(chopUpdateTerrainRecycling(&mGround) == 1)
797 /* mirror the sky if we've changed the ground */
798 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
800 iRotorOffset = iR(-1,1);
802 /* We need to have this here so particles move when we're dead */
804 for (i=0; i < NUMBER_OF_PARTICLES; i++)
805 if(mParticles[i].bIsActive == 1)
807 mParticles[i].iWorldX += mParticles[i].iSpeedX;
808 mParticles[i].iWorldY += mParticles[i].iSpeedY;
811 /* Redraw the main window: */
812 chopDrawScene();
815 iGravityTimerCountdown--;
817 if(iGravityTimerCountdown <= 0)
819 iGravityTimerCountdown = 3;
820 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
823 if(iCurrLevelMode == LEVEL_MODE_NORMAL)
824 chopGenerateBlockIfNeeded();
827 move_button=rb->button_status();
828 if (rb->button_get(false) == QUIT) {
829 ret = chopMenu(1);
830 if (ret != -1)
831 return PLUGIN_OK;
832 bdelay = 0;
833 last_button = BUTTON_NONE;
834 move_button = BUTTON_NONE;
837 switch (move_button) {
838 case ACTION:
839 #ifdef ACTION2
840 case ACTION2:
841 #endif
842 if (last_button != ACTION
843 #ifdef ACTION2
844 && last_button != ACTION2
845 #endif
847 bdelay = -2;
848 if (bdelay == 0)
849 iPlayerSpeedY = -3;
850 break;
852 default:
853 if (last_button == ACTION
854 #ifdef ACTION2
855 || last_button == ACTION2
856 #endif
858 bdelay = 3;
859 if (bdelay == 0)
860 iPlayerSpeedY = 4;
862 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
863 return PLUGIN_USB_CONNECTED;
864 break;
866 last_button = move_button;
868 if (bdelay < 0) {
869 iPlayerSpeedY = bdelay;
870 bdelay++;
871 } else if (bdelay > 0) {
872 iPlayerSpeedY = bdelay;
873 bdelay--;
876 iCameraPosX = iPlayerPosX - 25;
877 iPlayerPosX += iPlayerSpeedX;
878 iPlayerPosY += iPlayerSpeedY;
880 chopCounter++;
881 /* increase speed as we go along */
882 if (chopCounter == 100){
883 iPlayerSpeedX++;
884 chopCounter=0;
887 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
888 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
889 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
891 chopKillPlayer();
892 chopDrawScene();
893 ret = chopMenu(0);
894 if (ret != -1)
895 return ret;
898 for (i=0; i < NUMBER_OF_BLOCKS; i++)
899 if(mBlocks[i].bIsActive == 1)
900 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
901 chopKillPlayer();
902 chopDrawScene();
903 ret = chopMenu(0);
904 if (ret != -1)
905 return ret;
908 if (TIME_BEFORE(*rb->current_tick, end))
909 rb->sleep(end - *rb->current_tick); /* wait until time is over */
910 else
911 rb->yield();
914 return PLUGIN_OK;
917 static void chopDrawBlock(struct CBlock *mBlock)
919 int iPosX = (mBlock->iWorldX - iCameraPosX);
920 int iPosY = (mBlock->iWorldY);
921 #if LCD_DEPTH > 2
922 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
923 #elif LCD_DEPTH == 2
924 rb->lcd_set_foreground(LCD_BLACK);
925 #endif
926 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
927 SCALE(mBlock->iSizeY));
931 static void chopRenderTerrain(struct CTerrain *ter, bool isground)
934 int i = 1;
936 int oldx = 0;
938 while(i < ter->iNodesCount && oldx < iScreenX)
941 int x = ter->mNodes[i-1].x - iCameraPosX;
942 int y = ter->mNodes[i-1].y;
944 int x2 = ter->mNodes[i].x - iCameraPosX;
945 int y2 = ter->mNodes[i].y;
947 int ax, ay;
949 if ((y < y2) != isground)
951 ax = x2;
952 ay = y;
954 else
956 ax = x;
957 ay = y2;
959 #if LCD_DEPTH > 2
960 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
961 #elif LCD_DEPTH == 2
962 rb->lcd_set_foreground(LCD_DARKGRAY);
963 #endif
965 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
967 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
968 SCALE(ax), SCALE(ay));
970 if (isground)
972 y = ay;
973 y2 = (LCD_HEIGHT*SIZE);
975 else
977 y = 0;
978 y2 = ay;
980 if (y2-y > 0)
981 rb->lcd_fillrect(SCALE(x), SCALE(y), SCALE(x2-x)+1, SCALE(y2-y)+1);
983 oldx = x;
984 i++;
988 void chopper_load(bool newgame)
991 int i;
992 int g;
994 if (newgame) {
995 iScreenX = LCD_WIDTH * SIZE;
996 iScreenY = LCD_HEIGHT * SIZE;
997 blockh = iScreenY / 5;
998 blockw = iScreenX / 20;
999 iPlayerAlive = 1;
1000 iCurrLevelMode = iLevelMode;
1001 score = 0;
1003 iRotorOffset = 0;
1004 iPlayerPosX = 60;
1005 iPlayerPosY = (iScreenY * 4) / 10;
1006 iLastBlockPlacedPosX = 0;
1007 iGravityTimerCountdown = 2;
1008 chopCounter = 0;
1009 iPlayerSpeedX = 3;
1010 iPlayerSpeedY = 0;
1011 iCameraPosX = 30;
1013 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1014 mParticles[i].bIsActive = 0;
1016 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1017 mBlocks[i].bIsActive = 0;
1019 g = iScreenY - 10;
1020 chopClearTerrain(&mGround);
1022 for (i=0; i < MAX_TERRAIN_NODES; i++)
1023 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1025 if (chopUpdateTerrainRecycling(&mGround) == 1)
1026 /* mirror the sky if we've changed the ground */
1027 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1029 if (iCurrLevelMode == LEVEL_MODE_NORMAL)
1030 /* make it a bit more exciting, cause it's easy terrain... */
1031 iPlayerSpeedX *= 2;
1034 /* this is the plugin entry point */
1035 enum plugin_status plugin_start(const void* parameter)
1037 (void)parameter;
1038 int ret;
1040 rb->lcd_setfont(FONT_SYSFIXED);
1041 #if LCD_DEPTH > 1
1042 rb->lcd_set_backdrop(NULL);
1043 #endif
1044 #ifdef HAVE_LCD_COLOR
1045 rb->lcd_set_background(LCD_BLACK);
1046 rb->lcd_set_foreground(LCD_WHITE);
1047 #endif
1049 /* Turn off backlight timeout */
1050 backlight_ignore_timeout();
1052 rb->srand( *rb->current_tick );
1054 configfile_load(CFG_FILE, config, 1, 0);
1056 chopper_load(true);
1057 ret = chopGameLoop();
1059 configfile_save(CFG_FILE, config, 1, 0);
1061 rb->lcd_setfont(FONT_UI);
1062 /* Turn on backlight timeout (revert to settings) */
1063 backlight_use_settings();
1065 return ret;