|
@@ -31,73 +31,10 @@
|
|
|
#include "keybind.h"
|
|
#include "keybind.h"
|
|
|
#include "config.h"
|
|
#include "config.h"
|
|
|
#include "shader.h"
|
|
#include "shader.h"
|
|
|
|
|
+#include "tile_glyph.h"
|
|
|
|
|
|
|
|
struct app handle_events(struct app app);
|
|
struct app handle_events(struct app app);
|
|
|
|
|
|
|
|
-struct glyph {
|
|
|
|
|
- struct vec2i tile;
|
|
|
|
|
- int32_t c;
|
|
|
|
|
- struct vec4f fg_color;
|
|
|
|
|
- struct vec4f bg_color;
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-DA_DEF_STRUCT(struct glyph, glyphs);
|
|
|
|
|
-
|
|
|
|
|
-struct glyph_attr {
|
|
|
|
|
- GLint size;
|
|
|
|
|
- GLenum type;
|
|
|
|
|
- GLboolean norm;
|
|
|
|
|
- GLsizei stride;
|
|
|
|
|
- size_t offset;
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-enum glyph_attr_enum {
|
|
|
|
|
- GLYPH_ATTR_TILE = 0,
|
|
|
|
|
- GLYPH_ATTR_C,
|
|
|
|
|
- GLYPH_ATTR_FG_COLOR,
|
|
|
|
|
- GLYPH_ATTR_BG_COLOR,
|
|
|
|
|
- GLYPH_ATTR_TOTAL
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-static const struct glyph_attr glyph_attr[GLYPH_ATTR_TOTAL] = {
|
|
|
|
|
- [GLYPH_ATTR_TILE] = {
|
|
|
|
|
- .size = 2,
|
|
|
|
|
- .type = GL_INT,
|
|
|
|
|
- .norm = GL_FALSE,
|
|
|
|
|
- .stride = sizeof(struct glyph),
|
|
|
|
|
- .offset = offsetof(struct glyph, tile)
|
|
|
|
|
- },
|
|
|
|
|
- [GLYPH_ATTR_C] = {
|
|
|
|
|
- .size = 1,
|
|
|
|
|
- .type = GL_INT,
|
|
|
|
|
- .norm = GL_FALSE,
|
|
|
|
|
- .stride = sizeof(struct glyph),
|
|
|
|
|
- .offset = offsetof(struct glyph, c)
|
|
|
|
|
- },
|
|
|
|
|
- [GLYPH_ATTR_FG_COLOR] = {
|
|
|
|
|
- .size = 4,
|
|
|
|
|
- .type = GL_FLOAT,
|
|
|
|
|
- .norm = GL_FALSE,
|
|
|
|
|
- .stride = sizeof(struct glyph),
|
|
|
|
|
- .offset = offsetof(struct glyph, fg_color)
|
|
|
|
|
- },
|
|
|
|
|
- [GLYPH_ATTR_BG_COLOR] = {
|
|
|
|
|
- .size = 4,
|
|
|
|
|
- .type = GL_FLOAT,
|
|
|
|
|
- .norm = GL_FALSE,
|
|
|
|
|
- .stride = sizeof(struct glyph),
|
|
|
|
|
- .offset = offsetof(struct glyph, bg_color)
|
|
|
|
|
- },
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-struct glyphs _gl_render_text(struct app app, struct glyphs glys,
|
|
|
|
|
- const char *cstr, size_t cstr_size,
|
|
|
|
|
- struct vec2i tile,
|
|
|
|
|
- struct vec4f fg_color, struct vec4f bg_color);
|
|
|
|
|
-struct glyphs gl_render_text(struct app app, struct glyphs glys,
|
|
|
|
|
- struct vec2i tile);
|
|
|
|
|
-void glyphs_sync(struct app app, struct glyphs glys);
|
|
|
|
|
-
|
|
|
|
|
int32_t get_uniform(uint32_t prog, const char *name);
|
|
int32_t get_uniform(uint32_t prog, const char *name);
|
|
|
|
|
|
|
|
int32_t
|
|
int32_t
|
|
@@ -112,7 +49,7 @@ main(int32_t argc, char **argv)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
struct app app = app_create("ged");
|
|
struct app app = app_create("ged");
|
|
|
- const char *font_path = "./charmap-oldschool_white.png";
|
|
|
|
|
|
|
+ const char *font_path = "./fonts/charmap-oldschool_white.png";
|
|
|
app.font = font_create(app.rdr, font_path, 7, 18);
|
|
app.font = font_create(app.rdr, font_path, 7, 18);
|
|
|
app.font.scale = 4.0;
|
|
app.font.scale = 4.0;
|
|
|
|
|
|
|
@@ -124,10 +61,7 @@ main(int32_t argc, char **argv)
|
|
|
exit(EXIT_FAILURE);
|
|
exit(EXIT_FAILURE);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- struct glyphs glys;
|
|
|
|
|
- glys.cap = 1024 * 1024;
|
|
|
|
|
- glys.size = 0;
|
|
|
|
|
- glys.items = calloc(glys.cap, sizeof(struct glyph));
|
|
|
|
|
|
|
+ struct tile_glyph_da glys = tile_glyph_da_create();
|
|
|
|
|
|
|
|
uint32_t prog;
|
|
uint32_t prog;
|
|
|
uint32_t vert_shader;
|
|
uint32_t vert_shader;
|
|
@@ -161,36 +95,8 @@ main(int32_t argc, char **argv)
|
|
|
|
|
|
|
|
glUseProgram(prog);
|
|
glUseProgram(prog);
|
|
|
|
|
|
|
|
- uint32_t font_tex = 0;
|
|
|
|
|
- /* Init Font Texture */
|
|
|
|
|
- {
|
|
|
|
|
- int32_t w, h, n;
|
|
|
|
|
- uint8_t *data = stbi_load(font_path,
|
|
|
|
|
- &w, &h, &n,
|
|
|
|
|
- STBI_rgb_alpha);
|
|
|
|
|
-
|
|
|
|
|
- if ( data == NULL ) {
|
|
|
|
|
- fprintf(stderr, "ERROR loading file %s: %s",
|
|
|
|
|
- font_path, stbi_failure_reason());
|
|
|
|
|
- exit(EXIT_FAILURE);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
- glGenTextures(1, &font_tex);
|
|
|
|
|
- glBindTexture(GL_TEXTURE_2D, font_tex);
|
|
|
|
|
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
|
|
|
- GL_NEAREST);
|
|
|
|
|
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
|
|
|
- GL_NEAREST);
|
|
|
|
|
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
|
|
|
|
|
- GL_CLAMP_TO_EDGE);
|
|
|
|
|
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
|
|
|
|
|
- GL_CLAMP_TO_EDGE);
|
|
|
|
|
-
|
|
|
|
|
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h,
|
|
|
|
|
- 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
|
- data);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ uint32_t font_tex = tile_glyph_load_texture_atlas(font_path);
|
|
|
|
|
+ (void) font_tex;
|
|
|
|
|
|
|
|
/* Getting the uniforms */
|
|
/* Getting the uniforms */
|
|
|
app.uniforms.time = get_uniform(prog, "time");
|
|
app.uniforms.time = get_uniform(prog, "time");
|
|
@@ -206,34 +112,8 @@ main(int32_t argc, char **argv)
|
|
|
{
|
|
{
|
|
|
glGenVertexArrays(1, &vao);
|
|
glGenVertexArrays(1, &vao);
|
|
|
glBindVertexArray(vao);
|
|
glBindVertexArray(vao);
|
|
|
-
|
|
|
|
|
- glGenBuffers(1, &vbo);
|
|
|
|
|
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
|
- glBufferData(GL_ARRAY_BUFFER,
|
|
|
|
|
- (ssize_t)(glys.cap * sizeof(struct glyph)),
|
|
|
|
|
- glys.items,
|
|
|
|
|
- GL_DYNAMIC_DRAW);
|
|
|
|
|
-
|
|
|
|
|
- for ( size_t i = 0; i < GLYPH_ATTR_TOTAL; ++i ) {
|
|
|
|
|
- struct glyph_attr ga = glyph_attr[i];
|
|
|
|
|
- glEnableVertexAttribArray((uint32_t) i);
|
|
|
|
|
- switch ( ga.type ) {
|
|
|
|
|
- case GL_FLOAT: {
|
|
|
|
|
- glVertexAttribPointer((uint32_t) i,
|
|
|
|
|
- ga.size, ga.type,
|
|
|
|
|
- ga.norm, ga.stride,
|
|
|
|
|
- (void*)ga.offset);
|
|
|
|
|
- } break;
|
|
|
|
|
- case GL_INT: {
|
|
|
|
|
- glVertexAttribIPointer((uint32_t) i,
|
|
|
|
|
- ga.size, ga.type,
|
|
|
|
|
- ga.stride,
|
|
|
|
|
- (void*)ga.offset);
|
|
|
|
|
- } break;
|
|
|
|
|
- }
|
|
|
|
|
- glVertexAttribDivisor((uint32_t) i, 1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ vbo = tile_glyph_gen_buffer(glys);
|
|
|
|
|
+ (void) vbo;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
glUniform1f(app.uniforms.scale, (float)app.font.scale);
|
|
glUniform1f(app.uniforms.scale, (float)app.font.scale);
|
|
@@ -268,21 +148,18 @@ main(int32_t argc, char **argv)
|
|
|
|
|
|
|
|
/* Calc Camera */
|
|
/* Calc Camera */
|
|
|
{
|
|
{
|
|
|
- struct vec2 cur_pos = app_calc_cur_pos(app);
|
|
|
|
|
- struct vec2 cpos = buf->cam.pos;
|
|
|
|
|
- struct vec2 cvel = buf->cam.vel;
|
|
|
|
|
- cvel = vec2_sub(cur_pos, cpos);
|
|
|
|
|
- cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
|
|
|
|
|
- buf->cam.pos = cpos;
|
|
|
|
|
- buf->cam.vel = cvel;
|
|
|
|
|
|
|
+ struct vec2 cpos;
|
|
|
|
|
+ RET_UNWRAP2(app, cpos,
|
|
|
|
|
+ struct ret_app_vec2,
|
|
|
|
|
+ app_calc_buffer_cam(app, dt));
|
|
|
glUniform2f(app.uniforms.camera,
|
|
glUniform2f(app.uniforms.camera,
|
|
|
(float)cpos.x,
|
|
(float)cpos.x,
|
|
|
(float)cpos.y);
|
|
(float)cpos.y);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
glys.size = 0;
|
|
glys.size = 0;
|
|
|
- glys = gl_render_text(app, glys, vec2is(0));
|
|
|
|
|
- glyphs_sync(app, glys);
|
|
|
|
|
|
|
+ glys = tile_glyph_da_calc_buffer(app, glys, vec2is(0));
|
|
|
|
|
+ tile_glyph_da_sync(app, glys);
|
|
|
glDrawArraysInstanced(GL_TRIANGLE_STRIP,
|
|
glDrawArraysInstanced(GL_TRIANGLE_STRIP,
|
|
|
0, 4,
|
|
0, 4,
|
|
|
(int32_t) glys.size);
|
|
(int32_t) glys.size);
|
|
@@ -296,12 +173,12 @@ main(int32_t argc, char **argv)
|
|
|
|
|
|
|
|
const char *c = buf->data.items + buf->cur;
|
|
const char *c = buf->data.items + buf->cur;
|
|
|
glys.size = 0;
|
|
glys.size = 0;
|
|
|
- glys = _gl_render_text(app, glys,
|
|
|
|
|
- ( *c == '\n' ) ? " " : c, 1,
|
|
|
|
|
- vec2i((int32_t)col,
|
|
|
|
|
- -(int32_t)row),
|
|
|
|
|
- vec4fs(0), vec4fs(1));
|
|
|
|
|
- glyphs_sync(app, glys);
|
|
|
|
|
|
|
+ glys = tile_glyph_da_calc(app, glys,
|
|
|
|
|
+ ( *c == '\n' ) ? " " : c, 1,
|
|
|
|
|
+ vec2i((int32_t)col,
|
|
|
|
|
+ -(int32_t)row),
|
|
|
|
|
+ vec4fs(0), vec4fs(1));
|
|
|
|
|
+ tile_glyph_da_sync(app, glys);
|
|
|
glDrawArraysInstanced(GL_TRIANGLE_STRIP,
|
|
glDrawArraysInstanced(GL_TRIANGLE_STRIP,
|
|
|
0, 4,
|
|
0, 4,
|
|
|
(int32_t) glys.size);
|
|
(int32_t) glys.size);
|
|
@@ -323,52 +200,6 @@ main(int32_t argc, char **argv)
|
|
|
exit(EXIT_SUCCESS);
|
|
exit(EXIT_SUCCESS);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-struct glyphs
|
|
|
|
|
-_gl_render_text(struct app app, struct glyphs glys,
|
|
|
|
|
- const char *cstr, size_t cstr_size,
|
|
|
|
|
- struct vec2i tile,
|
|
|
|
|
- struct vec4f fg_color, struct vec4f bg_color)
|
|
|
|
|
-{
|
|
|
|
|
- (void) app;
|
|
|
|
|
- struct vec2i pen = tile;
|
|
|
|
|
- int32_t row = 0;
|
|
|
|
|
- for ( size_t i = 0; i < cstr_size; ++i ) {
|
|
|
|
|
- char c = cstr[i];
|
|
|
|
|
- if ( c == '\n' ) {
|
|
|
|
|
- pen.y -= 1;
|
|
|
|
|
- row = 0;
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- struct glyph gly = {
|
|
|
|
|
- .tile = vec2i_add(pen, vec2i(row, 0)),
|
|
|
|
|
- .c = c,
|
|
|
|
|
- .fg_color = fg_color,
|
|
|
|
|
- .bg_color = bg_color
|
|
|
|
|
- };
|
|
|
|
|
- DA_APPEND(glys, gly);
|
|
|
|
|
- ++row;
|
|
|
|
|
- }
|
|
|
|
|
- return glys;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-struct glyphs
|
|
|
|
|
-gl_render_text(struct app app, struct glyphs glys, struct vec2i tile)
|
|
|
|
|
-{
|
|
|
|
|
- return _gl_render_text(app, glys,
|
|
|
|
|
- app.buf.data.items, app.buf.data.size,
|
|
|
|
|
- tile, vec4fs(1), vec4fs(0));
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void
|
|
|
|
|
-glyphs_sync(struct app app, struct glyphs glys)
|
|
|
|
|
-{
|
|
|
|
|
- (void) app;
|
|
|
|
|
- glBufferSubData(GL_ARRAY_BUFFER,
|
|
|
|
|
- 0,
|
|
|
|
|
- (ssize_t) (glys.size * sizeof(struct glyph)),
|
|
|
|
|
- glys.items);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
int32_t
|
|
int32_t
|
|
|
get_uniform(uint32_t prog, const char *name)
|
|
get_uniform(uint32_t prog, const char *name)
|
|
|
{
|
|
{
|
|
@@ -381,99 +212,6 @@ get_uniform(uint32_t prog, const char *name)
|
|
|
return uni;
|
|
return uni;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int32_t main2(int32_t argc, char **argv);
|
|
|
|
|
-int32_t
|
|
|
|
|
-main2(int32_t argc, char **argv)
|
|
|
|
|
-{
|
|
|
|
|
- srand((uint32_t) time(NULL));
|
|
|
|
|
- if ( argc != 2 ) {
|
|
|
|
|
- fprintf(stderr, "MUST pass file to open\n");
|
|
|
|
|
- exit(EXIT_FAILURE);
|
|
|
|
|
- }
|
|
|
|
|
- /* (void) argc; (void) argv; */
|
|
|
|
|
-
|
|
|
|
|
- SCE(SDL_Init(SDL_INIT_VIDEO));
|
|
|
|
|
-
|
|
|
|
|
- const char *font_path = "./charmap-oldschool_white.png";
|
|
|
|
|
-
|
|
|
|
|
- struct app app = app_create("ged");
|
|
|
|
|
-
|
|
|
|
|
- app.font = font_create(app.rdr, font_path, 7, 18);
|
|
|
|
|
- app.font.scale = 2.0;
|
|
|
|
|
-
|
|
|
|
|
- /* const char *filepath = "./src/main.c"; */
|
|
|
|
|
- enum buffer_err err;
|
|
|
|
|
- RET_UNWRAP2(app.buf, err,
|
|
|
|
|
- struct ret_buffer_err,
|
|
|
|
|
- buffer_load_from_file(app.buf, argv[1]));
|
|
|
|
|
- if ( err != BUFFER_ERR_OK ) {
|
|
|
|
|
- printf("%d\n", err);
|
|
|
|
|
- fprintf(stderr, "Failed to open buffer\n");
|
|
|
|
|
- exit(EXIT_FAILURE);
|
|
|
|
|
- }
|
|
|
|
|
- (void) err;
|
|
|
|
|
-
|
|
|
|
|
- uint64_t i = 0;
|
|
|
|
|
- double dt = (1.0 / (double)app.target_fps) * 3;
|
|
|
|
|
- uint32_t frame_target_dur = (uint32_t)(1000 / app.target_fps);
|
|
|
|
|
-
|
|
|
|
|
- char *fps_text = calloc(1, 128);
|
|
|
|
|
- size_t fps_text_size = 0;
|
|
|
|
|
-
|
|
|
|
|
- uint32_t fr_end = 0;
|
|
|
|
|
- uint32_t fr_start = 0;
|
|
|
|
|
- uint32_t fr_dur = 1;
|
|
|
|
|
- while ( app.running == true ) {
|
|
|
|
|
- fr_start = SDL_GetTicks();
|
|
|
|
|
- struct buffer *buf = &app.buf;
|
|
|
|
|
- app = handle_events(app);
|
|
|
|
|
-
|
|
|
|
|
- SCE(SDL_SetRenderDrawColor(app.rdr, 0x0, 0, 0, 1));
|
|
|
|
|
- SCE(SDL_RenderClear(app.rdr));
|
|
|
|
|
-
|
|
|
|
|
- if ( app.show_fps == true ) {
|
|
|
|
|
- fps_text_size = (size_t) snprintf(fps_text, 128,
|
|
|
|
|
- "FPS: %.2f",
|
|
|
|
|
- 1000.0 / fr_dur);
|
|
|
|
|
- app_render_text(app, fps_text, fps_text_size,
|
|
|
|
|
- vec2s(0), 0xFF00FFFF, 5);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- {
|
|
|
|
|
- struct vec2 cur_pos = app_calc_cur_pos(app);
|
|
|
|
|
- cur_pos = vec2_sub(vec2(app.win.hw, app.win.hh),
|
|
|
|
|
- cur_pos);
|
|
|
|
|
- struct vec2 cpos = buf->cam.pos;
|
|
|
|
|
- struct vec2 cvel = buf->cam.vel;
|
|
|
|
|
- cvel = vec2_sub(cur_pos, cpos);
|
|
|
|
|
- cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
|
|
|
|
|
- buf->cam.pos = cpos;
|
|
|
|
|
- buf->cam.vel = cvel;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- app_render_buffer(app, buf->cam.pos, (uint32_t)
|
|
|
|
|
- (0xFF << 24)
|
|
|
|
|
- | (0xFF << 16)
|
|
|
|
|
- | (0xFF << 8 )
|
|
|
|
|
- | 0xFF
|
|
|
|
|
- /* 0xFFFFFFFF */, app.font.scale);
|
|
|
|
|
-
|
|
|
|
|
- SDL_RenderPresent(app.rdr);
|
|
|
|
|
- fr_end = SDL_GetTicks();
|
|
|
|
|
- fr_dur = (fr_end - fr_start);
|
|
|
|
|
- if ( fr_dur < frame_target_dur ) {
|
|
|
|
|
- SDL_Delay(frame_target_dur - fr_dur);
|
|
|
|
|
- }
|
|
|
|
|
- ++i;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- SDL_DestroyWindow(app.win.ptr);
|
|
|
|
|
- DA_DESTROY(app.buf.data);
|
|
|
|
|
- SDL_Quit();
|
|
|
|
|
- exit(EXIT_SUCCESS);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
struct app
|
|
struct app
|
|
|
handle_events(struct app app)
|
|
handle_events(struct app app)
|
|
|
{
|
|
{
|