-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_platform.cpp
More file actions
561 lines (473 loc) · 21 KB
/
linux_platform.cpp
File metadata and controls
561 lines (473 loc) · 21 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//
// Created by doug on 4/16/15.
//
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <xcb/xcb.h>
#include <xcb/xcb_image.h>
#include <xcb/xcb_keysyms.h>
#include <X11/keysym.h>
#include <alsa/asoundlib.h>
#include <linux/joystick.h>
#define class class_name
#include <xcb/xcb_icccm.h>
#undef class
#include "game.h"
#include "linux_platform.h"
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);
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
result.contents = (u8 *)string;
result.content_size = (u32) fsize;
return result;
}
void render_weird_gradient(i32 offset_x, i32 offset_y, offscreen_buffer_ offscreen_buffer){
u32* pixel = (u32*)offscreen_buffer.memory;
for (int y = 0; y < offscreen_buffer.height; ++y)
{
for (int x = 0; x < offscreen_buffer.width; ++x)
{
*pixel = (u8)(x + offset_x) | ((u8)(y + offset_y) << 8);
pixel++;
}
}
}
i16* init_alsa(snd_pcm_** out_handle) {
snd_pcm_* pcm_handle;
i32 err;
snd_output_* log;
snd_pcm_hw_params_* params;
i16* sound_buffer = (i16*)malloc(SOUND_BUFFER_SIZE * 2);
const char* default_hardware_device = (char*)"hw:0,0";
snd_output_stdio_attach(&log, stderr, 0);
err = snd_pcm_open(&pcm_handle, default_hardware_device, SND_PCM_STREAM_PLAYBACK, 0);
if (!err) {
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
snd_pcm_access_ access = SND_PCM_ACCESS_RW_INTERLEAVED;
snd_pcm_hw_params_set_access(pcm_handle, params, access);
snd_pcm_format_ format = SND_PCM_FORMAT_S16_LE; // signed 16-bit little-endian
snd_pcm_hw_params_set_format (pcm_handle, params, format);
snd_pcm_hw_params_set_rate(pcm_handle, params, AUDIO_SAMPLE_RATE, 0);
snd_pcm_hw_params_set_period_size(pcm_handle, params, PERIOD_SIZE, 0);
snd_pcm_hw_params_set_buffer_size(pcm_handle, params, SOUND_BUFFER_SIZE);
u32 channel_count = 2;
snd_pcm_hw_params_set_channels(pcm_handle, params, channel_count);
snd_pcm_hw_params(pcm_handle, params);
snd_pcm_dump(pcm_handle, log);
} else {
// TODO(doug): logging
}
*out_handle = pcm_handle;
return sound_buffer;
}
offscreen_buffer_ init_xcb_buffer(platform_context context) {
offscreen_buffer_ offscreen_buffer = {};
if (offscreen_buffer.xcb_image) {
xcb_image_destroy(offscreen_buffer.xcb_image);
}
if (offscreen_buffer.xcb_pixmap_id) {
xcb_free_pixmap(context.connection, offscreen_buffer.xcb_pixmap_id);
}
if (offscreen_buffer.xcb_gcontext_id) {
xcb_free_gc(context.connection, offscreen_buffer.xcb_gcontext_id);
}
u8 pad = 32;
u8 depth = 24;
u8 bpp = 32;
xcb_format_ *fmt = xcb_setup_pixmap_formats(context.setup);
xcb_format_ *fmtend = fmt + xcb_setup_pixmap_formats_length(context.setup);
while (fmt++ != fmtend)
{
if (fmt->scanline_pad == pad && fmt->depth == depth && fmt->bits_per_pixel == bpp)
{
break;
}
}
if (fmt > fmtend) {
fmt = 0;
}
size_t image_size = START_WIDTH * START_HEIGHT * (fmt->bits_per_pixel / 8);
u8 *image_data = (u8 *)malloc(image_size);
xcb_image_* xcb_image =
xcb_image_create(START_WIDTH,
START_HEIGHT,
XCB_IMAGE_FORMAT_Z_PIXMAP,
fmt->scanline_pad,
fmt->depth,
fmt->bits_per_pixel,
0,
(xcb_image_order_)context.setup->image_byte_order,
XCB_IMAGE_ORDER_LSB_FIRST,
image_data,
image_size,
image_data);
offscreen_buffer.width = START_WIDTH;
offscreen_buffer.height = START_HEIGHT;
offscreen_buffer.bytes_per_pixel = fmt->bits_per_pixel / 8;
offscreen_buffer.pitch = offscreen_buffer.bytes_per_pixel *
offscreen_buffer.width;
offscreen_buffer.xcb_image = xcb_image;
offscreen_buffer.memory = offscreen_buffer.xcb_image->data;
offscreen_buffer.xcb_pixmap_id = xcb_generate_id(context.connection);
xcb_create_pixmap(context.connection, fmt->depth,
offscreen_buffer.xcb_pixmap_id, context.window,
offscreen_buffer.width, offscreen_buffer.height);
xcb_flush(context.connection);
offscreen_buffer.xcb_gcontext_id = xcb_generate_id(context.connection);
xcb_create_gc(context.connection, offscreen_buffer.xcb_gcontext_id,
offscreen_buffer.xcb_pixmap_id, 0, 0);
xcb_flush(context.connection);
xcb_image_put(context.connection,
offscreen_buffer.xcb_pixmap_id,
offscreen_buffer.xcb_gcontext_id,
offscreen_buffer.xcb_image,
0, 0, 0);
xcb_flush(context.connection);
return offscreen_buffer;
}
int main(int argc, char const *argv[]) {
platform_context context = {};
context.connection = xcb_connect(0, 0);
context.key_symbols = xcb_key_symbols_alloc(context.connection);
context.setup = xcb_get_setup(context.connection);
xcb_screen_iterator_ iter = xcb_setup_roots_iterator(context.setup);
xcb_screen_* screen = iter.data;
context.window = xcb_generate_id(context.connection);
u32 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
u32 valwin[2] = {
0xffffffff,
XCB_EVENT_MASK_EXPOSURE |
XCB_EVENT_MASK_KEY_PRESS |
XCB_EVENT_MASK_KEY_RELEASE
};
xcb_create_window(context.connection,
XCB_COPY_FROM_PARENT,
context.window,
screen->root,
0, 0,
START_WIDTH, START_HEIGHT,
10,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
mask,
valwin);
u16 dw_name_length = 16;
u16 proto_name_length = 12;
xcb_intern_atom_cookie_ wm_delete_window_cookie =
xcb_intern_atom(context.connection,
0,
dw_name_length,
"WM_DELETE_WINDOW");
xcb_intern_atom_cookie_ wm_protocols_cookie =
xcb_intern_atom(context.connection,
0,
proto_name_length,
"WM_PROTOCOLS");
xcb_intern_atom_reply_* wm_delete_window_cookie_reply =
xcb_intern_atom_reply(context.connection,
wm_delete_window_cookie,
0);
xcb_intern_atom_reply_* wm_protocols_cookie_reply =
xcb_intern_atom_reply(context.connection, wm_protocols_cookie, 0);
xcb_atom_ wm_protocols_atom = {};
xcb_atom_ wm_delete_window_atom = {};
if (wm_protocols_cookie_reply)
{
wm_protocols_atom = wm_protocols_cookie_reply->atom;
} else {
// TODO(doug): Diagnostics
}
if (wm_delete_window_cookie_reply)
{
wm_delete_window_atom = wm_delete_window_cookie_reply->atom;
} else {
// TODO(doug): Diagnostics
}
xcb_change_property(context.connection,
XCB_PROP_MODE_REPLACE,
context.window,
wm_protocols_atom,
4, 32, 1,
&wm_delete_window_atom);
char title[] = "wiz biz";
xcb_change_property (context.connection,
XCB_PROP_MODE_REPLACE,
context.window,
XCB_ATOM_WM_NAME,
XCB_ATOM_STRING,
8,
7,
title );
xcb_map_window(context.connection, context.window);
xcb_flush(context.connection);
const u64 one_gig = 1024LL * 1024LL * 1024LL;
void* game_memory = calloc(one_gig, sizeof(u8));
offscreen_buffer_ offscreen_buffer = init_xcb_buffer(context);
snd_pcm_* pcm_handle;
i16* sound_buffer = init_alsa(&pcm_handle);
u32 frame_index = 0;
u32 s = 0;
bool running = true;
timespec previous_time = {};
timespec next_time = {};
clock_gettime(CLOCK_MONOTONIC, &previous_time);
game_input_ prev_input = {};
game_input_ next_input = {};
i32 game_pad = open("/dev/input/js0", O_NONBLOCK);
const u32 target_nanoseconds = 1000000000LL / FRAME_RATE;
while(running) {
clock_gettime(CLOCK_MONOTONIC, &next_time);
timespec target_time;
target_time.tv_nsec = previous_time.tv_nsec + target_nanoseconds;
target_time.tv_sec = previous_time.tv_sec;
const i64 one_billion = 1000 * 1000 * 1000;
if (target_time.tv_nsec > one_billion) {
target_time.tv_nsec -= one_billion;
target_time.tv_sec += 1;
}
i64 delta_tn =
(next_time.tv_nsec - previous_time.tv_nsec) +
((next_time.tv_sec - previous_time.tv_sec) * one_billion);
i64 target_delta_tn =
(target_time.tv_nsec - previous_time.tv_nsec) +
((target_time.tv_sec - previous_time.tv_sec) * one_billion);
while (delta_tn < target_delta_tn) {
clock_gettime(CLOCK_MONOTONIC, &next_time);
delta_tn =
(next_time.tv_nsec - previous_time.tv_nsec) +
((next_time.tv_sec - previous_time.tv_sec) * one_billion);
}
previous_time = next_time;
xcb_generic_event_ *event = 0;
xcb_generic_event_ *peek_event = 0;
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;
js_event game_pad_event;
// can't do a do...while :(
#define __CASE_UPDATE_GP_BUTTON(button, input_name) \
case (button): { \
next_input.input_name.ended_down = game_pad_event.value; \
next_input.input_name.transition_count++; \
} break;
while (read(game_pad, &game_pad_event, sizeof(game_pad_event)) > 0) {
if (game_pad_event.type == JS_EVENT_BUTTON) {
switch (game_pad_event.number) {
__CASE_UPDATE_GP_BUTTON(BUTTON_A, button_a)
__CASE_UPDATE_GP_BUTTON(BUTTON_B, button_b)
__CASE_UPDATE_GP_BUTTON(BUTTON_X, button_x)
__CASE_UPDATE_GP_BUTTON(BUTTON_Y, button_y)
__CASE_UPDATE_GP_BUTTON(BUTTON_R_BUMPER, button_r_bumper)
__CASE_UPDATE_GP_BUTTON(BUTTON_L_BUMPER, button_l_bumper)
__CASE_UPDATE_GP_BUTTON(BUTTON_L_STICK, button_l_stick)
__CASE_UPDATE_GP_BUTTON(BUTTON_R_STICK, button_r_stick)
}
} else if (game_pad_event.type == JS_EVENT_AXIS) {
switch (game_pad_event.number) {
case L_STICK_X: {
// don't use deadzones - we want game code to manage this
next_input.joystick_l.position.x =
((f32)game_pad_event.value) / 32768.0f;
} break;
case L_STICK_Y: {
next_input.joystick_l.position.y =
((f32)game_pad_event.value) / -32768.0f;
} break;
case R_STICK_X: {
next_input.joystick_r.position.x =
((f32)game_pad_event.value) / 32768.0f;
} break;
case R_STICK_Y: {
next_input.joystick_r.position.y =
((f32)game_pad_event.value) / -32768.0f;
} break;
case L_TRIGGER: {
next_input.analog_l_trigger.value =
(((f32)game_pad_event.value) / 65536.0f) + 0.5f;
} break;
case R_TRIGGER: {
next_input.analog_r_trigger.value =
(((f32)game_pad_event.value) / 65536.0f) + 0.5f;
} break;
}
}
}
while ((event = peek_event) ||
(event = xcb_poll_for_event(context.connection))) {
peek_event = 0;
// NOTE(nbm): The high-order bit of response_type is whether the event
// is synthetic. I'm not sure I care, but let's grab it in case.
b32 synthetic_event = (event->response_type & 0x80) != 0;
u8 response_type = event->response_type & ~0x80;
switch(response_type) {
case XCB_KEY_PRESS: {
xcb_keycode_ key_code = ((xcb_key_press_event_*)event)->detail;
xcb_keysym_ keysym = xcb_key_symbols_get_keysym(context.key_symbols, key_code, 0);
if (keysym == XK_Up) {
if (!prev_input.button_a.ended_down) {
next_input.button_a.ended_down = true;
next_input.button_a.transition_count++;
}
} else if (keysym == XK_Down) {
if (!prev_input.button_x.ended_down) {
next_input.button_x.ended_down = true;
next_input.button_x.transition_count++;
}
} else if (keysym == XK_Left) {
next_input.joystick_l.position.x = -1.0f;
} else if (keysym == XK_Right) {
next_input.joystick_l.position.x = 1.0f;
} else if (keysym == XK_z) {
if (!prev_input.lshift.ended_down) {
next_input.lshift.ended_down = true;
next_input.lshift.transition_count++;
}
} else if (keysym == XK_x) {
if (!prev_input.rshift.ended_down) {
next_input.rshift.ended_down = true;
next_input.rshift.transition_count++;
}
}
} break;
case XCB_KEY_RELEASE: {
xcb_keycode_ key_code = ((xcb_key_release_event_*)event)->detail;
event = 0;
if ((peek_event = xcb_poll_for_event(context.connection))) {
u8 peek_response_type = peek_event->response_type & ~0x80;
if (peek_response_type == XCB_KEY_PRESS) {
xcb_keycode_ peek_key_code = ((xcb_key_press_event_*)peek_event)->detail;
if (peek_key_code == key_code) {
peek_event = 0;
break;
}
}
}
xcb_keysym_ keysym = xcb_key_symbols_get_keysym(context.key_symbols, key_code, 0);
if (keysym == XK_Up) {
if (prev_input.button_a.ended_down) {
next_input.button_a.ended_down = false;
next_input.button_a.transition_count++;
}
} else if (keysym == XK_Down) {
if (prev_input.button_x.ended_down) {
next_input.button_x.ended_down = false;
next_input.button_x.transition_count++;
}
} else if (keysym == XK_Left) {
next_input.joystick_l.position.x = 0.0f;
} else if (keysym == XK_Right) {
next_input.joystick_l.position.x = 0.0f;
} else if (keysym == XK_z) {
if (prev_input.lshift.ended_down) {
next_input.lshift.ended_down = false;
next_input.lshift.transition_count++;
}
} else if (keysym == XK_x) {
if (prev_input.rshift.ended_down) {
next_input.rshift.ended_down = false;
next_input.rshift.transition_count++;
}
}
} break;
case XCB_CLIENT_MESSAGE: {
xcb_client_message_event_* client_message_event =
(xcb_client_message_event_*)event;
if (client_message_event->type == wm_protocols_atom)
{
if (client_message_event->data.data32[0] ==
wm_delete_window_atom)
{
running = false;
}
}
} break;
default: {
} break;
}
}
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 = offscreen_buffer.xcb_image->data;
game_buffer.width = offscreen_buffer.width;
game_buffer.height = offscreen_buffer.height;
game_buffer.pitch = offscreen_buffer.pitch;
game_buffer.bytes_per_pixel = offscreen_buffer.bytes_per_pixel;
// f32 dt = (f32)(((f64)delta_tn) / (f64)(1000LL * 1000LL * 1000LL));
f32 dt = (f32)(((f64)target_delta_tn) / (f64)(1000LL * 1000LL * 1000LL));
game_update_and_render((game_state_*)game_memory, dt, game_buffer, next_input);
prev_input = next_input;
xcb_image_put(context.connection, offscreen_buffer.xcb_pixmap_id,
offscreen_buffer.xcb_gcontext_id, offscreen_buffer.xcb_image, 0, 0, 0);
xcb_flush(context.connection);
xcb_copy_area(context.connection, offscreen_buffer.xcb_pixmap_id, context.window,
offscreen_buffer.xcb_gcontext_id, 0,0, 0, 0, offscreen_buffer.xcb_image->width,
offscreen_buffer.xcb_image->height);
xcb_flush(context.connection);
// i32 available_frames = snd_pcm_avail(pcm_handle);
// if (available_frames > 0) {
// if (available_frames < SOUND_BUFFER_SIZE) {
// game_get_sound_samples((game_state_*)game_memory, sound_buffer, available_frames);
// } else {
// game_get_sound_samples((game_state_*)game_memory, sound_buffer, SOUND_BUFFER_SIZE);
// }
// i32 frames_written = snd_pcm_writei(pcm_handle, sound_buffer, available_frames);
// if (frames_written < 0) {
// frames_written = snd_pcm_recover(pcm_handle, frames_written, 0);
// if (frames_written < 0) {
// //TODO(doug): logging
// }
// }
// }
}
xcb_flush(context.connection);
xcb_disconnect(context.connection);
return 0;
}