Update
[anarch.git] / main_gbmeta.ino
blob70bcadbeeed9c8a08443ac021ccf6916a3944572
1 /**
2   @file main_gbmeta.ino
4   This is Gamebuino Meta implementation of the game front end, using the
5   official library. Leaving out the library bloat could probably optimize this.
7   To compile using Arduin IDE you need to copy this file as well as all
8   necessary .h files into a project folder, then open the project and compile.
9   Do NOT put .c and .cpp files into the folder, stupid Arduino tries to compile
10   them even if they're not needed.
12   DON'T FORGET to set compiler flag to -O3 (default is -Os). With Arduino IDE
13   this is done in platform.txt file. 
15   by Miloslav Ciz (drummyfish), 2019
17   Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
18   plus a waiver of all other intellectual property. The goal of this work is to
19   be and remain completely in the public domain forever, available for any use
20   whatsoever.
23 #include <Gamebuino-Meta.h>
25 #define SFG_CAN_EXIT 0
26 #define SFG_FPS 22
27 #define SFG_TIME_MULTIPLIER 900    /* Without this the game seems too fast. This
28                                       also achieves an effective FPS of about
29                                       17. */
30 #define SFG_SCREEN_RESOLUTION_X 80
31 #define SFG_SCREEN_RESOLUTION_Y 64
32 #define SFG_RESOLUTION_SCALEDOWN 1
33 #define SFG_RAYCASTING_MAX_STEPS 11
34 #define SFG_RAYCASTING_MAX_HITS 3
35 #define SFG_RAYCASTING_SUBSAMPLE 2
36 #define SFG_DIMINISH_SPRITES 0
37 #define SFG_DITHERED_SHADOW 0
38 #define SFG_PLAYER_TURN_SPEED 135
40 #include "game.h"
42 Gamebuino_Meta::Color palette[256];
44 uint8_t blinkFramesLeft;
46 void blinkLED(Gamebuino_Meta::Color color)
48   gb.lights.fill(color);
49   blinkFramesLeft = 5;
52 const Gamebuino_Meta::SaveDefault saveDefault[] =
53   { { 0, SAVETYPE_BLOB, SFG_SAVE_SIZE, 0 } };
55 void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
57   Gamebuino_Meta::Color c = palette[colorIndex];
58   gb.display.drawPixel(x,y,c);
61 void SFG_sleepMs(uint16_t timeMs)
65 int8_t SFG_keyPressed(uint8_t key)
67   Gamebuino_Meta::Button button;
69   switch (key)
70   {
71     case SFG_KEY_UP: button = BUTTON_UP; break;
72     case SFG_KEY_RIGHT: button = BUTTON_RIGHT; break;
73     case SFG_KEY_DOWN: button = BUTTON_DOWN; break;
74     case SFG_KEY_LEFT: button = BUTTON_LEFT; break;
75     case SFG_KEY_A: button = BUTTON_A; break;
76     case SFG_KEY_B: button = BUTTON_B; break;
77     case SFG_KEY_C: button = BUTTON_MENU; break;
78     default: return 0; break;
79   }
81   return gb.buttons.timeHeld(button) > 0;
84 void SFG_processEvent(uint8_t event, uint8_t value)
86   switch (event)
87   {
88     case SFG_EVENT_LEVEL_STARTS: blinkLED(BLUE); break;
89     case SFG_EVENT_PLAYER_HURT: blinkLED(RED); break;
90     case SFG_EVENT_LEVEL_WON: blinkLED(YELLOW); break;
91     default: break;
92   }
95 void SFG_getMouseOffset(int16_t *x, int16_t *y)
99 void SFG_setMusic(uint8_t value)
103 void SFG_save(uint8_t data[SFG_SAVE_SIZE])
105   gb.save.set(0,data,SFG_SAVE_SIZE);
108 uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
110   gb.save.get(0,data,SFG_SAVE_SIZE);
111   return 1;
114 void SFG_playSound(uint8_t soundIndex, uint8_t volume)
116   switch (soundIndex)
117   {
118     case 2: 
119       gb.sound.playCancel();
120       break;
122     case 5:
123       gb.sound.playOK();
124       break;
126     default:
127       gb.sound.playTick();
128       break;
129   }
132 uint32_t SFG_getTimeMs()
134   return gb.frameStartMicros / 1000;
137 void setup()
139   gb.begin();
140   gb.setFrameRate(SFG_FPS);
141   gb.save.config(saveDefault);
143   uint8_t data[SFG_SAVE_SIZE];
145   gb.save.get(0,data,SFG_SAVE_SIZE);
147   uint8_t allZeros = 1;
149   for (uint8_t i = 0; i < SFG_SAVE_SIZE; ++i)
150     if (data[i] != 0)
151     {
152       allZeros = 0;
153       break;
154     }
156   if (allZeros) // 1st time save?
157   {
158     SFG_createDefaultSaveData(data);
159     gb.save.set(0,data,SFG_SAVE_SIZE);
160   }
162   for (int i = 0; i < 256; ++i)
163   {
164     uint16_t rgb565 = paletteRGB565[i];
165     palette[i] = gb.createColor((rgb565 & 0xf800) >> 8,(rgb565 & 0x07e0) >> 3,(rgb565 & 0x001f) << 3);
166   }
168   SFG_init();
169   blinkLED(RED);
172 uint8_t stop = 0;
174 void loop()
176   if (stop)
177     return;
179   while(!gb.update())
180   {
181   }
183   if (blinkFramesLeft != 0)
184   {
185     if (blinkFramesLeft == 1)
186       gb.lights.clear();
188     blinkFramesLeft--;
189   }
191   SFG_mainLoopBody();
192   
193   if (
194     gb.buttons.timeHeld(BUTTON_LEFT) >= 255 &&
195     gb.buttons.timeHeld(BUTTON_RIGHT) >= 255 &&
196     gb.buttons.timeHeld(BUTTON_B) >= 255)
197   {
198     // holding L+R+B in menu will erase all saved data
200     gb.save.del(0);
201     stop = 1;
202   }
204 #if 0
205   // debuggin performance
206   gb.display.setCursor(1,1);
207   gb.display.print(gb.getCpuLoad());
208 #endif