+76
-1
bite.c
+76
-1
bite.c
···
49
49
50
50
static be_Context _context;
51
51
52
+
static const be_Config _conf = {
53
+
.window = {
54
+
.title = "bitEngine "BITE_VERSION,
55
+
.width = 640, .height = 380,
56
+
.flags = 0
57
+
}
58
+
};
59
+
52
60
int _init_context(be_Context* ctx, const be_Config* conf);
53
61
void _poll_events(be_Context* ctx);
54
62
···
99
107
100
108
be_Config bite_init_config(const char* title, int w, int h) {
101
109
be_Config c = {0};
102
-
title = title != NULL ? title : "bitEngine";
110
+
title = title != NULL ? title : "bitEngine "BITE_VERSION;
111
+
int len = strlen(title);
103
112
strcpy(c.window.title, title);
104
113
c.window.width = w;
105
114
c.window.height = h;
···
107
116
}
108
117
109
118
be_Context* bite_create(const be_Config* conf) {
119
+
conf = conf ? conf : &(_conf);
110
120
be_Context* ctx = malloc(sizeof(*ctx));
111
121
if (!ctx) {
112
122
fprintf(stderr, "Failed to alloc memory for context\n");
···
187
197
return ctx->window.width;
188
198
}
189
199
200
+
/*********************
201
+
* Render
202
+
*********************/
203
+
204
+
static GLuint _create_program(GLuint vert, GLuint frag) {
205
+
GLuint program = glCreateProgram();
206
+
glAttachShader(program, vert);
207
+
glAttachShader(program, frag);
208
+
glLinkProgram(program);
209
+
210
+
int success;
211
+
glGetProgramiv(program, GL_LINK_STATUS, &success);
212
+
if (!success) {}
213
+
214
+
return program;
215
+
}
216
+
217
+
be_Shader* bite_create_shader(const char* vert, const char* frag) {
218
+
be_Shader* shader = NULL;
219
+
GLuint program;
220
+
GLuint vert, frag;
221
+
222
+
return shader;
223
+
}
224
+
225
+
void bite_render_clear_color(float r, float g, float b, float a) {
226
+
glClearColor(r, g, b, a);
227
+
}
228
+
229
+
void bite_render_clear(void) {
230
+
glClear(GL_COLOR_BUFFER_BIT);
231
+
}
232
+
233
+
struct be_Texture {
234
+
GLuint handle;
235
+
int width, height;
236
+
int filter[2];
237
+
int wrap[2];
238
+
};
239
+
240
+
struct be_Framebuffer {
241
+
GLuint handle;
242
+
be_Texture* texture;
243
+
};
244
+
245
+
struct be_Shader {
246
+
GLuint handle;
247
+
int world_uniform;
248
+
int modelview_uniform;
249
+
};
250
+
251
+
void bite_bind_framebuffer(be_Framebuffer* fbo) {
252
+
if (!fbo) glBindFramebuffer(GL_FRAMEBUFFER, 0);
253
+
else glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
254
+
}
255
+
256
+
void bite_bind_texture(be_Texture* tex) {
257
+
if (!tex) glBindTexture(GL_TEXTURE_2D, 0);
258
+
else glBindTexture(GL_TEXTURE_2D, tex->handle);
259
+
}
260
+
261
+
void bite_use_shader(be_Shader* shader) {
262
+
if (!shader) glUseProgram(0);
263
+
else glUseProgram(shader->handle);
264
+
}
190
265
191
266
/*********************
192
267
* Timer
+42
-1
bite.h
+42
-1
bite.h
···
13
13
typedef unsigned int be_u32;
14
14
typedef unsigned long int be_u64;
15
15
16
+
typedef struct {float x, y, w, h; } be_Rect;
17
+
16
18
typedef struct be_Context be_Context;
17
19
18
20
typedef struct be_Window be_Window;
···
24
26
typedef struct be_Font be_Font;
25
27
26
28
typedef void(*be_EventCallback)(be_Context*, be_Event*);
29
+
30
+
#define BITE_TRUE 1
31
+
#define BITE_FALSE 0
32
+
33
+
#define BITE_OK 0
34
+
#define BITE_ERROR -1
35
+
36
+
#define BITE_RESULT int
37
+
#define BITE_BOOL be_u8
27
38
28
39
enum {
29
40
BITE_NONE = 0,
···
52
63
BITEK_LEFT
53
64
};
54
65
66
+
enum {
67
+
BITE_POINTS = 0,
68
+
BITE_LINES,
69
+
BITE_TRIANGLES
70
+
};
71
+
55
72
struct be_Event {
56
73
int type;
57
74
union {
···
92
109
93
110
BITE_API void bite_register_callback(be_Context* ctx, int type, be_EventCallback callback);
94
111
95
-
BITE_API int bite_should_close(be_Context* ctx);
112
+
BITE_API BITE_BOOL bite_should_close(be_Context* ctx);
96
113
BITE_API void bite_set_should_close(be_Context* ctx, int should_close);
97
114
98
115
BITE_API void bite_poll_events(be_Context* ctx);
···
116
133
* Render
117
134
*********************/
118
135
136
+
BITE_API be_Shader* bite_create_shader(const char* vert, const char* src);
137
+
BITE_API void bite_destroy_shader(be_Shader* shader);
138
+
139
+
BITE_API be_Texture* bite_create_texture(int w, int h, int format, void* data);
140
+
BITE_API void bite_destroy_texture(be_Texture* texture);
141
+
142
+
BITE_API be_Framebuffer* bite_create_framebuffer(void);
143
+
BITE_API void bite_destroy_framebuffer(be_Framebuffer* fbo);
144
+
BITE_API BITE_RESULT bite_framebuffer_attach_texture(be_Framebuffer* fbo, be_Texture* texture);
145
+
146
+
BITE_API void bite_render_clear_color(float r, float g, float b, float a);
119
147
BITE_API void bite_render_clear(void);
148
+
149
+
BITE_API void bite_bind_framebuffer(be_Framebuffer* fbo);
150
+
BITE_API void bite_bind_texture(be_Texture* tex);
151
+
BITE_API void bite_use_shader(be_Shader* shader);
152
+
153
+
BITE_API void bite_render_point(float x, float y);
154
+
BITE_API void bite_render_line(float x0, float y0, float x1, float y1);
155
+
BITE_API void bite_render_rect(be_Rect* rect);
156
+
BITE_API void bite_render_rectangle(float x, float y, float w, float h);
157
+
BITE_API void bite_render_circle(float x, float y, float radius);
158
+
BITE_API void bite_render_triangle(float x0, float y0, float x1, float y1, float x2, float y2);
159
+
160
+
BITE_API void bite_render_texture(be_Texture* tex, be_Rect* src, be_Rect* dest);
120
161
121
162
/*********************
122
163
* Timer
-10
main.c
-10
main.c
···
19
19
// printf("Entering render function\n");
20
20
be_Context* ctx = (be_Context*)data;
21
21
bite_poll_events(ctx);
22
-
// glClearColor(0.3f, 0.4f, 0.4f, 1.f);
23
-
// glClear(GL_COLOR_BUFFER_BIT);
24
-
// glBegin(GL_TRIANGLES);
25
-
// glColor3f(1.f, 0.f, 0.f);
26
-
// glVertex2f(0.f, 0.5f);
27
-
// glColor3f(0.f, 1.f, 0.f);
28
-
// glVertex2f(-0.5f, -0.5f);
29
-
// glColor3f(0.f, 0.f, 1.f);
30
-
// glVertex2f(0.5f, -0.5f);
31
-
// glEnd();
32
22
bite_simple_triangle();
33
23
bite_swap(ctx);
34
24
}