-
Notifications
You must be signed in to change notification settings - Fork 0
/
bite.c
1601 lines (1424 loc) · 46.6 KB
/
bite.c
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
#include "bite.h"
#if defined(_WIN32)
#include <windows.h>
#ifndef WINDOWS_LEAN_AND_MEAN
#define WINDOWS_LEAN_AND_MEAN 1
#endif
#include <gl/GL.h>
#include <gl/GLU.h>
#include <tchar.h>
#elif defined(__EMSCRIPTEN__)
#include <GLES2/gl2.h>
#include <emscripten.h>
#include <emscripten/html5.h>
#include <time.h>
#else
#include <GL/gl.h>
#include <GL/glx.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <dlfcn.h>
#include <time.h>
#include <unistd.h>
#endif
#if defined(_WIN32)
char keys[512] = {
[VK_BACK] = BITEK_BACKSPACE,
[VK_TAB] = BITEK_TAB,
[VK_RETURN] = BITEK_RETURN,
[VK_SHIFT] = BITEK_SHIFT,
[VK_CONTROL] = BITEK_CONTROL,
[VK_ESCAPE] = BITEK_ESCAPE,
[VK_SPACE] = BITEK_SPACE,
[VK_PRIOR] = BITEK_PAGEUP,
[VK_NEXT] = BITEK_PAGEDOWN,
[VK_LEFT] = BITEK_LEFT,
[VK_UP] = BITEK_UP,
[VK_RIGHT] = BITEK_RIGHT,
[VK_DOWN] = BITEK_DOWN,
[VK_END] = BITEK_END,
[VK_HOME] = BITEK_HOME,
[VK_INSERT] = BITEK_INSERT,
[VK_DELETE] = BITEK_DELETE,
[0x30] = BITEK_0,
[0x31] = BITEK_1,
[0x32] = BITEK_2,
[0x33] = BITEK_3,
[0x34] = BITEK_4,
[0x35] = BITEK_5,
[0x36] = BITEK_6,
[0x37] = BITEK_7,
[0x38] = BITEK_8,
[0x39] = BITEK_9,
[0x41] = BITEK_A,
[0x42] = BITEK_B,
[0x43] = BITEK_C,
[0x44] = BITEK_D,
[0x45] = BITEK_E,
[0x46] = BITEK_F,
[0x47] = BITEK_G,
[0x48] = BITEK_H,
[0x49] = BITEK_I,
[0x4A] = BITEK_J,
[0x4B] = BITEK_K,
[0x4C] = BITEK_L,
[0x4D] = BITEK_M,
[0x4E] = BITEK_N,
[0x4F] = BITEK_O,
[0x50] = BITEK_P,
[0x51] = BITEK_Q,
[0x52] = BITEK_R,
[0x53] = BITEK_S,
[0x54] = BITEK_T,
[0x55] = BITEK_U,
[0x56] = BITEK_V,
[0x57] = BITEK_W,
[0x58] = BITEK_X,
[0x59] = BITEK_Y,
[0x5A] = BITEK_Z,
};
#else
char keys[] = {
[XK_BackSpace] = BITEK_BACKSPACE,
[XK_Tab] = BITEK_TAB,
[XK_Return] = BITEK_RETURN,
[XK_Escape] = BITEK_ESCAPE,
[XK_Delete] = BITEK_DELETE,
[XK_space] = BITEK_SPACE,
[XK_Alt_L] = BITEK_LALT,
[XK_Alt_R] = BITEK_RALT,
[XK_Shift_L] = BITEK_LSHIFT,
[XK_Shift_R] = BITEK_RSHIFT,
[XK_Control_L] = BITEK_LCONTROL,
[XK_Control_R] = BITEK_RCONTROL,
[XK_Super_L] = BITEK_LSUPER,
[XK_Super_R] = BITEK_RSUPER,
[XK_Left] = BITEK_LEFT,
[XK_Up] = BITEK_UP,
[XK_Right] = BITEK_RIGHT,
[XK_Down] = BITEK_DOWN,
[XK_Page_Up] = BITEK_PAGEUP,
[XK_Page_Down] = BITEK_PAGEDOWN,
[XK_End] = BITEK_END,
[XK_Home] = BITEK_HOME,
[XK_0] = BITEK_0,
[XK_1] = BITEK_1,
[XK_2] = BITEK_2,
[XK_3] = BITEK_3,
[XK_4] = BITEK_4,
[XK_5] = BITEK_5,
[XK_6] = BITEK_6,
[XK_7] = BITEK_7,
[XK_8] = BITEK_8,
[XK_9] = BITEK_9,
[XK_A] = BITEK_A,
[XK_B] = BITEK_B,
[XK_C] = BITEK_C,
[XK_D] = BITEK_D,
[XK_E] = BITEK_E,
[XK_F] = BITEK_F,
[XK_G] = BITEK_G,
[XK_H] = BITEK_H,
[XK_I] = BITEK_I,
[XK_J] = BITEK_J,
[XK_K] = BITEK_K,
[XK_L] = BITEK_L,
[XK_M] = BITEK_M,
[XK_N] = BITEK_N,
[XK_O] = BITEK_O,
[XK_P] = BITEK_P,
[XK_Q] = BITEK_Q,
[XK_R] = BITEK_R,
[XK_S] = BITEK_S,
[XK_T] = BITEK_T,
[XK_U] = BITEK_U,
[XK_V] = BITEK_V,
[XK_W] = BITEK_W,
[XK_X] = BITEK_X,
[XK_Y] = BITEK_Y,
[XK_Z] = BITEK_Z,
[XK_a] = BITEK_A,
[XK_b] = BITEK_B,
[XK_c] = BITEK_C,
[XK_d] = BITEK_D,
[XK_e] = BITEK_E,
[XK_f] = BITEK_F,
[XK_g] = BITEK_G,
[XK_h] = BITEK_H,
[XK_i] = BITEK_I,
[XK_j] = BITEK_J,
[XK_k] = BITEK_K,
[XK_l] = BITEK_L,
[XK_m] = BITEK_M,
[XK_n] = BITEK_N,
[XK_o] = BITEK_O,
[XK_p] = BITEK_P,
[XK_q] = BITEK_Q,
[XK_r] = BITEK_R,
[XK_s] = BITEK_S,
[XK_t] = BITEK_T,
[XK_u] = BITEK_U,
[XK_v] = BITEK_V,
[XK_w] = BITEK_W,
[XK_x] = BITEK_X,
[XK_y] = BITEK_Y,
[XK_z] = BITEK_Z,
[XK_KP_0] = BITEK_NUMPAD0,
[XK_KP_1] = BITEK_NUMPAD1,
[XK_KP_2] = BITEK_NUMPAD2,
[XK_KP_3] = BITEK_NUMPAD3,
[XK_KP_4] = BITEK_NUMPAD4,
[XK_KP_5] = BITEK_NUMPAD5,
[XK_KP_6] = BITEK_NUMPAD6,
[XK_KP_7] = BITEK_NUMPAD7,
[XK_KP_8] = BITEK_NUMPAD8,
[XK_KP_9] = BITEK_NUMPAD9,
[XK_F1] = BITEK_F1,
[XK_F2] = BITEK_F2,
[XK_F3] = BITEK_F3,
[XK_F4] = BITEK_F4,
[XK_F5] = BITEK_F5,
[XK_F6] = BITEK_F6,
[XK_F7] = BITEK_F7,
[XK_F8] = BITEK_F8,
[XK_F9] = BITEK_F9,
[XK_F10] = BITEK_F10,
[XK_F11] = BITEK_F11,
[XK_F12] = BITEK_F12,
[XK_minus] = BITEK_MINUS,
[XK_plus] = BITEK_PLUS,
[XK_less] = BITEK_LESS,
[XK_equal] = BITEK_EQUAL,
[XK_greater] = BITEK_GREATER,
};
#endif
struct be_Shader {
GLuint handle;
GLint world_uniform, modelview_uniform;
};
struct be_Texture {
GLuint handle;
int width, height;
int filter[2];
int wrap[2];
};
struct be_Framebuffer {
GLuint handle;
be_Texture texture;
};
struct be_Window {
int x, y;
int width, height;
#if defined(_WIN32)
HWND handle;
HDC dev_context;
#elif defined(__EMSCRIPTEN__)
#else
Display *display;
Window handle;
Colormap map;
GLXFBConfig fbconf;
#endif
};
typedef struct be_Render be_Render;
struct be_Render {
#if defined(_WIN32)
HGLRC gl_context;
#elif defined(__EMSCRIPTEN__)
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE gl_context;
#else
GLXContext gl_context;
#endif
int mode;
GLuint vao, vbo;
};
struct be_Context {
int running;
be_Window window;
be_Render render;
be_EventCallback callbacks[BITE_EVENTS];
};
static be_Context _context;
static const be_Config _conf = {.window = {.title = "bitEngine " BITE_VERSION,
.width = 640,
.height = 380,
.flags = 0}};
#if defined(_WIN32)
typedef HGLRC WINAPI wglCreateContextAttribsARBProc(HDC hdc,
HGLRC hShareContext,
const int *attribList);
wglCreateContextAttribsARBProc *wglCreateContextAttribsARB;
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
#define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
typedef BOOL WINAPI wglChoosePixelFormatARBProc(
HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList,
UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
wglChoosePixelFormatARBProc *wglChoosePixelFormatARB;
// See
// https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_pixel_format.txt
// for all values
#define WGL_DRAW_TO_WINDOW_ARB 0x2001
#define WGL_ACCELERATION_ARB 0x2003
#define WGL_SUPPORT_OPENGL_ARB 0x2010
#define WGL_DOUBLE_BUFFER_ARB 0x2011
#define WGL_PIXEL_TYPE_ARB 0x2013
#define WGL_COLOR_BITS_ARB 0x2014
#define WGL_DEPTH_BITS_ARB 0x2022
#define WGL_STENCIL_BITS_ARB 0x2023
#define WGL_FULL_ACCELERATION_ARB 0x2027
#define WGL_TYPE_RGBA_ARB 0x202B
#elif defined(__EMSCRIPTEN__)
#else
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display *, GLXFBConfig,
GLXContext, Bool,
const int *);
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#endif
#define DEFINE_GL(ret, name, ...) \
typedef ret name##Proc(__VA_ARGS__); \
static name##Proc *name = 0
#if !defined(__EMSCRIPTEN__)
#define GL_VERSION 0x1F02
#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
/* data types */
#define GL_BYTE 0x1400
#define GL_UNSIGNED_BYTE 0x1401
#define GL_SHORT 0x1402
#define GL_UNSIGNED_SHORT 0x1403
#define GL_INT 0x1404
#define GL_UNSIGNED_INT 0x1405
#define GL_FLOAT 0x1406
#define GL_2_BYTES 0x1407
#define GL_3_BYTES 0x1408
#define GL_4_BYTES 0x1409
#define GL_DOUBLE 0x140A
/* Clear buffer bits */
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_ACCUM_BUFFER_BIT 0x00000200
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_COLOR_BUFFER_BIT 0x00004000
#define GL_RGB 0x1907
#define GL_RGBA 0x1908
/* bgra */
#define GL_BGR 0x80E0
#define GL_BGRA 0x80E1
/* Primitives */
#define GL_POINTS 0x0000
#define GL_LINES 0x0001
#define GL_TRIANGLES 0x0004
#define GL_ARRAY_BUFFER 0x8892
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
#define GL_STREAM_DRAW 0x88E0
#define GL_STREAM_READ 0x88E1
#define GL_STREAM_COPY 0x88E2
#define GL_STATIC_DRAW 0x88E4
#define GL_STATIC_READ 0x88E5
#define GL_STATIC_COPY 0x88E6
#define GL_DYNAMIC_DRAW 0x88E8
#define GL_DYNAMIC_READ 0x88E9
#define GL_DYNAMIC_COPY 0x88EA
#define GL_TEXTURE_2D 0x0DE1
// #define GL_TEXTURE_MIN_FILTER 0x2800
// #define GL_TEXTURE_MAG_FILTER 0x2801
#define GL_TEXTURE_WRAP_S 0x2802
#define GL_TEXTURE_WRAP_T 0x2803
#define GL_NEAREST 0x2600
#define GL_REPEAT 0x2901
#define GL_CLAMP 0x2900
#define GL_CLAMP_TO_EDGE 0x812F /* 1.2 */
#define GL_CLAMP_TO_BORDER 0x812D /* 1.3 */
// Core
// DEFINE_GL(void, glClearColor, float, float, float, float);
// DEFINE_GL(void, glClear, GLenum);
// Transformation
// DEFINE_GL(void, glViewport, GLint, GLint, GLint, GLint);
// VAO
DEFINE_GL(void, glGenVertexArrays, GLsizei, GLuint *); // glGenVertexArrays
DEFINE_GL(void, glBindVertexArray, GLuint); // glBindVertexArray
DEFINE_GL(void, glDeleteVertexArrays, GLsizei,
GLuint *); // glDeleteVertexArrays
DEFINE_GL(void, glVertexAttribPointer, GLuint, GLint, GLuint, GLint, GLint,
const void *);
DEFINE_GL(void, glEnableVertexAttribArray, GLuint);
DEFINE_GL(void, glDisableVertexAttribArray, GLuint);
// Buffers
DEFINE_GL(void, glGenBuffers, GLsizei, GLuint *); // glGenBuffers
DEFINE_GL(void, glBindBuffer, GLenum, GLuint); // glBindBuffer
DEFINE_GL(void, glDeleteBuffers, GLsizei, GLuint *); // glDeleteBuffers
DEFINE_GL(void, glBufferData, GLenum, GLsizei, const void *,
GLenum); // glBufferData(GLenum target, GLsizeptr size, const void*
// data, GLenum usage)
DEFINE_GL(void, glBufferSubData, GLenum, GLsizei, GLsizei,
const void *); // glBufferSubData(GLenum target, GLintptr offset,
// GLsizeiptr, size, const void* data)
// DEFINE_GL(void, glDrawArrays, GLenum, GLint, GLsizei); // glDrawArrays
// SHADERS
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_VERTEX_SHADER 0x8B31
#define GL_COMPILE_STATUS 0x8B81
#define GL_LINK_STATUS 0x8B82
DEFINE_GL(GLuint, glCreateShader, GLenum); // glCreateShader
DEFINE_GL(void, glShaderSource, GLuint, GLsizei, const char **,
const GLint *); // glShaderSource
DEFINE_GL(void, glCompileShader, GLuint); // glCompileShader
#define GL_LINK_STATUS 0x8B82
DEFINE_GL(void, glGetShaderiv, GLuint, GLenum, GLint *); // glGetShaderiv
const int t = GL_LINK_STATUS;
DEFINE_GL(void, glGetShaderInfoLog, GLuint, GLsizei, GLsizei *,
char *); // glGetShaderInfoLog
DEFINE_GL(void, glDeleteShader, GLuint); // glDeleteShader
DEFINE_GL(GLuint, glCreateProgram, void); // glCreateProgram
DEFINE_GL(void, glAttachShader, GLuint, GLuint); // glAttachShader
DEFINE_GL(void, glLinkProgram, GLuint); // glLinkProgram
DEFINE_GL(void, glGetProgramiv, GLuint, GLenum, GLint *); // glGetProgramiv
DEFINE_GL(void, glGetProgramInfoLog, GLuint, GLsizei, GLsizei *,
char *); // glGetProgramInfoLog
DEFINE_GL(void, glUseProgram, GLuint); // glUseProgram
DEFINE_GL(void, glDeleteProgram, GLuint); // glUseProgram
DEFINE_GL(void, glGetActiveUniform, GLuint, GLuint, GLint, GLint *, GLint *,
GLint *, char *);
DEFINE_GL(GLint, glGetUniformLocation, GLuint, const char *);
#define DEFINE_GL_UNIFORM(X, T) \
DEFINE_GL(void, glUniform1##X, GLuint, T); \
DEFINE_GL(void, glUniform2##X, GLint, T, T); \
DEFINE_GL(void, glUniform3##X, GLint, T, T, T); \
DEFINE_GL(void, glUniform4##X, GLint, T, T, T, T); \
DEFINE_GL(void, glUniform1##X##v, GLint, GLint, const T *); \
DEFINE_GL(void, glUniform2##X##v, GLint, GLint, const T *); \
DEFINE_GL(void, glUniform3##X##v, GLint, GLint, const T *); \
DEFINE_GL(void, glUniform4##X##v, GLint, GLint, const T *)
// DEFINE_GL_UNIFORM(f, float);
// DEFINE_GL_UNIFORM(i, GLint);
DEFINE_GL(void, glUniformMatrix2fv, GLint, GLint, GLint, const float *);
DEFINE_GL(void, glUniformMatrix3fv, GLint, GLint, GLint, const float *);
DEFINE_GL(void, glUniformMatrix4fv, GLint, GLint, GLint, const float *);
DEFINE_GL(GLint, glGetAttribLocation, GLuint, const char *);
DEFINE_GL(void, glGetActiveAttribe, GLuint, GLuint, GLint, GLint *, GLint *,
GLuint *, char *);
DEFINE_GL(void, glBindAttribLocation, GLuint, GLuint, const char *);
#if defined(_WIN32)
static HMODULE _gl_sym;
#elif defined(__EMSCRIPTEN__)
#else
static void *_gl_sym;
#ifndef RTLD_LAZY
#define RTLD_LAZY 0x00001
#endif
#ifndef RTLD_GLOBAL
#define RTLD_GLOBAL 0x00100
#endif
#endif
static void *_get_proc(const char *name);
BITE_RESULT _load_gl(void);
void _setup_gl(void);
void _close_gl(void);
#endif
BITE_RESULT _init_context(be_Context *ctx, const be_Config *conf);
BITE_RESULT _init_window(be_Window *window, const be_Config *conf);
BITE_RESULT _init_render(be_Render *render, const be_Window *window,
const be_Config *conf);
void _poll_events(be_Context *ctx);
#if defined(_WIN32)
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
be_Context *ctx = (be_Context *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
be_Event e;
e.type = 0;
be_EventCallback fn = NULL;
switch (msg) {
case WM_CREATE: return 0;
case WM_CLOSE: {
e.type = BITE_WINDOW_CLOSE;
} break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSKEYDOWN:
case WM_KEYDOWN: {
e.type = BITE_KEY_PRESSED;
e.key.keycode = keys[(int)wParam];
} break;
case WM_SYSKEYUP:
case WM_KEYUP: {
e.type = BITE_KEY_RELEASED;
e.key.keycode = keys[(int)wParam];
printf("KeyPressed: %d\n", e.key.keycode);
} break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
if (ctx)
fn = ctx->callbacks[e.type];
if (fn)
fn(ctx, &e);
printf("Teste\n");
return 0;
}
#endif
void bite_simple_triangle(be_Context *ctx) {
glClearColor(0.3f, 0.4f, 0.4f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
// fprintf(stdout, "VAO: %d\n", ctx->render.vao);
#if !defined(__EMSCRIPTEN__)
glBindVertexArray(ctx->render.vao);
#else
glBindBuffer(GL_ARRAY_BUFFER, ctx->render.vbo);
#endif
glDrawArrays(GL_TRIANGLES, 0, 3);
#if !defined(__EMSCRIPTEN__)
glBindVertexArray(0);
#else
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
// glBegin(GL_TRIANGLES);
// glColor3f(1.f, 0.f, 0.f);
// glVertex2f(0.f, 0.5f);
// glColor3f(0.f, 1.f, 0.f);
// glVertex2f(-0.5f, -0.5f);
// glColor3f(0.f, 0.f, 1.f);
// glVertex2f(0.5f, -0.5f);
// glEnd();
}
be_Config bite_init_config(const char *title, int w, int h) {
be_Config c = {0};
title = title != NULL ? title : "bitEngine " BITE_VERSION;
size_t len = strlen(title);
memcpy(c.window.title, title, len);
c.window.width = w;
c.window.height = h;
return c;
}
be_Context *bite_create(const be_Config *conf) {
conf = conf ? conf : &(_conf);
be_Context *ctx = malloc(sizeof(*ctx));
if (!ctx) {
fprintf(stderr, "Failed to alloc memory for context\n");
return NULL;
}
int mag, min;
// glGetIntegerv(GL_MAJOR_VERSION, &mag);
// glGetIntegerv(GL_MINOR_VERSION, &min);
// printf("OpenGL loaded: %d.%d\n", mag, min);
ctx->running = 1;
for (int i = 0; i < BITE_EVENTS; i++) {
ctx->callbacks[i] = NULL;
}
if (_init_context(ctx, conf) < 0) {
free(ctx);
return NULL;
}
const char *ver = (const char*)glGetString(GL_VERSION);
printf("OpenGL loaded: %s\n", ver);
return ctx;
}
void bite_destroy(be_Context *ctx) {
if (!ctx)
return;
be_Window *window = &(ctx->window);
be_Render *render = &(ctx->render);
#if defined(_WIN32)
wglMakeCurrent(NULL, NULL);
wglDeleteContext(render->gl_context);
ReleaseDC(window->handle, window->dev_context);
DestroyWindow(window->handle);
#elif defined(__EMSCRIPTEN__)
emscripten_webgl_destroy_context(render->gl_context);
#else
glXDestroyContext(window->display, render->gl_context);
XDestroyWindow(window->display, window->handle);
XCloseDisplay(window->display);
#endif
free(ctx);
}
void bite_register_callback(be_Context *ctx, int type,
be_EventCallback callback) {
if (!ctx)
return;
if (type < BITE_QUIT || type >= BITE_EVENTS)
return;
ctx->callbacks[type] = callback;
}
int bite_should_close(be_Context *ctx) {
if (!ctx)
return 1;
return !(ctx->running);
}
void bite_set_should_close(be_Context *ctx, int should_close) {
if (!ctx)
return;
ctx->running = !should_close;
}
void bite_poll_events(be_Context *ctx) {
if (!ctx)
return;
_poll_events(ctx);
}
void bite_swap(be_Context *ctx) {
if (!ctx)
return;
be_Window *window = &(ctx->window);
#if defined(_WIN32)
SwapBuffers(window->dev_context);
#elif defined(__EMSCRIPTEN__)
emscripten_webgl_commit_frame();
#else
glXSwapBuffers(window->display, window->handle);
#endif
}
/*********************
* Window
*********************/
void bite_set_window_width(be_Context *ctx, int width) {}
int bite_get_window_width(be_Context *ctx) {
if (!ctx)
return -1;
return ctx->window.width;
}
/*********************
* Render
*********************/
static GLuint _compile_shader(GLenum type, const char *src) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char info_log[512];
glGetShaderInfoLog(shader, 512, NULL, info_log);
fprintf(stderr, "Failed to compile shader: %s\n", info_log);
glDeleteShader(shader);
return 0;
}
return shader;
}
static GLuint _create_program(GLuint vert, GLuint frag) {
#if 1
GLuint program = glCreateProgram();
glAttachShader(program, vert);
glAttachShader(program, frag);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char info_log[512];
glGetProgramInfoLog(program, 512, NULL, info_log);
fprintf(stderr, "Failed to link program: %s\n", info_log);
glDeleteProgram(program);
return 0;
}
return program;
#endif
return 0;
}
be_Texture *bite_create_texture(int width, int height, int format, void *data) {
be_Texture *texture = (be_Texture *)malloc(sizeof(*texture));
if (!texture) {
fprintf(stderr, "Failed to alloc memory for texture\n");
return NULL;
}
texture->filter[0] = GL_NEAREST;
texture->filter[1] = GL_NEAREST;
texture->wrap[0] = GL_CLAMP_TO_EDGE;
texture->wrap[1] = GL_CLAMP_TO_EDGE;
texture->width = width;
texture->height = height;
glGenTextures(1, &(texture->handle));
glBindTexture(GL_TEXTURE_2D, texture->handle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texture->filter[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture->filter[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texture->wrap[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texture->wrap[1]);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
be_Shader *bite_create_shader(const char *vert_src, const char *frag_src) {
be_Shader *shader = NULL;
GLuint vert, frag;
vert = _compile_shader(GL_VERTEX_SHADER, vert_src);
if (vert <= 0)
return shader;
frag = _compile_shader(GL_FRAGMENT_SHADER, frag_src);
if (frag <= 0)
return shader;
GLuint program = _create_program(vert, frag);
if (program <= 0)
return shader;
shader = malloc(sizeof(*shader));
shader->handle = program;
return shader;
}
void bite_render_clear_color(float r, float g, float b, float a) {
glClearColor(r, g, b, a);
}
void bite_render_clear(void) { glClear(GL_COLOR_BUFFER_BIT); }
void bite_bind_framebuffer(be_Framebuffer *fbo) {
#if 0
if (!fbo) glBindFramebuffer(GL_FRAMEBUFFER, 0);
else glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
#endif
}
void bite_bind_texture(be_Texture *tex) {
if (!tex)
glBindTexture(GL_TEXTURE_2D, 0);
else
glBindTexture(GL_TEXTURE_2D, tex->handle);
}
void bite_use_shader(be_Shader *shader) {
if (!shader)
glUseProgram(0);
else
glUseProgram(shader->handle);
}
/*********************
* Timer
*********************/
void bite_sleep(be_u64 ms) {
#if defined(_WIN32)
Sleep(ms);
#elif defined(__EMSCRIPTEN__)
emscripten_sleep((be_u32)ms);
#else
sleep(ms / 1000);
#endif
}
be_u64 bite_tick(void) {
be_u64 tick = 0;
#if defined(_WIN32)
LARGE_INTEGER freq, count;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&count);
tick = (be_u64)(count.QuadPart * 1000.0 / freq.QuadPart);
#else
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
tick = time.tv_sec * 1000 + (time.tv_nsec / 1000000);
#endif
return tick;
}
/*********************
* Internal
*********************/
// static void init_opengl_extensions(be_Context* ctx);
#if defined(_WIN32)
static void init_opengl_extensions(void) {
WNDCLASSA window_class = {.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
.lpfnWndProc = DefWindowProcA,
.hInstance = GetModuleHandle(0),
.lpszClassName = "Dummy_WGL_window"};
if (!RegisterClassA(&window_class)) {
fprintf(stderr, "Failed to register dummy OpenGL window\n");
exit(EXIT_FAILURE);
}
HWND dummy_window =
CreateWindowExA(0, window_class.lpszClassName, "Dummy OpenGL Window", 0,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, 0, 0, window_class.hInstance, 0);
if (!dummy_window) {
fprintf(stderr, "Failed to create dummy OpenGL window\n");
exit(EXIT_FAILURE);
}
HDC dummy_dc = GetDC(dummy_window);
PIXELFORMATDESCRIPTOR pfd = {.nSize = sizeof(pfd),
.nVersion = 1,
.iPixelType = PFD_TYPE_RGBA,
.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
.cColorBits = 32,
.cAlphaBits = 8,
.iLayerType = PFD_MAIN_PLANE,
.cDepthBits = 24,
.cStencilBits = 8};
int pixel_format = ChoosePixelFormat(dummy_dc, &pfd);
if (!pixel_format) {
fprintf(stderr, "Failed to find a suitable format\n");
exit(EXIT_FAILURE);
}
if (!SetPixelFormat(dummy_dc, pixel_format, &pfd)) {
fprintf(stderr, "Failed to set the pixel format\n");
exit(EXIT_FAILURE);
}
HGLRC dummy_context = wglCreateContext(dummy_dc);
if (!dummy_context) {
fprintf(stderr, "Failed to create dummy OpenGL context\n");
exit(EXIT_FAILURE);
}
if (!wglMakeCurrent(dummy_dc, dummy_context)) {
fprintf(stderr, "Failed to activate dummy OpenGL context\n");
exit(EXIT_FAILURE);
}
wglCreateContextAttribsARB =
(wglCreateContextAttribsARBProc *)wglGetProcAddress(
"wglCreateContextAttribsARB");
wglChoosePixelFormatARB = (wglChoosePixelFormatARBProc *)wglGetProcAddress(
"wglChoosePixelFormatARB");
printf("functions: %p %p\n", wglCreateContextAttribsARB,
wglChoosePixelFormatARB);
wglMakeCurrent(dummy_dc, 0);
wglDeleteContext(dummy_context);
ReleaseDC(dummy_window, dummy_dc);
DestroyWindow(dummy_window);
}
#else
static BITE_BOOL is_extension_supported(const char *extList,
const char *extension) {
const char *start;
const char *where, *terminator;
/* Extension names should not have spaces. */
where = strchr(extension, ' ');
if (where || *extension == '\0')
return BITE_FALSE;
/* It takes a bit of care to be fool-proof about parsing the
OpenGL extensions string. Don't be fooled by sub-strings,
etc. */
for (start = extList;;) {
where = strstr(start, extension);
if (!where)
break;
terminator = where + strlen(extension);
if (where == start || *(where - 1) == ' ')
if (*terminator == ' ' || *terminator == '\0')
return BITE_TRUE;
start = terminator;
}
return BITE_FALSE;
}
#endif
BITE_RESULT _init_context(be_Context *ctx, const be_Config *conf) {
if (_init_window(&(ctx->window), conf) != BITE_OK)
return BITE_ERROR;
#if defined(_WIN32)
SetWindowLongPtr(ctx->window.handle, GWLP_USERDATA, (LONG_PTR)ctx);
#endif
if (_init_render(&(ctx->render), &(ctx->window), conf) != BITE_OK)
return BITE_ERROR;
be_Render *render = &(ctx->render);
float vertices[] = {0.f, 0.5f, 1.f, 0.f, 0.f, 1.f, -0.5f, -0.5f, 0.f,
1.f, 0.f, 1.f, 0.5f, -0.5f, 0.f, 0.f, 1.f, 1.f};
#if !defined(__EMSCRIPTEN__)
glGenVertexArrays(1, &(render->vao));
glBindVertexArray(render->vao);
#endif
glGenBuffers(1, &(render->vbo));
glBindBuffer(GL_ARRAY_BUFFER, render->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
(void *)(2 * sizeof(float)));
// glBindBuffer(GL_ARRAY_BUFFER, 0);
#if !defined(__EMSCRIPTEN__)
glBindVertexArray(0);
#endif
return BITE_OK;
}
BITE_RESULT _init_window(be_Window *window, const be_Config *conf) {
#if defined(_WIN32)
HWND handle;
HMODULE hInstance = GetModuleHandle(0);
const TCHAR windowClass[] = _T("bitEngine");
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = windowClass;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Call to RegisterClassEx failed"), _T("bitEngine"),
NULL);
return BITE_ERROR;
}
handle = CreateWindow(windowClass, conf->window.title, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, conf->window.width,
conf->window.height, NULL, NULL, hInstance, NULL);
if (!handle) {
MessageBox(NULL, _T("Class to CreateWindow failed"), _T("bitEngine"), NULL);
return BITE_ERROR;
}
window->handle = handle;
window->dev_context = GetDC(handle);
ShowWindow(handle, SW_SHOWDEFAULT);
UpdateWindow(handle);
#elif (__EMSCRIPTEN__)
#else
Display *dpy;
Window handle;
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "Could not open X11 display\n");
return BITE_ERROR;
}
int scrId = DefaultScreen(dpy);
static int visual_attribs[] = {
GLX_X_RENDERABLE, True, GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8, GLX_DOUBLEBUFFER, True,
// GLX_SAMPLE_BUFFERS , 1,
// GLX_SAMPLES , 4,
None};
GLint major, minor;
glXQueryVersion(dpy, &major, &minor);
if ((major == 1 && minor < 3) || (major < 1)) {
fprintf(stderr, "Invalid GLX version\n");