-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabgl_elmuro.ino
318 lines (257 loc) · 8.01 KB
/
fabgl_elmuro.ino
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Created by Fabrizio Di Vittorio ([email protected]) - www.fabgl.com
Copyright (c) 2019-2020 Fabrizio Di Vittorio.
All rights reserved.
This file is part of FabGL Library.
FabGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FabGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FabGL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fabgl.h"
fabgl::PS2Controller PS2Controller;
SoundGenerator soundGenerator;
SquareWaveformGenerator swg;
#include "controllers.h"
#include "soundchip.h"
#define TOPMARGIN 6
#define SIDEMARGIN 14
#define INITIALDIR (-PI/2.5)
#define INITIALY 179
#define INITIALX 150
#define PADDLES 3
#define MINANGLE (PI/5)
volatile double objX = INITIALX;
volatile double objY = INITIALY;
static double objDir = INITIALDIR;
static double objVel = 4;
static constexpr int objSize = 8;
static TaskHandle_t mainTaskHandle;
static uint32_t bgParkMillerState = 1;
long offGame = 0;
// just to avoid floating point calculations inside MyDirectDrawVGAController::drawScanline()
volatile int objIntX;
volatile int objIntY;
volatile double oldobjX;
volatile double oldobjY;
int nBricks = 8*12;
bool brickMap[8][12];
int paddlePos = 320/2;
int nPaddles = PADDLES;
bool bAuto = false;
GameControllerMouse cMouse;
GameControllerKeys cKeysArrows;
struct MyDirectDrawVGAController : public fabgl::VGADirectController {
void IRAM_ATTR drawScanline(uint8_t * dest, int scanLine);
} DisplayController;
inline int fastRandom()
{
bgParkMillerState = (uint64_t)bgParkMillerState * 48271 % 0x7fffffff;
return bgParkMillerState % 4;
}
void IRAM_ATTR MyDirectDrawVGAController::drawScanline(uint8_t * dest, int scanLine)
{
auto fgcolor = createRawPixel(RGB222(3, 3, 3));
auto white = createRawPixel(RGB222(3, 3, 3));
auto black = createRawPixel(RGB222(0, 0, 0));
auto bgcolor = createRawPixel(RGB222(0, 0, 0));
if( scanLine >= 32 )
{
if(scanLine < 32+16)
{
bgcolor = createRawPixel(RGB222(1, 0, 0));
fgcolor = createRawPixel(RGB222(3, 0, 0));
}
else if(scanLine < 32+16+16)
{
bgcolor = createRawPixel(RGB222(1, 0, 1));
fgcolor = createRawPixel(RGB222(3, 0, 3));
}
else if(scanLine < 32+16+16+16)
{
bgcolor = createRawPixel(RGB222(0, 1, 0));
fgcolor = createRawPixel(RGB222(0, 3, 0));
}
else if(scanLine < 32+16+16+16+16)
{
bgcolor = createRawPixel(RGB222(1, 1, 0));
fgcolor = createRawPixel(RGB222(3, 3, 0));
}
}
auto width = getScreenWidth();
// fill line with background color
if( scanLine < TOPMARGIN)
memset(dest+8, fgcolor, width-16);
else
{
memset(dest, black, 16);
memset(dest+319-15, black, 16);
memset(dest+16, bgcolor, width-32);
for( int ncompta = 8; ncompta < SIDEMARGIN; ncompta++)
{
VGA_PIXELINROW(dest, ncompta) = white;
VGA_PIXELINROW(dest, 319-ncompta) = white;
}
}
// fill object with foreground color
if (scanLine >= objIntY - objSize / 2 && scanLine <= objIntY + objSize / 2)
{
int dist = abs(scanLine - objIntY) == 4 ? 1 : 0;
for (int col = (objIntX - objSize / 2)+dist; col < (objIntX + objSize / 2)-dist; ++col)
VGA_PIXELINROW(dest, col) = fgcolor;
}
if (scanLine >= 184 && scanLine < 192 )
{
for (int col = paddlePos - 16 ; col < paddlePos + 16; ++col)
VGA_PIXELINROW(dest, col) = white;
}
// drawBricks
if( scanLine >= 32 && scanLine < (32+16+16+16+16) && scanLine % 8 != 0)
{
for( int x = 0; x < 12*3*8; x++)
{
if( x%24 != 0 && x%24 != 23 && brickMap[(scanLine-32)/8][x/24])
VGA_PIXELINROW(dest, x+16) = fgcolor;
}
}
if( scanLine >= 10 && scanLine <= 13 )
for (int p = 0; p < nPaddles; p++)
for( int x = 0; x < 10; x++)
VGA_PIXELINROW(dest, 300 - x - p*14) = white;
if (scanLine == getScreenHeight() - 1) {
// signal end of screen
vTaskNotifyGiveFromISR(mainTaskHandle, NULL);
}
}
void resetGame()
{
if( nBricks )
playSoundReset();
else
playSoundWon();
memset( brickMap, true, sizeof(brickMap));
nBricks = 12*8;
objIntX = objX = INITIALX;
objIntY = objY = INITIALY;
objDir = INITIALDIR;
offGame = millis();
nPaddles = PADDLES;
}
void setup()
{
Serial.begin(115200); delay(500); Serial.write("\n\n\n"); // DEBUG ONLY
PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1, KbdMode::GenerateVirtualKeys);
cMouse = GameControllerMouse (0);
cKeysArrows = GameControllerKeys (1, fabgl::VK_UP, fabgl::VK_DOWN, fabgl::VK_LEFT, fabgl::VK_RIGHT, fabgl::VK_RSHIFT, fabgl::VK_ESCAPE);
mainTaskHandle = xTaskGetCurrentTaskHandle();
DisplayController.begin();
DisplayController.setResolution(VGA_320x200_75Hz);
soundGenerator.setVolume(127);
soundGenerator.play(true);
soundGenerator.attach( &swg);
resetGame();
delay(3000);
}
void setDir( double newDir )
{
objDir = newDir;
//normalize dir
while( objDir > PI) objDir -= PI*2;
while( objDir < -PI) objDir += PI*2;
}
void loop()
{
if( millis() - offGame > 4000)
offGame = false;
// check brick collision
static int oldbricky = -1;
int brickx = (objIntX-16)/24;
int bricky = (objIntY-32)/8;
if( bricky >= 0 && bricky <8 && brickx < 12 && brickMap[bricky][brickx] )
{
brickMap[bricky][brickx] = false;
nBricks--;
setDir ((( bricky != oldbricky ) ? 2 : 1) * PI - objDir);
playSoundPic(bricky/2);
}
if( nBricks == 0)
resetGame();
double dmin = objSize / 2 +SIDEMARGIN;
double dmax = 320 - objSize / 2-SIDEMARGIN;
// test collision with borders and bounce changing direction
if ( (objX < dmin && oldobjX >= dmin ) || (objX > dmax && oldobjX <= dmax ) )
{
setDir ( PI - objDir );
playSoundPong();
}
else if (objY < (objSize / 2 +TOPMARGIN) && (oldobjX >= (objSize / 2 +TOPMARGIN)))
{
setDir ( 2 * PI - objDir );
playSoundPong();
}
// check paddle collision
if ( !offGame
&& (objX > paddlePos -(16+objSize) && objX < paddlePos +(16+objSize) || oldobjX > paddlePos -(16+objSize) && oldobjX < paddlePos +(16+objSize) )
&& objY > 185-objSize/2 && oldobjY <= 185-objSize/2)
{
setDir ( 2 * PI - objDir );
setDir ( objDir + (objX - ((double)paddlePos))*((PI/2.0)/32.0) );
if( objDir > -MINANGLE) objDir = -MINANGLE;
if( objDir < -(PI-MINANGLE)) objDir = -(PI-MINANGLE);
playSoundTuc();
}
oldobjX = objX;
oldobjY = objY;
if( !offGame )
{
// calculate new coordinates
objX += objVel * cos(objDir);
objY += objVel * sin(objDir);
}
else
objX = paddlePos +4;
if(objY < 7) objY = 8;
// Ball lost
if(objY > 200)
{
playSoundOut();
offGame = millis();
nPaddles--;
if( nPaddles == -1)
resetGame();
else
{
objY = INITIALY;
objDir = INITIALDIR;
objX = paddlePos +4;
}
}
// convert object coordinate to integer
objIntX = objX;
objIntY = objY;
oldbricky = bricky;
cMouse.update();
paddlePos += cMouse.getDX();
if( cKeysArrows.isLeft() ) paddlePos -= 8;
if( cKeysArrows.isRight() ) paddlePos += 8;
if( cMouse.isButtonA() || cKeysArrows.isButtonA() )
offGame = false;
if( cMouse.isButtonB() || cKeysArrows.isButtonB() )
bAuto = not bAuto;
if(bAuto)
{
if (paddlePos < objX ) paddlePos +=2;
if (paddlePos > objX ) paddlePos -=2;
if( offGame ) offGame = false;
}
if( paddlePos < 32) paddlePos = 32;
if( paddlePos > 319-32) paddlePos = 319-32;
// wait for vertical sync
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}