New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / plugins / chopper.c
blobb497b161b531750e25211b0edd5f23882134c810
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"
28 PLUGIN_HEADER
31 Still To do:
32 - Make original speed and further increases in speed depend more on screen size
33 - attempt to make the tunnels get narrower as the game goes on
34 - make the chopper look better, maybe a picture, and scale according
35 to screen size
36 - use textures for the color screens for background and terrain,
37 eg stars on background
38 - allow choice of different levels [later: different screen themes]
39 - better high score handling, improved screen etc.
42 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
44 #define QUIT BUTTON_OFF
45 #define ACTION BUTTON_UP
46 #define ACTION2 BUTTON_SELECT
47 #define ACTIONTEXT "SELECT"
49 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
50 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
53 #define QUIT BUTTON_MENU
54 #define ACTION BUTTON_SELECT
55 #define ACTIONTEXT "SELECT"
57 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD /* grayscale at the moment */
59 #define QUIT BUTTON_POWER
60 #define ACTION BUTTON_UP
61 #define ACTION2 BUTTON_SELECT
62 #define ACTIONTEXT "SELECT"
64 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
65 #define QUIT BUTTON_POWER
66 #define ACTION BUTTON_RIGHT
67 #define ACTIONTEXT "RIGHT"
69 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
70 (CONFIG_KEYPAD == SANSA_C200_PAD)
71 #define QUIT BUTTON_POWER
72 #define ACTION BUTTON_SELECT
73 #define ACTIONTEXT "SELECT"
75 #elif CONFIG_KEYPAD == GIGABEAT_PAD
76 #define QUIT BUTTON_MENU
77 #define ACTION BUTTON_SELECT
78 #define ACTIONTEXT "SELECT"
80 #elif CONFIG_KEYPAD == RECORDER_PAD
81 #define QUIT BUTTON_OFF
82 #define ACTION BUTTON_PLAY
83 #define ACTIONTEXT "PLAY"
85 #elif CONFIG_KEYPAD == ONDIO_PAD
86 #define QUIT BUTTON_OFF
87 #define ACTION BUTTON_UP
88 #define ACTION2 BUTTON_MENU
89 #define ACTIONTEXT "UP"
91 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
92 #define QUIT BUTTON_BACK
93 #define ACTION BUTTON_SELECT
94 #define ACTION2 BUTTON_MENU
95 #define ACTIONTEXT "SELECT"
97 #elif CONFIG_KEYPAD == MROBE100_PAD
98 #define QUIT BUTTON_POWER
99 #define ACTION BUTTON_SELECT
100 #define ACTIONTEXT "SELECT"
102 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
103 #define QUIT BUTTON_RC_REC
104 #define ACTION BUTTON_RC_PLAY
105 #define ACTION2 BUTTON_RC_MODE
106 #define ACTIONTEXT "PLAY"
108 #elif CONFIG_KEYPAD == COWOND2_PAD
109 #define QUIT BUTTON_POWER
111 #elif CONFIG_KEYPAD == IAUDIO67_PAD
112 #define QUIT BUTTON_POWER
113 #define ACTION BUTTON_PLAY
114 #define ACTION2 BUTTON_STOP
115 #define ACTIONTEXT "PLAY"
117 #else
118 #error No keymap defined!
119 #endif
121 #ifdef HAVE_TOUCHSCREEN
122 #ifndef QUIT
123 #define QUIT BUTTON_TOPLEFT
124 #endif
125 #ifndef ACTION
126 #define ACTION BUTTON_BOTTOMLEFT
127 #endif
128 #ifndef ACTION2
129 #define ACTION2 BUTTON_BOTTOMRIGHT
130 #endif
131 #ifndef ACTIONTEXT
132 #define ACTIONTEXT "BOTTOMRIGHT"
133 #endif
134 #endif
136 static const struct plugin_api* rb;
138 #define NUMBER_OF_BLOCKS 8
139 #define NUMBER_OF_PARTICLES 3
140 #define MAX_TERRAIN_NODES 15
142 #define LEVEL_MODE_NORMAL 0
143 #define LEVEL_MODE_STEEP 1
145 #if LCD_WIDTH <= 112
146 #define CYCLETIME 100
147 #define SCALE(x) ((x)==1 ? (x) : ((x) >> 1))
148 #define SIZE 2
149 #else
150 #define CYCLETIME 60
151 #define SCALE(x) (x)
152 #define SIZE 1
153 #endif
155 /*Chopper's local variables to track the terrain position etc*/
156 static int chopCounter;
157 static int iRotorOffset;
158 static int iScreenX;
159 static int iScreenY;
160 static int iPlayerPosX;
161 static int iPlayerPosY;
162 static int iCameraPosX;
163 static int iPlayerSpeedX;
164 static int iPlayerSpeedY;
165 static int iLastBlockPlacedPosX;
166 static int iGravityTimerCountdown;
167 static int iPlayerAlive;
168 static int iLevelMode;
169 static int blockh,blockw;
170 static int highscore;
171 static int score;
173 #define CFG_FILE "chopper.cfg"
174 #define MAX_POINTS 50000
175 static struct configdata config[] =
177 {TYPE_INT, 0, MAX_POINTS, &highscore, "highscore", NULL, NULL}
180 struct CBlock
182 int iWorldX;
183 int iWorldY;
185 int iSizeX;
186 int iSizeY;
188 int bIsActive;
191 struct CParticle
193 int iWorldX;
194 int iWorldY;
196 int iSpeedX;
197 int iSpeedY;
199 int bIsActive;
202 struct CTerrainNode
204 int x;
205 int y;
208 struct CTerrain
210 struct CTerrainNode mNodes[MAX_TERRAIN_NODES];
211 int iNodesCount;
212 int iLastNodePlacedPosX;
215 struct CBlock mBlocks[NUMBER_OF_BLOCKS];
216 struct CParticle mParticles[NUMBER_OF_PARTICLES];
218 struct CTerrain mGround;
219 struct CTerrain mRoof;
221 /*Function declarations*/
222 static void chopDrawParticle(struct CParticle *mParticle);
223 static void chopDrawBlock(struct CBlock *mBlock);
224 static void chopRenderTerrain(struct CTerrain *ter);
225 void chopper_load(bool newgame);
226 void cleanup_chopper(void);
228 static void chopDrawPlayer(int x,int y) /* These are SCREEN coords, not world!*/
231 #if LCD_DEPTH > 2
232 rb->lcd_set_foreground(LCD_RGBPACK(50,50,200));
233 #elif LCD_DEPTH == 2
234 rb->lcd_set_foreground(LCD_DARKGRAY);
235 #endif
236 rb->lcd_fillrect(SCALE(x+6), SCALE(y+2), SCALE(12), SCALE(9));
237 rb->lcd_fillrect(SCALE(x-3), SCALE(y+6), SCALE(20), SCALE(3));
239 #if LCD_DEPTH > 2
240 rb->lcd_set_foreground(LCD_RGBPACK(50,50,50));
241 #elif LCD_DEPTH == 2
242 rb->lcd_set_foreground(LCD_DARKGRAY);
243 #endif
244 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(2), SCALE(3));
245 rb->lcd_fillrect(SCALE(x+10), SCALE(y), SCALE(1), SCALE(3));
247 #if LCD_DEPTH > 2
248 rb->lcd_set_foreground(LCD_RGBPACK(40,40,100));
249 #elif LCD_DEPTH == 2
250 rb->lcd_set_foreground(LCD_BLACK);
251 #endif
252 rb->lcd_drawline(SCALE(x), SCALE(y+iRotorOffset), SCALE(x+20),
253 SCALE(y-iRotorOffset));
255 #if LCD_DEPTH > 2
256 rb->lcd_set_foreground(LCD_RGBPACK(20,20,50));
257 #elif LCD_DEPTH == 2
258 rb->lcd_set_foreground(LCD_BLACK);
259 #endif
260 rb->lcd_fillrect(SCALE(x - 2), SCALE(y + 5), SCALE(2), SCALE(5));
264 static void chopClearTerrain(struct CTerrain *ter)
266 ter->iNodesCount = 0;
270 int iR(int low,int high)
272 return low+rb->rand()%(high-low+1);
275 static void chopCopyTerrain(struct CTerrain *src, struct CTerrain *dest,
276 int xOffset,int yOffset)
278 int i=0;
280 while(i < src->iNodesCount)
282 dest->mNodes[i].x = src->mNodes[i].x + xOffset;
283 dest->mNodes[i].y = src->mNodes[i].y + yOffset;
285 i++;
288 dest->iNodesCount = src->iNodesCount;
289 dest->iLastNodePlacedPosX = src->iLastNodePlacedPosX;
293 static void chopAddTerrainNode(struct CTerrain *ter, int x, int y)
295 int i=0;
297 if(ter->iNodesCount + 1 >= MAX_TERRAIN_NODES)
299 /* DEBUGF("ERROR: Not enough nodes!\n"); */
300 return;
303 ter->iNodesCount++;
305 i = ter->iNodesCount - 1;
307 ter->mNodes[i].x = x;
308 ter->mNodes[i].y= y;
310 ter->iLastNodePlacedPosX = x;
314 static void chopTerrainNodeDeleteAndShift(struct CTerrain *ter,int nodeIndex)
316 int i=nodeIndex;
318 while( i < ter->iNodesCount )
320 ter->mNodes[i - 1] = ter->mNodes[i];
321 i++;
324 ter->iNodesCount--;
329 int chopUpdateTerrainRecycling(struct CTerrain *ter)
331 int i=1;
332 int ret = 0;
333 int iNewNodePos,g,v;
334 while(i < ter->iNodesCount)
337 if( iCameraPosX > ter->mNodes[i].x)
340 chopTerrainNodeDeleteAndShift(ter,i);
342 iNewNodePos = ter->iLastNodePlacedPosX + 50;
343 g = iScreenY - 10;
345 v = 3*iPlayerSpeedX;
346 if(v>50)
347 v=50;
348 if(iLevelMode == LEVEL_MODE_STEEP)
349 v*=5;
351 chopAddTerrainNode(ter,iNewNodePos,g - iR(-v,v));
352 ret=1;
356 i++;
360 return 1;
363 int chopTerrainHeightAtPoint(struct CTerrain *ter, int pX)
366 int iNodeIndexOne=0,iNodeIndexTwo=0, h, terY1, terY2, terX1, terX2, a, b;
367 float c,d;
369 int i=0;
370 for(i=1;i<MAX_TERRAIN_NODES;i++)
372 if(ter->mNodes[i].x > pX)
374 iNodeIndexOne = i - 1;
375 break;
380 iNodeIndexTwo = iNodeIndexOne + 1;
381 terY1 = ter->mNodes[iNodeIndexOne].y;
382 terY2 = ter->mNodes[iNodeIndexTwo].y;
384 terX1 = 0;
385 terX2 = ter->mNodes[iNodeIndexTwo].x - ter->mNodes[iNodeIndexOne].x;
387 pX-= ter->mNodes[iNodeIndexOne].x;
389 a = terY2 - terY1;
390 b = terX2;
391 c = pX;
392 d = (c/b) * a;
394 h = d + terY1;
396 return h;
400 int chopPointInTerrain(struct CTerrain *ter, int pX, int pY, int iTestType)
402 int h = chopTerrainHeightAtPoint(ter, pX);
404 if(iTestType == 0)
405 return (pY > h);
406 else
407 return (pY < h);
410 static void chopAddBlock(int x,int y,int sx,int sy, int indexOverride)
412 int i=0;
414 if(indexOverride < 0)
416 while(mBlocks[i].bIsActive && i < NUMBER_OF_BLOCKS)
417 i++;
418 if(i==NUMBER_OF_BLOCKS)
420 DEBUGF("No blocks!\n");
421 return;
424 else
425 i = indexOverride;
427 mBlocks[i].bIsActive = 1;
428 mBlocks[i].iWorldX = x;
429 mBlocks[i].iWorldY = y;
430 mBlocks[i].iSizeX = sx;
431 mBlocks[i].iSizeY = sy;
433 iLastBlockPlacedPosX = x;
436 static void chopAddParticle(int x,int y,int sx,int sy)
438 int i=0;
440 while(mParticles[i].bIsActive && i < NUMBER_OF_PARTICLES)
441 i++;
443 if(i==NUMBER_OF_PARTICLES)
444 return;
446 mParticles[i].bIsActive = 1;
447 mParticles[i].iWorldX = x;
448 mParticles[i].iWorldY = y;
449 mParticles[i].iSpeedX = sx;
450 mParticles[i].iSpeedY = sy;
453 static void chopGenerateBlockIfNeeded(void)
455 int i=0;
456 int DistSpeedX = iPlayerSpeedX * 5;
457 if(DistSpeedX<200) DistSpeedX = 200;
459 while(i < NUMBER_OF_BLOCKS)
461 if(!mBlocks[i].bIsActive)
463 int iX,iY,sX,sY;
465 iX = iLastBlockPlacedPosX + (350-DistSpeedX);
466 sX = blockw;
468 iY = iR(0,iScreenY);
469 sY = blockh + iR(1,blockh/3);
471 chopAddBlock(iX,iY,sX,sY,i);
474 i++;
479 static int chopBlockCollideWithPlayer(struct CBlock *mBlock)
481 int px = iPlayerPosX;
482 int py = iPlayerPosY;
484 int x = mBlock->iWorldX-17;
485 int y = mBlock->iWorldY-11;
487 int x2 = x + mBlock->iSizeX+17;
488 int y2 = y + mBlock->iSizeY+11;
490 if(px>x && px<x2)
492 if(py>y && py<y2)
494 return 1;
498 return 0;
501 static int chopBlockOffscreen(struct CBlock *mBlock)
503 if(mBlock->iWorldX + mBlock->iSizeX < iCameraPosX)
504 return 1;
505 else
506 return 0;
509 static int chopParticleOffscreen(struct CParticle *mParticle)
511 if (mParticle->iWorldX < iCameraPosX || mParticle->iWorldY < 0 ||
512 mParticle->iWorldY > iScreenY || mParticle->iWorldX > iCameraPosX +
513 iScreenX)
515 return 1;
517 else
518 return 0;
521 static void chopKillPlayer(void)
523 int i, button;
525 for (i = 0; i < NUMBER_OF_PARTICLES; i++) {
526 mParticles[i].bIsActive = 0;
527 chopAddParticle(iPlayerPosX + iR(0,20), iPlayerPosY + iR(0,20),
528 iR(-2,2), iR(-2,2));
531 iPlayerAlive--;
533 if (iPlayerAlive == 0) {
534 rb->lcd_set_drawmode(DRMODE_FG);
535 #if LCD_DEPTH >= 2
536 rb->lcd_set_foreground(LCD_LIGHTGRAY);
537 #endif
538 rb->splash(HZ, "Game Over");
540 if (score > highscore) {
541 char scoretext[30];
542 highscore = score;
543 rb->snprintf(scoretext, sizeof(scoretext), "New High Score: %d",
544 highscore);
545 rb->splash(HZ*2, scoretext);
548 rb->splash(HZ/4, "Press " ACTIONTEXT " to continue");
549 rb->lcd_update();
551 rb->lcd_set_drawmode(DRMODE_SOLID);
553 while (true) {
554 button = rb->button_get(true);
555 if (button == ACTION
556 #ifdef ACTION2
557 || button == ACTION2
558 #endif
560 while (true) {
561 button = rb->button_get(true);
562 if (button == (ACTION | BUTTON_REL)
563 #ifdef ACTION2
564 || button == (ACTION2 | BUTTON_REL)
565 #endif
567 chopper_load(true);
568 return;
574 } else
575 chopper_load(false);
579 static void chopDrawTheWorld(void)
581 int i=0;
583 while(i < NUMBER_OF_BLOCKS)
585 if(mBlocks[i].bIsActive)
587 if(chopBlockOffscreen(&mBlocks[i]) == 1)
588 mBlocks[i].bIsActive = 0;
589 else
590 chopDrawBlock(&mBlocks[i]);
593 i++;
596 i=0;
598 while(i < NUMBER_OF_PARTICLES)
600 if(mParticles[i].bIsActive)
602 if(chopParticleOffscreen(&mParticles[i]) == 1)
603 mParticles[i].bIsActive = 0;
604 else
605 chopDrawParticle(&mParticles[i]);
608 i++;
611 chopRenderTerrain(&mGround);
612 chopRenderTerrain(&mRoof);
616 static void chopDrawParticle(struct CParticle *mParticle)
619 int iPosX = (mParticle->iWorldX - iCameraPosX);
620 int iPosY = (mParticle->iWorldY);
621 #if LCD_DEPTH > 2
622 rb->lcd_set_foreground(LCD_RGBPACK(192,192,192));
623 #elif LCD_DEPTH == 2
624 rb->lcd_set_foreground(LCD_LIGHTGRAY);
625 #endif
626 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(3), SCALE(3));
630 static void chopDrawScene(void)
632 char s[30];
633 int w;
634 #if LCD_DEPTH > 2
635 rb->lcd_set_background(LCD_BLACK);
636 #elif LCD_DEPTH == 2
637 rb->lcd_set_background(LCD_WHITE);
638 #endif
639 rb->lcd_clear_display();
641 chopDrawTheWorld();
642 chopDrawPlayer(iPlayerPosX - iCameraPosX, iPlayerPosY);
644 score = -20 + iPlayerPosX/3;
646 #if LCD_DEPTH == 1
647 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
648 #else
649 rb->lcd_set_drawmode(DRMODE_FG);
650 #endif
652 #if LCD_DEPTH > 2
653 rb->lcd_set_foreground(LCD_BLACK);
654 #elif LCD_DEPTH == 2
655 rb->lcd_set_foreground(LCD_WHITE);
656 #endif
658 #if LCD_WIDTH <= 128
659 rb->snprintf(s, sizeof(s), "Dist: %d", score);
660 #else
661 rb->snprintf(s, sizeof(s), "Distance: %d", score);
662 #endif
663 rb->lcd_getstringsize(s, &w, NULL);
664 rb->lcd_putsxy(2, 2, s);
665 if (score < highscore)
667 int w2;
668 #if LCD_WIDTH <= 128
669 rb->snprintf(s, sizeof(s), "Hi: %d", highscore);
670 #else
671 rb->snprintf(s, sizeof(s), "Best: %d", highscore);
672 #endif
673 rb->lcd_getstringsize(s, &w2, NULL);
674 if (LCD_WIDTH - 2 - w2 > w + 2)
675 rb->lcd_putsxy(LCD_WIDTH - 2 - w2, 2, s);
677 rb->lcd_set_drawmode(DRMODE_SOLID);
679 rb->lcd_update();
682 static int chopMenu(int menunum)
684 int result = (menunum==0)?0:1;
685 int res = 0;
686 bool menu_quit = false;
688 static const struct opt_items levels[2] = {
689 { "Normal", -1 },
690 { "Steep", -1 },
693 MENUITEM_STRINGLIST(menu,"Chopper Menu",NULL,"Start New Game","Resume Game",
694 "Level","Quit");
696 #ifdef HAVE_LCD_COLOR
697 rb->lcd_set_foreground(LCD_WHITE);
698 rb->lcd_set_background(LCD_BLACK);
699 #elif LCD_DEPTH == 2
700 rb->lcd_set_foreground(LCD_BLACK);
701 rb->lcd_set_background(LCD_WHITE);
702 #endif
704 rb->lcd_clear_display();
706 while (!menu_quit) {
707 switch(rb->do_menu(&menu, &result, NULL, false))
709 case 0: /* Start New Game */
710 menu_quit=true;
711 chopper_load(true);
712 res = -1;
713 break;
714 case 1: /* Resume Game */
715 if(menunum==1) {
716 menu_quit=true;
717 res = -1;
718 } else if(menunum==0){
719 rb->splash(HZ, "No game to resume");
721 break;
722 case 2:
723 rb->set_option("Level", &iLevelMode, INT, levels, 2, NULL);
724 break;
725 case 3:
726 menu_quit=true;
727 res = PLUGIN_OK;
728 break;
729 case MENU_ATTACHED_USB:
730 menu_quit=true;
731 res = PLUGIN_USB_CONNECTED;
732 break;
735 rb->lcd_clear_display();
736 return res;
739 static int chopGameLoop(void)
741 int move_button, ret;
742 bool exit=false;
743 int end, i=0, bdelay=0, last_button=BUTTON_NONE;
745 if (chopUpdateTerrainRecycling(&mGround) == 1)
746 /* mirror the sky if we've changed the ground */
747 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
749 ret = chopMenu(0);
750 if (ret != -1)
751 return PLUGIN_OK;
753 chopDrawScene();
755 while (!exit) {
757 end = *rb->current_tick + (CYCLETIME * HZ) / 1000;
759 if(chopUpdateTerrainRecycling(&mGround) == 1)
760 /* mirror the sky if we've changed the ground */
761 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
763 iRotorOffset = iR(-1,1);
765 /* We need to have this here so particles move when we're dead */
767 for (i=0; i < NUMBER_OF_PARTICLES; i++)
768 if(mParticles[i].bIsActive == 1)
770 mParticles[i].iWorldX += mParticles[i].iSpeedX;
771 mParticles[i].iWorldY += mParticles[i].iSpeedY;
774 /* Redraw the main window: */
775 chopDrawScene();
778 iGravityTimerCountdown--;
780 if(iGravityTimerCountdown <= 0)
782 iGravityTimerCountdown = 3;
783 chopAddParticle(iPlayerPosX, iPlayerPosY+5, 0, 0);
786 if(iLevelMode == LEVEL_MODE_NORMAL)
787 chopGenerateBlockIfNeeded();
790 move_button=rb->button_status();
791 if (rb->button_get(false) == QUIT) {
792 ret = chopMenu(1);
793 if (ret != -1)
794 return PLUGIN_OK;
795 bdelay = 0;
796 last_button = BUTTON_NONE;
797 move_button = BUTTON_NONE;
800 switch (move_button) {
801 case ACTION:
802 #ifdef ACTION2
803 case ACTION2:
804 #endif
805 if (last_button != ACTION
806 #ifdef ACTION2
807 && last_button != ACTION2
808 #endif
810 bdelay = -2;
811 if (bdelay == 0)
812 iPlayerSpeedY = -3;
813 break;
815 default:
816 if (last_button == ACTION
817 #ifdef ACTION2
818 || last_button == ACTION2
819 #endif
821 bdelay = 3;
822 if (bdelay == 0)
823 iPlayerSpeedY = 4;
825 if (rb->default_event_handler(move_button) == SYS_USB_CONNECTED)
826 return PLUGIN_USB_CONNECTED;
827 break;
829 last_button = move_button;
831 if (bdelay < 0) {
832 iPlayerSpeedY = bdelay;
833 bdelay++;
834 } else if (bdelay > 0) {
835 iPlayerSpeedY = bdelay;
836 bdelay--;
839 iCameraPosX = iPlayerPosX - 25;
840 iPlayerPosX += iPlayerSpeedX;
841 iPlayerPosY += iPlayerSpeedY;
843 chopCounter++;
844 /* increase speed as we go along */
845 if (chopCounter == 100){
846 iPlayerSpeedX++;
847 chopCounter=0;
850 if (iPlayerPosY > iScreenY-10 || iPlayerPosY < -5 ||
851 chopPointInTerrain(&mGround, iPlayerPosX, iPlayerPosY + 10, 0) ||
852 chopPointInTerrain(&mRoof, iPlayerPosX ,iPlayerPosY, 1))
854 chopKillPlayer();
855 chopDrawScene();
856 ret = chopMenu(0);
857 if (ret != -1)
858 return ret;
861 for (i=0; i < NUMBER_OF_BLOCKS; i++)
862 if(mBlocks[i].bIsActive == 1)
863 if(chopBlockCollideWithPlayer(&mBlocks[i])) {
864 chopKillPlayer();
865 chopDrawScene();
866 ret = chopMenu(0);
867 if (ret != -1)
868 return ret;
871 if (end > *rb->current_tick)
872 rb->sleep(end-*rb->current_tick);
873 else
874 rb->yield();
877 return PLUGIN_OK;
880 static void chopDrawBlock(struct CBlock *mBlock)
882 int iPosX = (mBlock->iWorldX - iCameraPosX);
883 int iPosY = (mBlock->iWorldY);
884 #if LCD_DEPTH > 2
885 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
886 #elif LCD_DEPTH == 2
887 rb->lcd_set_foreground(LCD_BLACK);
888 #endif
889 rb->lcd_fillrect(SCALE(iPosX), SCALE(iPosY), SCALE(mBlock->iSizeX),
890 SCALE(mBlock->iSizeY));
894 static void chopRenderTerrain(struct CTerrain *ter)
897 int i=1;
899 int oldx=0;
901 int ay=0;
902 if(ter->mNodes[0].y < (LCD_HEIGHT*SIZE)/2)
903 ay=0;
904 else
905 ay=(LCD_HEIGHT*SIZE);
907 while(i < ter->iNodesCount && oldx < iScreenX)
910 int x = ter->mNodes[i-1].x - iCameraPosX;
911 int y = ter->mNodes[i-1].y;
913 int x2 = ter->mNodes[i].x - iCameraPosX;
914 int y2 = ter->mNodes[i].y;
915 #if LCD_DEPTH > 2
916 rb->lcd_set_foreground(LCD_RGBPACK(100,255,100));
917 #elif LCD_DEPTH == 2
918 rb->lcd_set_foreground(LCD_DARKGRAY);
919 #endif
921 rb->lcd_drawline(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2));
923 xlcd_filltriangle(SCALE(x), SCALE(y), SCALE(x2), SCALE(y2),
924 SCALE(x2), SCALE(ay));
925 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x2), SCALE(y2),
926 SCALE(x2), SCALE(ay));
928 if (ay == 0)
929 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
930 SCALE(x2), SCALE(y2 / 2));
931 else
932 xlcd_filltriangle(SCALE(x), SCALE(ay), SCALE(x), SCALE(y),
933 SCALE(x2), SCALE((LCD_HEIGHT*SIZE) -
934 ((LCD_HEIGHT*SIZE) - y2) / 2));
936 oldx = x;
937 i++;
943 void chopper_load(bool newgame)
946 int i;
947 int g;
949 if (newgame) {
950 iScreenX = LCD_WIDTH * SIZE;
951 iScreenY = LCD_HEIGHT * SIZE;
952 blockh = iScreenY / 5;
953 blockw = iScreenX / 20;
954 iPlayerAlive = 1;
955 score = 0;
957 iRotorOffset = 0;
958 iPlayerPosX = 60;
959 iPlayerPosY = (iScreenY * 4) / 10;
960 iLastBlockPlacedPosX = 0;
961 iGravityTimerCountdown = 2;
962 chopCounter = 0;
963 iPlayerSpeedX = 3;
964 iPlayerSpeedY = 0;
965 iCameraPosX = 30;
967 for (i=0; i < NUMBER_OF_PARTICLES; i++)
968 mParticles[i].bIsActive = 0;
970 for (i=0; i < NUMBER_OF_BLOCKS; i++)
971 mBlocks[i].bIsActive = 0;
973 g = iScreenY - 10;
974 chopClearTerrain(&mGround);
976 for (i=0; i < MAX_TERRAIN_NODES; i++)
977 chopAddTerrainNode(&mGround,i * 30,g - iR(0,20));
979 if (chopUpdateTerrainRecycling(&mGround) == 1)
980 /* mirror the sky if we've changed the ground */
981 chopCopyTerrain(&mGround, &mRoof, 0, - ( (iScreenY * 3) / 4));
983 iLevelMode = LEVEL_MODE_NORMAL;
984 if (iLevelMode == LEVEL_MODE_NORMAL)
985 /* make it a bit more exciting, cause it's easy terrain... */
986 iPlayerSpeedX *= 2;
989 /* this is the plugin entry point */
990 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
992 (void)parameter;
993 rb = api;
994 int ret;
996 rb->lcd_setfont(FONT_SYSFIXED);
997 #if LCD_DEPTH > 1
998 rb->lcd_set_backdrop(NULL);
999 #endif
1000 #ifdef HAVE_LCD_COLOR
1001 rb->lcd_set_background(LCD_BLACK);
1002 rb->lcd_set_foreground(LCD_WHITE);
1003 #endif
1005 /* Turn off backlight timeout */
1006 backlight_force_on(rb); /* backlight control in lib/helper.c */
1008 rb->srand( *rb->current_tick );
1010 xlcd_init(rb);
1011 configfile_init(rb);
1012 configfile_load(CFG_FILE, config, 1, 0);
1014 chopper_load(true);
1015 ret = chopGameLoop();
1017 configfile_save(CFG_FILE, config, 1, 0);
1019 rb->lcd_setfont(FONT_UI);
1020 /* Turn on backlight timeout (revert to settings) */
1021 backlight_use_settings(rb); /* backlight control in lib/helper.c */
1023 return ret;