Use albumart opt in sliding puzzle manual
[kugel-rb.git] / apps / plugins / chopper.c
blob1217ea75ac2b962d31ba25a35ba8856cf1011967
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
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_SA9200_PAD
139 #define QUIT BUTTON_POWER
140 #define ACTION BUTTON_MENU
141 #define ACTION2 BUTTON_PLAY
142 #define ACTIONTEXT "MENU"
144 #elif CONFIG_KEYPAD == ONDAVX747_PAD || \
145 CONFIG_KEYPAD == ONDAVX777_PAD || \
146 CONFIG_KEYPAD == MROBE500_PAD
147 #define QUIT BUTTON_POWER
149 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
150 #define QUIT BUTTON_LEFT
151 #define ACTION BUTTON_RIGHT
152 #define ACTIONTEXT "RIGHT"
154 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
155 #define QUIT BUTTON_REC
156 #define ACTION BUTTON_PLAY
157 #define ACTION2 BUTTON_UP
158 #define ACTIONTEXT "PLAY"
160 #else
161 #error No keymap defined!
162 #endif
164 #ifdef HAVE_TOUCHSCREEN
165 #ifndef QUIT
166 #define QUIT BUTTON_TOPLEFT
167 #endif
168 #ifndef ACTION
169 #define ACTION BUTTON_BOTTOMLEFT
170 #endif
171 #ifndef ACTION2
172 #define ACTION2 BUTTON_BOTTOMRIGHT
173 #endif
174 #ifndef ACTIONTEXT
175 #define ACTIONTEXT "BOTTOMRIGHT"
176 #endif
177 #endif
179 #define NUMBER_OF_BLOCKS 8
180 #define NUMBER_OF_PARTICLES 3
181 #define MAX_TERRAIN_NODES 15
183 #define LEVEL_MODE_NORMAL 0
184 #define LEVEL_MODE_STEEP 1
186 #if LCD_HEIGHT <= 64
187 #define CYCLES 100
188 static inline int SCALE(int x)
190 return x == 1 ? x : x >> 1;
192 #define SIZE 2
193 #else
194 #define CYCLES 60
195 #define SCALE(x) (x)
196 #define SIZE 1
197 #endif
199 /* in 10 milisecond (ticks) */
200 #define CYCLETIME ((CYCLES*HZ)/1000)
202 /*Chopper's local variables to track the terrain position etc*/
203 static int chopCounter;
204 static int iRotorOffset;
205 static int iScreenX;
206 static int iScreenY;
207 static int iPlayerPosX;
208 static int iPlayerPosY;
209 static int iCameraPosX;
210 static int iPlayerSpeedX;
211 static int iPlayerSpeedY;
212 static int iLastBlockPlacedPosX;
213 static int iGravityTimerCountdown;
214 static int iPlayerAlive;
215 static int iLevelMode, iCurrLevelMode;
216 static int blockh,blockw;
217 static int highscore;
218 static int score;
220 #define CFG_FILE "chopper.cfg"
221 #define MAX_POINTS 50000
222 static struct configdata config[] =
224 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
227 struct CBlock
229 int iWorldX;
230 int iWorldY;
232 int iSizeX;
233 int iSizeY;
235 int bIsActive;
238 struct CParticle
240 int iWorldX;
241 int iWorldY;
243 int iSpeedX;
244 int iSpeedY;
246 int bIsActive;
249 struct CTerrainNode
251 int x;
252 int y;
255 struct CTerrain
257 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
258 int iNodesCount;
259 int iLastNodePlacedPosX;
262 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
263 struct CParticle mParticles[NUMBER_OF_PARTICLES];
265 struct CTerrain mGround;
266 struct CTerrain mRoof;
268 /*Function declarations*/
269 static void chopDrawParticle(struct CParticle *mParticle);
270 static void chopDrawBlock(struct CBlock *mBlock);
271 static void chopRenderTerrain(struct CTerrain *ter, bool isground);
272 void chopper_load(bool newgame);
273 void cleanup_chopper(void);
275 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
278 #if LCD_DEPTH > 2
279 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
280 #elif LCD_DEPTH == 2
281 rb->lcd_set_foreground(LCD_DARKGRAY);
282 #endif
283 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
284 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
286 #if LCD_DEPTH > 2
287 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
288 #elif LCD_DEPTH == 2
289 rb->lcd_set_foreground(LCD_DARKGRAY);
290 #endif
291 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
292 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
294 #if LCD_DEPTH > 2
295 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
296 #elif LCD_DEPTH == 2
297 rb->lcd_set_foreground(LCD_BLACK);
298 #endif
299 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
300 SCALE(y-iRotorOffset));
302 #if LCD_DEPTH > 2
303 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
304 #elif LCD_DEPTH == 2
305 rb->lcd_set_foreground(LCD_BLACK);
306 #endif
307 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
311 static void chopClearTerrain(struct CTerrain *ter)
313 ter->iNodesCount = 0;
317 int iR(int low,int high)
319 return low+rb->rand()%(high-low+1);
322 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
323 int xOffset,int yOffset)
325 int i=0;
327 while(i < src->iNodesCount)
329 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
330 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
332 i++;
335 dest->iNodesCount = src->iNodesCount;
336 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
340 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
342 int i=0;
344 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
346 /* DEBUGF("ERROR: Not enough nodes!\n"); */
347 return;
350 ter->iNodesCount++;
352 i = ter->iNodesCount - 1;
354 ter->mNodes[i].x = x;
355 ter->mNodes[i].y= y;
357 ter->iLastNodePlacedPosX = x;
361 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
363 int i=nodeIndex;
365 while( i < ter->iNodesCount )
367 ter->mNodes[i - 1] = ter->mNodes[i];
368 i++;
371 ter->iNodesCount--;
376 int chopUpdateTerrainRecycling(struct CTerrain *ter)
378 int i=1;
379 int ret = 0;
380 int iNewNodePos,g,v;
381 while(i < ter->iNodesCount)
384 if( iCameraPosX > ter->mNodes[i].x)
387 chopTerrainNodeDeleteAndShift(ter,i);
389 iNewNodePos = ter->iLastNodePlacedPosX + 50;
390 g = iScreenY - 10;
392 v = 3*iPlayerSpeedX;
393 if(v>50)
394 v=50;
395 if(iCurrLevelMode == LEVEL_MODE_STEEP)
396 v*=5;
398 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
399 ret=1;
403 i++;
407 return 1;
410 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
413 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
414 float c,d;
416 int i=0;
417 for(i=1;i<MAX_TERRAIN_NODES;i++)
419 if(ter->mNodes[i].x > pX)
421 iNodeIndexOne = i - 1;
422 break;
427 iNodeIndexTwo = iNodeIndexOne + 1;
428 terY1 = ter->mNodes[iNodeIndexOne].y;
429 terY2 = ter->mNodes[iNodeIndexTwo].y;
431 terX1 = 0;
432 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
434 pX-= ter->mNodes[iNodeIndexOne].x;
436 a = terY2 - terY1;
437 b = terX2;
438 c = pX;
439 d = (c/b) * a;
441 h = d + terY1;
443 return h;
447 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
449 int h = chopTerrainHeightAtPoint(ter, pX);
451 if(iTestType == 0)
452 return (pY > h);
453 else
454 return (pY < h);
457 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
459 int i=0;
461 if(indexOverride < 0)
463 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
464 i++;
465 if(i==NUMBER_OF_BLOCKS)
467 DEBUGF("No blocks!\n");
468 return;
471 else
472 i = indexOverride;
474 mBlocks[i].bIsActive = 1;
475 mBlocks[i].iWorldX = x;
476 mBlocks[i].iWorldY = y;
477 mBlocks[i].iSizeX = sx;
478 mBlocks[i].iSizeY = sy;
480 iLastBlockPlacedPosX = x;
483 static void chopAddParticle(int x,int y,int sx,int sy)
485 int i=0;
487 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
488 i++;
490 if(i==NUMBER_OF_PARTICLES)
491 return;
493 mParticles[i].bIsActive = 1;
494 mParticles[i].iWorldX = x;
495 mParticles[i].iWorldY = y;
496 mParticles[i].iSpeedX = sx;
497 mParticles[i].iSpeedY = sy;
500 static void chopGenerateBlockIfNeeded(void)
502 int i=0;
503 int DistSpeedX = iPlayerSpeedX * 5;
504 if(DistSpeedX<200) DistSpeedX = 200;
506 while(i < NUMBER_OF_BLOCKS)
508 if(!mBlocks[i].bIsActive)
510 int iX,iY,sX,sY;
512 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
513 sX = blockw;
515 iY = iR(0,iScreenY);
516 sY = blockh + iR(1,blockh/3);
518 chopAddBlock(iX,iY,sX,sY,i);
521 i++;
526 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
528 int px = iPlayerPosX;
529 int py = iPlayerPosY;
531 int x = mBlock->iWorldX-17;
532 int y = mBlock->iWorldY-11;
534 int x2 = x + mBlock->iSizeX+17;
535 int y2 = y + mBlock->iSizeY+11;
537 if(px>x && px<x2)
539 if(py>y && py<y2)
541 return 1;
545 return 0;
548 static int chopBlockOffscreen(struct CBlock *mBlock)
550 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
551 return 1;
552 else
553 return 0;
556 static int chopParticleOffscreen(struct CParticle *mParticle)
558 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
559 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
560 iScreenX)
562 return 1;
564 else
565 return 0;
568 static void chopKillPlayer(void)
570 int i;
572 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
573 mParticles[i].bIsActive = 0;
574 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
575 iR(-2,2), iR(-2,2));
578 iPlayerAlive--;
580 if (iPlayerAlive == 0) {
581 rb->splash(HZ, "Game Over");
583 if (score > highscore) {
584 char scoretext[30];
585 highscore = score;
586 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
587 highscore);
588 rb->splash(HZ*2, scoretext);
590 } else
591 chopper_load(false);
594 static void chopDrawTheWorld(void)
596 int i=0;
598 while(i < NUMBER_OF_BLOCKS)
600 if(mBlocks[i].bIsActive)
602 if(chopBlockOffscreen(&mBlocks[i]) == 1)
603 mBlocks[i].bIsActive = 0;
604 else
605 chopDrawBlock(&mBlocks[i]);
608 i++;
611 i=0;
613 while(i < NUMBER_OF_PARTICLES)
615 if(mParticles[i].bIsActive)
617 if(chopParticleOffscreen(&mParticles[i]) == 1)
618 mParticles[i].bIsActive = 0;
619 else
620 chopDrawParticle(&mParticles[i]);
623 i++;
626 chopRenderTerrain(&mGround, true);
627 chopRenderTerrain(&mRoof, false);
631 static void chopDrawParticle(struct CParticle *mParticle)
634 int iPosX = (mParticle->iWorldX - iCameraPosX);
635 int iPosY = (mParticle->iWorldY);
636 #if LCD_DEPTH > 2
637 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
638 #elif LCD_DEPTH == 2
639 rb->lcd_set_foreground(LCD_LIGHTGRAY);
640 #endif
641 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
645 static void chopDrawScene(void)
647 char s[30];
648 int w;
649 #if LCD_DEPTH > 2
650 rb->lcd_set_background(LCD_BLACK);
651 #elif LCD_DEPTH == 2
652 rb->lcd_set_background(LCD_WHITE);
653 #endif
654 rb->lcd_clear_display();
655 chopDrawTheWorld();
656 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
658 score = -20 + iPlayerPosX/3;
660 #if LCD_DEPTH == 1
661 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
662 #else
663 rb->lcd_set_drawmode(DRMODE_FG);
664 #endif
666 #if LCD_DEPTH > 2
667 rb->lcd_set_foreground(LCD_BLACK);
668 #elif LCD_DEPTH == 2
669 rb->lcd_set_foreground(LCD_WHITE);
670 #endif
672 #if LCD_WIDTH <= 128
673 rb->snprintf(s, sizeof(s), "Dist: %d", score);
674 #else
675 rb->snprintf(s, sizeof(s), "Distance: %d", score);
676 #endif
677 rb->lcd_getstringsize(s, &w, NULL);
678 rb->lcd_putsxy(2, 2, s);
679 if (score < highscore)
681 int w2;
682 #if LCD_WIDTH <= 128
683 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
684 #else
685 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
686 #endif
687 rb->lcd_getstringsize(s, &w2, NULL);
688 if (LCD_WIDTH - 2 - w2 > w + 2)
689 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
691 rb->lcd_set_drawmode(DRMODE_SOLID);
693 rb->lcd_update();
696 static bool _ingame;
697 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
699 if(action == ACTION_REQUEST_MENUITEM
700 && !_ingame && ((intptr_t)this_item)==0)
701 return ACTION_EXIT_MENUITEM;
702 return action;
704 static int chopMenu(int menunum)
706 int result = 0;
707 int res = 0;
708 bool menu_quit = false;
710 static const struct opt_items levels[2] = {
711 { "Normal", -1 },
712 { "Steep", -1 },
715 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
716 "Resume Game","Start New Game",
717 "Level","Playback Control","Quit");
718 _ingame = (menunum!=0);
720 #ifdef HAVE_LCD_COLOR
721 rb->lcd_set_foreground(LCD_WHITE);
722 rb->lcd_set_background(LCD_BLACK);
723 #elif LCD_DEPTH == 2
724 rb->lcd_set_foreground(LCD_BLACK);
725 rb->lcd_set_background(LCD_WHITE);
726 #endif
728 rb->lcd_clear_display();
729 rb->button_clear_queue();
731 while (!menu_quit) {
732 switch(rb->do_menu(&menu, &result, NULL, false))
734 case 0: /* Resume Game */
735 menu_quit=true;
736 res = -1;
737 break;
738 case 1: /* Start New Game */
739 menu_quit=true;
740 chopper_load(true);
741 res = -1;
742 break;
743 case 2:
744 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
745 break;
746 case 3:
747 playback_control(NULL);
748 break;
749 case 4:
750 menu_quit=true;
751 res = PLUGIN_OK;
752 break;
753 case MENU_ATTACHED_USB:
754 menu_quit=true;
755 res = PLUGIN_USB_CONNECTED;
756 break;
759 rb->lcd_clear_display();
760 return res;
763 static int chopGameLoop(void)
765 int move_button, ret;
766 bool exit=false;
767 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
769 if (chopUpdateTerrainRecycling(&mGround) == 1)
770 /* mirror the sky if we've changed the ground */
771 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
773 ret = chopMenu(0);
774 if (ret != -1)
775 return PLUGIN_OK;
777 chopDrawScene();
779 while (!exit) {
781 end = *rb->current_tick + CYCLETIME;
783 if(chopUpdateTerrainRecycling(&mGround) == 1)
784 /* mirror the sky if we've changed the ground */
785 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
787 iRotorOffset = iR(-1,1);
789 /* We need to have this here so particles move when we're dead */
791 for (i=0; i < NUMBER_OF_PARTICLES; i++)
792 if(mParticles[i].bIsActive == 1)
794 mParticles[i].iWorldX += mParticles[i].iSpeedX;
795 mParticles[i].iWorldY += mParticles[i].iSpeedY;
798 /* Redraw the main window: */
799 chopDrawScene();
802 iGravityTimerCountdown--;
804 if(iGravityTimerCountdown <= 0)
806 iGravityTimerCountdown = 3;
807 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
810 if(iCurrLevelMode == LEVEL_MODE_NORMAL)
811 chopGenerateBlockIfNeeded();
814 move_button=rb->button_status();
815 if (rb->button_get(false) == QUIT) {
816 ret = chopMenu(1);
817 if (ret != -1)
818 return PLUGIN_OK;
819 bdelay = 0;
820 last_button = BUTTON_NONE;
821 move_button = BUTTON_NONE;
824 switch (move_button) {
825 case ACTION:
826 #ifdef ACTION2
827 case ACTION2:
828 #endif
829 if (last_button != ACTION
830 #ifdef ACTION2
831 && last_button != ACTION2
832 #endif
834 bdelay = -2;
835 if (bdelay == 0)
836 iPlayerSpeedY = -3;
837 break;
839 default:
840 if (last_button == ACTION
841 #ifdef ACTION2
842 || last_button == ACTION2
843 #endif
845 bdelay = 3;
846 if (bdelay == 0)
847 iPlayerSpeedY = 4;
849 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
850 return PLUGIN_USB_CONNECTED;
851 break;
853 last_button = move_button;
855 if (bdelay < 0) {
856 iPlayerSpeedY = bdelay;
857 bdelay++;
858 } else if (bdelay > 0) {
859 iPlayerSpeedY = bdelay;
860 bdelay--;
863 iCameraPosX = iPlayerPosX - 25;
864 iPlayerPosX += iPlayerSpeedX;
865 iPlayerPosY += iPlayerSpeedY;
867 chopCounter++;
868 /* increase speed as we go along */
869 if (chopCounter == 100){
870 iPlayerSpeedX++;
871 chopCounter=0;
874 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
875 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
876 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
878 chopKillPlayer();
879 chopDrawScene();
880 ret = chopMenu(0);
881 if (ret != -1)
882 return ret;
885 for (i=0; i < NUMBER_OF_BLOCKS; i++)
886 if(mBlocks[i].bIsActive == 1)
887 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
888 chopKillPlayer();
889 chopDrawScene();
890 ret = chopMenu(0);
891 if (ret != -1)
892 return ret;
895 if (TIME_BEFORE(*rb->current_tick, end))
896 rb->sleep(end - *rb->current_tick); /* wait until time is over */
897 else
898 rb->yield();
901 return PLUGIN_OK;
904 static void chopDrawBlock(struct CBlock *mBlock)
906 int iPosX = (mBlock->iWorldX - iCameraPosX);
907 int iPosY = (mBlock->iWorldY);
908 #if LCD_DEPTH > 2
909 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
910 #elif LCD_DEPTH == 2
911 rb->lcd_set_foreground(LCD_BLACK);
912 #endif
913 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
914 SCALE(mBlock->iSizeY));
918 static void chopRenderTerrain(struct CTerrain *ter, bool isground)
921 int i = 1;
923 int oldx = 0;
925 while(i < ter->iNodesCount && oldx < iScreenX)
928 int x = ter->mNodes[i-1].x - iCameraPosX;
929 int y = ter->mNodes[i-1].y;
931 int x2 = ter->mNodes[i].x - iCameraPosX;
932 int y2 = ter->mNodes[i].y;
934 int ax, ay;
936 if ((y < y2) != isground)
938 ax = x2;
939 ay = y;
941 else
943 ax = x;
944 ay = y2;
946 #if LCD_DEPTH > 2
947 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
948 #elif LCD_DEPTH == 2
949 rb->lcd_set_foreground(LCD_DARKGRAY);
950 #endif
952 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
954 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
955 SCALE(ax), SCALE(ay));
957 if (isground)
959 y = ay;
960 y2 = (LCD_HEIGHT*SIZE);
962 else
964 y = 0;
965 y2 = ay;
967 if (y2-y > 0)
968 rb->lcd_fillrect(SCALE(x), SCALE(y), SCALE(x2-x)+1, SCALE(y2-y)+1);
970 oldx = x;
971 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 iCurrLevelMode = iLevelMode;
988 score = 0;
990 iRotorOffset = 0;
991 iPlayerPosX = 60;
992 iPlayerPosY = (iScreenY * 4) / 10;
993 iLastBlockPlacedPosX = 0;
994 iGravityTimerCountdown = 2;
995 chopCounter = 0;
996 iPlayerSpeedX = 3;
997 iPlayerSpeedY = 0;
998 iCameraPosX = 30;
1000 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1001 mParticles[i].bIsActive = 0;
1003 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1004 mBlocks[i].bIsActive = 0;
1006 g = iScreenY - 10;
1007 chopClearTerrain(&mGround);
1009 for (i=0; i < MAX_TERRAIN_NODES; i++)
1010 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1012 if (chopUpdateTerrainRecycling(&mGround) == 1)
1013 /* mirror the sky if we've changed the ground */
1014 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1016 if (iCurrLevelMode == 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;