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