-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_arduino.cpp
256 lines (218 loc) · 5.52 KB
/
cross_arduino.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "cross_arduino.h"
// #define DEBUG_MODE
Arduboy2Base arduboy;
uint16_t buffer[8];
ArduboyTonesFX sound(arduboy.audio.enabled, buffer);
#define ARDBITMAP
#ifdef ARDBITMAP
#define ARDBITMAP_SBUF arduboy.getBuffer()
#include "ArdBitmap.h"
ArdBitmap<WIDTH, HEIGHT> ardbitmap;
#endif
unsigned long currentTime, frameTime, fps, frameMs = 0;
const unsigned int FRAME_RATE = 60; // Frame rate in frames per second
void cross_save(SaveData saveData) {
FX::saveGameState(saveData);
}
SaveData cross_load() {
SaveData saveData;
if (FX::loadGameState(saveData)) {
return saveData;
}
// If no data is found, save blank data
FX::loadGameState(saveData);
return saveData;
}
uint8_t checkDesired(uint8_t desired) {
uint8_t desiredKeyPresses[11] = {1,1,2,2,3,4,3,4,6,5,5};
if (arduboy.justPressed(UP_BUTTON)) {
return desiredKeyPresses[desired] == 1 ? 2 : 1;
} else if (arduboy.justPressed(DOWN_BUTTON)) {
return desiredKeyPresses[desired] == 2 ? 2 : 1;
} else if (arduboy.justPressed(LEFT_BUTTON)) {
return desiredKeyPresses[desired] == 3 ? 2 : 1;
} else if (arduboy.justPressed(RIGHT_BUTTON)) {
return desiredKeyPresses[desired] == 4 ? 2 : 1;
} else if (arduboy.justPressed(A_BUTTON)) {
return desiredKeyPresses[desired] == 5 ? 2 : 1;
} else if (arduboy.justPressed(B_BUTTON)) {
return desiredKeyPresses[desired] == 6 ? 2 : 1;
};
return 0;
}
bool cross_input_up()
{
return arduboy.pressed(UP_BUTTON);
}
bool cross_input_down()
{
return arduboy.pressed(DOWN_BUTTON);
}
bool cross_input_left()
{
return arduboy.pressed(LEFT_BUTTON);
}
bool cross_input_right()
{
return arduboy.pressed(RIGHT_BUTTON);
}
bool cross_input_a()
{
return arduboy.pressed(A_BUTTON);
}
bool cross_input_b()
{
return arduboy.pressed(B_BUTTON);
}
bool cross_input_up_now()
{
return arduboy.justPressed(UP_BUTTON);
}
bool cross_input_down_now()
{
return arduboy.justPressed(DOWN_BUTTON);
}
bool cross_input_left_now()
{
return arduboy.justPressed(LEFT_BUTTON);
}
bool cross_input_right_now()
{
return arduboy.justPressed(RIGHT_BUTTON);
}
bool cross_input_a_now()
{
return arduboy.justPressed(A_BUTTON);
}
bool cross_input_b_now()
{
return arduboy.justPressed(B_BUTTON);
}
constexpr int sizeX = 4;
constexpr int sizeY = 6;
constexpr int spacingX = 1;
constexpr int spacingY = 0;
int lineX;
int lineY;
void cross_print(int x, int y, int size, char *string) {
lineX = x;
lineY = y;
for (const char * pointer = string; *pointer != '\0'; ++pointer) {
if (*pointer == '\n') {
lineY += sizeY + spacingY;
lineX = x;
continue;
}
if (pointer != string) {
cross_drawVLine(lineX - spacingX, lineY, sizeY, 0);
}
int fontId = FXFONT(*pointer);
FX::drawBitmap(lineX, lineY, FX_DATA_FONT46, fontId, dbmNormal);
lineX += (sizeX + spacingX);
}
}
void cross_print(int x, int y, int size, __uint24 address) {
lineX = x;
lineY = y;
int i = 0;
for(;;)
{
FX::seekData(address++);
uint8_t c = FX::readEnd();
if (!c) break;
if (c =='\n') {
lineY += sizeY + spacingY;
lineX = x;
continue;
}
if (i > 0) {
cross_drawVLine(lineX-spacingX,lineY,sizeY, 0);
}
if (c == 95) c=32;
int fontId = FXFONT(c);
FX::drawBitmap(lineX, lineY, FX_DATA_FONT46, fontId, dbmNormal);
lineX += (sizeX+spacingX);
i++;
}
}
void cross_drawPixel(int x, int y, bool colour)
{
arduboy.drawPixel(x, y, colour);
}
void cross_drawHLine(int x, int y, int length, bool colour)
{
arduboy.drawFastHLine(x,y,length,colour);
}
void cross_drawVLine(int x, int y, int length, bool colour)
{
arduboy.drawFastVLine(x,y,length,colour);
}
void cross_set_leds(uint8_t red, uint8_t green, uint8_t blue)
{
arduboy.setRGBled(red,green,blue);
}
static void cross_drawBitmapTile(int x, int y, int width, int height, int colour, int mirror, float zoom, __uint24 fximage)
{
#ifdef ARDBITMAP
ardbitmap.drawFXCompressedResized(x, y, fximage, colour, 0, mirror, zoom);
// ardbitmap.drawFXBitmapResized(x, y, fximage, width, height, colour, 0, mirror, zoom);
#endif
}
bool cross_getPixel(int x, int y) {
return arduboy.getPixel(x,y);
}
void cross_playSound(bool makeSound, uint16_t hertz, uint8_t duration) {
if (makeSound) sound.tone(hertz, duration); // play a 1000Hz tone for 500ms
};
void cross_play_audio(bool makeSound, uint24_t fxsound) {
if (makeSound && !sound.playing()) {
sound.tonesFromFX(fxsound);
}
}
void cross_stop_audio(bool makeSound) {
if (makeSound) {
sound.noTone();
}
}
void cross_setup()
{
arduboy.boot();
FX::begin(FX_DATA_PAGE, FX_SAVE_PAGE); // // initialise FX chip
arduboy.initRandomSeed();
arduboy.setFrameRate(FRAME_RATE);
arduboy.audio.on();
#ifdef DEBUG_MODE
Serial.begin(9600);
#endif
}
bool cross_loop_start()
{
if (!(arduboy.nextFrame()))
return false;
sound.fillBufferFromFX();
frameTime = currentTime;
currentTime = millis();
frameMs = currentTime - frameTime;
if (frameMs == 0)
frameMs = 1;
arduboy.pollButtons();
return true;
}
void cross_loop_end()
{
FX::display(CLEAR_BUFFER); // Using CLEAR_BUFFER will clear the display buffer after it is displayed
}
unsigned long getFrameMs()
{
return frameMs;
}
unsigned long getCurrentMs()
{
return currentTime;
}
#ifdef PERF_RENDER
unsigned long getMs()
{
return millis();
}
#endif