|
|
@@ -1,6 +1,9 @@
|
|
|
#ifndef TILE_GLYPH_H
|
|
|
#define TILE_GLYPH_H
|
|
|
|
|
|
+#include "app.h"
|
|
|
+#include "buffer.h"
|
|
|
+
|
|
|
#include "glyph_attr.h"
|
|
|
|
|
|
struct tile_glyph {
|
|
|
@@ -43,24 +46,31 @@ struct ret_uint32_t_uint32_t {
|
|
|
uint32_t f2;
|
|
|
};
|
|
|
|
|
|
-struct tile_glyph_render tile_glyph_render_create(const char *font_path);
|
|
|
+struct tile_glyph_render tile_glyph_render_create(const char *font_path,
|
|
|
+ const char *vert_path,
|
|
|
+ const char *frag_path);
|
|
|
|
|
|
struct tile_glyph_da tile_glyph_da_create(void);
|
|
|
const struct glyph_attr* tile_glyph_get_attrs(void);
|
|
|
|
|
|
-struct tile_glyph_da tile_glyph_da_calc(struct app app,
|
|
|
- struct tile_glyph_da glys,
|
|
|
- const char *cstr, size_t cstr_size,
|
|
|
- struct vec2i tile,
|
|
|
- struct vec4f fg_color,
|
|
|
- struct vec4f bg_color);
|
|
|
-struct tile_glyph_da tile_glyph_da_calc_buffer(struct app app,
|
|
|
- struct tile_glyph_da glys,
|
|
|
- struct vec2i tile);
|
|
|
-void tile_glyph_da_sync(struct app app, struct tile_glyph_da glys);
|
|
|
-struct ret_uint32_t_uint32_t tile_glyph_gen_buffer(struct tile_glyph_da glys);
|
|
|
-uint32_t tile_glyph_load_texture_atlas(const char *filepath);
|
|
|
-struct ret_uint32_t_uint32_t tile_glyph_compile_shaders_or_exit(void);
|
|
|
+struct tile_glyph_render tile_glyph_render_calc(
|
|
|
+ struct tile_glyph_render tgr, const char *cstr, size_t cstr_size,
|
|
|
+ struct vec2i tile, struct vec4f fg_color, struct vec4f bg_color);
|
|
|
+
|
|
|
+struct tile_glyph_render tile_glyph_render_calc_buffer(
|
|
|
+ struct tile_glyph_render tgr, struct buffer buf, struct vec2i tile);
|
|
|
+
|
|
|
+struct tile_glyph_render tile_glyph_render_sync(struct tile_glyph_render tgr);
|
|
|
+struct tile_glyph_render tile_glyph_render_gen_buffer(
|
|
|
+ struct tile_glyph_render tgr);
|
|
|
+
|
|
|
+struct tile_glyph_render tile_glyph_render_load_texture_atlas_or_exit(
|
|
|
+ struct tile_glyph_render tgr,
|
|
|
+ const char *filepath);
|
|
|
+
|
|
|
+struct tile_glyph_render tile_glyph_compile_shaders_or_exit(
|
|
|
+ struct tile_glyph_render tgr,
|
|
|
+ const char *vert_path, const char *frag_path);
|
|
|
|
|
|
#if defined(TILE_GLYPH_IMP) || defined(IMP)
|
|
|
|
|
|
@@ -76,52 +86,30 @@ struct ret_uint32_t_uint32_t tile_glyph_compile_shaders_or_exit(void);
|
|
|
#include "unwrap.h"
|
|
|
|
|
|
struct tile_glyph_render
|
|
|
-tile_glyph_render_create(const char *font_path)
|
|
|
+tile_glyph_render_create(const char *font_path, const char *vert_path,
|
|
|
+ const char *frag_path)
|
|
|
{
|
|
|
- uint32_t font_tex = tile_glyph_load_texture_atlas(font_path);
|
|
|
+ struct tile_glyph_render tgr = {0};
|
|
|
+ tgr = tile_glyph_render_load_texture_atlas_or_exit(tgr, font_path);
|
|
|
|
|
|
- struct tile_glyph_da glys = tile_glyph_da_create();
|
|
|
-
|
|
|
- uint32_t vert_shader;
|
|
|
- uint32_t frag_shader;
|
|
|
- uint32_t prog;
|
|
|
+ tgr.glyphs = tile_glyph_da_create();
|
|
|
|
|
|
/* Loading shaders and linking prog */
|
|
|
- RET_UNWRAP2(vert_shader, frag_shader,
|
|
|
- struct ret_uint32_t_uint32_t,
|
|
|
- tile_glyph_compile_shaders_or_exit());
|
|
|
- prog = program_link_or_exit(vert_shader, frag_shader);
|
|
|
+ tgr = tile_glyph_compile_shaders_or_exit(tgr, vert_path, frag_path);
|
|
|
+ tgr.prog = program_link_or_exit(tgr.shaders.vert, tgr.shaders.frag);
|
|
|
|
|
|
- glUseProgram(prog);
|
|
|
+ glUseProgram(tgr.prog);
|
|
|
|
|
|
- int32_t resolution = get_uniform_or_exit(prog, "resolution");
|
|
|
- int32_t scale = get_uniform_or_exit(prog, "scale");
|
|
|
- int32_t time = get_uniform_or_exit(prog, "time");
|
|
|
- int32_t camera = get_uniform_or_exit(prog, "camera");
|
|
|
+ tgr.uniforms.resolution = get_uniform_or_exit(tgr.prog, "resolution");
|
|
|
+ tgr.uniforms.scale = get_uniform_or_exit(tgr.prog, "scale");
|
|
|
+ tgr.uniforms.time = get_uniform_or_exit(tgr.prog, "time");
|
|
|
+ tgr.uniforms.camera = get_uniform_or_exit(tgr.prog, "camera");
|
|
|
|
|
|
- glUniform1f(scale, 1.0f);
|
|
|
+ glUniform1f(tgr.uniforms.scale, 1.0f);
|
|
|
|
|
|
- uint32_t vao, vbo;
|
|
|
- RET_UNWRAP2(vao, vbo,
|
|
|
- struct ret_uint32_t_uint32_t, tile_glyph_gen_buffer(glys));
|
|
|
+ tgr = tile_glyph_render_gen_buffer(tgr);
|
|
|
|
|
|
- return (struct tile_glyph_render) {
|
|
|
- .font_tex = font_tex,
|
|
|
- .shaders = {
|
|
|
- .vert = vert_shader,
|
|
|
- .frag = frag_shader,
|
|
|
- },
|
|
|
- .prog = prog,
|
|
|
- .uniforms = {
|
|
|
- .resolution = resolution,
|
|
|
- .scale = scale,
|
|
|
- .time = time,
|
|
|
- .camera = camera,
|
|
|
- },
|
|
|
- .vao = vao,
|
|
|
- .vbo = vbo,
|
|
|
- .glyphs = glys,
|
|
|
- };
|
|
|
+ return tgr;
|
|
|
}
|
|
|
|
|
|
struct tile_glyph_da
|
|
|
@@ -172,12 +160,11 @@ tile_glyph_get_attrs(void)
|
|
|
return tile_glyph_attr;
|
|
|
}
|
|
|
|
|
|
-struct tile_glyph_da
|
|
|
-tile_glyph_da_calc(struct app app, struct tile_glyph_da glys,
|
|
|
+struct tile_glyph_render
|
|
|
+tile_glyph_render_calc(struct tile_glyph_render tgr,
|
|
|
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 ) {
|
|
|
@@ -193,56 +180,50 @@ tile_glyph_da_calc(struct app app, struct tile_glyph_da glys,
|
|
|
.fg_color = fg_color,
|
|
|
.bg_color = bg_color
|
|
|
};
|
|
|
- DA_APPEND(glys, gly);
|
|
|
+ DA_APPEND(tgr.glyphs, gly);
|
|
|
++row;
|
|
|
}
|
|
|
- return glys;
|
|
|
+ return tgr;
|
|
|
}
|
|
|
|
|
|
-struct tile_glyph_da
|
|
|
-tile_glyph_da_calc_buffer(struct app app, struct tile_glyph_da glys,
|
|
|
- struct vec2i tile)
|
|
|
+struct tile_glyph_render
|
|
|
+tile_glyph_render_calc_buffer(struct tile_glyph_render tgr, struct buffer buf,
|
|
|
+ struct vec2i tile)
|
|
|
{
|
|
|
- return tile_glyph_da_calc(app, glys,
|
|
|
- app.buf.data.items, app.buf.data.size,
|
|
|
- tile, vec4fs(1), vec4fs(0));
|
|
|
+ return tile_glyph_render_calc(tgr, buf.data.items, buf.data.size,
|
|
|
+ tile, vec4fs(1), vec4fs(0));
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
-tile_glyph_da_sync(struct app app, struct tile_glyph_da glys)
|
|
|
+struct tile_glyph_render
|
|
|
+tile_glyph_render_sync(struct tile_glyph_render tgr)
|
|
|
{
|
|
|
- (void) app;
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0,
|
|
|
- (ssize_t) (glys.size * sizeof(struct tile_glyph)),
|
|
|
- glys.items);
|
|
|
+ (ssize_t) (tgr.glyphs.size
|
|
|
+ * sizeof(struct tile_glyph)),
|
|
|
+ tgr.glyphs.items);
|
|
|
+ return tgr;
|
|
|
}
|
|
|
|
|
|
-struct ret_uint32_t_uint32_t
|
|
|
-tile_glyph_gen_buffer(struct tile_glyph_da glys)
|
|
|
+struct tile_glyph_render
|
|
|
+tile_glyph_render_gen_buffer(struct tile_glyph_render tgr)
|
|
|
{
|
|
|
- uint32_t vao = 0;
|
|
|
+ glGenVertexArrays(1, &tgr.vao);
|
|
|
+ glBindVertexArray(tgr.vao);
|
|
|
|
|
|
- glGenVertexArrays(1, &vao);
|
|
|
- glBindVertexArray(vao);
|
|
|
-
|
|
|
- uint32_t vbo = 0;
|
|
|
- glGenBuffers(1, &vbo);
|
|
|
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
+ glGenBuffers(1, &tgr.vbo);
|
|
|
+ glBindBuffer(GL_ARRAY_BUFFER, tgr.vbo);
|
|
|
glBufferData(GL_ARRAY_BUFFER,
|
|
|
- (ssize_t)(glys.cap * sizeof(struct tile_glyph)),
|
|
|
- glys.items,
|
|
|
+ (ssize_t)(tgr.glyphs.cap * sizeof(struct tile_glyph)),
|
|
|
+ tgr.glyphs.items,
|
|
|
GL_DYNAMIC_DRAW);
|
|
|
glyph_attr_declare(tile_glyph_get_attrs(), TILE_GLYPH_ATTR_TOTAL);
|
|
|
- return (struct ret_uint32_t_uint32_t) {
|
|
|
- .f1 = vao,
|
|
|
- .f2 = vbo,
|
|
|
- };
|
|
|
+ return tgr;
|
|
|
}
|
|
|
|
|
|
-uint32_t
|
|
|
-tile_glyph_load_texture_atlas(const char *filepath)
|
|
|
+struct tile_glyph_render
|
|
|
+tile_glyph_render_load_texture_atlas_or_exit(struct tile_glyph_render tgr,
|
|
|
+ const char *filepath)
|
|
|
{
|
|
|
- uint32_t id;
|
|
|
int32_t w, h, n;
|
|
|
uint8_t *data = stbi_load(filepath,
|
|
|
&w, &h, &n,
|
|
|
@@ -255,8 +236,8 @@ tile_glyph_load_texture_atlas(const char *filepath)
|
|
|
}
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
- glGenTextures(1, &id);
|
|
|
- glBindTexture(GL_TEXTURE_2D, id);
|
|
|
+ glGenTextures(1, &tgr.font_tex);
|
|
|
+ glBindTexture(GL_TEXTURE_2D, tgr.font_tex);
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
|
GL_NEAREST);
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
|
@@ -270,18 +251,18 @@ tile_glyph_load_texture_atlas(const char *filepath)
|
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
data);
|
|
|
stbi_image_free(data);
|
|
|
- return id;
|
|
|
+ return tgr;
|
|
|
}
|
|
|
|
|
|
-struct ret_uint32_t_uint32_t
|
|
|
-tile_glyph_compile_shaders_or_exit(void)
|
|
|
+struct tile_glyph_render
|
|
|
+tile_glyph_compile_shaders_or_exit(struct tile_glyph_render tgr,
|
|
|
+ const char *vert_path, const char *frag_path)
|
|
|
{
|
|
|
- return (struct ret_uint32_t_uint32_t) {
|
|
|
- .f1 = shader_compile_file_or_exit("./shaders/tile_font.vert",
|
|
|
- GL_VERTEX_SHADER),
|
|
|
- .f2 = shader_compile_file_or_exit("./shaders/tile_font.frag",
|
|
|
- GL_FRAGMENT_SHADER),
|
|
|
- };
|
|
|
+ tgr.shaders.vert = shader_compile_file_or_exit(vert_path,
|
|
|
+ GL_VERTEX_SHADER);
|
|
|
+ tgr.shaders.frag = shader_compile_file_or_exit(frag_path,
|
|
|
+ GL_FRAGMENT_SHADER);
|
|
|
+ return tgr;
|
|
|
}
|
|
|
|
|
|
#endif /* defined(TILE_GLYPH_IMP) || defined(IMP) */
|