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 | bool running = true;
#include "assert.h"
#include "cj_engine.h"
GLenum err = 0;
bool init = false;
struct RENDERER
{
u32 VAO;
};
struct CJ_VTX_QUAD
{
V2f pos;
V4f col;
V2f tCoord;
};
struct TILEMAP
{
u32 w;
u32 h;
ENTITY *entity;
};
struct GAME_HANDLER
{
ENTITY entity_batch[1000];
u32 entity_used;
u32 entity_id;
CJ_VTX_QUAD *pVertex;
u32 vtx_id;
TILEMAP tilemap;
ENTITY *player;
ENTITY *monster;
ENTITY *camera;
};
struct CJ_VBO
{
u32 id;
u8 *base;
u32 used;
u32 size;
};
RENDERER CreateRenderer()
{
RENDERER renderer = {};
glGenVertexArrays(1, &renderer.VAO);
glBindVertexArray(renderer.VAO);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return renderer;
}
struct CJ_VTX_ATTRIB
{
u32 size;
GLenum type;
u32 stride;
u32 offset;
};
u32 CreateText(CJ_PLATFORM *platform, CJ_VBO *vbo, TEXTUREINFO tex_i, char *str, V2f pos, V4f col);
void InitializeVBO(CJ_VBO *vbo, u8* base, u32 size, u32 n_attribs, CJ_VTX_ATTRIB *vtx_attrib)
{
vbo->base = base;
vbo->size = size;
glGenBuffers(1, &vbo->id);
glBindBuffer(GL_ARRAY_BUFFER, vbo->id);
glBufferData(GL_ARRAY_BUFFER, vbo->size, vbo->base, GL_DYNAMIC_DRAW);
for(u32 i = 0; i < n_attribs; i++)
{
vtx_attrib[i].offset *= sizeof(float);
vtx_attrib[i].stride *= sizeof(float);
glEnableVertexAttribArray(i); glVertexAttribPointer(i, vtx_attrib[i].size, vtx_attrib[i].type, false, vtx_attrib[i].stride, (void*)vtx_attrib[i].offset);
}
}
void InitQuad(CJ_VBO *vbo, u32 index, V2f pos, V2f size)
{
CJ_VTX_QUAD *vertex = (CJ_VTX_QUAD*)vbo->base;
vertex[index + 0].pos = v2f(pos.x, pos.y);
vertex[index + 1].pos = v2f(pos.x, pos.y + size.y);
vertex[index + 2].pos = v2f(pos.x + size.x, pos.y + size.y);
vertex[index + 3].pos = v2f(pos.x + size.x, pos.y);
}
void SetQuad_X(CJ_VBO *vbo, u32 index, float x)
{
CJ_VTX_QUAD *vertex = (CJ_VTX_QUAD*)vbo->base;
vertex[index + 0].pos.x = x;
vertex[index + 1].pos.x = x;
}
void SetQuad_Y(CJ_VBO *vbo, u32 index, float y)
{
CJ_VTX_QUAD *vertex = (CJ_VTX_QUAD*)vbo->base;
vertex[index + 0].pos.y = y;
vertex[index + 1].pos.y = y;
}
void SetQuad_W(CJ_VBO *vbo, u32 index, float w)
{
CJ_VTX_QUAD *vertex = (CJ_VTX_QUAD*)vbo->base;
vertex[index + 2].pos.x = vertex[index + 0].pos.x + w;
vertex[index + 3].pos.x = vertex[index + 0].pos.x + w;
}
void SetQuad_H(CJ_VBO *vbo, u32 index, float h)
{
CJ_VTX_QUAD *vertex = (CJ_VTX_QUAD*)vbo->base;
vertex[index + 2].pos.y = vertex[index + 0].pos.y + h;
vertex[index + 3].pos.y = vertex[index + 0].pos.y + h;
}
void SetQuad_Col(CJ_VBO *vbo, u32 index, V4f col)
{
CJ_VTX_QUAD *vertex = (CJ_VTX_QUAD*)vbo->base;
vertex[index + 0].col = col;
vertex[index + 1].col = col;
vertex[index + 2].col = col;
vertex[index + 3].col = col;
}
ENTITY *PushEntity(GAME_HANDLER *game, u32 size)
{
assert(game->entity_used + size <= ArrayCount(game->entity_batch));
ENTITY *entity = 0;
CJ_VTX_QUAD *vtx = 0;
entity = game->entity_batch + game->entity_used;;
entity->id = game->entity_id;
vtx = game->pVertex + game->vtx_id;
game->vtx_id += 4 * size;
game->entity_used += size;
game->entity_id++;
return entity;
}
void UpdateVBO_Entity(CJ_VBO *vbo, GAME_HANDLER *game)
{
CJ_VTX_QUAD *pVertex = (CJ_VTX_QUAD*)vbo->base;
assert(game->entity_used <= ArrayCount(game->entity_batch));
for(u32 i = 0; i < game->entity_used; i++)
{
V2f pos = game->entity_batch[i].pos;
V2f size = game->entity_batch[i].size;
V4f col = game->entity_batch[i].col;
V2f tCoord_p = game->entity_batch[i].texcoord_pos;
V2f tCoord_sz = game->entity_batch[i].texcoord_size;
pVertex[(i * 4) + 0].pos = v2f(pos.x, pos.y);
pVertex[(i * 4) + 1].pos = v2f(pos.x, pos.y + size.y);
pVertex[(i * 4) + 2].pos = v2f(pos.x + size.x, pos.y + size.y);
pVertex[(i * 4) + 3].pos = v2f(pos.x + size.y, pos.y);
pVertex[(i * 4) + 0].col = col;
pVertex[(i * 4) + 1].col = col;
pVertex[(i * 4) + 2].col = col;
pVertex[(i * 4) + 3].col = col;
pVertex[(i * 4) + 0].tCoord = v2f(tCoord_p.x, tCoord_p.y);
pVertex[(i * 4) + 1].tCoord = v2f(tCoord_p.x, tCoord_p.y + tCoord_sz.y);
pVertex[(i * 4) + 2].tCoord = v2f(tCoord_p.x + tCoord_sz.x, tCoord_p.y + tCoord_sz.y);
pVertex[(i * 4) + 3].tCoord = v2f(tCoord_p.x + tCoord_sz.y, tCoord_p.y);
}
}
void OnGameInit(CJ_PLATFORM *platform, GAME_HANDLER *game)
{
if(!init)
{
u32 tilemap_w = 2;
u32 tilemap_h = 2;
V2f start_pos = v2f(1.0f, 0.0f);
V2f tile_size = v2f(0.11f, 0.11f);
game->tilemap.entity = PushEntity(game, tilemap_w * tilemap_h);
game->tilemap.w = tilemap_w;
game->tilemap.h = tilemap_h;
for(u32 y = 0; y < tilemap_h; y++)
{
for(u32 x = 0; x < tilemap_w; x++)
{
u32 index = x + (y * tilemap_w);
V2f pos = v2f(start_pos.x + (x * tile_size.x), start_pos.y + (y * tile_size.y));
V2f size = v2f(tile_size.x - 0.01f, tile_size.y - 0.01f);
V4f col = v4f(1.0f, 1.0f, 1.0f, 0.2f);
game->tilemap.entity[index].pos = pos;
game->tilemap.entity[index].size = size;
game->tilemap.entity[index].col = col;
game->tilemap.entity[index].texcoord_pos = v2f(0.0f, 0.0f);
game->tilemap.entity[index].texcoord_size = v2f(1.0f, 1.0f);
}
}
game->player = PushEntity(game, 1);
game->player->pos = v2f(0.0f, 0.0f);
game->player->size = v2f(0.3f, 0.3f);
game->player->col = v4f(1.0f, 1.0f, 0.0f, 0.2f);
game->player->texcoord_pos = v2f(0.0f, 0.0f);
game->player->texcoord_size = v2f(1.0f, 1.0f);
game->monster = PushEntity(game, 1);
game->monster->pos = v2f(1.0f, 1.0f);
game->monster->size = v2f(0.2f, 0.2f);
game->monster->texcoord_pos = v2f(0.0f, 0.0f);
game->monster->texcoord_size = v2f(1.0f, 1.0f);
game->monster->col = v4f(1.0f, 1.0f, 0.0f, 0.2f);
game->camera = PushEntity(game, 1);
game->camera->pos = v2f(0.0f, 0.0f);
init = true;
}
}
void OnGameUpdate(CJ_PLATFORM *platform, GAME_HANDLER *game)
{
if(glfwGetKey(platform->window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
running = false;
}
float mov = 0.02f;
game->camera->vel = v2f(0.f, 0.f);
if(glfwGetKey(platform->window, GLFW_KEY_UP) == GLFW_PRESS)
{
game->camera->vel.y = mov;
}
if(glfwGetKey(platform->window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
game->camera->vel.y = -mov;
}
if(glfwGetKey(platform->window, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
game->camera->vel.x = mov;
}
if(glfwGetKey(platform->window, GLFW_KEY_LEFT) == GLFW_PRESS)
{
game->camera->vel.x = -mov;
}
game->player->pos += game->camera->vel;
bool clamped = false;
if(game->player->pos.x < game->player->size.x)
{
clamped = true;
game->player->pos.x = game->player->size.x;
}
if(game->player->pos.x + game->player->size.x > 2.0f - game->player->size.x)
{
clamped = true;
game->player->pos.x = 2.0f - (2.0f*game->player->size.x);
}
if(game->player->pos.y < game->player->size.y)
{
clamped = true;
game->player->pos.y = game->player->size.y;
}
if(game->player->pos.y + game->player->size.y > 2.0f - game->player->size.y)
{
clamped = true;
game->player->pos.y = 2.0f - (2.0f*game->player->size.y);
}
if(clamped)
{
for(u32 i = game->tilemap.entity->id; i < game->tilemap.w * game->tilemap.h; i++)
{
game->entity_batch[i].pos -= game->camera->vel;
}
}
}
int main()
{
CJ_PLATFORM platform = CreatePlatform(640, 480, "Platform", 0);
RENDERER renderer = CreateRenderer();
SHADER_PROGRAM shader_solid_col = {};
CreateShader(&shader_solid_col, "..\\shader\\VS.h", GL_VERTEX_SHADER);
CreateShader(&shader_solid_col, "..\\shader\\FS_solid_col.h", GL_FRAGMENT_SHADER);
LinkShaderProgram(&shader_solid_col);
UseShaderProgram(&shader_solid_col);
SHADER_PROGRAM shader_textured = {};
CreateShader(&shader_textured, "..\\shader\\VS.h", GL_VERTEX_SHADER);
CreateShader(&shader_textured, "..\\shader\\FS_Texture.h", GL_FRAGMENT_SHADER);
LinkShaderProgram(&shader_textured);
UseShaderProgram(&shader_textured);
SHADER_PROGRAM shader_font = {};
CreateShader(&shader_font, "..\\shader\\VS.h", GL_VERTEX_SHADER);
CreateShader(&shader_font, "..\\shader\\FS_Texture.h", GL_FRAGMENT_SHADER);
LinkShaderProgram(&shader_font);
UseShaderProgram(&shader_font);
CJ_VTX_ATTRIB vbo_text_attrib[3];
vbo_text_attrib[0].size = 2;
vbo_text_attrib[0].type = GL_FLOAT;
vbo_text_attrib[0].stride = 8;
vbo_text_attrib[0].offset = 0;
vbo_text_attrib[1].size = 4;
vbo_text_attrib[1].type = GL_FLOAT;
vbo_text_attrib[1].stride = 8;
vbo_text_attrib[1].offset = 2;
vbo_text_attrib[2].size = 2;
vbo_text_attrib[2].type = GL_FLOAT;
vbo_text_attrib[2].stride = 8;
vbo_text_attrib[2].offset = 6;
u8 *vertex_memory_for_text = (u8*)calloc(4 * 6, sizeof(float));
CJ_VBO vbo_text = {};
InitializeVBO(&vbo_text, vertex_memory_for_text, 4*6*sizeof(float), ArrayCount(vbo_text_attrib), vbo_text_attrib);
CJ_VTX_ATTRIB vbo_sprite_attrib[3];
vbo_sprite_attrib[0].size = 2;
vbo_sprite_attrib[0].type = GL_FLOAT;
vbo_sprite_attrib[0].stride = 8;
vbo_sprite_attrib[0].offset = 0;
vbo_sprite_attrib[1].size = 4;
vbo_sprite_attrib[1].type = GL_FLOAT;
vbo_sprite_attrib[1].stride = 8;
vbo_sprite_attrib[1].offset = 2;
vbo_sprite_attrib[2].size = 2;
vbo_sprite_attrib[2].type = GL_FLOAT;
vbo_sprite_attrib[2].stride = 8;
vbo_sprite_attrib[2].offset = 6;
u8 *vertex_memory = (u8*)calloc(4 * 32 * 1000, sizeof(float));
const u32 size = 4 * 32 * 1000 * sizeof(float);
CJ_VBO vbo_sprite = {};
InitializeVBO(&vbo_sprite, vertex_memory, size, ArrayCount(vbo_sprite_attrib), vbo_sprite_attrib);
u32 IBO = 0;
const u32 quads = size / (sizeof(CJ_VTX_QUAD) * 4);
u32 ibo_data[quads * 6] = {};
for(u32 i = 0; i < quads; i++)
{
ibo_data[0 + (i * 6)] = 0 + (i * 4);
ibo_data[1 + (i * 6)] = 1 + (i * 4);
ibo_data[2 + (i * 6)] = 2 + (i * 4);
ibo_data[3 + (i * 6)] = 0 + (i * 4);
ibo_data[4 + (i * 6)] = 2 + (i * 4);
ibo_data[5 + (i * 6)] = 3 + (i * 4);
}
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ibo_data), ibo_data, GL_DYNAMIC_DRAW);
GAME_HANDLER game = {};
game.pVertex = (CJ_VTX_QUAD*)vbo_sprite.base;
TEXTUREINFO texture_batch[10] = {};
LoadTexture(&texture_batch[0], "assets\\tileSheet32x32.png", LOAD_IMAGE, 0);
LoadTexture(&texture_batch[1], "C:\\windows\\fonts\\cour.ttf", LOAD_FONT, 48);
while(!glfwWindowShouldClose(platform.window) && running)
{
glfwPollEvents();
glfwGetWindowSize(platform.window, &platform.win_w, &platform.win_h);
glViewport(0, 0, platform.win_w, platform.win_h);
glClearColor(0.2f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
OnGameInit(&platform, &game);
OnGameUpdate(&platform, &game);
glBindBuffer(GL_ARRAY_BUFFER, vbo_sprite.id);
UpdateVBO_Entity(&vbo_sprite, &game);
glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_sprite.size, (void*)vbo_sprite.base);
UseShaderProgram(&shader_textured);
glBindTexture(GL_TEXTURE_2D, texture_batch[0].texture_id);
glDrawElements(GL_TRIANGLES, 6 * 1000, GL_UNSIGNED_INT, (const void*)0);
UseShaderProgram(&shader_solid_col);
CreateText(&platform, &vbo_text, texture_batch[1], "12345", v2f(1.0f, 1.0f), v4f(1.0f, 1.0f, 1.0f, 1.0f));
SwapBuffers(platform);
}
glfwTerminate();
return 0;
}
u32 CreateText(CJ_PLATFORM *platform, CJ_VBO *vbo, TEXTUREINFO tex_i, char *str, V2f pos, V4f col)
{
char line[1024] = {};
strcpy(line, str);
// xp & yp is baseline in stbtt_GetPackedQuad();
float yp = 0.0f;
float xp = 0.0f;
float t_pos_x = 0.0f;
float t_pos_y = 0.0f;
float t_sz_x = 0.0f;
float t_sz_y = 0.0f;
float c_x = 0.0f;
float c_y = 0.0f;
float c_w = 0.0f;
float c_h = 0.0f;
glBindBuffer(GL_ARRAY_BUFFER, vbo->id);
glBindTexture(GL_TEXTURE_2D, tex_i.texture_id);
u32 last_quad_index = 0;
u32 last_text_q = 0;
u32 n_quads_written = 0;
float *pVertex = (float*)vbo->base;
for(i32 i = 0; i < strlen(line); i++)
{
stbtt_GetPackedQuad(tex_i.packed_char, 1024, 1024, line[i] - ' ', &xp, &yp, &tex_i.glyph_quad, 0);
t_pos_x = tex_i.glyph_quad.s0;
t_pos_y = 1.0f - tex_i.glyph_quad.t1;
t_sz_x = tex_i.glyph_quad.s1 - tex_i.glyph_quad.s0;
t_sz_y = (1.0f - tex_i.glyph_quad.t0) - (1.0f - tex_i.glyph_quad.t1);
// The glyph_quad.x0 & glyph_quad.y0 should be the the advancing positions in x/y, yp is the baseline which has to used when calculating the char y_pos
c_x = pos.x + (tex_i.glyph_quad.x0) / platform->win_w;
c_y = pos.y + ((yp + (yp - tex_i.glyph_quad.y1))) / platform->win_h;
c_w = (tex_i.glyph_quad.x1 - tex_i.glyph_quad.x0) / platform->win_w;
c_h = (tex_i.glyph_quad.y1 - tex_i.glyph_quad.y0) / platform->win_h;
pVertex[0] = c_x; pVertex[1] = c_y; pVertex[2] = t_pos_x; pVertex[3] = t_pos_y;
pVertex[4] = c_x; pVertex[5] = c_y + c_h; pVertex[6] = t_pos_x; pVertex[7] = t_pos_y + t_sz_y;
pVertex[8] = c_x + c_w; pVertex[9] = c_y + c_h; pVertex[10] = t_pos_x + t_sz_x; pVertex[11] = t_pos_y + t_sz_y;
pVertex[12] = c_x; pVertex[13] = c_y; pVertex[14] = t_pos_x; pVertex[15] = t_pos_y;
pVertex[16] = c_x + c_w; pVertex[17] = c_y + c_h; pVertex[18] = t_pos_y + t_sz_x; pVertex[19] = t_pos_y + t_sz_y;
pVertex[20] = c_x + c_w; pVertex[21] = c_y; pVertex[22] = t_pos_y + t_sz_x; pVertex[23] = t_pos_y;
//float vertices[6][4] =
//{
// c_x, c_y, t_pos_x, t_pos_y,
// c_x, c_y + c_h, t_pos_x, t_pos_y + t_sz_y,
// c_x + c_w, c_y + c_h, t_pos_x + t_sz_x, t_pos_y + t_sz_y,
// c_x, c_y, t_pos_x, t_pos_y,
// c_x + c_w, c_y + c_h, t_pos_x + t_sz_x, t_pos_y + t_sz_y,
// c_x + c_w, c_y, t_pos_x +t_sz_x, t_pos_y
//};
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vbo->base), vbo->base);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
//return last_quad_index;
return last_text_q;
}
|