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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
|
#include <SDL2/SDL.h>
#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <ft2build.h>
#include FT_FREETYPE_H
/*
* Project Estimation/Target ~ 1 - 2 months (was so off on this)
* well, to be fair, if I account for actual time worked, then I was not
* so off on my estimate, I think I am still in the estimation range
* Tasks:
* DONE:
* - gravity - very barebones version done
* - horizontal motion on ground
* - accelerate
* - constant speed
* - decelerate to give impression of sliding
* - horizontal motion when falling
* - inertia-like movement when accelerating prior to falling
* - loss of inertia when player was at high speed before
* sliding off and other small movement when in free fall
* - movement and deceleration when not moving in free fall
* - Jumping
* - Fixed framerate to prevent weird quirks with movement
* - Basic camera follower
* - move from row-major to column major setup for math library
* TODO:
* - Efficient Quad Renderer
* - Some way to make and define levels
* - Level Creation
* - Update camera follower for centering player in view (with limits) after
* a few seconds (maybe like 2 seconds)
* - Level completion Object
* - Implement Broad Phase Collision for efficient collision handling
* - Audio
*/
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef float r32;
typedef double r64;
typedef u8 b8;
#include "memory/arena.h"
#include "math.h"
enum PMoveState {
NO_MOVE = 0,
MOVE = 1,
FALL_MOVE = 2,
};
enum PlatformKey {
PK_NIL = 0,
PK_W = 1,
PK_A = 2,
PK_S = 3,
PK_D = 4,
};
struct TextChar {
s64 advance;
Vec2 size;
Vec2 bearing;
};
struct TextState {
u32 pixel_size;
u32 texture_atlas_id;
u32 sp;
u32 vao;
u32 vbo;
u32 chunk_size;
s32* char_indexes;
Mat4* transforms;
TextChar* char_map;
};
#define BATCH_SIZE 500
#define MAT4_ELE 4*4
struct r32_array {
r32* buffer;
u32 size;
u32 capacity;
};
void array_init(Arena* a, r32_array* arr, u32 capacity) {
arr->buffer = (r32*)arena_alloc(a, capacity*sizeof(r32));
assert(arr->buffer != NULL);
arr->size = 0;
arr->capacity = capacity;
}
void array_insert(r32_array* arr, r32* ele, u32 ele_size) {
b8 assert_cond = arr->size + ele_size <= arr->capacity;
if (!assert_cond) {
SDL_Log("arr->size: %d, arr->capacity: %d", arr->size, arr->capacity);
}
assert(assert_cond);
void* ptr = &arr->buffer[arr->size];
memcpy(ptr, ele, sizeof(r32)*ele_size);
arr->size += ele_size;
}
void array_clear(r32_array* arr) {
memset(arr->buffer, 0, sizeof(r32)*arr->capacity);
arr->size = 0;
}
struct GLRenderer {
// colored quad
b8 cq_init;
u32 cq_sp;
u32 cq_vao;
// camera
b8 cam_update;
Vec3 preset_up_dir;
Vec3 cam_pos;
Vec3 cam_look;
Mat4 cam_view;
Mat4 cam_proj;
// Batched cq
// batching buffer
u32 cq_batch_sp;
u32 cq_batch_vao;
u32 cq_batch_vbo;
u32 cq_batch_count;
r32_array cq_pos_batch;
r32_array cq_mvp_batch;
r32_array cq_color_batch;
// ui text
TextState ui_text;
};
struct Controller {
b8 move_up;
b8 move_down;
b8 move_left;
b8 move_right;
b8 jump;
b8 toggle_gravity;
};
struct Rect {
Vec2 tl;
Vec2 br;
Vec2 size;
Vec3 position;
};
class FrameTimerV2 {
public:
FrameTimerV2();
void update();
void enforceFramerate(u32 framerate);
public:
r64 m_tCurr;
r64 m_tPrev;
r64 m_tDeltaMS;
r64 m_tDelta;
};
FrameTimerV2::FrameTimerV2() {
m_tCurr = SDL_GetTicks64();
m_tPrev = m_tCurr;
m_tDeltaMS = m_tCurr - m_tPrev;
m_tDelta = m_tDeltaMS / 1000.0f;
}
void FrameTimerV2::update() {
m_tPrev = m_tCurr;
m_tCurr = SDL_GetTicks64();
m_tDeltaMS = m_tCurr - m_tPrev;
m_tDelta = m_tDeltaMS / 1000.0f;
}
void FrameTimerV2::enforceFramerate(u32 target) {
r64 target_frametime = SDL_floor(
1000.0f/(r64)target
);
while(m_tDeltaMS < target_frametime) {
m_tCurr = SDL_GetTicks64();
m_tDeltaMS = m_tCurr - m_tPrev;
m_tDelta = m_tDeltaMS / 1000.0f;
// pass time
continue;
}
}
struct FrameTimer {
r64 tCurr;
r64 tPrev;
r64 tDeltaMS;
r64 tDelta;
};
FrameTimer frametimer() {
FrameTimer res = {};
res.tCurr = SDL_GetTicks64();
res.tPrev = res.tCurr;
res.tDeltaMS = res.tCurr - res.tPrev;
res.tDelta = res.tDeltaMS / 1000.0f;
return res;
}
void update_frame_timer(FrameTimer *ft) {
ft->tPrev = ft->tCurr;
ft->tCurr = SDL_GetTicks64();
ft->tDeltaMS = ft->tCurr - ft->tPrev;
ft->tDelta = ft->tDeltaMS / 1000.0f;
}
void enforce_frame_rate(FrameTimer *ft, u32 target) {
r64 target_frametime = SDL_floor(
1000.0f/(r64)target
);
while(ft->tDeltaMS < target_frametime) {
ft->tCurr = SDL_GetTicks64();
ft->tDeltaMS = ft->tCurr - ft->tPrev;
ft->tDelta = ft->tDeltaMS / 1000.0f;
// pass time
continue;
}
}
struct GameState {
// the default size the game is designed around
Vec2 world_size;
Vec2 screen_size;
// the scaling factor to increase/decrease size of game assets
Vec2 render_scale;
// player
Rect player;
Rect floor;
Rect wall;
};
u32 gl_shader_program(char* vs, char* fs)
{
int status;
char info_log[512];
// =============
// vertex shader
u32 vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vs, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &status);
if (status == 0)
{
glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
printf("== ERROR: Vertex Shader Compilation Failed ==\n");
printf("%s\n", info_log);
}
// ===============
// fragment shader
u32 fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fs, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &status);
if (status == 0)
{
glGetShaderInfoLog(fragment_shader, 512, NULL, info_log);
printf("== ERROR: Fragment Shader Compilation Failed ==\n");
printf("%s\n", info_log);
}
// ==============
// shader program
u32 shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
glGetProgramiv(shader_program, GL_LINK_STATUS, &status);
if(status == 0)
{
glGetProgramInfoLog(shader_program, 512, NULL, info_log);
printf("== ERROR: Shader Program Linking Failed\n");
printf("%s\n", info_log);
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return shader_program;
}
u32 gl_shader_program_from_path(const char* vspath, const char* fspath)
{
size_t read_count;
char* vs = (char*)SDL_LoadFile(vspath, &read_count);
if (read_count == 0)
{
printf("Error! Failed to read vertex shader file at path %s\n", vspath);
return 0;
}
char* fs = (char*)SDL_LoadFile(fspath, &read_count);
if (read_count == 0)
{
printf("Error! Failed to read fragment shader file at path %s\n", vspath);
return 0;
}
u32 shader_program = gl_shader_program(vs, fs);
return shader_program;
}
u32 gl_setup_colored_quad(u32 sp)
{
// @todo: make this use index buffer maybe?
r32 vertices[] = {
-1.0f, -1.0f, 0.0f, // bottom-left
1.0f, -1.0f, 0.0f, // bottom-right
1.0f, 1.0f, 0.0f, // top-right
1.0f, 1.0f, 0.0f, // top-right
-1.0f, 1.0f, 0.0f, // top-left
-1.0f, -1.0f, 0.0f, // bottom-left
};
u32 vao, vbo;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(r32), (void*)0);
glBindVertexArray(0);
// now return or store the vao, vbo state somewhere
return vao;
}
void gl_setup_colored_quad_optimized(
GLRenderer* renderer,
u32 sp
) {
// @todo: make this use index buffer maybe?
glGenVertexArrays(1, &renderer->cq_batch_vao);
glGenBuffers(1, &renderer->cq_batch_vbo);
glBindVertexArray(renderer->cq_batch_vao);
glBindBuffer(GL_ARRAY_BUFFER, renderer->cq_batch_vbo);
glBufferData(
GL_ARRAY_BUFFER, (
renderer->cq_pos_batch.capacity + renderer->cq_color_batch.capacity
) * sizeof(r32), NULL, GL_DYNAMIC_DRAW
);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(r32), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(
1, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(r32), (void*)(renderer->cq_pos_batch.capacity*sizeof(r32))
);
glBindVertexArray(0);
}
void gl_cq_flush(GLRenderer* renderer) {
glUseProgram(renderer->cq_batch_sp);
glBindBuffer(GL_ARRAY_BUFFER, renderer->cq_batch_vbo);
// fill batch data
// position batch
glBufferSubData(
GL_ARRAY_BUFFER,
0,
renderer->cq_pos_batch.size*sizeof(r32),
renderer->cq_pos_batch.buffer
);
// color batch
glBufferSubData(
GL_ARRAY_BUFFER,
renderer->cq_pos_batch.size*sizeof(r32),
renderer->cq_color_batch.size*sizeof(r32),
renderer->cq_color_batch.buffer
);
glBindVertexArray(renderer->cq_batch_vao);
glDrawArrays(GL_TRIANGLES, 0, renderer->cq_batch_count*6);
array_clear(&renderer->cq_pos_batch);
array_clear(&renderer->cq_color_batch);
renderer->cq_batch_count = 0;
}
void gl_draw_colored_quad_optimized(
GLRenderer* renderer,
Vec3 position,
Vec2 size,
Vec3 color
) {
Vec4 vertices[6] = {
init4v(-1.0f, -1.0f, 0.0f, 1.0f),// bottom-left
init4v( 1.0f, -1.0f, 0.0f, 1.0f),// bottom-right
init4v( 1.0f, 1.0f, 0.0f, 1.0f),// top-right
init4v( 1.0f, 1.0f, 0.0f, 1.0f),// top-right
init4v(-1.0f, 1.0f, 0.0f, 1.0f),// top-left
init4v(-1.0f, -1.0f, 0.0f, 1.0f) // bottom-left
};
// setting quad size
Mat4 model = init_value4m(1.0);
Mat4 scale = scaling_matrix4m(size.x, size.y, 0.0f);
model = multiply4m(scale, model);
// setting quad position
Mat4 translation = translation_matrix4m(position.x, position.y, position.z);
model = multiply4m(translation, model);
Mat4 mvp = calculate_mvp4m(model, renderer->cam_view, renderer->cam_proj);
Vec4 mvp_pos;
mvp_pos = multiply4mv(mvp, vertices[0]);
vertices[0] = mvp_pos;
mvp_pos = multiply4mv(mvp, vertices[1]);
vertices[1] = mvp_pos;
mvp_pos = multiply4mv(mvp, vertices[2]);
vertices[2] = mvp_pos;
mvp_pos = multiply4mv(mvp, vertices[3]);
vertices[3] = mvp_pos;
mvp_pos = multiply4mv(mvp, vertices[4]);
vertices[4] = mvp_pos;
mvp_pos = multiply4mv(mvp, vertices[5]);
vertices[5] = mvp_pos;
array_insert(&renderer->cq_pos_batch, vertices[0].data, 4);
array_insert(&renderer->cq_pos_batch, vertices[1].data, 4);
array_insert(&renderer->cq_pos_batch, vertices[2].data, 4);
array_insert(&renderer->cq_pos_batch, vertices[3].data, 4);
array_insert(&renderer->cq_pos_batch, vertices[4].data, 4);
array_insert(&renderer->cq_pos_batch, vertices[5].data, 4);
// initialise color to be per vertex to allow batching
// @todo: really need to optimise this
array_insert(&renderer->cq_color_batch, color.data, 3);
array_insert(&renderer->cq_color_batch, color.data, 3);
array_insert(&renderer->cq_color_batch, color.data, 3);
array_insert(&renderer->cq_color_batch, color.data, 3);
array_insert(&renderer->cq_color_batch, color.data, 3);
array_insert(&renderer->cq_color_batch, color.data, 3);
renderer->cq_batch_count++;
if(renderer->cq_batch_count == BATCH_SIZE) {
gl_cq_flush(renderer);
}
}
void gl_draw_colored_quad(
GLRenderer* renderer,
Vec3 position,
Vec2 size,
Vec3 color
) {
glEnable(GL_DEPTH_TEST);
glUseProgram(renderer->cq_sp);
if (renderer->cq_init == 0)
{
glUniformMatrix4fv(
glGetUniformLocation(renderer->cq_sp, "Projection"),
1, GL_FALSE, (renderer->cam_proj).buffer
);
renderer->cq_init = 1;
}
// setting quad size
Mat4 model = init_value4m(1.0);
Mat4 scale = scaling_matrix4m(size.x, size.y, 0.0f);
model = multiply4m(scale, model);
// setting quad position
Mat4 translation = translation_matrix4m(position.x, position.y, position.z);
model = multiply4m(translation, model);
// setting color
glUniform3fv(glGetUniformLocation(renderer->cq_sp, "Color"), 1, color.data);
glUniformMatrix4fv(
glGetUniformLocation(renderer->cq_sp, "Model"),
1, GL_FALSE, model.buffer
);
glUniformMatrix4fv(
glGetUniformLocation(renderer->cq_sp, "View"),
1, GL_FALSE, (renderer->cam_view).buffer
);
glBindVertexArray(renderer->cq_vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void gl_setup_text(TextState* state, FT_Face font_face)
{
FT_Set_Pixel_Sizes(font_face, state->pixel_size, state->pixel_size);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &(state->texture_atlas_id));
glBindTexture(GL_TEXTURE_2D_ARRAY, state->texture_atlas_id);
// generate texture
glTexImage3D(
GL_TEXTURE_2D_ARRAY,
0,
GL_R8,
state->pixel_size,
state->pixel_size,
128,
0,
GL_RED,
GL_UNSIGNED_BYTE,
0
);
// generate characters
for (u32 c = 0; c < 128; c++)
{
if (FT_Load_Char(font_face, c, FT_LOAD_RENDER))
{
printf("ERROR :: Freetype failed to load glyph: %c", c);
}
else
{
glTexSubImage3D(
GL_TEXTURE_2D_ARRAY,
0,
0, 0, // x, y offset
int(c),
font_face->glyph->bitmap.width,
font_face->glyph->bitmap.rows,
1,
GL_RED,
GL_UNSIGNED_BYTE,
font_face->glyph->bitmap.buffer
);
// set texture options
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
TextChar tc;
tc.size = Vec2{
(r32)font_face->glyph->bitmap.width,
(r32)font_face->glyph->bitmap.rows
};
tc.bearing = Vec2{
(r32)font_face->glyph->bitmap_left,
(r32)font_face->glyph->bitmap_top
};
tc.advance = font_face->glyph->advance.x;
state->char_map[c] = tc;
}
}
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
// @note: this data is used for GL_TRIANGLE_STRIP
// as such the order for vertices for this is AntiCW -> CW -> AntiCW
// that can be seen in this array as it goes from ACW -> CW
r32 vertices[] = {
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
glGenVertexArrays(1, &(state->vao));
glGenBuffers(1, &(state->vbo));
glBindVertexArray(state->vao);
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void gl_render_text(GLRenderer *renderer, char* text, Vec2 position, r32 size, Vec3 color)
{
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(renderer->ui_text.sp);
glUniformMatrix4fv(glGetUniformLocation(renderer->ui_text.sp, "View"),
1, GL_FALSE, renderer->cam_view.buffer);
glUniformMatrix4fv(glGetUniformLocation(renderer->ui_text.sp, "Projection"),
1, GL_FALSE, renderer->cam_proj.buffer);
glUniform3fv(glGetUniformLocation(renderer->ui_text.sp, "TextColor"), 1, color.data);
glBindVertexArray(renderer->ui_text.vao);
glBindTexture(GL_TEXTURE_2D_ARRAY, renderer->ui_text.texture_atlas_id);
glBindBuffer(GL_ARRAY_BUFFER, renderer->ui_text.vbo);
glActiveTexture(GL_TEXTURE0);
u32 running_index = 0;
r32 startx = position.x;
r32 starty = position.y;
r32 linex = startx;
r32 scale = size/renderer->ui_text.pixel_size;
memset(renderer->ui_text.transforms, 0, renderer->ui_text.chunk_size);
memset(renderer->ui_text.char_indexes, 0, renderer->ui_text.chunk_size);
char *char_iter = text;
while (*char_iter != '\0')
{
TextChar render_char = renderer->ui_text.char_map[*char_iter];
if (*char_iter == '\n')
{
linex = startx;
starty = starty - (render_char.size.y * 1.5 * scale);
}
else if (*char_iter == ' ')
{
linex += (render_char.advance >> 6) * scale;
}
else
{
r32 xpos = linex + (scale * render_char.bearing.x);
r32 ypos = starty - (renderer->ui_text.pixel_size - render_char.bearing.y) * scale;
r32 w = scale * renderer->ui_text.pixel_size;
r32 h = scale * renderer->ui_text.pixel_size;
Mat4 sm = scaling_matrix4m(w, h, 0);
Mat4 tm = translation_matrix4m(xpos, ypos, 0);
Mat4 model = multiply4m(tm, sm);
renderer->ui_text.transforms[running_index] = model;
renderer->ui_text.char_indexes[running_index] = int(*char_iter);
linex += (render_char.advance >> 6) * scale;
running_index++;
if (running_index > renderer->ui_text.chunk_size - 1)
{
r32 transform_loc = glGetUniformLocation(renderer->ui_text.sp, "LetterTransforms");
glUniformMatrix4fv(transform_loc, renderer->ui_text.chunk_size,
GL_FALSE, &(renderer->ui_text.transforms[0].buffer[0]));
r32 texture_map_loc = glGetUniformLocation(renderer->ui_text.sp, "TextureMap");
glUniform1iv(texture_map_loc, renderer->ui_text.chunk_size, renderer->ui_text.char_indexes);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, renderer->ui_text.chunk_size);
running_index = 0;
memset(renderer->ui_text.transforms, 0, renderer->ui_text.chunk_size);
memset(renderer->ui_text.char_indexes, 0, renderer->ui_text.chunk_size);
}
}
char_iter++;
}
if (running_index > 0)
{
u32 render_count = running_index < renderer->ui_text.chunk_size ? running_index : renderer->ui_text.chunk_size;
r32 transform_loc = glGetUniformLocation(renderer->ui_text.sp, "LetterTransforms");
glUniformMatrix4fv(transform_loc, render_count,
GL_FALSE, &(renderer->ui_text.transforms[0].buffer[0]));
r32 texture_map_loc = glGetUniformLocation(renderer->ui_text.sp, "TextureMap");
glUniform1iv(texture_map_loc, render_count, renderer->ui_text.char_indexes);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, render_count);
running_index = 0;
memset(renderer->ui_text.transforms, 0, render_count);
memset(renderer->ui_text.char_indexes, 0, render_count);
}
}
Rect rect(Vec3 position, Vec2 size) {
Rect r = {0};
r.position = position;
r.size = size;
r.tl.x = position.x - size.x;
r.tl.y = position.y + size.y;
Vec2 br;
r.br.x = position.x + size.x;
r.br.y = position.y - size.y;
return r;
}
Vec2 get_move_dir(Controller c) {
Vec2 dir = {};
if (c.move_up) {
dir.y = 1.0f;
}
if (c.move_down) {
dir.y = -1.0f;
}
if (c.move_left) {
dir.x = -1.0f;
}
if (c.move_right) {
dir.x = 1.0f;
}
return dir;
}
void update_camera(GLRenderer *renderer) {
if (renderer->cam_update == true) {
renderer->cam_view = camera_create4m(
renderer->cam_pos,
add3v(renderer->cam_pos, renderer->cam_look),
renderer->preset_up_dir
);
renderer->cam_update = false;
}
}
Vec3 get_world_position_from_percent(GameState state, Vec3 v) {
Vec3 world_pos = v;
world_pos.x = state.render_scale.x*state.world_size.x*v.x/100.0f;
world_pos.y = state.render_scale.y*state.world_size.y*v.y/100.0f;
return world_pos;
}
Vec2 get_screen_position_from_percent(GameState state, Vec2 v) {
Vec2 screen_pos = v;
screen_pos.x = state.render_scale.x*state.screen_size.x*v.x/100.0f;
screen_pos.y = state.render_scale.y*state.screen_size.y*v.y/100.0f;
return screen_pos;
}
Vec3 get_screen_position_from_percent(GameState state, Vec3 v) {
Vec3 screen_pos = v;
screen_pos.x = state.render_scale.x*state.screen_size.x*v.x/100.0f;
screen_pos.y = state.render_scale.y*state.screen_size.y*v.y/100.0f;
return screen_pos;
}
int main(int argc, char* argv[])
{
u32 scr_width = 1024;
u32 scr_height = 768;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
printf("Error initialising SDL2: %s\n", SDL_GetError());
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window* window = SDL_CreateWindow("simple platformer",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
scr_width, scr_height,
SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context)
{
printf("ERROR :: OpenGL context creation failed: %s\n", SDL_GetError());
return -1;
}
// load glad
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
printf("ERROR :: Failed to initialize Glad\n");
return -1;
}
// vsync controls: 0 = OFF | 1 = ON (Default)
SDL_GL_SetSwapInterval(0);
GLRenderer *renderer = new GLRenderer();
// @resume: I am working on creating an efficient quad renderer via
// instancing, in order to achieve that I have absolutely butchered the
// performance of my program by making it render 200,000 quads
// the goal is to use this to see when performance plummets
//
// so far the progress is that, I have somewhat implemented a jank system
// I need to write shaders, it compiles and runs but it is useless
// @todo: fix the renderer.
// @note: batch rendering
// 3 columns, 6 rows
u32 pos_ele_count = BATCH_SIZE * 4*6;
u32 color_ele_count = BATCH_SIZE * 3*6;
u32 mem_size = pos_ele_count + color_ele_count;
void* batch_memory = calloc(mem_size, sizeof(r32));
Arena batch_arena = {0};
arena_init(&batch_arena, (unsigned char*)batch_memory, mem_size*sizeof(r32));
array_init(&batch_arena, &(renderer->cq_pos_batch), pos_ele_count);
array_init(&batch_arena, &(renderer->cq_color_batch), color_ele_count);
u32 quad_sp = gl_shader_program_from_path(
"./source/shaders/colored_quad.vs.glsl",
"./source/shaders/colored_quad.fs.glsl"
);
u32 ui_text_sp = gl_shader_program_from_path(
"./source/shaders/ui_text.vs.glsl",
"./source/shaders/ui_text.fs.glsl"
);
u32 cq_batch_sp = gl_shader_program_from_path(
"./source/shaders/cq_batched.vs.glsl",
"./source/shaders/cq_batched.fs.glsl"
);
u32 quad_vao = gl_setup_colored_quad(quad_sp);
renderer->cq_sp = quad_sp;
renderer->cq_vao = quad_vao;
renderer->cq_batch_sp = cq_batch_sp;
gl_setup_colored_quad_optimized(renderer, cq_batch_sp);
r32 render_scale = 8.0f;
// ==========
// setup text
// 1. setup free type library stuff
FT_Library ft_lib;
FT_Face roboto_font_face;
if (FT_Init_FreeType(&ft_lib))
{
printf("ERROR :: Could not init freetype library\n");
return -1;
}
FT_Error error = FT_New_Face(
ft_lib,
"assets/fonts/Roboto.ttf",
0, &roboto_font_face
);
if (error == FT_Err_Unknown_File_Format)
{
printf("ERROR :: Font Loading Failed. The font format is unsupported\n");
return -1;
}
else if (error)
{
printf("ERROR :: Font Loading Failed. Unknown error code: %d\n", error);
return -1;
}
// 2. setup gl text
// @note: we only support 128 characters, which is the basic ascii set
renderer->ui_text.chunk_size = 32;
renderer->ui_text.pixel_size = 32*render_scale;
renderer->ui_text.sp = ui_text_sp;
renderer->ui_text.transforms = (Mat4*)malloc(
renderer->ui_text.chunk_size*sizeof(Mat4)
);
renderer->ui_text.char_indexes = (s32*)malloc(
renderer->ui_text.chunk_size*sizeof(s32)
);
renderer->ui_text.char_map = (TextChar*)malloc(
128*sizeof(TextChar)
);
gl_setup_text(&(renderer->ui_text), roboto_font_face);
// ============
// setup camera
Vec3 preset_up_dir = Vec3{0.0f, 1.0f, 0.0f};
renderer->preset_up_dir = preset_up_dir;
renderer->cam_update = false;
renderer->cam_pos = Vec3{0.0f, 0.0f, 1.0f};
renderer->cam_look = camera_look_around(TO_RAD(0.0f), -TO_RAD(90.0f));
renderer->cam_view = camera_create4m(
renderer->cam_pos,
add3v(renderer->cam_pos, renderer->cam_look), renderer->preset_up_dir
);
renderer->cam_proj = orthographic4m(
0.0f, (r32)scr_width*render_scale,
0.0f, (r32)scr_height*render_scale,
0.1f, 10.0f
);
// @section: gameplay variables
r32 motion_scale = 1.5f;
r32 fall_accelx = 3.0f*motion_scale;
r32 move_accelx = 6.0f*motion_scale;
r32 freefall_accel = -11.8f*motion_scale;
r32 jump_force = 6.5f*motion_scale;
r32 effective_force = 0.0f;
Vec2 player_velocity = Vec2{0.0f, 0.0f};
Vec2 p_move_dir = Vec2{0.0f, 0.0f};
// direction in which player is effectively travelling
Vec2 p_motion_dir = Vec2{0.0f, 0.0f};
GameState state = {0};
state.world_size = v2(scr_width, scr_height);
state.screen_size = v2(scr_width, scr_height);
state.render_scale = v2(render_scale);
Vec3 player_position = Vec3{0.0f, 70.0f, -1.0f};
Vec2 player_size = Vec2{40.0f, 40.0f};
state.player = rect(player_position, player_size);
// @thinking: level object handling
// there should be a most smallest supported unit
// smallest_size: 16x16
// object placement should be in pixels
// in order to scale to different resolutions it should be multiplied by
// scaling factor
Vec2 atom_size = {16.0f, 16.0f};
Vec3 floor_position = Vec3{640.0f*render_scale, 400.0f*render_scale, -2.0f};
Vec2 floor_size = atom_size*Vec2{40.0f, 1.5f};
state.floor = rect(floor_position, floor_size);
Vec3 wall_position = get_world_position_from_percent(
state, Vec3{20.0f, 10.0f, -2.0f}
);
Vec2 wall_size = atom_size*Vec2{1.5f, 8.0f};
state.wall = rect(wall_position, wall_size);
// gameplay camera movement stuff
Vec2 cam_lt_limit = {0};
Vec2 cam_rb_limit = {0};
cam_lt_limit = get_screen_position_from_percent(
state, Vec2{20.0f, 80.0f}
);
cam_rb_limit = get_screen_position_from_percent(
state, Vec2{80.0f, 20.0f}
);
Controller controller = {0};
r32 key_down_time[5] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
b8 is_key_down_x = false;
// gravity calculations
b8 collidex = 0;
b8 collidey = 0;
b8 is_gravity = 0;
b8 game_running = 1;
FrameTimer timer = frametimer();
while (game_running)
{
update_frame_timer(&timer);
//enforce_frame_rate(&timer, 60);
controller.jump = 0;
controller.toggle_gravity = 0;
SDL_Event ev;
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
case (SDL_QUIT):
{
game_running = 0;
} break;
case (SDL_KEYDOWN):
{
if (ev.key.keysym.sym == SDLK_w)
{
controller.move_up = 1;
}
if (ev.key.keysym.sym == SDLK_a)
{
controller.move_left = 1;
key_down_time[PK_A] = timer.tCurr;
}
if (ev.key.keysym.sym == SDLK_s)
{
controller.move_down = 1;
}
if (ev.key.keysym.sym == SDLK_d)
{
controller.move_right = 1;
key_down_time[PK_D] = timer.tCurr;
}
if (ev.key.keysym.sym == SDLK_SPACE)
{
controller.jump = 1;
}
if (ev.key.keysym.sym == SDLK_g)
{
controller.toggle_gravity = 1;
}
} break;
case (SDL_KEYUP):
{
if (ev.key.keysym.sym == SDLK_w)
{
controller.move_up = 0;
}
if (ev.key.keysym.sym == SDLK_a)
{
controller.move_left = 0;
key_down_time[PK_A] = 0.0f;
}
if (ev.key.keysym.sym == SDLK_s)
{
controller.move_down = 0;
}
if (ev.key.keysym.sym == SDLK_d)
{
controller.move_right = 0;
key_down_time[PK_D] = 0.0f;
}
} break;
default:
{
break;
}
}
}
// @section: input processing
if (controller.toggle_gravity)
{
is_gravity = !is_gravity;
player_velocity = Vec2{0.0f, 0.0f};
p_move_dir.x = 0.0f;
effective_force = 0.0f;
p_motion_dir = {0};
}
if (controller.move_up)
{
p_move_dir.y = 1.0f;
}
if (controller.move_down)
{
p_move_dir.y = -1.0f;
}
PlatformKey horizontal_move = PK_NIL;
is_key_down_x = false;
if (
key_down_time[PK_A] != 0.0f ||
key_down_time[PK_D] != 0.0f
) {
horizontal_move = (
key_down_time[PK_A] > key_down_time[PK_D] ? PK_A : PK_D
);
}
if (horizontal_move == PK_A && controller.move_left)
{
p_move_dir.x = -1.0f;
is_key_down_x = true;
}
if (horizontal_move == PK_D && controller.move_right)
{
p_move_dir.x = 1.0f;
is_key_down_x = true;
}
// @section: gravity
Vec2 pd_1 = Vec2{0.0f, 0.0f};
p_motion_dir = {0};
r32 accel_computed = 0.0f;
if (collidey)
{
player_velocity.y = 0.0f;
}
if (collidex)
{
player_velocity.x = 0.0f;
}
if (is_gravity)
{
// calculate force acting on player
if (collidey) {
// @note: can I reduce the states here like I did in the falling case
// without separate checks
if (is_key_down_x) {
r32 updated_force = (
effective_force + p_move_dir.x*move_accelx*timer.tDelta
);
updated_force = clampf(
updated_force, -move_accelx, move_accelx
);
effective_force = updated_force;
} else {
r32 friction = 0.0f;
if (effective_force > 0.0f) {
friction = -move_accelx*timer.tDelta;
} else if (effective_force < 0.0f) {
friction = move_accelx*timer.tDelta;
}
r32 updated_force = effective_force + friction;
effective_force = (
ABS(updated_force) < 0.5f ?
0.0f : updated_force
);
}
} else {
r32 smoothing_force = effective_force;
r32 net_force = 0.0f;
r32 active_force = 0.0f;
if (!collidex) {
net_force += effective_force;
{
// @note: air resistance
// (arbitrary force in opposite direction to reduce speed)
// reason: seems that it would work well for the case where
// player moves from platform move to free_fall
// since the max speed in respective stages is different this can
// function as a speed smoother, without too many checks and
// explicit checking
b8 is_force_pos = effective_force > 0.0f;
b8 is_force_neg = effective_force < 0.0f;
r32 friction = 0.0f;
if (is_force_pos) {
friction = -fall_accelx*timer.tDelta;
} else if (is_force_neg) {
friction = fall_accelx*timer.tDelta;
}
net_force += friction;
}
{
// @note: player movement force
active_force = p_move_dir.x*fall_accelx;
r32 interm_total_force = net_force + active_force;
if (ABS(interm_total_force) > fall_accelx) {
r32 deficit = MIN(fall_accelx - ABS(net_force), 0.0f);
active_force = p_move_dir.x*deficit;
}
net_force += active_force;
}
}
effective_force = net_force;
}
{
// horizontal motion setting
r32 dx1 = effective_force;
if ( dx1 == 0.0f ) {
p_move_dir.x = 0.0f;
}
if (dx1 < 0.0f) {
p_motion_dir.x = -1.0f;
} else if (dx1 > 0.0f) {
p_motion_dir.x = 1.0f;
}
accel_computed = (dx1 - player_velocity.x)/timer.tDelta;
player_velocity.x = dx1;
pd_1.x = dx1;
}
{
// vertical motion when falling
r32 dy1 = player_velocity.y;
dy1 = dy1 + freefall_accel*timer.tDelta;
if (controller.jump) {
dy1 = jump_force;
}
if (dy1 < 0.0f) {
p_motion_dir.y = -1.0f;
} else if (dy1 > 0.0f) {
p_motion_dir.y = 1.0f;
}
player_velocity.y = dy1;
pd_1.y = dy1;
}
}
else
{
Vec2 dir = get_move_dir(controller);
pd_1 = dir * 1.0f;
if (pd_1.x < 0.0f) {
p_motion_dir.x = -1.0f;
} else if (pd_1.x > 0.0f) {
p_motion_dir.x = 1.0f;
}
if (pd_1.y < 0.0f) {
p_motion_dir.y = -1.0f;
} else if (pd_1.y > 0.0f) {
p_motion_dir.y = 1.0f;
}
}
// @section: collision
Vec3 next_player_position;
next_player_position.x = state.player.position.x + pd_1.x;
next_player_position.y = state.player.position.y + pd_1.y;
Rect player_next = rect(next_player_position, state.player.size);
Rect collision_targets[2] = {state.wall, state.floor};
b8 is_collide_x = 0;
b8 is_collide_y = 0;
for (u32 i = 0; i < 2; i++) {
Rect target = collision_targets[i];
// @func: check_if_player_colliding_with_target
b8 t_collide_x = 0;
// need to adjust player position in case of vertical collisions
// so need to check which player side collides
b8 t_collide_bottom = 0;
b8 t_collide_top = 0;
r32 prev_top = state.player.tl.y;
r32 prev_left = state.player.tl.x;
r32 prev_bottom = state.player.br.y;
r32 prev_right = state.player.br.x;
r32 p_top = player_next.tl.y;
r32 p_left = player_next.tl.x;
r32 p_bottom = player_next.br.y;
r32 p_right = player_next.br.x;
r32 t_left = target.tl.x;
r32 t_top = target.tl.y;
r32 t_right = target.br.x;
r32 t_bottom = target.br.y;
b8 prev_collide_x = !(prev_left > t_right || prev_right < t_left);
b8 new_collide_yb = (p_bottom < t_top && p_top > t_top);
b8 new_collide_yt = (p_top > t_bottom && p_bottom < t_bottom);
if (prev_collide_x && new_collide_yb) {
t_collide_top = 1;
}
if (prev_collide_x && new_collide_yt) {
t_collide_bottom = 1;
}
b8 prev_collide_y = !(prev_top < t_bottom || prev_bottom > t_top);
b8 new_collide_x = !(p_right < t_left || p_left > t_right);
if (prev_collide_y && new_collide_x) {
t_collide_x = 1;
}
// @func: update_player_positions_if_sides_colliding
if (t_collide_top) {
state.player.position.y -= (prev_bottom - t_top - 0.1f);
} else if (t_collide_bottom) {
state.player.position.y += (t_bottom - prev_top - 0.1f);
}
is_collide_x = is_collide_x || t_collide_x;
is_collide_y = is_collide_y || t_collide_top || t_collide_bottom;
}
if (!is_collide_x) {
if (p_motion_dir.x != 0.0f) {
renderer->cam_update = true;
}
state.player.position.x = next_player_position.x;
}
if (!is_collide_y) {
if (p_motion_dir.y != 0.0f) {
renderer->cam_update = true;
}
state.player.position.y = next_player_position.y;
}
state.player = rect(state.player.position, state.player.size);
collidex = is_collide_x;
collidey = is_collide_y;
// @func: update_camera
if (renderer->cam_update == true) {
renderer->cam_update = false;
Vec2 player_screen = state.player.position.v2() - renderer->cam_pos.v2();
if (player_screen.x <= cam_lt_limit.x && p_motion_dir.x == -1) {
renderer->cam_pos.x += pd_1.x;
renderer->cam_update = true;
}
if (player_screen.y >= cam_lt_limit.y && p_motion_dir.y == 1) {
renderer->cam_pos.y += pd_1.y;
renderer->cam_update = true;
}
if (player_screen.x >= cam_rb_limit.x && p_motion_dir.x == 1) {
renderer->cam_pos.x += pd_1.x;
renderer->cam_update = true;
}
if (player_screen.y <= cam_rb_limit.y && p_motion_dir.y == -1) {
renderer->cam_pos.y += pd_1.y;
renderer->cam_update = true;
}
if (renderer->cam_update == true) {
renderer->cam_view = camera_create4m(
renderer->cam_pos,
add3v(renderer->cam_pos, renderer->cam_look),
renderer->preset_up_dir
);
renderer->cam_update = false;
}
}
// output
glClearColor(0.8f, 0.5f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// player
gl_draw_colored_quad(renderer,
state.player.position, // position
state.player.size, // size
Vec3{0.45f, 0.8f, 0.2f});
// floor
gl_draw_colored_quad(renderer,
state.floor.position,
state.floor.size,
Vec3{1.0f, 1.0f, 1.0f});
// wall
gl_draw_colored_quad(renderer,
state.wall.position,
state.wall.size,
Vec3{1.0f, 0.0f, 0.0f});
for (int i=0;i<4000;i++) {
u32 max_row_ele = 100;
Vec2 based_size = div2vf(state.render_scale*state.screen_size, max_row_ele);
Vec3 pos_i = Vec3{
(atom_size.x+based_size.x)*(r32)(i%max_row_ele),
(atom_size.y+based_size.y)*(r32)(i/max_row_ele),
-5.0f
};
r32 color_factor = (r32)(1000-i)/1000.0f;
gl_draw_colored_quad_optimized(
renderer,
pos_i,
atom_size,
Vec3{color_factor, color_factor, color_factor}
);
}
gl_cq_flush(renderer);
array_clear(&renderer->cq_pos_batch);
array_clear(&renderer->cq_color_batch);
renderer->cq_batch_count = 0;
// render ui text
if (is_collide_x || is_collide_y)
{
gl_render_text(renderer,
"is colliding",
Vec2{500.0f, 700.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
char movedir_output[50];
sprintf(movedir_output, "move_dir = %f", p_move_dir.x);
gl_render_text(renderer,
movedir_output,
Vec2{500.0f, 60.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
char speed_output[50];
sprintf(speed_output, "%f pps", player_velocity.x);
gl_render_text(renderer,
speed_output,
Vec2{500.0f, 100.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
}
char accel_output[50];
sprintf(accel_output, "effective_force %f", effective_force);
gl_render_text(renderer,
accel_output,
Vec2{500.0f, 150.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
char move_state_output[50];
char fmt_buffer[50];
sprintf(fmt_buffer, "player moving? %d", is_key_down_x);
gl_render_text(renderer,
fmt_buffer,
Vec2{900.0f, 40.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
sprintf(fmt_buffer, "frametime: %f", timer.tDelta);
gl_render_text(renderer,
fmt_buffer,
Vec2{900.0f, 90.0f}, // position
280.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
sprintf(fmt_buffer, "%f pixels", pd_1.x);
gl_render_text(renderer,
fmt_buffer,
Vec2{500.0f, 200.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
sprintf(fmt_buffer, "collide: x(%d),y(%d)", collidex, collidey);
gl_render_text(renderer,
fmt_buffer,
Vec2{500.0f, 1000.0f}, // position
28.0f, // size
Vec3{0.0f, 0.0f, 0.0f}); // color
SDL_GL_SwapWindow(window);
}
arena_clear(&batch_arena);
free(renderer->ui_text.transforms);
free(renderer->ui_text.char_indexes);
free(renderer->ui_text.char_map);
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
|