-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_platform.cpp
396 lines (323 loc) · 13.6 KB
/
sdl_platform.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
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
//
// Created by doug on 4/16/15.
//
#include <math.h>
#include <time.h>
#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include "SDL2/SDL.h"
#include "game.h"
#include "sdl_platform.h"
void platform_start_task(task_queue_* queue, task_callback_* callback, void* data) {
i32 next_write_index = (queue->write_index + 1) % TASK_QUEUE_MAX_ENTRIES;
assert_(next_write_index != queue->read_index);
task_* task = queue->tasks + queue->write_index;
task->callback = callback;
task->data = data;
queue->write_index = next_write_index;
__atomic_add_fetch(&queue->remaining, 1, __ATOMIC_SEQ_CST);
SDL_SemPost(queue->semaphore);
}
b32 platform_execute_next_task(task_queue_* queue) {
b32 sleep = false;
i32 original = queue->read_index;
i32 read_index = (original + 1) % TASK_QUEUE_MAX_ENTRIES;
if (original != queue->write_index) {
if (__atomic_compare_exchange_n(&queue->read_index,
&original, read_index,
false,
__ATOMIC_SEQ_CST,
__ATOMIC_SEQ_CST)) {
task_* task = queue->tasks + original;
task->callback(queue, task->data);
__atomic_sub_fetch(&queue->remaining, 1, __ATOMIC_SEQ_CST);
}
} else {
sleep = true;
}
return sleep;
}
void platform_wait_on_queue(task_queue_* queue) {
while (queue->remaining) {
platform_execute_next_task(queue);
}
}
int thread_func(void* ptr) {
task_queue_* queue = (task_queue_*)ptr;
while (true) {
if (platform_execute_next_task(queue)) {
SDL_SemWait(queue->semaphore);
}
}
return 0;
}
void platform_free_file_memory(void* memory) {
free(memory);
}
platform_read_entire_file_result_ platform_read_entire_file(const char * filename) {
platform_read_entire_file_result_ result = {};
FILE *f = fopen(filename, "rb");
if (f == 0) {
printf("Failed to open file %s\n", filename);
return result;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *string = (char *)malloc(fsize + 1);
if (!fread(string, fsize, 1, f)) {
printf("No results from fread\n");
}
fclose(f);
string[fsize] = 0;
result.contents = (u8 *)string;
result.content_size = (u32) fsize;
return result;
}
b32 handle_sdl_event(SDL_Event* event, platform_context_* context) {
b32 should_quit = false;
switch (event->type) {
case SDL_QUIT: {
printf("SDL_QUIT\n");
should_quit = true;
} break;
case SDL_WINDOWEVENT: {
switch (event->window.event) {
case SDL_WINDOWEVENT_RESIZED: {
printf("SDL_WINDOWEVENT_RESIZED (%d, %d)\n", event->window.data1,
event->window.data2);
} break;
case SDL_WINDOWEVENT_EXPOSED: {
printf("SDL_WINDOWEVENT_EXPOSED\n");
} break;
} break;
} break;
case SDL_KEYDOWN:
case SDL_KEYUP: {
SDL_Keycode key_code = event->key.keysym.sym;
b32 ended_down = event->key.state != SDL_RELEASED;
if (event->type == SDL_KEYUP || event->key.repeat == 0) {
switch (key_code) {
case SDLK_UP: {
context->next_input->button_a.ended_down = ended_down;
context->next_input->button_a.transition_count++;
} break;
case SDLK_DOWN: {
context->next_input->button_x.ended_down = ended_down;
context->next_input->button_x.transition_count++;
} break;
case SDLK_LEFT: {
context->next_input->joystick_l.position.x = ended_down ? -1.0f : 0.0f;
} break;
case SDLK_RIGHT: {
context->next_input->joystick_l.position.x = ended_down ? 1.0f : 0.0f;
} break;
}
}
} break;
case SDL_CONTROLLERDEVICEADDED: {
if (context->controller_handle) {
SDL_JoystickClose(context->controller_handle);
}
context->controller_handle = SDL_JoystickOpen(event->cdevice.which);
} break;
case SDL_CONTROLLERDEVICEREMOVED: {
if (context->controller_handle) {
SDL_JoystickClose(context->controller_handle);
}
} break;
}
SDL_Joystick* handle = context->controller_handle;
if(handle && SDL_JoystickGetAttached(handle))
{
// NOTE: We have a controller with index ControllerIndex.
b32 up = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_DPAD_UP);
b32 down = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
b32 left = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
b32 right = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
b32 start = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_START);
b32 back = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_BACK);
b32 l_shoulder = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
b32 r_shoulder = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
b32 button_a = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_A);
b32 button_b = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_B);
b32 button_x = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_X);
b32 button_y = SDL_JoystickGetButton(handle, SDL_CONTROLLER_BUTTON_Y);
i16 stick_x = SDL_JoystickGetAxis(handle, SDL_CONTROLLER_AXIS_LEFTX);
i16 stick_y = SDL_JoystickGetAxis(handle, SDL_CONTROLLER_AXIS_LEFTY);
context->next_input->up.ended_down = up;
context->next_input->down.ended_down = down;
context->next_input->left.ended_down = left;
context->next_input->right.ended_down = right;
context->next_input->button_a.ended_down = button_a;
context->next_input->button_b.ended_down = button_b;
context->next_input->button_x.ended_down = button_x;
context->next_input->button_y.ended_down = button_y;
context->next_input->joystick_l.position.x = ((f32)stick_x) / 32768.0f;
context->next_input->joystick_l.position.y = ((f32)stick_y) / 32768.0f;
}
return should_quit;
}
SDL_Joystick* find_controller_handle() {
i32 num_joysticks = SDL_NumJoysticks();
for (int i = 0; i < num_joysticks; ++i) {
printf("%s\n", SDL_JoystickName(SDL_JoystickOpen(i)));
return SDL_JoystickOpen(i);
}
printf("No controller found. Joysticks attached: %d\n", num_joysticks);
return 0;
}
f32 get_seconds_elapsed(u64 old, u64 current) {
return ((f32)(current - old) / (f32)(SDL_GetPerformanceFrequency()));
}
void segv_handler(int sig) {
void *array[10];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 10);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
void printer_task(task_queue_* queue, void* data) {
char* as_str = (char*)data;
printf("%s\n", as_str);
}
const char* to_print = "hello, world";
int main(int argc, char const *argv[]) {
signal(SIGSEGV, segv_handler);
setlocale(LC_NUMERIC, "");
platform_context_ context = {0};
if (SDL_Init(SDL_INIT_EVERYTHING)) {
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
task_queue_ primary_queue = {0};
primary_queue.semaphore = SDL_CreateSemaphore(0);
task_queue_ secondary_queue = {0};
secondary_queue.semaphore = SDL_CreateSemaphore(0);
task_queue_ render_queue = {0};
render_queue.semaphore = SDL_CreateSemaphore(0);
platform_services_ platform = {0};
platform.primary_queue = &primary_queue;
platform.secondary_queue = &secondary_queue;
platform.render_queue = &render_queue;
platform.start_task = &platform_start_task;
platform.wait_on_queue = &platform_wait_on_queue;
for (int i = 0; i < 8; ++i) {
char buffer[10];
sprintf(buffer, "thread%d", i);
SDL_CreateThread(thread_func, buffer, (void*)&render_queue);
}
context.window = SDL_CreateWindow("hello",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
START_WIDTH,
START_HEIGHT,
SDL_WINDOW_RESIZABLE);
if (!context.window) {
printf("Unable to create window: %s\n", SDL_GetError());
return 1;
}
context.renderer = SDL_CreateRenderer(context.window, -1, SDL_RENDERER_PRESENTVSYNC);
if (!context.renderer) {
printf("Unable to create renderer: %s\n", SDL_GetError());
return 1;
}
context.texture = SDL_CreateTexture(context.renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
START_WIDTH,
START_HEIGHT);
if (!context.texture) {
printf("Unable to create texture: %s\n", SDL_GetError());
return 1;
}
size_t pixel_bytes = START_WIDTH * START_HEIGHT * 4;
void* pixels = malloc(pixel_bytes);
// void* pixels = aligned_alloc(64, pixel_bytes + (pixel_bytes % 64));
const u64 one_gig = 1024LL * 1024LL * 1024LL;
void* game_memory = calloc(one_gig, sizeof(u8));
context.controller_handle = find_controller_handle();
u32 frame_index = 0;
u32 s = 0;
bool running = true;
game_input_ prev_input = {};
game_input_ next_input = {};
context.next_input = &next_input;
context.prev_input = &prev_input;
u64 last_counter = SDL_GetPerformanceCounter();
const f32 target_seconds_per_frame = 1.0f / (f32)FRAME_RATE;
while(running) {
f32 elapsed = get_seconds_elapsed(last_counter, SDL_GetPerformanceCounter());
if (elapsed < target_seconds_per_frame) {
u32 sleep_time = ((target_seconds_per_frame - elapsed) * 1000) - 1;
SDL_Delay(sleep_time);
elapsed = get_seconds_elapsed(last_counter, SDL_GetPerformanceCounter());
while (get_seconds_elapsed(last_counter, SDL_GetPerformanceCounter()) <
target_seconds_per_frame) { }
}
f32 dt = get_seconds_elapsed(last_counter, SDL_GetPerformanceCounter());
last_counter = SDL_GetPerformanceCounter();
ZERO_STRUCT(next_input);
next_input.up.ended_down = prev_input.up.ended_down;
next_input.down.ended_down = prev_input.down.ended_down;
next_input.left.ended_down = prev_input.left.ended_down;
next_input.right.ended_down = prev_input.right.ended_down;
next_input.lshift.ended_down = prev_input.lshift.ended_down;
next_input.rshift.ended_down = prev_input.rshift.ended_down;
next_input.button_a.ended_down = prev_input.button_a.ended_down;
next_input.button_b.ended_down = prev_input.button_b.ended_down;
next_input.button_x.ended_down = prev_input.button_x.ended_down;
next_input.button_y.ended_down = prev_input.button_y.ended_down;
next_input.button_l_bumper.ended_down =
prev_input.button_l_bumper.ended_down;
next_input.button_r_bumper.ended_down =
prev_input.button_r_bumper.ended_down;
next_input.button_l_stick.ended_down =
prev_input.button_l_stick.ended_down;
next_input.button_r_stick.ended_down =
prev_input.button_r_stick.ended_down;
next_input.analog_l_trigger.value = prev_input.analog_l_trigger.value;
next_input.analog_r_trigger.value = prev_input.analog_r_trigger.value;
next_input.joystick_l.position = prev_input.joystick_l.position;
next_input.joystick_r.position = prev_input.joystick_r.position;
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (handle_sdl_event(&event, &context)) {
running = false;
}
}
next_input.analog_l_trigger.delta =
next_input.analog_l_trigger.value -
prev_input.analog_l_trigger.value;
next_input.analog_r_trigger.delta =
next_input.analog_r_trigger.value -
prev_input.analog_r_trigger.value;
next_input.joystick_l.delta =
next_input.joystick_l.position - prev_input.joystick_l.position;
next_input.joystick_r.delta =
next_input.joystick_r.position - prev_input.joystick_r.position;
window_description_ game_buffer = {};
game_buffer.memory = pixels;
game_buffer.width = START_WIDTH;
game_buffer.height = START_HEIGHT;
game_buffer.pitch = START_WIDTH * 4;
game_buffer.bytes_per_pixel = 4;
game_update_and_render(platform, (game_state_*)game_memory, dt, game_buffer, next_input);
prev_input = next_input;
if (SDL_UpdateTexture(context.texture,0, pixels, START_WIDTH * 4)) {
printf("Unable to update SDL texture: %s\n", SDL_GetError());
return 1;
}
SDL_RenderCopy(context.renderer, context.texture, 0, 0);
SDL_RenderPresent(context.renderer);
print_debug_log();
}
return 0;
}