-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.c
291 lines (255 loc) · 6.54 KB
/
platform.c
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
/* Includes ----------------------------------------------------------------- */
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#include "platform.h"
#include "constants.h"
#include "sound.h"
#include "input.h"
#ifdef USE_RAYLIB
#include "raylib.h"
/* Definitions -------------------------------------------------------------- */
#define WINDOW_ZOOM 4
#define WINDOW_WIDTH (SCREEN_WIDTH * WINDOW_ZOOM)
#define WINDOW_HEIGHT (SCREEN_HEIGHT * WINDOW_ZOOM)
#define AUDIO_SAMPLING_RATE 44100.0f
#define AUDIO_BUFFER_MAX_SAMPLES 512
#define AUDIO_BUFFER_DEFAULT_SIZE 4096
#define AUDIO_SAMPLE_SIZE 16
#define AUDIO_CHANNEL_NUM 1
/* Global variables --------------------------------------------------------- */
static uint32_t clock_t0;
static bool audio_is_playing;
static AudioStream audio_stream;
/* Function prototypes ------------------------------------------------------ */
void platform_audio_callback(void *buffer, unsigned int frames);
/* Function definitions ----------------------------------------------------- */
/**
* @brief PLATFORM initialize user-defined functions.
*
*/
void platform_init(void)
{
/* Window initialization */
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Doom Pico");
SetTargetFPS(FPS);
/* Audio initialization */
InitAudioDevice();
SetAudioStreamBufferSizeDefault(AUDIO_BUFFER_DEFAULT_SIZE);
audio_stream = LoadAudioStream(
AUDIO_SAMPLING_RATE,
AUDIO_SAMPLE_SIZE,
AUDIO_CHANNEL_NUM);
SetAudioStreamCallback(audio_stream, platform_audio_callback);
PlayAudioStream(audio_stream);
PauseAudioStream(audio_stream);
audio_is_playing = false;
clock_t0 = clock();
}
/**
* @brief PLATFORM start drawing a new frame.
*
*/
void platform_draw_start(void)
{
BeginDrawing();
}
/**
* @brief PLATFORM stop drawing current frame.
*
*/
void platform_draw_stop(void)
{
EndDrawing();
}
/**
* @brief PLATFORM write pixel value to screen.
*
* @param x X coordinate
* @param y Y coordinate
* @param color Pixel color
*/
void platform_draw_pixel(uint8_t x, uint8_t y, bool color)
{
#if (WINDOW_ZOOM > 1)
Color c = color ? WHITE : BLACK;
for (int i = x * WINDOW_ZOOM; i < (x + 1) * WINDOW_ZOOM; i++)
{
for (int j = y * WINDOW_ZOOM; j < (y + 1) * WINDOW_ZOOM; j++)
{
DrawPixel(i, j, c);
}
}
#else
DrawPixel(x, y, color);
#endif
}
/**
* @brief PLATFORM play audio effect through speaker.
*
*/
void platform_audio_play(void)
{
if (!audio_is_playing)
{
audio_is_playing = true;
ResumeAudioStream(audio_stream);
}
}
/**
* @brief PLATFORM play audio callback needed for concurrent execution.
*
* NOTE: This callback is specifically required by Raylib, in a microcontroller
* setting, this callback may be an Interrupt Service Routine (ISR) that
* autonomously drives the speaker.
*
* @param buffer Sample buffer
* @param frames Number of samples
*/
void platform_audio_callback(void *buffer, unsigned int frames)
{
// Get next frequency to play
uint16_t frequency = sound_get_frequency();
// End of sound, pause stream
if (frequency == 0)
{
if (audio_is_playing)
{
audio_is_playing = false;
PauseAudioStream(audio_stream);
}
return;
}
else
{
if (!audio_is_playing)
{
audio_is_playing = true;
ResumeAudioStream(audio_stream);
}
}
// Create square wave with given frequency
uint16_t wave_length = AUDIO_SAMPLING_RATE / frequency;
uint16_t *buf = (uint16_t *)buffer;
for (uint16_t i = 0; i < frames; i++)
buf[i] = (i % wave_length) < (wave_length / 2) ? SHRT_MAX : SHRT_MIN;
}
/**
* @brief PLATFORM read user controls and update button state.
*
*/
void platform_input_update(void)
{
input_button = 0;
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W))
input_button |= UP;
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S))
input_button |= DOWN;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
input_button |= LEFT;
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
input_button |= RIGHT;
if (IsKeyDown(KEY_SPACE))
input_button |= FIRE;
if (IsKeyDown(KEY_LEFT_SHIFT))
input_button |= JUMP;
if (IsKeyDown(KEY_ENTER))
input_button |= HOME;
if (IsKeyDown(KEY_ESCAPE))
input_button |= EXIT;
}
/**
* @brief PLATFORM get time in milliseconds from start of execution.
*
* @return uint32_t Start time in milliseconds
*/
uint32_t platform_millis(void)
{
return ((clock() - clock_t0) * 1000) / CLOCKS_PER_SEC;
}
/**
* @brief PLATFORM apply blocking delay in milliseconds.
*
* @param ms Delay in milliseconds
*/
void platform_delay(uint32_t ms)
{
uint32_t t0 = platform_millis();
while ((platform_millis() - t0) < ms)
{
asm("nop");
};
}
#else /* User-defined platform functions ------------------------------------ */
/**
* @brief PLATFORM initialize user-defined functions.
*
*/
void platform_init(void)
{
/* Add definition here */
}
/**
* @brief PLATFORM start drawing a new frame.
*
*/
void platform_draw_start(void)
{
/* Add definition here */
}
/**
* @brief PLATFORM stop drawing current frame.
*
*/
void platform_draw_stop(void)
{
/* Add definition here */
}
/**
* @brief PLATFORM write pixel value to screen.
*
* @param x X coordinate
* @param y Y coordinate
* @param color Pixel color
*/
void platform_draw_pixel(uint8_t x, uint8_t y, bool color)
{
/* Add definition here */
}
/**
* @brief PLATFORM play audio effect through speaker.
*
*/
void platform_audio_play(void)
{
/* Add definition here */
}
/**
* @brief PLATFORM read user controls and update button state.
*
*/
void platform_input_update(void)
{
/* Add definition here */
}
/**
* @brief PLATFORM get time in milliseconds from start of execution.
*
* @return uint32_t Start time in milliseconds
*/
uint32_t platform_millis(void)
{
/* Add definition here */
}
/**
* @brief PLATFORM apply blocking delay in milliseconds.
*
* @param ms Delay in milliseconds
*/
void platform_delay(uint32_t ms)
{
/* Add definition here */
}
#endif /* USE_RAYLIB */
/* -------------------------------------------------------------------------- */