-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_gfx.h
1925 lines (1665 loc) · 46.5 KB
/
d_gfx.h
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
// TODO: resize
// TODO: QOI image
#ifndef D_GFX_H
#define D_GFX_H
#ifndef D_MATH_H
#include "d_math.h"
#endif
typedef struct d_gfx_desc {
int width;
int height;
bool no_depth_test;
bool no_backface_cull;
bool anti_alias;
d_color clear_color;
} d_gfx_desc;
typedef enum {
D_ORIG_CENTER,
D_ORIG_TOP_LEFT,
D_ORIG_TOP,
D_ORIG_TOP_RIGHT,
D_ORIG_LEFT,
D_ORIG_RIGHT,
D_ORIG_BOT_LEFT,
D_ORIG_BOT,
D_ORIG_BOT_RIGHT,
} d_origin;
typedef enum {
D_BLEND_ALPHA,
D_BLEND_REPLACE,
D_BLEND_ADD,
} d_blend;
typedef enum {
D_WRAP_BORDER,
D_WRAP_EDGE,
D_WRAP_REPEAT,
} d_wrap;
typedef struct {
int width;
int height;
d_color* pixels;
} d_img;
typedef struct {
int width;
int height;
int* buf;
} d_ibuf;
typedef struct {
uint8_t* buf;
int size;
} d_bitset;
// TODO: bitset might be slower in runtime
typedef struct {
int width;
int height;
d_bitset bitset;
} d_bbuf;
d_bitset d_bitset_new(size_t size);
void d_bitset_set(d_bitset* m, int n, bool b);
bool d_bitset_get(d_bitset* m, int n);
void d_bitset_clear(d_bitset* m, bool b);
void d_bitset_free(d_bitset* m);
typedef struct {
int gw;
int gh;
int charmap[128];
uint8_t* bitmap;
} d_font;
typedef struct {
d_vec3 pos;
d_vec2 uv;
d_vec3 normal;
d_color color;
} d_vertex;
typedef uint32_t d_index;
typedef struct {
d_vertex* verts;
int num_verts;
d_index* indices;
int num_indices;
} d_mesh;
typedef struct {
d_vec3 pos;
d_quat rot;
d_vec3 scale;
} d_transform;
typedef struct {
d_mat4 transform;
float time;
} d_model_anim_frame;
typedef struct {
char* name;
d_model_anim_frame* frames;
int num_frames;
} d_model_anim;
typedef struct d_model_node {
d_mat4 t;
struct d_model_node* children;
int num_children;
d_mesh* meshes;
int num_meshes;
d_model_anim* anims;
int num_anims;
} d_model_node;
typedef struct {
d_model_node* nodes;
int num_nodes;
d_img* images;
int num_images;
d_box bbox;
d_vec3 center;
} d_model;
typedef struct {
d_vec2 pos;
d_vec2 scale;
d_vec2 origin;
float rot;
} d_t2;
typedef d_color(*d_shader)(d_color);
void d_gfx_init(d_gfx_desc);
#ifdef D_APP_H
void d_gfx_present(void);
d_vec2 d_gfx_mouse_pos(void);
d_vec2 d_gfx_mouse_dpos(void);
#endif
int d_gfx_width(void);
int d_gfx_height(void);
d_img d_img_new(int w, int h);
d_img d_img_parse(uint8_t* bytes, size_t size);
#ifdef D_FS_H
d_img d_img_load(char* path);
#endif
void d_img_set(d_img* img, int x, int y, d_color c);
d_color d_img_get(d_img* img, int x, int y);
void d_img_fill(d_img* img, d_color c);
void d_img_save_png(d_img* img, char* path);
d_img d_img_clone(d_img* img);
void d_img_free(d_img* img);
d_ibuf d_ibuf_new(int w, int h);
void d_ibuf_free(d_ibuf*);
void d_ibuf_set(d_ibuf* ibuf, int x, int y, int v);
int d_ibuf_get(d_ibuf* ibuf, int x, int y);
void d_ibuf_clear(d_ibuf* ibuf, int v);
d_bbuf d_bbuf_new(int w, int h);
void d_bbuf_free(d_bbuf*);
void d_bbuf_set(d_bbuf* bbuf, int x, int y, bool v);
bool d_bbuf_get(d_bbuf* bbuf, int x, int y);
d_mesh d_mesh_new(
d_vertex* verts,
int num_verts,
d_index* indices,
int num_indices
);
void d_mesh_free(d_mesh* mesh);
void d_mesh_gen_normals(d_mesh* mesh);
d_font d_font_parse(uint8_t* bytes);
#ifdef D_FS_H
d_font d_font_load(char* path);
#endif
void d_font_free(d_font* font);
d_model d_model_parse(uint8_t* bytes, int size);
#ifdef D_FS_H
d_model d_model_load(char* path);
#endif
void d_model_gen_bbox(d_model* model);
void d_model_free(d_model* model);
void d_gfx_clear(void);
void d_gfx_set_blend(d_blend b);
void d_gfx_set_wrap(d_wrap w);
void d_gfx_draw_pixel(int x, int y, int z, d_color c);
void d_gfx_blit_pixel(int x, int y, d_color c);
d_color d_gfx_get(int x, int y);
void d_blit_img(d_img* img, d_vec2 pos);
void d_blit_text(char* text, d_vec2 pos, d_color c, bool bold, bool italic);
void d_blit_bg(void);
void d_blit_rect(d_vec2 p1, d_vec2 p2, d_color c);
void d_blit_circle(d_vec2 center, float r, d_color c);
void d_blit_line(d_vec2 p1, d_vec2 p2, d_color c);
void d_blit_tri(d_vec2 p1, d_vec2 p2, d_vec2 p3, d_color c);
void d_blit_poly(d_poly p, d_color c);
void d_draw_prim_tri(d_vertex v1, d_vertex v2, d_vertex v3, d_img* tex);
void d_draw_prim_quad(d_vertex v1, d_vertex v2, d_vertex v3, d_vertex v4, d_img* tex);
void d_draw_img(d_img* img);
void d_draw_tri(d_vec2 p1, d_vec2 p2, d_vec2 p3, d_color c);
void d_draw_rect(d_vec2 p1, d_vec2 p2, d_color c);
void d_draw_line(d_vec2 p1, d_vec2 p2, d_color c);
void d_draw_line3(d_vec3 p1, d_vec3 p2, d_color c);
void d_draw_poly(d_poly p, d_color c);
void d_draw_poly_outline(d_poly p, d_color c);
void d_draw_mesh(d_mesh* mesh, d_img* tex);
void d_draw_model(d_model* model);
void d_draw_bbox(d_box bbox, d_color c);
void d_transform_push(void);
void d_transform_pop(void);
void d_transform_use(d_mat4 m);
void d_transform_pos(d_vec2 p);
void d_transform_pos3(d_vec3 p);
void d_transform_scale(d_vec2 s);
void d_transform_scale3(d_vec3 s);
void d_transform_rot(float a);
void d_transform_rot_x(float a);
void d_transform_rot_y(float a);
void d_transform_rot_z(float a);
d_vec2 d_transform_apply_vec2(d_vec2 p);
d_vec3 d_transform_apply_vec3(d_vec3 p);
void d_transform_set(d_mat4 m);
d_mat4 d_transform_get(void);
void d_gfx_drawon(d_img* img);
d_img* d_gfx_canvas(void);
void d_gfx_set_shader(d_shader func);
void d_gfx_set_backface_cull(bool b);
void d_gfx_set_depth_test(bool b);
void d_gfx_set_bbuf_write(bool b);
void d_gfx_set_bbuf_test(bool b);
bool d_gfx_bbuf_get(int x, int y);
void d_gfx_bbuf_clear(void);
void d_t2_apply(d_t2);
d_mat4 d_t2_get_mat4(d_t2 t);
#endif // #ifndef D_GFX_H
#ifdef D_IMPL
#define D_GFX_IMPL
#endif
#ifdef D_GFX_IMPL
#ifndef D_GFX_IMPL_ONCE
#define D_GFX_IMPL_ONCE
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define D_MAX_TSTACK 64
static uint8_t proggy_bytes[] = {
0x64, 0x31, 0x52, 0x74, 0x79, 0x66, 0x30, 0x4e, 0x74, 0x07, 0x0d, 0x5f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43,
0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b,
0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x10, 0x20, 0x40, 0x81, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x42,
0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28,
0x53, 0xf2, 0x85, 0x3f, 0x28, 0x50, 0x00, 0x00, 0x00, 0x00, 0x04, 0x1e,
0x50, 0xa0, 0xe0, 0xa1, 0x4f, 0x04, 0x00, 0x00, 0x00, 0x02, 0x2a, 0x95,
0x14, 0x0a, 0x2a, 0x55, 0x10, 0x00, 0x00, 0x00, 0x00, 0x30, 0x91, 0x21,
0x94, 0xa8, 0x91, 0x1d, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x82, 0x04, 0x08,
0x10, 0x20, 0x20, 0x40, 0x40, 0x00, 0x08, 0x08, 0x10, 0x10, 0x20, 0x40,
0x81, 0x04, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x04, 0x2a, 0x38, 0xa8,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x0f, 0x84, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04,
0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x00,
0x00, 0x00, 0x00, 0x40, 0x82, 0x04, 0x10, 0x20, 0x81, 0x04, 0x08, 0x00,
0x00, 0x00, 0x00, 0xe2, 0x24, 0x4a, 0x95, 0x22, 0x44, 0x70, 0x00, 0x00,
0x00, 0x00, 0x08, 0x30, 0xa0, 0x40, 0x81, 0x02, 0x1f, 0x00, 0x00, 0x00,
0x00, 0x03, 0x88, 0x81, 0x04, 0x10, 0x41, 0x03, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x71, 0x10, 0x21, 0x80, 0x81, 0x22, 0x38, 0x00, 0x00, 0x00, 0x00,
0x01, 0x06, 0x14, 0x49, 0x13, 0xf0, 0x40, 0x80, 0x00, 0x00, 0x00, 0x03,
0xe4, 0x08, 0x1e, 0x02, 0x04, 0x88, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x18,
0x41, 0x03, 0xc4, 0x48, 0x91, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x81,
0x04, 0x08, 0x20, 0x41, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x24,
0x47, 0x11, 0x22, 0x44, 0x70, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x44, 0x89,
0x11, 0xe0, 0x41, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x08,
0x00, 0x00, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
0x00, 0x10, 0x20, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x63, 0x01,
0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x7e,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0xc0, 0x63, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x11, 0x02, 0x08, 0x20, 0x40, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x29, 0xb5, 0x6a, 0xce, 0x40, 0x78,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x48, 0x91, 0xe4, 0x28, 0x50, 0x80,
0x00, 0x00, 0x00, 0x07, 0x88, 0x91, 0x3e, 0x42, 0x85, 0x0b, 0xe0, 0x00,
0x00, 0x00, 0x00, 0x79, 0x02, 0x04, 0x08, 0x10, 0x20, 0x3c, 0x00, 0x00,
0x00, 0x00, 0x1e, 0x22, 0x42, 0x85, 0x0a, 0x14, 0x4f, 0x00, 0x00, 0x00,
0x00, 0x03, 0xe4, 0x08, 0x1e, 0x20, 0x40, 0x81, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x7c, 0x81, 0x03, 0xc4, 0x08, 0x10, 0x20, 0x00, 0x00, 0x00, 0x00,
0x03, 0x88, 0xa0, 0x40, 0x9d, 0x09, 0x11, 0xc0, 0x00, 0x00, 0x00, 0x01,
0x0a, 0x14, 0x2f, 0xd0, 0xa1, 0x42, 0x84, 0x00, 0x00, 0x00, 0x00, 0x1c,
0x10, 0x20, 0x40, 0x81, 0x02, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x03, 0x81,
0x02, 0x04, 0x08, 0x10, 0x23, 0x80, 0x00, 0x00, 0x00, 0x00, 0x85, 0x12,
0x45, 0x0e, 0x12, 0x22, 0x42, 0x00, 0x00, 0x00, 0x00, 0x10, 0x20, 0x40,
0x81, 0x02, 0x04, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x06, 0x3c, 0x75, 0x6a,
0xc9, 0x93, 0x06, 0x08, 0x00, 0x00, 0x00, 0x00, 0x62, 0xc5, 0x4a, 0x94,
0xa9, 0x51, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x03, 0x09, 0x21, 0x42, 0x85,
0x09, 0x21, 0x80, 0x00, 0x00, 0x00, 0x01, 0xe2, 0x24, 0x48, 0x9e, 0x20,
0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x24, 0x85, 0x0a, 0x14, 0x24,
0x86, 0x81, 0x00, 0x00, 0x00, 0x07, 0x88, 0x91, 0x22, 0x78, 0x91, 0x12,
0x10, 0x00, 0x00, 0x00, 0x00, 0x79, 0x0a, 0x03, 0x01, 0x80, 0xa1, 0x3c,
0x00, 0x00, 0x00, 0x00, 0x3f, 0x88, 0x10, 0x20, 0x40, 0x81, 0x02, 0x00,
0x00, 0x00, 0x00, 0x02, 0x14, 0x28, 0x50, 0xa1, 0x42, 0x84, 0xf0, 0x00,
0x00, 0x00, 0x00, 0x83, 0x05, 0x12, 0x22, 0x85, 0x04, 0x08, 0x00, 0x00,
0x00, 0x00, 0x10, 0x64, 0xc9, 0xab, 0x55, 0xb2, 0x24, 0x40, 0x00, 0x00,
0x00, 0x01, 0x0a, 0x12, 0x43, 0x06, 0x12, 0x42, 0x84, 0x00, 0x00, 0x00,
0x00, 0x41, 0x82, 0x88, 0xa0, 0x81, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00,
0x07, 0xe0, 0x41, 0x04, 0x10, 0x41, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x38,
0x40, 0x81, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0xe0, 0x00, 0x08, 0x10,
0x10, 0x20, 0x20, 0x40, 0x40, 0x80, 0x81, 0x00, 0x00, 0x00, 0xe0, 0x40,
0x81, 0x02, 0x04, 0x08, 0x10, 0x20, 0x43, 0x80, 0x00, 0x08, 0x10, 0x50,
0xa2, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x11,
0xe4, 0x48, 0x8f, 0x00, 0x00, 0x00, 0x02, 0x04, 0x08, 0x1e, 0x22, 0x44,
0x89, 0x13, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc4, 0x48, 0x10,
0x22, 0x38, 0x00, 0x00, 0x00, 0x00, 0x81, 0x02, 0x3c, 0x89, 0x12, 0x24,
0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x11, 0x3e, 0x40, 0x88,
0xe0, 0x00, 0x00, 0x00, 0x0e, 0x20, 0x41, 0xe1, 0x02, 0x04, 0x08, 0x10,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x44, 0x89, 0x12, 0x23, 0xc0,
0x81, 0x1c, 0x00, 0x81, 0x02, 0x07, 0x88, 0x91, 0x22, 0x44, 0x88, 0x00,
0x00, 0x00, 0x04, 0x00, 0x00, 0x60, 0x40, 0x81, 0x02, 0x04, 0x00, 0x00,
0x00, 0x00, 0x40, 0x00, 0x06, 0x04, 0x08, 0x10, 0x20, 0x40, 0x8e, 0x00,
0x00, 0x40, 0x81, 0x02, 0x24, 0x8a, 0x1c, 0x24, 0x44, 0x00, 0x00, 0x00,
0x06, 0x04, 0x08, 0x10, 0x20, 0x40, 0x81, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1d, 0xa4, 0xc9, 0x93, 0x26, 0x48, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xe2, 0x24, 0x48, 0x91, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1c, 0x44, 0x89, 0x12, 0x23, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x88, 0x91, 0x22, 0x44, 0xf1, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00,
0x79, 0x12, 0x24, 0x48, 0x8f, 0x02, 0x04, 0x08, 0x00, 0x00, 0x00, 0x16,
0x32, 0x40, 0x81, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe4,
0x06, 0x02, 0x02, 0x78, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x3c, 0x40,
0x81, 0x02, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x91, 0x22,
0x44, 0x88, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x12, 0x22, 0x85,
0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x93, 0x26, 0xab,
0x64, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x45, 0x04, 0x08, 0x28,
0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x12, 0x24, 0x48, 0x8f,
0x02, 0x04, 0x70, 0x00, 0x00, 0x00, 0x1f, 0x02, 0x08, 0x20, 0x83, 0xe0,
0x00, 0x00, 0x00, 0x0c, 0x20, 0x40, 0x81, 0x0c, 0x04, 0x08, 0x10, 0x20,
0x30, 0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x81, 0x02, 0x04, 0x08,
0x00, 0x01, 0x80, 0x81, 0x02, 0x04, 0x06, 0x10, 0x20, 0x40, 0x86, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x99, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00
};
typedef struct {
d_img def_canvas;
d_img* cur_canvas;
d_ibuf depth_buf;
d_bbuf bbuf;
bool depth_test;
bool backface_cull;
bool anti_alias;
bool bbuf_write;
bool bbuf_test;
d_font def_font;
d_font* cur_font;
d_color clear_color;
d_blend blend;
d_wrap wrap;
d_mat4 t;
d_mat4 tstack[D_MAX_TSTACK];
int tstack_len;
} d_gfx_ctx;
static d_gfx_ctx d_gfx;
void d_gfx_init(d_gfx_desc desc) {
d_gfx.def_canvas = d_img_new(desc.width, desc.height);
d_gfx.cur_canvas = &d_gfx.def_canvas;
d_gfx.depth_buf = d_ibuf_new(desc.width, desc.height);
d_gfx.bbuf = d_bbuf_new(desc.width, desc.height);
d_gfx.depth_test = !desc.no_depth_test;
d_gfx.backface_cull = !desc.no_backface_cull;
d_gfx.anti_alias = desc.anti_alias;
d_gfx.bbuf_write = false;
d_gfx.bbuf_test = false;
d_gfx.def_font = d_font_parse(proggy_bytes);
d_gfx.cur_font = &d_gfx.def_font;
d_gfx.blend = D_BLEND_ALPHA;
d_gfx.wrap = D_WRAP_BORDER;
d_gfx.clear_color = desc.clear_color;
d_gfx.t = d_mat4u();
memset(d_gfx.tstack, 0, sizeof(d_mat4) * D_MAX_TSTACK);
d_gfx.tstack_len = 0;
d_gfx_clear();
}
#ifdef D_APP_H
d_vec2 d_gfx_mouse_pos(void) {
d_vec2 mpos = d_app_mouse_pos();
return d_vec2f(
mpos.x * d_gfx_width() / d_app_width(),
mpos.y * d_gfx_height() / d_app_height()
);
}
d_vec2 d_gfx_mouse_dpos(void) {
d_vec2 mdpos = d_app_mouse_dpos();
return d_vec2f(
mdpos.x * d_gfx_width() / d_app_width(),
mdpos.y * d_gfx_height() / d_app_height()
);
}
void d_gfx_present(void) {
d_img* c = d_gfx.cur_canvas;
d_app_present(c->width, c->height, c->pixels);
d_gfx.cur_canvas = &d_gfx.def_canvas;
}
#endif // #ifdef D_APP_H
int d_gfx_width(void) {
return d_gfx.cur_canvas->width;
}
int d_gfx_height(void) {
return d_gfx.cur_canvas->height;
}
d_ibuf d_ibuf_new(int w, int h) {
return (d_ibuf) {
.width = w,
.height = h,
.buf = calloc(w * h, sizeof(int)),
};
}
void d_ibuf_free(d_ibuf* ibuf) {
free(ibuf->buf);
memset(ibuf, 0, sizeof(d_ibuf));
}
void d_ibuf_set(d_ibuf* ibuf, int x, int y, int v) {
ibuf->buf[y * ibuf->width + x] = v;
}
int d_ibuf_get(d_ibuf* ibuf, int x, int y) {
return ibuf->buf[y * ibuf->width + x];
}
void d_ibuf_clear(d_ibuf* ibuf, int v) {
for (int i = 0; i < ibuf->width * ibuf->height; i++) {
ibuf->buf[i] = v;
}
}
d_bbuf d_bbuf_new(int w, int h) {
return (d_bbuf) {
.width = w,
.height = h,
.bitset = d_bitset_new(w * h),
};
}
void d_bbuf_free(d_bbuf* bbuf) {
d_bitset_free(&bbuf->bitset);
memset(bbuf, 0, sizeof(d_bbuf));
}
void d_bbuf_set(d_bbuf* bbuf, int x, int y, bool b) {
d_bitset_set(&bbuf->bitset, y * bbuf->width + x, b);
}
bool d_bbuf_get(d_bbuf* bbuf, int x, int y) {
return d_bitset_get(&bbuf->bitset, y * bbuf->width + x);
}
void d_bbuf_clear(d_bbuf* bbuf, bool b) {
d_bitset_clear(&bbuf->bitset, b);
}
d_bitset d_bitset_new(size_t size) {
size = (size_t)ceil(size / 8.0f);
uint8_t* buf = malloc(size);
memset(buf, 0, size);
return (d_bitset) {
.buf = buf,
.size = size,
};
}
// TODO: is << faster than an uint8_t[8] lookup?
void d_bitset_set(d_bitset* m, int n, bool b) {
if (b) {
m->buf[n / 8] |= (1 << (7 - n % 8));
} else {
m->buf[n / 8] ^= (1 << (7 - n % 8));
}
}
bool d_bitset_get(d_bitset* m, int n) {
return m->buf[n / 8] & (1 << (7 - n % 8));
}
void d_bitset_clear(d_bitset* m, bool b) {
uint8_t n = b ? 0xff : 0x00;
for (int i = 0; i < m->size; i++) {
m->buf[i] = n;
}
}
void d_bitset_free(d_bitset* m) {
free(m->buf);
}
d_img d_img_new(int w, int h) {
return (d_img) {
.width = w,
.height = h,
.pixels = calloc(w * h, sizeof(d_color)),
};
}
d_img d_img_empty(void) {
return (d_img) {
.width = 0,
.height = 0,
.pixels = malloc(0),
};
}
static uint8_t png_sig[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
static uint8_t jpeg_sig[] = { 0xff, 0xd8, 0xff };
d_img d_img_parse(uint8_t* bytes, size_t size) {
if (
memcmp(bytes, png_sig, sizeof(png_sig)) == 0
|| memcmp(bytes, jpeg_sig, sizeof(jpeg_sig)) == 0
) {
#ifdef STB_IMAGE_IMPLEMENTATION
int w;
int h;
uint8_t* pixels = stbi_load_from_memory(bytes, size, &w, &h, NULL, 4);
return (d_img) {
.width = w,
.height = h,
.pixels = (d_color*)pixels,
};
#else
fprintf(stderr, "jpeg / png support requires 'stb_image.h'\n");
return d_img_empty();
#endif // #ifdef STB_IMAGE_IMPLEMENTATION
} else {
fprintf(stderr, "unsupported image format\n");
return d_img_empty();
}
}
#ifdef D_FS_H
d_img d_img_load(char* path) {
size_t size;
uint8_t* bytes = d_read_bytes(path, &size);
if (!bytes) {
fprintf(stderr, "failed to load img from '%s'\n", path);
return d_img_empty();
}
d_img img = d_img_parse(bytes, size);
free(bytes);
return img;
}
#endif // #ifdef D_FS_H
void d_img_set(d_img* img, int x, int y, d_color c) {
img->pixels[y * img->width + x] = c;
}
d_color d_img_get(d_img* img, int x, int y) {
return img->pixels[y * img->width + x];
}
void d_img_fill(d_img* img, d_color c) {
for (int i = 0; i < img->width * img->height; i++) {
img->pixels[i] = c;
}
}
void d_img_save_png(d_img* img, char* path) {
#ifdef STB_IMAGE_WRITE_IMPLEMENTATION
stbi_write_png(
path,
img->width,
img->height,
4,
(uint8_t*)img->pixels,
img->width * sizeof(d_color)
);
#else
fprintf(stderr, "image save only available with 'stb_image_write.h'\n");
#endif // #ifdef STB_IMAGE_WRITE_IMPLEMENTATION
}
d_img d_img_clone(d_img* img) {
int w = img->width;
int h = img->height;
d_img img2 = d_img_new(w, h);
memcpy(img2.pixels, img->pixels, w * h * sizeof(d_color));
return img2;
}
void d_img_free(d_img* img) {
free(img->pixels);
memset(img, 0, sizeof(d_img));
}
d_font d_font_parse(uint8_t* bytes) {
d_font f;
memset(&f, 0, sizeof(d_font));
f.gw = bytes[9];
f.gh = bytes[10];
uint8_t chars_size = bytes[11];
size_t bitmap_size = ceil(chars_size * f.gw * f.gh / 8.0f);
uint8_t* chars_buf = bytes + 9 + 1 + 1 + 1;
uint8_t* bitmap_buf = chars_buf + chars_size;
f.bitmap = calloc(chars_size * f.gw * f.gh, 1);
for (int i = 0; i < bitmap_size; i++) {
for (int j = 0; j < 8; j++) {
if (bitmap_buf[i] & (1 << (7 - j))) {
f.bitmap[(i * 8 + j)] = 1;
}
}
}
for (int i = 0; i < chars_size; i++) {
f.charmap[(int)chars_buf[i]] = i;
}
return f;
}
d_font d_font_empty(void) {
return (d_font) {
.gw = 0,
.gh = 0,
.charmap = {0},
.bitmap = malloc(0),
};
}
#ifdef D_FS_H
d_font d_font_load(char* path) {
size_t size;
uint8_t* bytes = d_read_bytes(path, &size);
if (!bytes) {
fprintf(stderr, "failed to load font from '%s'\n", path);
return (d_font) {0};
}
d_font font = d_font_parse(bytes);
free(bytes);
return font;
}
#endif // #ifdef D_FS_H
void d_font_free(d_font* f) {
free(f->bitmap);
memset(f, 0, sizeof(d_font));
}
void d_gfx_clear(void) {
d_img_fill(d_gfx.cur_canvas, d_gfx.clear_color);
d_ibuf_clear(&d_gfx.depth_buf, -INT_MAX);
}
void d_gfx_bbuf_clear(void) {
d_bbuf_clear(&d_gfx.bbuf, false);
}
void d_gfx_set_backface_cull(bool b) {
d_gfx.backface_cull = b;
}
void d_gfx_set_depth_test(bool b) {
d_gfx.depth_test = b;
}
void d_gfx_set_bbuf_write(bool b) {
d_gfx.bbuf_write = b;
}
void d_gfx_set_bbuf_test(bool b) {
d_gfx.bbuf_test = b;
}
bool d_gfx_bbuf_get(int x, int y) {
return d_bbuf_get(&d_gfx.bbuf, x, y);
}
void d_gfx_draw_pixel(int x, int y, int z, d_color c) {
d_img* img = d_gfx.cur_canvas;
if (x < 0 || x >= img->width || y < 0 || y >= img->height) {
return;
}
if (d_gfx.depth_test) {
if (d_ibuf_get(&d_gfx.depth_buf, x, y) <= z) {
d_ibuf_set(&d_gfx.depth_buf, x, y, z);
} else {
return;
}
}
if (d_gfx.bbuf_write) {
if (c.a != 0) {
d_bbuf_set(&d_gfx.bbuf, x, y, true);
}
}
if (d_gfx.bbuf_test) {
if (!d_bbuf_get(&d_gfx.bbuf, x, y)) {
return;
}
}
int i = y * img->width + x;
switch (d_gfx.blend) {
case D_BLEND_ALPHA: {
if (c.a == 255) {
img->pixels[i] = c;
} else if (c.a != 0) {
d_color rc = img->pixels[i];
img->pixels[i] = (d_color) {
.r = (rc.r * (255 - c.a) + c.r * c.a) / 255,
.g = (rc.g * (255 - c.a) + c.g * c.a) / 255,
.b = (rc.b * (255 - c.a) + c.b * c.a) / 255,
.a = (rc.a * (255 - c.a) + c.a * c.a) / 255,
};
}
break;
}
case D_BLEND_REPLACE:
img->pixels[i] = c;
break;
case D_BLEND_ADD:
if (c.a != 0) {
d_color rc = img->pixels[i];
img->pixels[i] = (d_color) {
.r = (rc.r * rc.a + c.r * c.a) / 255,
.g = (rc.g * rc.a + c.g * c.a) / 255,
.b = (rc.b * rc.a + c.b * c.a) / 255,
.a = (rc.a * rc.a + c.a * c.a) / 255,
};
}
break;
}
}
void d_gfx_blit_pixel(int x, int y, d_color c) {
d_img* img = d_gfx.cur_canvas;
if (x < 0 || x >= img->width || y < 0 || y >= img->height) {
return;
}
if (c.a != 0) {
img->pixels[y * img->width + x] = c;
}
}
d_color d_gfx_get(int x, int y) {
d_img* img = d_gfx.cur_canvas;
switch (d_gfx.wrap) {
case D_WRAP_BORDER:
if (x < 0 || x >= img->width || y < 0 || y >= img->height) {
return d_colori(0, 0, 0, 0);
} else {
return img->pixels[y * img->width + x];
}
case D_WRAP_EDGE: {
int xx = d_clampi(x, 0, img->width - 1);
int yy = d_clampi(y, 0, img->height - 1);
return img->pixels[yy * img->width + xx];
}
case D_WRAP_REPEAT: {
int xx = x % img->width;
int yy = y % img->height;
return img->pixels[yy * img->width + xx];
}
}
return d_colori(0, 0, 0, 0);
}
void d_blit_img(d_img* img, d_vec2 pos) {
for (int x = 0; x < img->width; x++) {
for (int y = 0; y < img->height; y++) {
d_gfx_blit_pixel(
x + pos.x,
y + pos.y,
img->pixels[y * img->width + x]
);
}
}
}
void d_blit_bg(void) {
d_color c1 = d_colori(128, 128, 128, 255);
d_color c2 = d_colori(191, 191, 191, 255);
int s = 32;
for (int x = 0; x < d_gfx_width(); x++) {
for (int y = 0; y < d_gfx_height(); y++) {
d_color c = (x / s % 2 + y / s % 2) == 1 ? c1 : c2;
d_gfx_blit_pixel(x, y, c);
}
}
}
void d_blit_rect(d_vec2 p1, d_vec2 p2, d_color c) {
int x1 = p1.x < p2.x ? p1.x : p2.x;
int x2 = p1.x > p2.x ? p1.x : p2.x;
int y1 = p1.y < p2.y ? p1.y : p2.y;
int y2 = p1.y > p2.y ? p1.y : p2.y;
for (int x = x1; x < x2; x++) {
for (int y = y1; y < y2; y++) {
d_gfx_blit_pixel(x, y, c);
}
}
}
void d_blit_circle(d_vec2 center, float r, d_color c) {
for (int i = center.x - r; i <= center.x + r; i++) {
for (int j = center.y - r; j <= center.y + r; j++) {
d_vec2 p = d_vec2f(i, j);
float d = d_vec2_dist(p, center);
if (d < r) {
d_gfx_blit_pixel(p.x, p.y, c);
}
}
}
}
void d_blit_text(char* text, d_vec2 pos, d_color c, bool bold, bool italic) {
int num_chars = strlen(text);
d_font* font = &d_gfx.def_font;
int ox = 0;
for (int i = 0; i < num_chars; i++) {
int charpos = font->charmap[(int)text[i]];
for (int x = 0; x < font->gw; x++) {
for (int y = 0; y < font->gh; y++) {
int idx = (charpos * font->gh + y) * font->gw + x;
int dx = italic ? (font->gh - y) * 0.3 : 0;
if (font->bitmap[idx]) {
d_gfx_blit_pixel(pos.x + ox + x + dx, pos.y + y, c);
} else if (bold && font->bitmap[idx - 1]) {
d_gfx_blit_pixel(pos.x + ox + x + dx, pos.y + y, c);
}
}
}
ox += font->gw;
}
}
void d_blit_line(d_vec2 p1, d_vec2 p2, d_color c) {
int x1 = p1.x;
int y1 = p1.y;
int x2 = p2.x;
int y2 = p2.y;
bool steep = abs(y2 - y1) > abs(x2 - x1);
if (steep) {
d_swapi(&x1, &y1);
d_swapi(&x2, &y2);
}
if (x1 > x2) {
d_swapi(&x1, &x2);
d_swapi(&y1, &y2);
}
int dx = x2 - x1;
int dy = abs(y2 - y1);
int err = dx / 2;
int step = y1 < y2 ? 1 : -1;
int y = y1;
for (int x = x1; x <= x2; x++) {
if (steep) {
d_gfx_blit_pixel(y, x, c);
} else {
d_gfx_blit_pixel(x, y, c);
}
err -= dy;
if (err < 0) {
y += step;
err += dx;
}
}
}
void d_blit_tri(d_vec2 p1, d_vec2 p2, d_vec2 p3, d_color c) {
// TODO
d_draw_tri(p1, p2, p3, c);
}
// TODO: concave
void d_blit_poly(d_poly p, d_color c) {
d_vec2 p1 = p.verts[0];
for (int i = 1; i < p.num_verts - 1; i++) {
d_vec2 p2 = p.verts[i];
d_vec2 p3 = p.verts[i + 1];
d_blit_tri(p1, p2, p3, c);
}
}
void d_vertex_swap(d_vertex* v1, d_vertex* v2) {
d_vertex v3 = *v2;
*v2 = *v1;
*v1 = v3;
}
void d_draw_prim_tri(
d_vertex v1,
d_vertex v2,
d_vertex v3,
d_img* tex
) {
d_vec3 p1 = d_transform_apply_vec3(v1.pos);
d_vec3 p2 = d_transform_apply_vec3(v2.pos);
d_vec3 p3 = d_transform_apply_vec3(v3.pos);
if (d_gfx.backface_cull) {
d_vec3 normal = d_vec3_unit(
d_vec3_cross(
d_vec3_sub(p3, p1),
d_vec3_sub(p2, p1)
)
);
if (d_vec3_dot(normal, d_vec3f(0, 0, 1)) < 0) {
// TODO: wrongfully culling normal 2d shapes
return;
}
}
if (p1.y > p2.y) {
d_vertex_swap(&v1, &v2);
d_vec3_swap(&p1, &p2);
}
if (p1.y > p3.y) {
d_vertex_swap(&v1, &v3);
d_vec3_swap(&p1, &p3);
}
if (p2.y > p3.y) {
d_vertex_swap(&v2, &v3);
d_vec3_swap(&p2, &p3);
}
int x1 = p1.x;
int y1 = p1.y;
int z1 = p1.z;
int x2 = p2.x;
int y2 = p2.y;
int z2 = p2.z;
int x3 = p3.x;
int y3 = p3.y;
int z3 = p3.z;
// d_vec3 n1 = d_vec3_unit(d_transform_apply_vec3(v1.normal));
// d_vec3 n2 = d_vec3_unit(d_transform_apply_vec3(v2.normal));
// d_vec3 n3 = d_vec3_unit(d_transform_apply_vec3(v3.normal));
int gw = d_gfx_width();
int gh = d_gfx_height();
for (int y = y1; y < y3; y++) {
if (y < 0 || y >= gh) {
continue;