-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
460 lines (416 loc) · 13.2 KB
/
main.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "SDL.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
unsigned round_up_to_power_of_2(unsigned n); // Needed for initializers.
char instructions[] =
"Welcome to Pitch Black.\n"
"AWSD to move.\n"
"Hold shift to run.\n"
"Hold JKL to point sonar left, back or right.\n"
"Control-Q to quit.\n"
"Control-H to repeat instructions.\n"
;
char instructions_image_filename[] = "instructions.bmp";
char gameboard_image_filename[] = "gameboard.bmp";
char player_image_filename[] = "player.bmp";
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
// Game parameters.
#define FPS (5) // 1 frame = 1/5 sec.
const int FRAME_DUR_MS = 1000 / FPS;
const double TURN_CIRCLE_UNITS = 10.0; // It takes 2 secs to turn 360.
const double MOVE_RATE = 5.0; // Move 5 pixels per frame.
const double PLAYER_RADIUS = 3.0;
// Audio parameters.
int AUDIO_AMPLITUDE = 28000;
//int AUDIO_FREQUENCY = 48000;
//int AUDIO_FREQUENCY = 44100;
int AUDIO_FREQUENCY = 22050;
int AUDIO_CHANNELS = 2; // stereo
//int AUDIO_SAMPLES = 512;
int AUDIO_SAMPLES = 256;
//int AUDIO_FORMAT = AUDIO_S16SYS;
int AUDIO_FORMAT = AUDIO_S8;
typedef int8_t AudioSample_t; // should match audio format
void init_parameters() {
}
// SDL setup info.
SDL_Window* window;
SDL_Renderer* renderer = NULL;
SDL_Texture* instructions_image = NULL;
SDL_Texture* gameboard_image = NULL;
SDL_Texture* player_image = NULL;
SDL_AudioDeviceID audio_device_id;
//MixChunk* footstep_chunk;
//MixChunk* sonar_chunk;
// Game state.
int quitting = 0; // 1 = quit the game.
int show_game = 1; // Set at startup.
int player_facing; // TURN_CIRCLE_UNITS, 0 = east, increases ccw.
double player_x = 100.0, player_y = 100.0; // FIXME: Improve this.
volatile double sonar_frequency = 0.0; // This is set in every frame.
int player_moved = 0; // This is set in every frame.
void init();
SDL_Texture* load_image_as_texture(const char* image_filename);
void main_loop();
void pump_events();
void poll_keyboard();
void play_sounds();
void audio_callback(void* userdata, Uint8* stream, int len);
//void set_sonar_frequency(double beep_frequency);
void update_display();
int main(int argc, char** argv) {
init_parameters();
printf(instructions);
fflush(stdout);
init();
main_loop();
return 0;
}
void error(const char* msg, const char* detail) {
fprintf(stderr, "Fatal error, aborting the game.\n");
fprintf(stderr, msg);
if (detail != NULL)
fprintf(stderr, ": %s\n", detail);
else
fprintf(stderr, ".\n");
exit(1);
}
const char* audio_format_to_string(SDL_AudioFormat f) {
static char str[64];
unsigned bits = f & 127;
sprintf(str, "AUDIO_%s%d%s"
, SDL_AUDIO_ISFLOAT(f) ? "F"
: SDL_AUDIO_ISSIGNED(f) ? "S" : "U"
, bits
, bits == 8 ? "" : SDL_AUDIO_ISBIGENDIAN(f) ? "MSB" : "LSB"
);
return str;
}
void init() {
// Initialize some constants.
player_facing = TURN_CIRCLE_UNITS / 4; // Due north.
// Set up SDL.
if (0 != SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
error("Unable to initialize SDL", SDL_GetError());
atexit(SDL_Quit);
window = SDL_CreateWindow("PitchBlack",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_INPUT_GRABBED);
if (window == NULL)
error("Unable to open a window", SDL_GetError());
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == NULL)
error("Unable to create SDL renderer", SDL_GetError());
// Set up audio.
SDL_AudioSpec requested_audio_spec;
SDL_AudioSpec granted_audio_spec;
SDL_memset(&requested_audio_spec, 0, sizeof(requested_audio_spec));
requested_audio_spec.freq = AUDIO_FREQUENCY;
requested_audio_spec.format = AUDIO_FORMAT;
requested_audio_spec.channels = AUDIO_CHANNELS;
requested_audio_spec.samples = AUDIO_SAMPLES;
requested_audio_spec.callback = audio_callback;
audio_device_id = SDL_OpenAudioDevice(
NULL, // no specifically requested device
0, // is not a recording device
&requested_audio_spec, // input parameters
&granted_audio_spec, // ignore output parameters (automatically convert format)
SDL_AUDIO_ALLOW_FORMAT_CHANGE // allow chosen format to be different
); // from requested format
SDL_PauseAudioDevice(audio_device_id, 0); // start playing immediately
printf("Audio spec:\n"
"freq = %d\n"
"format = %s\n"
"channels = %d\n"
"samples = %d\n"
"size = %d\n"
"buffer time = %g ms\n"
, granted_audio_spec.freq
, audio_format_to_string(granted_audio_spec.format)
, granted_audio_spec.channels
, granted_audio_spec.samples
, granted_audio_spec.size
, (double)1000.0 * granted_audio_spec.samples / granted_audio_spec.freq
);
fflush(stdout);
// Load image files.
instructions_image = load_image_as_texture(instructions_image_filename);
gameboard_image = load_image_as_texture(gameboard_image_filename);
player_image = load_image_as_texture(player_image_filename);
}
SDL_Texture* load_image_as_texture(const char* image_filename) {
char path[100];
sprintf(path, "assets/%s", image_filename);
SDL_Surface* surface = SDL_LoadBMP(path);
if (surface == NULL) {
fprintf(stderr, "Failed to load image: %s\n", image_filename);
error("Load failed", SDL_GetError());
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture == NULL) {
fprintf(stderr, "Failed to load image: %s\n", image_filename);
error("Failed to convert image to texture", SDL_GetError());
}
SDL_FreeSurface(surface);
return texture;
}
void main_loop()
{
Uint32 next_frame_ticks = SDL_GetTicks() + FRAME_DUR_MS;
while (!quitting) {
pump_events(); // Check control keypresses.
poll_keyboard(); // Movement and turning.
if (show_game) {
// Blit the game display.
update_display();
} else {
// Blit the instructions on the screen.
SDL_RenderCopy(renderer, instructions_image, NULL, NULL);
}
SDL_UpdateWindowSurface(window);
play_sounds();
// Wait for the next frame.
while (SDL_GetTicks() < next_frame_ticks) {
SDL_Delay(0);
}
next_frame_ticks += FRAME_DUR_MS;
}
}
void pump_events() {
SDL_Event e;
while(SDL_PollEvent(&e) != 0) {
switch (e.type) {
case SDL_QUIT:
quitting = 1;
return;
break;
case SDL_KEYDOWN:
{
// This just handles control key presses.
// None of these are the same as the keys that
// are handled by the keyboard state poll, so
// the state poll shouldn't have to worry about
// the state of the control key.
SDL_KeyboardEvent* ke = (SDL_KeyboardEvent*)&e;
if ((ke->keysym.mod & KMOD_CTRL) != 0) {
switch (ke->keysym.sym) {
case SDLK_h:
// Play help.
break;
case SDLK_q:
quitting = 1;
return;
}
}
}
break;
}
}
}
void poll_keyboard() {
int numkeys;
const Uint8* keyboard_state = SDL_GetKeyboardState(&numkeys);
// Facing 0 = due east (right side of the screen).
// Increasing values rotate counterclockwise.
// Turn the player.
if (keyboard_state[SDL_SCANCODE_A]) {
// Turn left.
player_facing++;
}
if (keyboard_state[SDL_SCANCODE_D]) {
// Turn right.
player_facing--;
}
if (player_facing < 0) {
player_facing = TURN_CIRCLE_UNITS - 1;
} else if (player_facing == TURN_CIRCLE_UNITS) {
player_facing = 0;
}
// Convert player heading to radians.
double player_heading =
2.0 * M_PI * ((double)player_facing / (double)TURN_CIRCLE_UNITS);
// Decompose player movement into N and E vectors.
double player_move_n = MOVE_RATE * sin(player_heading);
double player_move_e = MOVE_RATE * cos(player_heading);
// Move the player.
double dest_x = player_x;
double dest_y = player_y;
if (keyboard_state[SDL_SCANCODE_W]) {
// Move forward.
dest_x = player_x + player_move_e;
dest_y = player_y - player_move_n;
}
if (keyboard_state[SDL_SCANCODE_S]) {
// Move back.
dest_x = player_x - player_move_e;
dest_y = player_y + player_move_n;
}
// Do collision detection, and change movement if needed.
// TODO
// Update the player's position.
if (player_x == dest_x && player_y == dest_y) {
player_moved = 0;
} else {
player_x = dest_x, player_y = dest_y;
player_moved = 1;
}
// TODO: Round facing to fixed points to prevent
// cumulative rounding errors.
// Sonar points straight ahead unless altered.
double sonar_facing = player_heading;
if (keyboard_state[SDL_SCANCODE_J]) {
// Point sonar left.
sonar_facing = player_heading + 0.5 * M_PI;
}
if (keyboard_state[SDL_SCANCODE_K]) {
// Point sonar back.
sonar_facing = player_heading + 1.0 * M_PI;
}
if (keyboard_state[SDL_SCANCODE_L]) {
// Point sonar right.
sonar_facing = player_heading + 1.5 * M_PI;
}
// Keep facing in range [0, 2*M_PI).
if (sonar_facing >= 2.0 * M_PI) {
sonar_facing -= 2.0 * M_PI;
}
#if 0
// Calculate sonar distance.
if (detect_collision_point(
double origin_x, double origin_y,
double dest_x, double dest_y,
double* collision_x, double* collision_y
))
{
// do nothing for now.
}
#endif
}
#if 0
int detect_collision_point(
double origin_x, double origin_y,
double dest_x, double dest_y,
double* collision_x, double* collision_y
)
{
double delta_x = dest_x - origin_x;
double delta_y = dest_y - origin_y;
double direction = arctan(delta_y / delta_x);
double distance_x = abs(delta_x);
double distance_y = abs(delta_y);
double move_x = 0, move_y = 0;
while (move_x < distance_x && move_y < distance_y) {
if (
}
}
#endif
void play_sounds() {
SDL_LockAudioDevice(audio_device_id);
// Play sonar.
if (sonar_frequency == 261.63) {
sonar_frequency = 293.66; // D4
} else {
sonar_frequency = 261.63; // middle C (C4)
}
// Play footstep.
if (player_moved) {
// No footstep for now.
}
SDL_UnlockAudioDevice(audio_device_id);
}
#if 0
void audio_callback(void* userdata, Uint8* stream, int len) {
//fprintf(stderr, "Len: %d, Freq: %g\n", len, sonar_frequency);
static double sonar_phase[4] = { 0, 0, 0, 0 };
memset(stream, 0, len); // fill buffer with silence
int16_t chord[4];
chord[0] = sonar_frequency;
if (chord[0] == 261.63) {
chord[1] = 329.63;
chord[2] = 392.00;
chord[3] = 523.25;
} else if (chord[0] == 293.66) {
chord[1] = 349.23;
chord[2] = 440.00;
chord[3] = 587.33;
} else {
chord[1] = 0;
chord[2] = 0;
chord[3] = 0;
}
double current_phase = sonar_phase;
unsigned n_samples = len / sizeof(int16_t);
int16_t* stream16 = (int16_t*)stream; // use 16-bit samples
for (int note = 0; note < 4; note++) {
double frequency = chord[note];
current_phase = sonar_phase;
if (frequency > 0) {
// 16-bit samples, stereo order LRLRLR
double wave_period_samples = (double)AUDIO_FREQUENCY / frequency;
double sonar_phase_increment = 2 * M_PI / wave_period_samples;
double volume = AUDIO_AMPLITUDE / 8;
unsigned i = 0;
while (i < n_samples) {
int16_t sample_value = volume * sin(current_phase);
// sample_value = sample_value > 0 ? volume : -volume; // square wave
stream16[i++] += sample_value;
stream16[i++] += sample_value;
current_phase += sonar_phase_increment;
if (current_phase >= 2 * M_PI)
current_phase = 0.0;
}
}
}
sonar_phase = current_phase;
}
#endif
void audio_callback(void* userdata, Uint8* stream, int len) {
//fprintf(stderr, "Len: %d, Freq: %g\n", len, sonar_frequency);
static double sonar_phase = 0;
AudioSample_t* streamFormatted = (AudioSample_t*)stream;
memset(stream, 0, len); // fill buffer with silence
unsigned n_samples = len / sizeof(AudioSample_t);
double frequency = sonar_frequency;
double amplitude = 100;
if (frequency > 0) {
double wave_period_samples = (double)AUDIO_FREQUENCY / frequency;
double sonar_phase_increment = 2 * M_PI / wave_period_samples;
unsigned i = 0;
while (i < n_samples) {
AudioSample_t sample_value = amplitude * sin(sonar_phase);
// sample_value = sample_value > 0 ? amplitude : -amplitude; // square wave
//fprintf(stderr, "Phase: %3.2f ; Sample: %3d\n", sonar_phase, sample_value);
streamFormatted[i++] += sample_value;
streamFormatted[i++] += sample_value;
sonar_phase += sonar_phase_increment;
if (sonar_phase >= 2 * M_PI)
sonar_phase = 0;
}
}
}
void update_display() {
// Draw the base of the gameboard.
SDL_RenderCopy(renderer, gameboard_image, NULL, NULL);
// Draw the players.
// TODO: Multiple players.
SDL_Rect dest_rect;
dest_rect.x = (int)player_x;
dest_rect.y = (int)player_y;
SDL_RenderCopy(renderer, player_image, NULL, &dest_rect);
}
unsigned round_up_to_power_of_2(unsigned n) {
if (n == 0)
return 0;
unsigned rounded = 1;
while (rounded < n) {
rounded <<= 1;
}
return rounded;
}