-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32sdl_platform.cpp
1136 lines (1033 loc) · 43.2 KB
/
win32sdl_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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by doug on 4/16/15.
//
#pragma comment(lib, "SDL2main.lib")
#pragma comment(lib, "SDL2.lib")
#include <math.h>
#include <time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <intrin.h>
// #include <unistd.h>
#include <locale.h>
#include <Windows.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_syswm.h"
#include "easytab.h"
#include "game.h"
#include "win32sdl_platform.h"
void exit_gracefully(int error_code) {
EasyTab_Unload();
exit(error_code);
}
void platform_debug_print(char* str) {
OutputDebugString(str);
}
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;
InterlockedIncrement((LONG volatile*)&queue->remaining);
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 (InterlockedCompareExchange((LONG volatile*)&queue->read_index,
read_index,
original) == original) {
task_* task = queue->tasks + original;
task->callback(queue, task->data);
InterlockedDecrement((LONG volatile*)&queue->remaining);
}
} 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: {
} break;
case SDL_WINDOWEVENT_EXPOSED: {
} break;
} break;
} break;
case SDL_SYSWMEVENT: {
SDL_SysWMEvent sysevent = event->syswm;
EasyTabResult er = EASYTAB_EVENT_NOT_HANDLED;
if (!EasyTab) { break; }
i32 bit_touch_old = (EasyTab->Buttons & EasyTab_Buttons_Pen_Touch);
switch(sysevent.msg->subsystem) {
case SDL_SYSWM_WINDOWS: {
er = EasyTab_HandleEvent(sysevent.msg->msg.win.hwnd,
sysevent.msg->msg.win.msg,
sysevent.msg->msg.win.lParam,
sysevent.msg->msg.win.wParam);
} break;
}
if (er == EASYTAB_OK) {
i32 bit_touch = (EasyTab->Buttons & EasyTab_Buttons_Pen_Touch);
i32 bit_lower = (EasyTab->Buttons & EasyTab_Buttons_Pen_Lower);
i32 bit_upper = (EasyTab->Buttons & EasyTab_Buttons_Pen_Upper);
// Pen in use but not drawing
b32 taking_pen_input = bit_touch
&& !( bit_upper || bit_lower );
if (taking_pen_input) {
i32 window_width = 0;
i32 window_height = 0;
SDL_GetWindowSize(context->window,
&window_width,
&window_height);
pen_input_* pen = &context->next_input->pen;
b32 was_down = pen->touch_state.ended_down;
pen->touch_state.ended_down = true;
if (!was_down) {
pen->touch_state.transition_count++;
}
v2 previous_position = pen->position;
pen->position.x = EasyTab->PosX;
pen->position.y = EasyTab->PosY;
pen->delta.x = pen->position.x - previous_position.x;
pen->delta.y = pen->position.y - previous_position.y;
pen->normalized_position.x =
((f32)pen->position.x) / (0.5f * (f32)window_width) - 1.0f;
pen->normalized_position.y =
-1.0f * (((f32)pen->position.y) / (0.5f * (f32)window_height) - 1.0f);
pen->normalized_delta.x = (f32)pen->delta.x / (0.5f * (f32)window_width);
pen->normalized_delta.y = -((f32)pen->delta.y) / (0.5f * (f32)window_height);
pen->pressure = EasyTab->Pressure;
}
}
} 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->keyboard.up.ended_down = ended_down;
context->next_input->keyboard.up.transition_count++;
context->next_input->button_a.ended_down = ended_down;
context->next_input->button_a.transition_count++;
} break;
case SDLK_DOWN: {
context->next_input->keyboard.down.ended_down = ended_down;
context->next_input->keyboard.down.transition_count++;
context->next_input->button_x.ended_down = ended_down;
context->next_input->button_x.transition_count++;
} break;
case SDLK_LEFT: {
context->next_input->keyboard.left.ended_down = ended_down;
context->next_input->keyboard.left.transition_count++;
context->next_input->joystick_l.position.x = ended_down ?
-1.0f :
(context->next_input->keyboard.right.ended_down ? 1.0f : 0.0f);
} break;
case SDLK_RIGHT: {
context->next_input->keyboard.right.ended_down = ended_down;
context->next_input->keyboard.right.transition_count++;
context->next_input->joystick_l.position.x = ended_down ?
1.0f :
(context->next_input->keyboard.left.ended_down ? -1.0f : 0.0f);
} break;
case SDLK_0: {
context->next_input->keyboard.k0.ended_down = ended_down;
context->next_input->keyboard.k0.transition_count++;
} break;
case SDLK_1: {
context->next_input->keyboard.k1.ended_down = ended_down;
context->next_input->keyboard.k1.transition_count++;
} break;
case SDLK_2: {
context->next_input->keyboard.k2.ended_down = ended_down;
context->next_input->keyboard.k2.transition_count++;
} break;
case SDLK_3: {
context->next_input->keyboard.k3.ended_down = ended_down;
context->next_input->keyboard.k3.transition_count++;
} break;
case SDLK_4: {
context->next_input->keyboard.k4.ended_down = ended_down;
context->next_input->keyboard.k4.transition_count++;
} break;
case SDLK_5: {
context->next_input->keyboard.k5.ended_down = ended_down;
context->next_input->keyboard.k5.transition_count++;
} break;
case SDLK_6: {
context->next_input->keyboard.k6.ended_down = ended_down;
context->next_input->keyboard.k6.transition_count++;
} break;
case SDLK_7: {
context->next_input->keyboard.k7.ended_down = ended_down;
context->next_input->keyboard.k7.transition_count++;
} break;
case SDLK_8: {
context->next_input->keyboard.k8.ended_down = ended_down;
context->next_input->keyboard.k8.transition_count++;
} break;
case SDLK_9: {
context->next_input->keyboard.k9.ended_down = ended_down;
context->next_input->keyboard.k9.transition_count++;
} break;
case SDLK_a: {
context->next_input->keyboard.a.ended_down = ended_down;
context->next_input->keyboard.a.transition_count++;
} break;
case SDLK_b: {
context->next_input->keyboard.b.ended_down = ended_down;
context->next_input->keyboard.b.transition_count++;
} break;
case SDLK_c: {
context->next_input->keyboard.c.ended_down = ended_down;
context->next_input->keyboard.c.transition_count++;
} break;
case SDLK_d: {
context->next_input->keyboard.d.ended_down = ended_down;
context->next_input->keyboard.d.transition_count++;
} break;
case SDLK_e: {
context->next_input->keyboard.e.ended_down = ended_down;
context->next_input->keyboard.e.transition_count++;
} break;
case SDLK_f: {
context->next_input->keyboard.f.ended_down = ended_down;
context->next_input->keyboard.f.transition_count++;
} break;
case SDLK_g: {
context->next_input->keyboard.g.ended_down = ended_down;
context->next_input->keyboard.g.transition_count++;
} break;
case SDLK_h: {
context->next_input->keyboard.h.ended_down = ended_down;
context->next_input->keyboard.h.transition_count++;
} break;
case SDLK_i: {
context->next_input->keyboard.i.ended_down = ended_down;
context->next_input->keyboard.i.transition_count++;
} break;
case SDLK_j: {
context->next_input->keyboard.j.ended_down = ended_down;
context->next_input->keyboard.j.transition_count++;
} break;
case SDLK_k: {
context->next_input->keyboard.k.ended_down = ended_down;
context->next_input->keyboard.k.transition_count++;
} break;
case SDLK_l: {
context->next_input->keyboard.l.ended_down = ended_down;
context->next_input->keyboard.l.transition_count++;
} break;
case SDLK_m: {
context->next_input->keyboard.m.ended_down = ended_down;
context->next_input->keyboard.m.transition_count++;
} break;
case SDLK_n: {
context->next_input->keyboard.n.ended_down = ended_down;
context->next_input->keyboard.n.transition_count++;
} break;
case SDLK_o: {
context->next_input->keyboard.o.ended_down = ended_down;
context->next_input->keyboard.o.transition_count++;
} break;
case SDLK_p: {
context->next_input->keyboard.p.ended_down = ended_down;
context->next_input->keyboard.p.transition_count++;
} break;
case SDLK_q: {
context->next_input->keyboard.q.ended_down = ended_down;
context->next_input->keyboard.q.transition_count++;
} break;
case SDLK_r: {
context->next_input->keyboard.r.ended_down = ended_down;
context->next_input->keyboard.r.transition_count++;
} break;
case SDLK_s: {
context->next_input->keyboard.s.ended_down = ended_down;
context->next_input->keyboard.s.transition_count++;
} break;
case SDLK_t: {
context->next_input->keyboard.t.ended_down = ended_down;
context->next_input->keyboard.t.transition_count++;
} break;
case SDLK_u: {
context->next_input->keyboard.u.ended_down = ended_down;
context->next_input->keyboard.u.transition_count++;
} break;
case SDLK_v: {
context->next_input->keyboard.v.ended_down = ended_down;
context->next_input->keyboard.v.transition_count++;
} break;
case SDLK_w: {
context->next_input->keyboard.w.ended_down = ended_down;
context->next_input->keyboard.w.transition_count++;
} break;
case SDLK_x: {
context->next_input->keyboard.x.ended_down = ended_down;
context->next_input->keyboard.x.transition_count++;
context->next_input->analog_r_trigger.value = ended_down ? 1.0f : 0.0f;
} break;
case SDLK_y: {
context->next_input->keyboard.y.ended_down = ended_down;
context->next_input->keyboard.y.transition_count++;
} break;
case SDLK_z: {
context->next_input->keyboard.z.ended_down = ended_down;
context->next_input->keyboard.z.transition_count++;
context->next_input->analog_l_trigger.value = ended_down ? 1.0f : 0.0f;
} break;
case SDLK_LALT:
case SDLK_RALT: {
context->next_input->keyboard.alt_down = ended_down;
} break;
case SDLK_LCTRL:
case SDLK_RCTRL: {
context->next_input->keyboard.ctrl_down = ended_down;
} break;
case SDLK_LSHIFT:
case SDLK_RSHIFT: {
context->next_input->keyboard.shift_down = ended_down;
} break;
case SDLK_QUOTE: {
} break;
case SDLK_BACKQUOTE: {
} break;
case SDLK_BACKSLASH: {
} break;
case SDLK_BACKSPACE: {
} break;
case SDLK_COMMA: {
} break;
case SDLK_DELETE: {
} break;
case SDLK_LEFTBRACKET: {
} break;
case SDLK_MINUS: {
} break;
case SDLK_PERIOD: {
} break;
case SDLK_RETURN: {
} break;
case SDLK_RIGHTBRACKET: {
} break;
case SDLK_SEMICOLON: {
} break;
case SDLK_SLASH: {
} break;
case SDLK_SPACE: {
} break;
case SDLK_RETURN2: {
} break;
case SDLK_TAB: {
} break;
case SDLK_AC_BACK: {
} break;
case SDLK_AC_BOOKMARKS: {
} break;
case SDLK_AC_FORWARD: {
} break;
case SDLK_AC_HOME: {
} break;
case SDLK_AC_REFRESH: {
} break;
case SDLK_AC_SEARCH: {
} break;
case SDLK_AC_STOP: {
} break;
case SDLK_AGAIN: {
} break;
case SDLK_ALTERASE: {
} break;
case SDLK_APPLICATION: {
} break;
case SDLK_AUDIOMUTE: {
} break;
case SDLK_AUDIONEXT: {
} break;
case SDLK_AUDIOPLAY: {
} break;
case SDLK_AUDIOPREV: {
} break;
case SDLK_AUDIOSTOP: {
} break;
case SDLK_BRIGHTNESSDOWN: {
} break;
case SDLK_BRIGHTNESSUP: {
} break;
case SDLK_CALCULATOR: {
} break;
case SDLK_CANCEL: {
} break;
case SDLK_CAPSLOCK: {
} break;
case SDLK_CLEAR: {
} break;
case SDLK_CLEARAGAIN: {
} break;
case SDLK_COMPUTER: {
} break;
case SDLK_COPY: {
} break;
case SDLK_CRSEL: {
} break;
case SDLK_CURRENCYSUBUNIT: {
} break;
case SDLK_CURRENCYUNIT: {
} break;
case SDLK_CUT: {
} break;
case SDLK_DECIMALSEPARATOR: {
} break;
case SDLK_DISPLAYSWITCH: {
} break;
case SDLK_EJECT: {
} break;
case SDLK_END: {
} break;
case SDLK_EQUALS: {
} break;
case SDLK_ESCAPE: {
} break;
case SDLK_EXECUTE: {
} break;
case SDLK_EXSEL: {
} break;
case SDLK_F1: {
} break;
case SDLK_F10: {
} break;
case SDLK_F11: {
} break;
case SDLK_F12: {
} break;
case SDLK_F13: {
} break;
case SDLK_F14: {
} break;
case SDLK_F15: {
} break;
case SDLK_F16: {
} break;
case SDLK_F17: {
} break;
case SDLK_F18: {
} break;
case SDLK_F19: {
} break;
case SDLK_F2: {
} break;
case SDLK_F20: {
} break;
case SDLK_F21: {
} break;
case SDLK_F22: {
} break;
case SDLK_F23: {
} break;
case SDLK_F24: {
} break;
case SDLK_F3: {
} break;
case SDLK_F4: {
} break;
case SDLK_F5: {
} break;
case SDLK_F6: {
} break;
case SDLK_F7: {
} break;
case SDLK_F8: {
} break;
case SDLK_F9: {
} break;
case SDLK_FIND: {
} break;
case SDLK_HELP: {
} break;
case SDLK_HOME: {
} break;
case SDLK_INSERT: {
} break;
case SDLK_KBDILLUMDOWN: {
} break;
case SDLK_KBDILLUMTOGGLE: {
} break;
case SDLK_KBDILLUMUP: {
} break;
case SDLK_KP_0: {
} break;
case SDLK_KP_00: {
} break;
case SDLK_KP_000: {
} break;
case SDLK_KP_1: {
} break;
case SDLK_KP_2: {
} break;
case SDLK_KP_3: {
} break;
case SDLK_KP_4: {
} break;
case SDLK_KP_5: {
} break;
case SDLK_KP_6: {
} break;
case SDLK_KP_7: {
} break;
case SDLK_KP_8: {
} break;
case SDLK_KP_9: {
} break;
case SDLK_KP_A: {
} break;
case SDLK_KP_AMPERSAND: {
} break;
case SDLK_KP_AT: {
} break;
case SDLK_KP_B: {
} break;
case SDLK_KP_BACKSPACE: {
} break;
case SDLK_KP_BINARY: {
} break;
case SDLK_KP_C: {
} break;
case SDLK_KP_CLEAR: {
} break;
case SDLK_KP_CLEARENTRY: {
} break;
case SDLK_KP_COLON: {
} break;
case SDLK_KP_COMMA: {
} break;
case SDLK_KP_D: {
} break;
case SDLK_KP_DBLAMPERSAND: {
} break;
case SDLK_KP_DBLVERTICALBAR: {
} break;
case SDLK_KP_DECIMAL: {
} break;
case SDLK_KP_DIVIDE: {
} break;
case SDLK_KP_E: {
} break;
case SDLK_KP_ENTER: {
} break;
case SDLK_KP_EQUALS: {
} break;
case SDLK_KP_EQUALSAS400: {
} break;
case SDLK_KP_EXCLAM: {
} break;
case SDLK_KP_F: {
} break;
case SDLK_KP_GREATER: {
} break;
case SDLK_KP_HASH: {
} break;
case SDLK_KP_HEXADECIMAL: {
} break;
case SDLK_KP_LEFTBRACE: {
} break;
case SDLK_KP_LEFTPAREN: {
} break;
case SDLK_KP_LESS: {
} break;
case SDLK_KP_MEMADD: {
} break;
case SDLK_KP_MEMCLEAR: {
} break;
case SDLK_KP_MEMDIVIDE: {
} break;
case SDLK_KP_MEMMULTIPLY: {
} break;
case SDLK_KP_MEMRECALL: {
} break;
case SDLK_KP_MEMSTORE: {
} break;
case SDLK_KP_MEMSUBTRACT: {
} break;
case SDLK_KP_MINUS: {
} break;
case SDLK_KP_MULTIPLY: {
} break;
case SDLK_KP_OCTAL: {
} break;
case SDLK_KP_PERCENT: {
} break;
case SDLK_KP_PERIOD: {
} break;
case SDLK_KP_PLUS: {
} break;
case SDLK_KP_PLUSMINUS: {
} break;
case SDLK_KP_POWER: {
} break;
case SDLK_KP_RIGHTBRACE: {
} break;
case SDLK_KP_RIGHTPAREN: {
} break;
case SDLK_KP_SPACE: {
} break;
case SDLK_KP_TAB: {
} break;
case SDLK_KP_VERTICALBAR: {
} break;
case SDLK_KP_XOR: {
} break;
case SDLK_LGUI: {
} break;
case SDLK_MAIL: {
} break;
case SDLK_MEDIASELECT: {
} break;
case SDLK_MENU: {
} break;
case SDLK_MODE: {
} break;
case SDLK_MUTE: {
} break;
case SDLK_NUMLOCKCLEAR: {
} break;
case SDLK_OPER: {
} break;
case SDLK_OUT: {
} break;
case SDLK_PAGEDOWN: {
} break;
case SDLK_PAGEUP: {
} break;
case SDLK_PASTE: {
} break;
case SDLK_PAUSE: {
} break;
case SDLK_POWER: {
} break;
case SDLK_PRINTSCREEN: {
} break;
case SDLK_PRIOR: {
} break;
case SDLK_RGUI: {
} break;
case SDLK_SCROLLLOCK: {
} break;
case SDLK_SELECT: {
} break;
case SDLK_SEPARATOR: {
} break;
case SDLK_SLEEP: {
} break;
case SDLK_STOP: {
} break;
case SDLK_SYSREQ: {
} break;
case SDLK_THOUSANDSSEPARATOR: {
} break;
case SDLK_UNDO: {
} break;
case SDLK_UNKNOWN: {
} break;
case SDLK_VOLUMEDOWN: {
} break;
case SDLK_VOLUMEUP: {
} break;
case SDLK_WWW: {
} break;
}
}
} break;
case SDL_CONTROLLERDEVICEADDED: {
if (context->controller_handle) {
SDL_GameControllerClose(context->controller_handle);
}
context->controller_handle = SDL_GameControllerOpen(event->cdevice.which);
} break;
case SDL_CONTROLLERDEVICEREMOVED: {
if (context->controller_handle) {
SDL_GameControllerClose(context->controller_handle);
}
} break;
case SDL_MOUSEMOTION: {
i32 window_width = 0;
i32 window_height = 0;
SDL_GetWindowSize(context->window,
&window_width,
&window_height);
v2 previous_position = context->next_input->mouse.position;
context->next_input->mouse.position.x = (f32)event->motion.x;
context->next_input->mouse.position.y = (f32)event->motion.y;
context->next_input->mouse.delta.x = (f32)event->motion.xrel;
context->next_input->mouse.delta.y = (f32)event->motion.yrel;
context->next_input->mouse.normalized_position.x =
((f32)event->motion.x) / (0.5f * (f32)window_width) - 1.0f;
context->next_input->mouse.normalized_position.y =
-1.0f * (((f32)event->motion.y) / (0.5f * (f32)window_height) - 1.0f);
context->next_input->mouse.normalized_delta.x =
(f32)event->motion.xrel / (0.5f * (f32)window_width);
context->next_input->mouse.normalized_delta.y =
-((f32)event->motion.yrel) / (0.5f * (f32)window_height);
} break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
b32 ended_down = event->button.state != SDL_RELEASED;
switch (event->button.button) {
case SDL_BUTTON_LEFT: {
context->next_input->mouse.left_click.ended_down = ended_down;
context->next_input->mouse.left_click.transition_count++;
} break;
case SDL_BUTTON_RIGHT: {
context->next_input->mouse.right_click.ended_down = ended_down;
context->next_input->mouse.right_click.transition_count++;
} break;
case SDL_BUTTON_MIDDLE: {
context->next_input->mouse.middle_click.ended_down = ended_down;
context->next_input->mouse.middle_click.transition_count++;
} break;
}
} break;
case SDL_MOUSEWHEEL: {
context->next_input->mouse.wheel_delta = event->wheel.y;
} break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP: {
u8 button = event->cbutton.button;
b32 ended_down = event->cbutton.state != SDL_RELEASED;
switch (button) {
case SDL_CONTROLLER_BUTTON_A: {
context->next_input->button_a.ended_down = ended_down;
context->next_input->button_a.transition_count++;
} break;
case SDL_CONTROLLER_BUTTON_B: {
context->next_input->button_b.ended_down = ended_down;
context->next_input->button_b.transition_count++;
} break;
case SDL_CONTROLLER_BUTTON_X: {
context->next_input->button_x.ended_down = ended_down;
context->next_input->button_x.transition_count++;
} break;
case SDL_CONTROLLER_BUTTON_Y: {
context->next_input->button_y.ended_down = ended_down;
context->next_input->button_y.transition_count++;
} break;
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: {
context->next_input->joystick_l.position.x = ended_down ? -1.0f : 0.0f;
} break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: {
context->next_input->joystick_l.position.x = ended_down ? 1.0f : 0.0f;
} break;
case SDL_CONTROLLER_BUTTON_BACK: {
} break;
case SDL_CONTROLLER_BUTTON_GUIDE: {
} break;
case SDL_CONTROLLER_BUTTON_START: {
} break;
case SDL_CONTROLLER_BUTTON_LEFTSTICK: {
} break;
case SDL_CONTROLLER_BUTTON_RIGHTSTICK: {
} break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: {
} break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: {
} break;
case SDL_CONTROLLER_BUTTON_DPAD_UP: {
} break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: {
} break;
}
} break;
case SDL_CONTROLLERAXISMOTION: {
u8 axis = event->caxis.axis;
i32 value = event->caxis.value;
switch (axis) {
case SDL_CONTROLLER_AXIS_LEFTX: {
context->next_input->joystick_l.position.x = ((f32)value) / 32768.0f;
} break;
case SDL_CONTROLLER_AXIS_LEFTY: {
context->next_input->joystick_l.position.y = ((f32)value) / 32768.0f;
} break;
case SDL_CONTROLLER_AXIS_RIGHTX: {
context->next_input->joystick_r.position.x = ((f32)value) / 32768.0f;
} break;
case SDL_CONTROLLER_AXIS_RIGHTY: {
context->next_input->joystick_r.position.y = ((f32)value) / 32768.0f;
} break;
case SDL_CONTROLLER_AXIS_TRIGGERLEFT: {
context->next_input->analog_l_trigger.value = ((f32)value) / 32768.0f;
} break;
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: {
context->next_input->analog_r_trigger.value = ((f32)value) / 32768.0f;
} break;
}
} break;
}
return should_quit;
}
SDL_GameController* find_controller_handle() {
i32 num_joysticks = SDL_NumJoysticks();
for (int i = 0; i < num_joysticks; ++i) {
printf("%s\n", SDL_GameControllerName(SDL_GameControllerOpen(i)));
return SDL_GameControllerOpen(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 printer_task(task_queue_* queue, void* data) {
char* as_str = (char*)data;
printf("%s\n", as_str);
}
void check_sdl_error(int line = -1) {
const char *error = SDL_GetError();
if (*error != '\0')
{
char buffer[1024];
sprintf(buffer, "SDL Error: %s\n", error);
if (line != -1)
sprintf(buffer, " + line: %i\n", line);
SDL_ClearError();
OutputDebugString(buffer);
}
}
const char* to_print = "hello, world";
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
) {
setlocale(LC_NUMERIC, "");
platform_context_ context = {0};
if (SDL_Init(SDL_INIT_EVERYTHING)) {
printf("Unable to init SDL: %s\n", SDL_GetError());
exit_gracefully(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);
}
check_sdl_error(__LINE__);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
// SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
context.window = SDL_CreateWindow("Physica",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
START_WIDTH,
START_HEIGHT,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
check_sdl_error(__LINE__);
context.gl_context = SDL_GL_CreateContext(context.window);
check_sdl_error(__LINE__);
SDL_GL_MakeCurrent(context.window, context.gl_context);
check_sdl_error(__LINE__);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
OutputDebugString("Error initializing GLEW");
exit_gracefully(1);
}
if( SDL_GL_SetSwapInterval(1) < 0 ){
check_sdl_error(__LINE__);
}
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(2.0f);
glClearColor(0.784f, 0.8745f, 0.925f, 1.0f);
// context.renderer = SDL_CreateRenderer(context.window, -1, SDL_RENDERER_PRESENTVSYNC);
// check_sdl_error(__LINE__);
// SDL_RendererInfo renderer_info;
// SDL_GetRendererInfo(context.renderer, &renderer_info);
// if ((renderer_info.flags & SDL_RENDERER_ACCELERATED) == 0 ||
// (renderer_info.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
// assert_(false);
// }
// 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) {