diff options
| -rw-r--r-- | src/text.cc | 75 | ||||
| -rw-r--r-- | src/text.h | 16 | ||||
| -rw-r--r-- | src/wayclock.cc | 10 |
3 files changed, 100 insertions, 1 deletions
diff --git a/src/text.cc b/src/text.cc index 51ddc9b..4a01c8e 100644 --- a/src/text.cc +++ b/src/text.cc @@ -289,3 +289,78 @@ void wayc_text_mesh_deinit(struct text_mesh_s* mesh) { mesh->vertices = nullptr; mesh->indices = nullptr; } + +static inline void wayc_text_buf_desc(struct sg_buffer_desc* vbo, + struct sg_buffer_desc* ebo) { + wayc_notnull(vbo); + wayc_notnull(ebo); + + struct sg_buffer_usage usage = {}; + usage.vertex_buffer = true; + usage.dynamic_update = false; + usage.stream_update = true; + usage.immutable = false; + + vbo->usage = usage; + usage.vertex_buffer = false; + usage.index_buffer = true; + ebo->usage = usage; +} + +bool wayc_text_group_init(struct text_group_s* group, struct font_s* font, + usize glyph_capacity) { + wayc_notnull(group); + wayc_notnull(font); + memset(group, 0, sizeof(*group)); + + bool success = false; + + usize nverts = glyph_capacity * WAYC_QUAD_NVERTS; + usize nindices = glyph_capacity * WAYC_QUAD_NINDICES; + + usize verts_bytes = nverts * sizeof(struct text_vertex_s); + usize indices_bytes = nindices * sizeof(u16); + + struct sg_buffer_desc vbo_desc = {}; + struct sg_buffer_desc ebo_desc = {}; + wayc_text_buf_desc(&vbo_desc, &ebo_desc); + + vbo_desc.size = verts_bytes; + ebo_desc.size = indices_bytes; + + struct sg_buffer vbuf = sg_make_buffer(&vbo_desc); + if (sg_query_buffer_state(vbuf) != SG_RESOURCESTATE_VALID) return false; + wayc_defer_cond(sg_destroy_buffer(vbuf), success, true); + + struct sg_buffer ebuf = sg_make_buffer(&ebo_desc); + if (sg_query_buffer_state(ebuf) != SG_RESOURCESTATE_VALID) return false; + wayc_defer_cond(sg_destroy_buffer(ebuf), success, true); + + group->vbuf = vbuf; + group->ebuf = ebuf; + group->glyph_capacity = glyph_capacity; + group->font = font; + + success = true; + return success; +} + +void wayc_text_group_begin(struct text_group_s* group) { + wayc_notnull(group); + + group->index_count = 0; + group->glyph_count = 0; +} + +void wayc_text_group_end(struct text_group_s* group, + struct font_context_s* context) { + wayc_notnull(group); + wayc_notnull(context); +} + +void wayc_text_group_deinit(struct text_group_s* group) { + wayc_notnull(group); + + sg_destroy_buffer(group->vbuf); + sg_destroy_buffer(group->ebuf); +} @@ -79,3 +79,19 @@ struct text_mesh_s { bool wayc_text_mesh_init(struct text_mesh_s* mesh, const char* text, usize text_len, struct font_s* font, vec2 position); void wayc_text_mesh_deinit(struct text_mesh_s* mesh); + +struct text_group_s { + struct font_s* font; + sg_buffer vbuf; + sg_buffer ebuf; + usize index_count; + usize glyph_count; + usize glyph_capacity; +}; + +bool wayc_text_group_init(struct text_group_s* group, struct font_s* font, + usize glyph_capacity); +void wayc_text_group_begin(struct text_group_s* group); +void wayc_text_group_end(struct text_group_s* group, + struct font_context_s* context); +void wayc_text_group_deinit(struct text_group_s* group); diff --git a/src/wayclock.cc b/src/wayclock.cc index 390d9a1..45d22fe 100644 --- a/src/wayclock.cc +++ b/src/wayclock.cc @@ -19,6 +19,8 @@ struct app_s { struct renderer_s* renderer; + struct text_group_s* text_group; + struct font_context_s* font_context; struct font_s* font; }; @@ -38,6 +40,8 @@ void wayc_frame(struct app_s* app, struct window_s* window, wayc_defer(wayc_text_mesh_deinit(&text_mesh)); wayc_renderer_begin(renderer); + wayc_text_group_begin(app->text_group); + wayc_text_group_end(app->text_group, app->font_context); wayc_renderer_end(renderer); } @@ -111,7 +115,11 @@ int main() { wayc_panic("Failed to initialize font"); wayc_defer(wayc_font_deinit(&font)); - struct app_s app = {&renderer, &font}; + struct text_group_s text_group; + if (!wayc_text_group_init(&text_group, &font, 128)) + wayc_panic("Failed to initialize text group"); + + struct app_s app = {&renderer, &text_group, &font_context, &font}; while (wayc_eventloop_running(&loop)) { wayc_eventloop_update(&loop, (u8*)&app); |
