Correct bitmap color depth
[kugel-rb.git] / apps / plugins / chopper.c
blob67cb9ee7608037d16f6e01c5745b2bfd203da0b1
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 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
161 #define QUIT (BUTTON_REC|BUTTON_PLAY)
162 #define ACTION BUTTON_SELECT
163 #define ACTIONTEXT "SELECT"
165 #else
166 #error No keymap defined!
167 #endif
169 #ifdef HAVE_TOUCHSCREEN
170 #ifndef QUIT
171 #define QUIT BUTTON_TOPLEFT
172 #endif
173 #ifndef ACTION
174 #define ACTION BUTTON_BOTTOMLEFT
175 #endif
176 #ifndef ACTION2
177 #define ACTION2 BUTTON_BOTTOMRIGHT
178 #endif
179 #ifndef ACTIONTEXT
180 #define ACTIONTEXT "BOTTOMRIGHT"
181 #endif
182 #endif
184 #define NUMBER_OF_BLOCKS 8
185 #define NUMBER_OF_PARTICLES 3
186 #define MAX_TERRAIN_NODES 15
188 #define LEVEL_MODE_NORMAL 0
189 #define LEVEL_MODE_STEEP 1
191 #if LCD_HEIGHT <= 64
192 #define CYCLES 100
193 static inline int SCALE(int x)
195 return x == 1 ? x : x >> 1;
197 #define SIZE 2
198 #else
199 #define CYCLES 60
200 #define SCALE(x) (x)
201 #define SIZE 1
202 #endif
204 /* in 10 milisecond (ticks) */
205 #define CYCLETIME ((CYCLES*HZ)/1000)
207 /*Chopper's local variables to track the terrain position etc*/
208 static int chopCounter;
209 static int iRotorOffset;
210 static int iScreenX;
211 static int iScreenY;
212 static int iPlayerPosX;
213 static int iPlayerPosY;
214 static int iCameraPosX;
215 static int iPlayerSpeedX;
216 static int iPlayerSpeedY;
217 static int iLastBlockPlacedPosX;
218 static int iGravityTimerCountdown;
219 static int iPlayerAlive;
220 static int iLevelMode, iCurrLevelMode;
221 static int blockh,blockw;
222 static int highscore;
223 static int score;
225 #define CFG_FILE "chopper.cfg"
226 #define MAX_POINTS 50000
227 static struct configdata config[] =
229 {TYPE_INT, 0, MAX_POINTS, { .int_p = &highscore }, "highscore", NULL}
232 struct CBlock
234 int iWorldX;
235 int iWorldY;
237 int iSizeX;
238 int iSizeY;
240 int bIsActive;
243 struct CParticle
245 int iWorldX;
246 int iWorldY;
248 int iSpeedX;
249 int iSpeedY;
251 int bIsActive;
254 struct CTerrainNode
256 int x;
257 int y;
260 struct CTerrain
262 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
263 int iNodesCount;
264 int iLastNodePlacedPosX;
267 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
268 struct CParticle mParticles[NUMBER_OF_PARTICLES];
270 struct CTerrain mGround;
271 struct CTerrain mRoof;
273 /*Function declarations*/
274 static void chopDrawParticle(struct CParticle *mParticle);
275 static void chopDrawBlock(struct CBlock *mBlock);
276 static void chopRenderTerrain(struct CTerrain *ter, bool isground);
277 void chopper_load(bool newgame);
278 void cleanup_chopper(void);
280 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
283 #if LCD_DEPTH > 2
284 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
285 #elif LCD_DEPTH == 2
286 rb->lcd_set_foreground(LCD_DARKGRAY);
287 #endif
288 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
289 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
291 #if LCD_DEPTH > 2
292 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
293 #elif LCD_DEPTH == 2
294 rb->lcd_set_foreground(LCD_DARKGRAY);
295 #endif
296 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
297 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
299 #if LCD_DEPTH > 2
300 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
301 #elif LCD_DEPTH == 2
302 rb->lcd_set_foreground(LCD_BLACK);
303 #endif
304 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
305 SCALE(y-iRotorOffset));
307 #if LCD_DEPTH > 2
308 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
309 #elif LCD_DEPTH == 2
310 rb->lcd_set_foreground(LCD_BLACK);
311 #endif
312 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
316 static void chopClearTerrain(struct CTerrain *ter)
318 ter->iNodesCount = 0;
322 int iR(int low,int high)
324 return low+rb->rand()%(high-low+1);
327 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
328 int xOffset,int yOffset)
330 int i=0;
332 while(i < src->iNodesCount)
334 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
335 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
337 i++;
340 dest->iNodesCount = src->iNodesCount;
341 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
345 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
347 int i=0;
349 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
351 /* DEBUGF("ERROR: Not enough nodes!\n"); */
352 return;
355 ter->iNodesCount++;
357 i = ter->iNodesCount - 1;
359 ter->mNodes[i].x = x;
360 ter->mNodes[i].y= y;
362 ter->iLastNodePlacedPosX = x;
366 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
368 int i=nodeIndex;
370 while( i < ter->iNodesCount )
372 ter->mNodes[i - 1] = ter->mNodes[i];
373 i++;
376 ter->iNodesCount--;
381 int chopUpdateTerrainRecycling(struct CTerrain *ter)
383 int i=1;
384 int ret = 0;
385 int iNewNodePos,g,v;
386 while(i < ter->iNodesCount)
389 if( iCameraPosX > ter->mNodes[i].x)
392 chopTerrainNodeDeleteAndShift(ter,i);
394 iNewNodePos = ter->iLastNodePlacedPosX + 50;
395 g = iScreenY - 10;
397 v = 3*iPlayerSpeedX;
398 if(v>50)
399 v=50;
400 if(iCurrLevelMode == LEVEL_MODE_STEEP)
401 v*=5;
403 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
404 ret=1;
408 i++;
412 return 1;
415 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
418 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
419 float c,d;
421 int i=0;
422 for(i=1;i<MAX_TERRAIN_NODES;i++)
424 if(ter->mNodes[i].x > pX)
426 iNodeIndexOne = i - 1;
427 break;
432 iNodeIndexTwo = iNodeIndexOne + 1;
433 terY1 = ter->mNodes[iNodeIndexOne].y;
434 terY2 = ter->mNodes[iNodeIndexTwo].y;
436 terX1 = 0;
437 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
439 pX-= ter->mNodes[iNodeIndexOne].x;
441 a = terY2 - terY1;
442 b = terX2;
443 c = pX;
444 d = (c/b) * a;
446 h = d + terY1;
448 return h;
452 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
454 int h = chopTerrainHeightAtPoint(ter, pX);
456 if(iTestType == 0)
457 return (pY > h);
458 else
459 return (pY < h);
462 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
464 int i=0;
466 if(indexOverride < 0)
468 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
469 i++;
470 if(i==NUMBER_OF_BLOCKS)
472 DEBUGF("No blocks!\n");
473 return;
476 else
477 i = indexOverride;
479 mBlocks[i].bIsActive = 1;
480 mBlocks[i].iWorldX = x;
481 mBlocks[i].iWorldY = y;
482 mBlocks[i].iSizeX = sx;
483 mBlocks[i].iSizeY = sy;
485 iLastBlockPlacedPosX = x;
488 static void chopAddParticle(int x,int y,int sx,int sy)
490 int i=0;
492 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
493 i++;
495 if(i==NUMBER_OF_PARTICLES)
496 return;
498 mParticles[i].bIsActive = 1;
499 mParticles[i].iWorldX = x;
500 mParticles[i].iWorldY = y;
501 mParticles[i].iSpeedX = sx;
502 mParticles[i].iSpeedY = sy;
505 static void chopGenerateBlockIfNeeded(void)
507 int i=0;
508 int DistSpeedX = iPlayerSpeedX * 5;
509 if(DistSpeedX<200) DistSpeedX = 200;
511 while(i < NUMBER_OF_BLOCKS)
513 if(!mBlocks[i].bIsActive)
515 int iX,iY,sX,sY;
517 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
518 sX = blockw;
520 iY = iR(0,iScreenY);
521 sY = blockh + iR(1,blockh/3);
523 chopAddBlock(iX,iY,sX,sY,i);
526 i++;
531 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
533 int px = iPlayerPosX;
534 int py = iPlayerPosY;
536 int x = mBlock->iWorldX-17;
537 int y = mBlock->iWorldY-11;
539 int x2 = x + mBlock->iSizeX+17;
540 int y2 = y + mBlock->iSizeY+11;
542 if(px>x && px<x2)
544 if(py>y && py<y2)
546 return 1;
550 return 0;
553 static int chopBlockOffscreen(struct CBlock *mBlock)
555 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
556 return 1;
557 else
558 return 0;
561 static int chopParticleOffscreen(struct CParticle *mParticle)
563 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
564 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
565 iScreenX)
567 return 1;
569 else
570 return 0;
573 static void chopKillPlayer(void)
575 int i;
577 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
578 mParticles[i].bIsActive = 0;
579 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
580 iR(-2,2), iR(-2,2));
583 iPlayerAlive--;
585 if (iPlayerAlive == 0) {
586 rb->splash(HZ, "Game Over");
588 if (score > highscore) {
589 char scoretext[30];
590 highscore = score;
591 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
592 highscore);
593 rb->splash(HZ*2, scoretext);
595 } else
596 chopper_load(false);
599 static void chopDrawTheWorld(void)
601 int i=0;
603 while(i < NUMBER_OF_BLOCKS)
605 if(mBlocks[i].bIsActive)
607 if(chopBlockOffscreen(&mBlocks[i]) == 1)
608 mBlocks[i].bIsActive = 0;
609 else
610 chopDrawBlock(&mBlocks[i]);
613 i++;
616 i=0;
618 while(i < NUMBER_OF_PARTICLES)
620 if(mParticles[i].bIsActive)
622 if(chopParticleOffscreen(&mParticles[i]) == 1)
623 mParticles[i].bIsActive = 0;
624 else
625 chopDrawParticle(&mParticles[i]);
628 i++;
631 chopRenderTerrain(&mGround, true);
632 chopRenderTerrain(&mRoof, false);
636 static void chopDrawParticle(struct CParticle *mParticle)
639 int iPosX = (mParticle->iWorldX - iCameraPosX);
640 int iPosY = (mParticle->iWorldY);
641 #if LCD_DEPTH > 2
642 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
643 #elif LCD_DEPTH == 2
644 rb->lcd_set_foreground(LCD_LIGHTGRAY);
645 #endif
646 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
650 static void chopDrawScene(void)
652 char s[30];
653 int w;
654 #if LCD_DEPTH > 2
655 rb->lcd_set_background(LCD_BLACK);
656 #elif LCD_DEPTH == 2
657 rb->lcd_set_background(LCD_WHITE);
658 #endif
659 rb->lcd_clear_display();
660 chopDrawTheWorld();
661 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
663 score = -20 + iPlayerPosX/3;
665 #if LCD_DEPTH == 1
666 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
667 #else
668 rb->lcd_set_drawmode(DRMODE_FG);
669 #endif
671 #if LCD_DEPTH > 2
672 rb->lcd_set_foreground(LCD_BLACK);
673 #elif LCD_DEPTH == 2
674 rb->lcd_set_foreground(LCD_WHITE);
675 #endif
677 #if LCD_WIDTH <= 128
678 rb->snprintf(s, sizeof(s), "Dist: %d", score);
679 #else
680 rb->snprintf(s, sizeof(s), "Distance: %d", score);
681 #endif
682 rb->lcd_getstringsize(s, &w, NULL);
683 rb->lcd_putsxy(2, 2, s);
684 if (score < highscore)
686 int w2;
687 #if LCD_WIDTH <= 128
688 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
689 #else
690 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
691 #endif
692 rb->lcd_getstringsize(s, &w2, NULL);
693 if (LCD_WIDTH - 2 - w2 > w + 2)
694 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
696 rb->lcd_set_drawmode(DRMODE_SOLID);
698 rb->lcd_update();
701 static bool _ingame;
702 static int chopMenuCb(int action, const struct menu_item_ex *this_item)
704 if(action == ACTION_REQUEST_MENUITEM
705 && !_ingame && ((intptr_t)this_item)==0)
706 return ACTION_EXIT_MENUITEM;
707 return action;
709 static int chopMenu(int menunum)
711 int result = 0;
712 int res = 0;
713 bool menu_quit = false;
715 static const struct opt_items levels[2] = {
716 { "Normal", -1 },
717 { "Steep", -1 },
720 MENUITEM_STRINGLIST(menu,"Chopper Menu",chopMenuCb,
721 "Resume Game","Start New Game",
722 "Level","Playback Control","Quit");
723 _ingame = (menunum!=0);
725 #ifdef HAVE_LCD_COLOR
726 rb->lcd_set_foreground(LCD_WHITE);
727 rb->lcd_set_background(LCD_BLACK);
728 #elif LCD_DEPTH == 2
729 rb->lcd_set_foreground(LCD_BLACK);
730 rb->lcd_set_background(LCD_WHITE);
731 #endif
733 rb->lcd_clear_display();
734 rb->button_clear_queue();
736 while (!menu_quit) {
737 switch(rb->do_menu(&menu, &result, NULL, false))
739 case 0: /* Resume Game */
740 menu_quit=true;
741 res = -1;
742 break;
743 case 1: /* Start New Game */
744 menu_quit=true;
745 chopper_load(true);
746 res = -1;
747 break;
748 case 2:
749 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
750 break;
751 case 3:
752 playback_control(NULL);
753 break;
754 case 4:
755 menu_quit=true;
756 res = PLUGIN_OK;
757 break;
758 case MENU_ATTACHED_USB:
759 menu_quit=true;
760 res = PLUGIN_USB_CONNECTED;
761 break;
764 rb->lcd_clear_display();
765 return res;
768 static int chopGameLoop(void)
770 int move_button, ret;
771 bool exit=false;
772 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
774 if (chopUpdateTerrainRecycling(&mGround) == 1)
775 /* mirror the sky if we've changed the ground */
776 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
778 ret = chopMenu(0);
779 if (ret != -1)
780 return PLUGIN_OK;
782 chopDrawScene();
784 while (!exit) {
786 end = *rb->current_tick + CYCLETIME;
788 if(chopUpdateTerrainRecycling(&mGround) == 1)
789 /* mirror the sky if we've changed the ground */
790 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
792 iRotorOffset = iR(-1,1);
794 /* We need to have this here so particles move when we're dead */
796 for (i=0; i < NUMBER_OF_PARTICLES; i++)
797 if(mParticles[i].bIsActive == 1)
799 mParticles[i].iWorldX += mParticles[i].iSpeedX;
800 mParticles[i].iWorldY += mParticles[i].iSpeedY;
803 /* Redraw the main window: */
804 chopDrawScene();
807 iGravityTimerCountdown--;
809 if(iGravityTimerCountdown <= 0)
811 iGravityTimerCountdown = 3;
812 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
815 if(iCurrLevelMode == LEVEL_MODE_NORMAL)
816 chopGenerateBlockIfNeeded();
819 move_button=rb->button_status();
820 if (rb->button_get(false) == QUIT) {
821 ret = chopMenu(1);
822 if (ret != -1)
823 return PLUGIN_OK;
824 bdelay = 0;
825 last_button = BUTTON_NONE;
826 move_button = BUTTON_NONE;
829 switch (move_button) {
830 case ACTION:
831 #ifdef ACTION2
832 case ACTION2:
833 #endif
834 if (last_button != ACTION
835 #ifdef ACTION2
836 && last_button != ACTION2
837 #endif
839 bdelay = -2;
840 if (bdelay == 0)
841 iPlayerSpeedY = -3;
842 break;
844 default:
845 if (last_button == ACTION
846 #ifdef ACTION2
847 || last_button == ACTION2
848 #endif
850 bdelay = 3;
851 if (bdelay == 0)
852 iPlayerSpeedY = 4;
854 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
855 return PLUGIN_USB_CONNECTED;
856 break;
858 last_button = move_button;
860 if (bdelay < 0) {
861 iPlayerSpeedY = bdelay;
862 bdelay++;
863 } else if (bdelay > 0) {
864 iPlayerSpeedY = bdelay;
865 bdelay--;
868 iCameraPosX = iPlayerPosX - 25;
869 iPlayerPosX += iPlayerSpeedX;
870 iPlayerPosY += iPlayerSpeedY;
872 chopCounter++;
873 /* increase speed as we go along */
874 if (chopCounter == 100){
875 iPlayerSpeedX++;
876 chopCounter=0;
879 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
880 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
881 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
883 chopKillPlayer();
884 chopDrawScene();
885 ret = chopMenu(0);
886 if (ret != -1)
887 return ret;
890 for (i=0; i < NUMBER_OF_BLOCKS; i++)
891 if(mBlocks[i].bIsActive == 1)
892 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
893 chopKillPlayer();
894 chopDrawScene();
895 ret = chopMenu(0);
896 if (ret != -1)
897 return ret;
900 if (TIME_BEFORE(*rb->current_tick, end))
901 rb->sleep(end - *rb->current_tick); /* wait until time is over */
902 else
903 rb->yield();
906 return PLUGIN_OK;
909 static void chopDrawBlock(struct CBlock *mBlock)
911 int iPosX = (mBlock->iWorldX - iCameraPosX);
912 int iPosY = (mBlock->iWorldY);
913 #if LCD_DEPTH > 2
914 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
915 #elif LCD_DEPTH == 2
916 rb->lcd_set_foreground(LCD_BLACK);
917 #endif
918 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
919 SCALE(mBlock->iSizeY));
923 static void chopRenderTerrain(struct CTerrain *ter, bool isground)
926 int i = 1;
928 int oldx = 0;
930 while(i < ter->iNodesCount && oldx < iScreenX)
933 int x = ter->mNodes[i-1].x - iCameraPosX;
934 int y = ter->mNodes[i-1].y;
936 int x2 = ter->mNodes[i].x - iCameraPosX;
937 int y2 = ter->mNodes[i].y;
939 int ax, ay;
941 if ((y < y2) != isground)
943 ax = x2;
944 ay = y;
946 else
948 ax = x;
949 ay = y2;
951 #if LCD_DEPTH > 2
952 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
953 #elif LCD_DEPTH == 2
954 rb->lcd_set_foreground(LCD_DARKGRAY);
955 #endif
957 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
959 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
960 SCALE(ax), SCALE(ay));
962 if (isground)
964 y = ay;
965 y2 = (LCD_HEIGHT*SIZE);
967 else
969 y = 0;
970 y2 = ay;
972 if (y2-y > 0)
973 rb->lcd_fillrect(SCALE(x), SCALE(y), SCALE(x2-x)+1, SCALE(y2-y)+1);
975 oldx = x;
976 i++;
980 void chopper_load(bool newgame)
983 int i;
984 int g;
986 if (newgame) {
987 iScreenX = LCD_WIDTH * SIZE;
988 iScreenY = LCD_HEIGHT * SIZE;
989 blockh = iScreenY / 5;
990 blockw = iScreenX / 20;
991 iPlayerAlive = 1;
992 iCurrLevelMode = iLevelMode;
993 score = 0;
995 iRotorOffset = 0;
996 iPlayerPosX = 60;
997 iPlayerPosY = (iScreenY * 4) / 10;
998 iLastBlockPlacedPosX = 0;
999 iGravityTimerCountdown = 2;
1000 chopCounter = 0;
1001 iPlayerSpeedX = 3;
1002 iPlayerSpeedY = 0;
1003 iCameraPosX = 30;
1005 for (i=0; i < NUMBER_OF_PARTICLES; i++)
1006 mParticles[i].bIsActive = 0;
1008 for (i=0; i < NUMBER_OF_BLOCKS; i++)
1009 mBlocks[i].bIsActive = 0;
1011 g = iScreenY - 10;
1012 chopClearTerrain(&mGround);
1014 for (i=0; i < MAX_TERRAIN_NODES; i++)
1015 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
1017 if (chopUpdateTerrainRecycling(&mGround) == 1)
1018 /* mirror the sky if we've changed the ground */
1019 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
1021 if (iCurrLevelMode == LEVEL_MODE_NORMAL)
1022 /* make it a bit more exciting, cause it's easy terrain... */
1023 iPlayerSpeedX *= 2;
1026 /* this is the plugin entry point */
1027 enum plugin_status plugin_start(const void* parameter)
1029 (void)parameter;
1030 int ret;
1032 rb->lcd_setfont(FONT_SYSFIXED);
1033 #if LCD_DEPTH > 1
1034 rb->lcd_set_backdrop(NULL);
1035 #endif
1036 #ifdef HAVE_LCD_COLOR
1037 rb->lcd_set_background(LCD_BLACK);
1038 rb->lcd_set_foreground(LCD_WHITE);
1039 #endif
1041 /* Turn off backlight timeout */
1042 backlight_force_on(); /* backlight control in lib/helper.c */
1044 rb->srand( *rb->current_tick );
1046 configfile_load(CFG_FILE, config, 1, 0);
1048 chopper_load(true);
1049 ret = chopGameLoop();
1051 configfile_save(CFG_FILE, config, 1, 0);
1053 rb->lcd_setfont(FONT_UI);
1054 /* Turn on backlight timeout (revert to settings) */
1055 backlight_use_settings(); /* backlight control in lib/helper.c */
1057 return ret;