-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmg_plot.h
More file actions
5233 lines (4523 loc) · 370 KB
/
mg_plot.h
File metadata and controls
5233 lines (4523 loc) · 370 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
#ifndef MGP_PLOT_H
#define MGP_PLOT_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>
typedef int8_t mgp_i8;
typedef int32_t mgp_i32;
typedef int64_t mgp_i64;
typedef uint8_t mgp_u8;
typedef uint16_t mgp_u16;
typedef uint32_t mgp_u32;
typedef uint64_t mgp_u64;
typedef mgp_i8 mgp_b8;
typedef mgp_i32 mgp_b32;
typedef float mgp_f32;
typedef double mgp_f64;
static_assert(sizeof(mgp_f32) == 4, "f32 size");
static_assert(sizeof(mgp_f64) == 8, "f64 size");
typedef struct {
mgp_u64 size;
mgp_u8* str;
} mgp_string8;
#define MGP_STR8(s) ((mgp_string8){ sizeof(s)-1, (mgp_u8*)s })
typedef struct { mgp_f32 x, y; } mgp_vec2f;
typedef struct { mgp_f32 x, y, z; } mgp_vec3f;
typedef struct { mgp_f32 x, y, z, w; } mgp_vec4f;
typedef struct { mgp_i32 x, y, w, h; } mgp_recti;
typedef struct { mgp_f32 x, y, w, h; } mgp_rectf;
typedef union {
struct {
mgp_vec2f p0;
mgp_vec2f p1;
mgp_vec2f p2;
mgp_vec2f p3;
};
mgp_vec2f p[4];
} mgp_quadf;
typedef struct {
mgp_f32 size;
mgp_vec2f* data;
} mgp_points_cmd;
typedef struct {
enum {
MGP_LINE_SOLID,
MGP_LINE_DASHED
} type;
mgp_f32 width;
mgp_vec2f* data;
} mgp_lines_cmd;
typedef struct {
mgp_rectf* data;
} mgp_rects_cmd;
typedef struct {
mgp_quadf* data;
} mgp_quads_cmd;
typedef struct {
mgp_vec4f color;
mgp_vec4f* colors;
mgp_string8 label;
mgp_u32 size;
enum {
MGP_DRAW_POINTS,
MGP_DRAW_LINES,
MGP_DRAW_RECTS,
MGP_DRAW_QUADS,
} type;
union {
mgp_points_cmd points;
mgp_lines_cmd lines;
mgp_rects_cmd rects;
mgp_quads_cmd quads;
};
} mgp_draw_cmd;
typedef struct {
// Background of main area above buttons
mgp_vec4f background;
// Background of graph area
mgp_vec4f graph_background;
// 1 pixel outline of graph area
mgp_vec4f graph_outline;
// Title and axes text
mgp_vec4f axes_text;
// Background of legend box
mgp_vec4f legend_background;
// Outline of legend box
mgp_vec4f legend_outline;
// Color of legend text
mgp_vec4f legend_text;
// Background color of bottom strip (where buttons are)
mgp_vec4f bottom_strip;
// Color of button icons
mgp_vec4f button_foreground;
// Background color of button icons
mgp_vec4f button_background;
// Background color of hovered button
mgp_vec4f button_hovered;
// Background color of toggled button
mgp_vec4f button_toggled;
// Color of zoom rect
mgp_vec4f zoom_rect;
// Default colors for draw commands
mgp_vec4f default_draw[8];
} mgp_color_scheme;
typedef struct {
mgp_f32 left, right;
mgp_f32 bottom, top;
} mgp_view;
typedef enum {
MGP_ALIGN_RIGHT = 0,
MGP_ALIGN_LEFT,
} mgp_legend_align;
void mgp_init(void);
void mgp_cmd_push(const mgp_draw_cmd* cmd);
void mgp_plot_show(void);
void mgp_enable_legend(mgp_legend_align align);
// Color values of (vec4f){ 0 } are set to the default color scheme color
void mgp_set_color_scheme(const mgp_color_scheme* colors);
void mgp_set_view(mgp_view new_view);
void mgp_set_win_size(mgp_u32 width, mgp_u32 height);
void mgp_set_title(mgp_string8 title);
// Set color to (vec4f){ 0 } for default
// colors can be NULL
void mgp_points_ex(mgp_u32 num_points, mgp_f32* xs, mgp_f32* ys, mgp_f32 size, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label);
void mgp_lines_ex(mgp_u32 num_points, mgp_f32* xs, mgp_f32* ys, mgp_f32 width, mgp_u32 type, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label);
void mgp_rects_ex(mgp_u32 num_rects, mgp_rectf* rects, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label);
void mgp_quads_ex(mgp_u32 num_quads, mgp_quadf* quads, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label);
#define mgp_points(num_points, xs, ys) mgp_points_ex(num_points, xs, ys, 3.0f, (mgp_vec4f){ 0 }, (void*)0, (mgp_string8){ 0 })
#define mgp_lines(num_points, xs, ys) mgp_lines_ex(num_points, xs, ys, 2.0f, MGP_LINE_SOLID, (mgp_vec4f){ 0 }, (void*)0, (mgp_string8){ 0 })
#define mgp_rects(num_rects, rects) mgp_rects_ex(num_rects, rects, (mgp_vec4f){ 0 }, (void*)0, (mgp_string8){ 0 })
#define mgp_quads(num_quads, quads) mgp_quads_ex(num_quads, quads, (mgp_vec4f){ 0 }, (void*)0, (mgp_string8){ 0 })
#endif // MGP_PLOT_H
#ifdef MG_PLOT_IMPL
#ifndef MG_ARENA_H
# error "mg_arena.h required by mg_plot.h"
#endif
#include <math.h>
#include <stdio.h>
#include <string.h>
/*
=============================
=============================
HEADERS
=============================
=============================
*/
#if defined(_WIN32)
# define PLATFORM_WIN32
#elif defined(__linux__)
# define PLATFORM_LINUX
#endif
#if defined(PLATFORM_WIN32)
# define UNICODE
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# include <GL/gl.h>
#elif defined(PLATFORM_LINUX)
# include <GL/gl.h>
# include <unistd.h>
#endif
#define UNUSED(x) (void)(x)
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define ABS(n) ((n) < 0 ? -(n) : (n))
#define SIGN(n) ((n) < 0 ? -1 : 1)
#define SLL_PUSH_BACK(f, l, n) ((f) == 0 ? \
((f) = (l) = (n)) : \
((l)->next = (n), (l) = (n)), \
((n)->next = 0)) \
mgp_string8 _str8_from_range(mgp_u8* start, mgp_u8* end);
mgp_string8 _str8_from_cstr(mgp_u8* cstr);
mgp_string8 _str8_copy(mg_arena* arena, mgp_string8 str);
mgp_vec2f _vec2f_add(mgp_vec2f a, mgp_vec2f b);
mgp_vec2f _vec2f_sub(mgp_vec2f a, mgp_vec2f b);
mgp_vec2f _vec2f_scl(mgp_vec2f v, mgp_f32 s);
mgp_f32 _vec2f_dot(mgp_vec2f a, mgp_vec2f b);
mgp_vec2f _vec2f_nrm(mgp_vec2f v);
mgp_f32 _vec2f_len(mgp_vec2f v);
mgp_b32 _vec2f_in_rectf(mgp_rectf r, mgp_vec2f p);
typedef struct _gfx_win_backend _gfx_win_backend;
typedef struct {
mgp_string8 title;
mgp_u32 width, height;
mgp_b32 should_close;
mgp_vec2f mouse_pos;
mgp_b8 mouse_buttons[5];
mgp_b8 prev_mouse_buttons[5];
_gfx_win_backend* backend;
} gfx_window;
gfx_window* gfx_win_create(mg_arena* arena, mgp_u32 width, mgp_u32 height, mgp_string8 title);
void gfx_win_destroy(gfx_window* win);
void gfx_win_process_events(gfx_window* win);
void gfx_win_make_current(gfx_window* win);
void gfx_win_swap_buffers(gfx_window* win);
#define GFX_IS_MOUSE_DOWN(win, mb) ( win->mouse_buttons[mb])
#define GFX_IS_MOUSE_UP(win, mb) (!win->mouse_buttons[mb])
#define GFX_IS_MOUSE_JUST_DOWN(win, mb) (win->mouse_buttons[mb] && !win->prev_mouse_buttons[mb])
#define GFX_IS_MOUSE_JUST_UP(win, mb) (!win->mouse_buttons[mb] && win->prev_mouse_buttons[mb])
typedef enum {
GFX_MB_LEFT,
GFX_MB_MIDDLE,
GFX_MB_RIGHT
} gfx_mouse_button;
#define _BASE_OPENGL_FUNCS \
X(void, glBindBuffer, (GLenum target, GLuint buffer)) \
X(void, glBindFramebuffer, (GLenum target, GLuint framebuffer)) \
X(void, glBufferData, (GLenum target, GLsizeiptr size, const void *data, GLenum usage)) \
X(void, glBufferSubData, (GLenum target, GLintptr offset, GLsizeiptr size, const void *data)) \
X(GLenum, glCheckFramebufferStatus, (GLenum target)) \
X(void, glCompileShader, (GLuint shader)) \
X(GLuint, glCreateProgram, (void)) \
X(GLuint, glCreateShader, (GLenum type)) \
X(void, glDeleteBuffers, (GLsizei n, const GLuint *buffers)) \
X(void, glDeleteFramebuffers, (GLsizei n, const GLuint *framebuffers)) \
X(void, glDeleteProgram, (GLuint program)) \
X(void, glDeleteShader, (GLuint shader)) \
X(void, glDisableVertexAttribArray, (GLuint index)) \
X(void, glEnableVertexAttribArray, (GLuint index)) \
X(void, glFramebufferTexture2D, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)) \
X(void, glGenBuffers, (GLsizei n, GLuint *buffers)) \
X(void, glGenFramebuffers, (GLsizei n, GLuint *framebuffers)) \
X(void, glGetProgramiv, (GLuint program, GLenum pname, GLint *params)) \
X(void, glGetProgramInfoLog, (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog)) \
X(void, glGetShaderiv, (GLuint shader, GLenum pname, GLint *params)) \
X(void, glGetShaderInfoLog, (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog)) \
X(GLint, glGetUniformLocation, (GLuint program, const GLchar *name)) \
X(void, glLinkProgram, (GLuint program)) \
X(void, glAttachShader, (GLuint program, GLuint shader)) \
X(void, glShaderSource, (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length)) \
X(void, glUniform1i, (GLint location, GLint v0)) \
X(void, glUniform2f, (GLint location, GLfloat v0, GLfloat v1)) \
X(void, glUniform4f, (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)) \
X(void, glUniformMatrix4fv, (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)) \
X(void, glUseProgram, (GLuint program)) \
X(void, glVertexAttribPointer, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer)) \
X(void, glBindVertexArray, (GLuint array)) \
X(void, glDeleteVertexArrays, (GLsizei n, const GLuint *arrays)) \
X(void, glGenVertexArrays, (GLsizei n, GLuint *arrays)) \
X(void, glDrawArraysInstanced, (GLenum mode, GLint first, GLsizei count, GLsizei instancecount)) \
X(void, glVertexAttribDivisor, (GLuint index, GLuint divisor))
#ifndef __linux__
# define _OPENGL_FUNCS \
_BASE_OPENGL_FUNCS \
X(void, glActiveTexture, (GLenum texture))
#else
# define _OPENGL_FUNCS _BASE_OPENGL_FUNCS
#endif
typedef mgp_f32 GLfloat;
typedef int GLint;
typedef intptr_t GLintptr;
typedef unsigned int GLenum;
typedef mgp_i64 GLsizeiptr;
typedef unsigned int GLuint;
typedef int GLsizei;
typedef char GLchar;
typedef unsigned char GLboolean;
#ifdef PLATFORM_WIN32
# define OPENGL_CALLSTYLE __stdcall
#else
# define OPENGL_CALLSTYLE
#endif
#define X(ret, name, args) typedef ret (OPENGL_CALLSTYLE* gl_##name##_func)args; extern gl_##name##_func name;
_OPENGL_FUNCS
#undef X
#define GL_TEXTURE_MIN_FILTER 0x2801
#define GL_STATIC_DRAW 0x88E4
#define GL_R8 0x8229
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
#define GL_SCISSOR_TEST 0x0C11
#define GL_UNSIGNED_INT 0x1405
#define GL_ARRAY_BUFFER 0x8892
#define GL_UNSIGNED_BYTE 0x1401
#define GL_RGBA 0x1908
#define GL_BLEND 0x0BE2
#define GL_COLOR_BUFFER_BIT 0x00004000
#define GL_LINEAR 0x2601
#define GL_UNPACK_ALIGNMENT 0x0CF5
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
#define GL_FALSE 0
#define GL_NEAREST 0x2600
#define GL_TRIANGLES 0x0004
#define GL_FLOAT 0x1406
#define GL_SRC_ALPHA 0x0302
#define GL_RGBA8 0x8058
#define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_TEXTURE_2D 0x0DE1
#define GL_COLOR_ATTACHMENT0 0x8CE0
#define GL_FRAMEBUFFER 0x8D40
#define GL_TEXTURE0 0x84C0
#define GL_STREAM_DRAW 0x88E0
#define GL_RED 0x1903
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_VERTEX_SHADER 0x8B31
#define GL_COMPILE_STATUS 0x8B81
#define GL_LINK_STATUS 0x8B82
mgp_u32 glh_create_shader(const char* vertex_source, const char* fragment_source);
mgp_u32 glh_create_buffer(mgp_u32 buffer_type, mgp_u64 size, void* data, mgp_u32 draw_type);
typedef struct {
mgp_rectf rect;
mgp_f32 xoffset;
mgp_f32 yoffset;
mgp_f32 xadvance;
} _font_glyph_data;
static const mgp_u32 _font_img_width;
static const mgp_u32 _font_img_height;
static const mgp_u8 _font_img[49588];
static const mgp_f32 _font_orig_size;
static const mgp_f32 _font_line_height;
static const _font_glyph_data _font_glyphs_data[95];
#ifdef INCLUDE_STB_IMAGE_WRITE_H
# define _HAS_STBI
#endif
/*
=============================
=============================
PLOTTING
=============================
=============================
*/
typedef struct _mgp_draw_cmd_node {
mgp_draw_cmd cmd;
struct _mgp_draw_cmd_node* next;
} _mgp_draw_cmd_node;
typedef struct {
mgp_u32 size;
_mgp_draw_cmd_node* first;
_mgp_draw_cmd_node* last;
} _mgp_draw_cmd_list;
static mg_arena* _mgp_arena = NULL;
static _mgp_draw_cmd_list _mgp_draw_cmds = { 0 };
static mgp_u32 _mgp_default_win_width = 800;
static mgp_u32 _mgp_default_win_height = 600;
static mgp_u32 _mgp_win_width = 800;
static mgp_u32 _mgp_win_height = 600;
static mgp_b32 _mgp_enable_legend = false;
static mgp_legend_align _mgp_legend_align = MGP_ALIGN_RIGHT;
static mgp_color_scheme _mgp_default_colors = {
.background = (mgp_vec4f){ 0.15f, 0.15f, 0.15f, 1.0f },
.graph_background = (mgp_vec4f){ 0.25f, 0.25f, 0.25f, 1.0f },
.graph_outline = (mgp_vec4f){ 1.0f, 1.0f, 1.0f, 1.0f },
.axes_text = (mgp_vec4f){ 1.0f, 1.0f, 1.0f, 1.0f },
.legend_background = (mgp_vec4f){ 0.5f, 0.5f, 0.5f, 1.0f },
.legend_outline = (mgp_vec4f){ 1.0f, 1.0f, 1.0f, 1.0f },
.legend_text = (mgp_vec4f){ 1.0f, 1.0f, 1.0f, 1.0f },
.bottom_strip = (mgp_vec4f){ 0.67f, 0.67f, 0.67f, 1.0f },
.button_foreground = (mgp_vec4f){ 1.0f, 1.0f, 1.0f, 1.0f },
.button_background = (mgp_vec4f){ 0.0f, 0.0f, 0.0f, 0.0f },
.button_hovered = (mgp_vec4f){ 0.0f, 0.0f, 0.0f, 0.5f },
.button_toggled = (mgp_vec4f){ 0.0f, 0.0f, 0.0f, 0.2f },
.zoom_rect = (mgp_vec4f){ 1.0f, 1.0f, 1.0f, 1.0f },
.default_draw = {
(mgp_vec4f){ 0.0f, 0.5f, 1.0f, 1.0f },
(mgp_vec4f){ 1.0f, 0.0f, 0.0f, 1.0f },
(mgp_vec4f){ 0.0f, 1.0f, 0.0f, 1.0f },
(mgp_vec4f){ 1.0f, 0.0f, 1.0f, 1.0f },
(mgp_vec4f){ 0.0f, 1.0f, 1.0f, 1.0f },
(mgp_vec4f){ 1.0f, 1.0f, 0.0f, 1.0f },
(mgp_vec4f){ 1.0f, 0.5f, 0.5f, 1.0f },
(mgp_vec4f){ 0.5f, 0.5f, 1.0f, 1.0f },
}
};
static mgp_color_scheme _mgp_colors = { 0 };
static mgp_u32 _mgp_draw_col_index = 0;
static mgp_view _mgp_view = { 0 };
static mgp_string8 _mgp_title = { 0 };
void mgp_init(void) {
if (_mgp_arena == NULL) {
mga_desc desc = {
.desired_max_size = MGA_MiB(4),
.desired_block_size = MGA_KiB(64)
};
_mgp_arena = mga_create(&desc);
}
_mgp_colors = _mgp_default_colors;
}
void mgp_cmd_push(const mgp_draw_cmd* cmd) {
_mgp_draw_cmd_node* node = MGA_PUSH_ZERO_STRUCT(_mgp_arena, _mgp_draw_cmd_node);
memcpy(&node->cmd, cmd, sizeof(mgp_draw_cmd));
if (cmd->colors != NULL) {
node->cmd.colors = MGA_PUSH_ARRAY(_mgp_arena, mgp_vec4f, cmd->size);
memcpy(node->cmd.colors, cmd->colors, sizeof(mgp_vec4f) * cmd->size);
}
if (cmd->label.size != 0) {
node->cmd.label = _str8_copy(_mgp_arena, cmd->label);
}
mgp_vec4f col = node->cmd.color;
if (node->cmd.colors == NULL && (col.x == 0.0f && col.y == 0.0f && col.z == 0.0f && col.w == 0.0f)) {
node->cmd.color = _mgp_colors.default_draw[_mgp_draw_col_index++];
_mgp_draw_col_index %= sizeof(_mgp_colors.default_draw) / sizeof(_mgp_colors.default_draw[0]);
}
switch(node->cmd.type) {
case MGP_DRAW_POINTS: {
node->cmd.points.data = MGA_PUSH_ARRAY(_mgp_arena, mgp_vec2f, cmd->size);
memcpy(node->cmd.points.data, cmd->points.data, sizeof(mgp_vec2f) * cmd->size);
} break;
case MGP_DRAW_LINES: {
node->cmd.lines.data = MGA_PUSH_ARRAY(_mgp_arena, mgp_vec2f, cmd->size);
memcpy(node->cmd.lines.data, cmd->lines.data, sizeof(mgp_vec2f) * cmd->size);
} break;
case MGP_DRAW_RECTS: {
node->cmd.rects.data = MGA_PUSH_ARRAY(_mgp_arena, mgp_rectf, cmd->size);
memcpy(node->cmd.rects.data, cmd->rects.data, sizeof(mgp_rectf) * cmd->size);
} break;
case MGP_DRAW_QUADS: {
node->cmd.quads.data = MGA_PUSH_ARRAY(_mgp_arena, mgp_quadf, cmd->size);
memcpy(node->cmd.quads.data, cmd->quads.data, sizeof(mgp_quadf) * cmd->size);
} break;
// TODO: Error handling
default: break;
}
_mgp_draw_cmds.size++;
SLL_PUSH_BACK(_mgp_draw_cmds.first, _mgp_draw_cmds.last, node);
}
// mgp_plot_show() is below
void mgp_enable_legend(mgp_legend_align align) {
_mgp_enable_legend = true;
_mgp_legend_align = align;
}
#define _TRY_COL(new, orig) if (new.x != 0.0f || new.y != 0.0f || new.z != 0.0f || new.w != 0.0f) { orig = new; }
void mgp_set_color_scheme(const mgp_color_scheme* colors) {
_TRY_COL(colors->background, _mgp_colors.background);
_TRY_COL(colors->graph_background, _mgp_colors.graph_background);
_TRY_COL(colors->graph_outline, _mgp_colors.graph_outline);
_TRY_COL(colors->axes_text, _mgp_colors.axes_text);
_TRY_COL(colors->bottom_strip, _mgp_colors.bottom_strip);
_TRY_COL(colors->button_foreground, _mgp_colors.button_foreground);
_TRY_COL(colors->button_background, _mgp_colors.button_background);
_TRY_COL(colors->button_hovered, _mgp_colors.button_hovered);
_TRY_COL(colors->button_toggled, _mgp_colors.button_toggled);
_TRY_COL(colors->zoom_rect, _mgp_colors.zoom_rect);
for (mgp_u32 i = 0; i < 8; i++) {
_TRY_COL(colors->default_draw[i], _mgp_colors.default_draw[i]);
}
}
void mgp_set_view(mgp_view new_view) {
_mgp_view = new_view;
}
void mgp_set_win_size(mgp_u32 width, mgp_u32 height) {
_mgp_win_width = width;
_mgp_win_height = height;
}
void mgp_set_title(mgp_string8 title) {
_mgp_title = _str8_copy(_mgp_arena, title);
}
void mgp_points_ex(mgp_u32 num_points, mgp_f32* xs, mgp_f32* ys, mgp_f32 size, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label) {
mga_temp scratch = mga_scratch_get(NULL, 0);
mgp_points_cmd points = {
.size = size,
.data = MGA_PUSH_ARRAY(scratch.arena, mgp_vec2f, num_points)
};
for (mgp_u32 i = 0; i < num_points; i++) {
points.data[i] = (mgp_vec2f){ xs[i], ys[i] };
}
mgp_draw_cmd cmd = {
.color = color,
.colors = colors,
.label = label,
.size = num_points,
.type = MGP_DRAW_POINTS,
.points = points
};
mgp_cmd_push(&cmd);
mga_scratch_release(scratch);
}
void mgp_lines_ex(mgp_u32 num_points, mgp_f32* xs, mgp_f32* ys, mgp_f32 width, mgp_u32 type, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label) {
mga_temp scratch = mga_scratch_get(NULL, 0);
mgp_lines_cmd lines = {
.type = type,
.width = width,
.data = MGA_PUSH_ARRAY(scratch.arena, mgp_vec2f, num_points)
};
for (mgp_u32 i = 0; i < num_points; i++) {
lines.data[i] = (mgp_vec2f){ xs[i], ys[i] };
}
mgp_draw_cmd cmd = {
.color = color,
.colors = colors,
.label = label,
.size = num_points,
.type = MGP_DRAW_LINES,
.lines = lines
};
mgp_cmd_push(&cmd);
mga_scratch_release(scratch);
}
void mgp_rects_ex(mgp_u32 num_rects, mgp_rectf* rects, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label) {
mga_temp scratch = mga_scratch_get(NULL, 0);
mgp_draw_cmd cmd = {
.type = MGP_DRAW_RECTS,
.size = num_rects,
.color = color,
.colors = colors,
.label = label,
.rects.data = rects
};
mgp_cmd_push(&cmd);
mga_scratch_release(scratch);
}
void mgp_quads_ex(mgp_u32 num_quads, mgp_quadf* quads, mgp_vec4f color, mgp_vec4f* colors, mgp_string8 label) {
mga_temp scratch = mga_scratch_get(NULL, 0);
mgp_draw_cmd cmd = {
.type = MGP_DRAW_QUADS,
.size = num_quads,
.color = color,
.colors = colors,
.label = label,
.quads.data = quads
};
mgp_cmd_push(&cmd);
mga_scratch_release(scratch);
}
typedef mgp_f32 mat4f[16];
typedef struct {
mgp_vec2f pos;
mgp_vec4f col;
} _quad_vert;
typedef struct {
mgp_vec2f pos;
mgp_vec4f col;
mgp_f32 size;
} _point_vert;
typedef struct {
mgp_vec2f pos;
mgp_vec2f miter;
mgp_f32 dash_progress;
mgp_vec4f col;
} _line_vert;
typedef struct {
mgp_rectf rect;
mgp_rectf uv_rect;
} _text_vert;
typedef struct {
mgp_u32 num_quads;
mgp_u32 shader;
mgp_u32 mat_loc;
mgp_u32 vertex_array;
mgp_u32 vertex_buffer;
mgp_u32 index_buffer;
} _quad_batch;
typedef struct {
mgp_u32 shader;
mgp_u32 resolution_loc;
mgp_u32 offset_loc;
mgp_u32 vertex_array;
mgp_u32 vertex_buffer;
mgp_u32 index_buffer;
mgp_u32 num_vertices;
mgp_u32 num_indices;
// For panning
mgp_vec2f init_view_dim;
mgp_vec2f init_view_topleft;
mgp_vec2f offset;
} _line_batch;
typedef struct {
mgp_u32 num_points;
mgp_u32 shader;
mgp_u32 mat_loc;
mgp_u32 scale_loc;
mgp_u32 vertex_array;
mgp_u32 pos_pattern_buffer;
mgp_u32 vertex_buffer;
} _point_batch;
typedef struct {
mgp_u32 shader;
mgp_u32 mat_loc;
mgp_u32 texture_loc;
mgp_u32 color_loc;
mgp_u32 texture;
mgp_u32 pos_pattern_buffer;
mat4f mat;
} _text_data;
typedef struct {
mgp_vec4f color;
mgp_u32 vertex_array;
mgp_u32 vertex_buffer;
mgp_u32 size;
mgp_u32 capacity;
} _text_batch;
typedef struct {
mgp_vec4f color;
mgp_rectf rect;
mgp_string8 label;
} _legend_entry;
typedef struct {
mgp_u32 num_entries;
_legend_entry* entries;
mgp_rectf rect;
_text_batch text_batch;
} _legend;
typedef struct {
mgp_u32 framebuffer;
mgp_u32 texture;
mgp_recti fb_rect;
mgp_recti graph_rect;
mgp_u32 shader;
mgp_u32 scale_loc;
mgp_u32 offset_loc;
mgp_u32 texture_loc;
mgp_u32 vertex_array;
mgp_u32 vertex_buffer;
} _framebuffer;
typedef struct {
mgp_f32 right, left, top, bottom;
} _view;
#define _BUTTON_TEXTURE_SIZE 16
#define _NUM_BUTTONS 4
typedef struct _mgp_state _mgp_state;
typedef void (_button_func)(_mgp_state* state, mgp_b32 toggled);
typedef struct {
_button_func* func;
mgp_rectf rect;
mgp_rectf uv_rect;
mgp_vec4f foreground_col;
mgp_vec4f background_col;
mgp_vec4f hovered_bg_col;
mgp_vec4f toggled_bg_col;
// If this gets toggled, exclusive_with gets untoggled
// -1 for nothing
mgp_i64 exclusive_with;
mgp_b8 active;
mgp_b8 enable_toggle;
mgp_b8 toggled;
mgp_b8 hovered;
} _button;
typedef struct {
_button buttons[_NUM_BUTTONS];
mgp_u32 texture;
} _buttons;
typedef struct {
mgp_u32 shader;
mgp_u32 mat_loc;
mgp_u32 rect_loc;
mgp_u32 uv_rect_loc;
mgp_u32 texture_loc;
mgp_u32 foreground_col_loc;
mgp_u32 background_col_loc;
mgp_u32 outline_loc;
// 1x1 black texture
mgp_u32 default_texture;
mgp_u32 current_texture;
mgp_u32 vertex_array;
mgp_u32 vertex_buffer;
} _screen_rect;
typedef struct _mgp_state {
gfx_window* win;
mgp_string8 title;
_quad_batch quads;
_line_batch lines;
_point_batch points;
_text_data text_data;
_text_batch axes_text;
_legend legend;
_framebuffer fb;
_view init_view;
_view view;
mat4f view_mat;
// For drawing single rectangles given a rect and screen rect
_screen_rect screen_rect;
enum {
_MODE_VIEW = 0,
_MODE_PAN,
_MODE_ZOOM
} mode;
_buttons buttons;
mgp_b8 view_change, win_change;
} _mgp_state;
typedef enum {
ALIGN_LEFT = 0,
ALIGN_CENTER,
ALIGN_RIGHT
} _x_text_align;
typedef enum {
ALIGN_TOP = 0,
ALIGN_MIDDLE,
ALIGN_BOTTOM,
} _y_text_align;
static void _ortho_mat(mat4f mat, _view view);
static mgp_vec2f _screen_to_graph(_mgp_state* state, mgp_vec2f p);
static void _compute_init_view(_mgp_state* state);
static mgp_vec2f _measure_text(mgp_string8 text, mgp_f32 font_size);
static mgp_b32 _push_text(_text_vert* verts, mgp_u32 capacity, mgp_u32* size, mgp_string8 text, mgp_vec2f pos, _x_text_align x_align, _y_text_align y_align, mgp_f32 font_size);
static void _init_framebuffer(_mgp_state* state);
static void _init_text_data(_mgp_state* state);
static void _init_legend(_mgp_state* state);
static void _init_screen_rect(_mgp_state* state);
static void _init_buttons(_mgp_state* state);
static void _init_graph_shaders(_mgp_state* state);
static void _init_graph_buffers(_mgp_state* state);
static void _button_init_view(_mgp_state* state, mgp_b32 toggled);
static void _button_pan_mode(_mgp_state* state, mgp_b32 toggled);
static void _button_zoom_mode(_mgp_state* state, mgp_b32 toggled);
static void _button_save_img(_mgp_state* state, mgp_b32 toggled);
// -1 for default texture
static void _update_screen_rect(_mgp_state* state, mgp_rectf new_screen_rect, mgp_i64 new_texture);
static void _update_buttons(_mgp_state* state); // Every frame update
static void _update_framebuffer(_mgp_state* state);
static void _update_legend(_mgp_state* state);
static void _update_axes_text(_mgp_state* state);
static void _update_line_buffer(_mgp_state* state);
static void _draw_points(_mgp_state* state);
static void _draw_lines(_mgp_state* state);
static void _draw_quads(_mgp_state* state);
static void _draw_legend(_mgp_state* state);
static void _draw_graph(_mgp_state* state);
static void _draw_framebuffer(_mgp_state* state);
static void _draw_buttons(_mgp_state* state);
static void _draw_screen_rect(_mgp_state* state, mgp_rectf rect, mgp_rectf uv_rect, mgp_f32 outline_width, mgp_vec4f bg_col, mgp_vec4f fg_col);
static void _delete_framebuffer(_mgp_state* state);
static void _delete_text_data(_mgp_state* state);
static void _delete_legend(_mgp_state* state);
static void _delete_screen_rect(_mgp_state* state);
static void _delete_buttons(_mgp_state* state);
static void _delete_graph_shaders(_mgp_state* state);
static void _delete_graph_buffers(_mgp_state* state);
static void _init_text_batch(_mgp_state* state, _text_batch* text_batch);
static void _draw_text_batch(_mgp_state* state, _text_batch* text_batch);
static void _delete_text_batch(_text_batch* text_batch);
void mgp_plot_show(void) {
_mgp_state state = { 0 };
if (_mgp_title.size == 0) {
state.title = MGP_STR8("Plot");
} else {
state.title = _mgp_title;
}
state.win = gfx_win_create(_mgp_arena, _mgp_win_width, _mgp_win_height, state.title);
if (_mgp_view.left != 0.0f || _mgp_view.right != 0.0f || _mgp_view.bottom != 0.0f || _mgp_view.top != 0.0f) {
state.view = (_view){
.left = _mgp_view.left,
.right = _mgp_view.right,
.bottom = _mgp_view.bottom,
.top = _mgp_view.top,
};
_ortho_mat(state.view_mat, state.view);
} else {
_compute_init_view(&state);
}
state.init_view = state.view;
// 4 sigfigs + 6 for (-, ., and scientific notation)
#define _AXES_TEXT_MAX_CHARS 10
// For axes
state.axes_text.capacity += (_AXES_TEXT_MAX_CHARS) * 6 * 2;
// For title
state.axes_text.capacity += state.title.size;
state.axes_text.color = _mgp_colors.axes_text;
_init_text_batch(&state, &state.axes_text);
if (_mgp_enable_legend) {
_init_legend(&state);
}
_init_framebuffer(&state);
_init_text_data(&state);
_init_buttons(&state);
_init_screen_rect(&state);
_init_graph_shaders(&state);
_init_graph_buffers(&state);
mgp_u32 prev_width = state.win->width;
mgp_u32 prev_height = state.win->height;
// Ensures everything is initialized
state.win_change = true;
state.view_change = true;
mgp_vec2f init_pan_pos = { 0 };
_view init_pan_view = { 0 };
mgp_vec2f init_zoom_pos = { 0 };
mgp_vec2f init_zoom_mouse_pos = { 0 };
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while (!state.win->should_close) {
gfx_win_process_events(state.win);
if (prev_width != state.win->width || prev_height != state.win->height) {
state.win_change = true;
}
prev_width = state.win->width;
prev_height = state.win->height;
if (state.win_change) {
_update_framebuffer(&state);
}
_update_buttons(&state);
switch (state.mode) {
case _MODE_VIEW: break;
case _MODE_PAN: {
mgp_rectf graph_mgp_rectf = {
state.fb.graph_rect.x, state.fb.graph_rect.y,
state.fb.graph_rect.w, state.fb.graph_rect.h,
};
if (_vec2f_in_rectf(graph_mgp_rectf, state.win->mouse_pos)) {
if (GFX_IS_MOUSE_JUST_DOWN(state.win, GFX_MB_LEFT)) {
init_pan_pos = _vec2f_sub(
_screen_to_graph(&state, state.win->mouse_pos),
(mgp_vec2f){ state.view.left, state.view.bottom }
);
init_pan_view = state.view;
}
if (GFX_IS_MOUSE_DOWN(state.win, GFX_MB_LEFT)) {
mgp_vec2f cur_pan_pos = _vec2f_sub(
_screen_to_graph(&state, state.win->mouse_pos),
(mgp_vec2f){ state.view.left, state.view.bottom }
);
mgp_vec2f diff = _vec2f_sub(cur_pan_pos, init_pan_pos);
diff = _vec2f_scl(diff, -1.0f);
state.view.left = init_pan_view.left + diff.x;