summaryrefslogtreecommitdiff
path: root/src/text.cc
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-12 22:06:02 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-12 22:06:02 +0100
commitd590442470f8d858c88517aa563cf3e74bd63b24 (patch)
treed76ef829a330370ecb90c819cad2323d3de6289c /src/text.cc
parentd10134d6c74cb9eef83956a8ec5ce4dce69b5ee0 (diff)
working on atlas and text rendering
Diffstat (limited to 'src/text.cc')
-rw-r--r--src/text.cc52
1 files changed, 36 insertions, 16 deletions
diff --git a/src/text.cc b/src/text.cc
index 58fedad..ab2155f 100644
--- a/src/text.cc
+++ b/src/text.cc
@@ -2,11 +2,12 @@
#include <cstddef>
+#include "atlas.h"
#include "freetype/freetype.h"
#include "shaders.h"
#include "utils.h"
-static void font_context_sampler_desc(struct sg_sampler_desc* desc) {
+static void wayc_font_context_sampler_desc(struct sg_sampler_desc* desc) {
wayc_notnull(desc);
desc->min_filter = SG_FILTER_LINEAR;
@@ -15,7 +16,7 @@ static void font_context_sampler_desc(struct sg_sampler_desc* desc) {
desc->wrap_v = SG_WRAP_CLAMP_TO_EDGE;
}
-static void font_context_layout(struct sg_vertex_layout_state* layout) {
+static void wayc_font_context_layout(struct sg_vertex_layout_state* layout) {
wayc_notnull(layout);
layout->buffers[0].stride = sizeof(text_vertex_s);
@@ -29,7 +30,7 @@ static void font_context_layout(struct sg_vertex_layout_state* layout) {
layout->attrs[ATTR_text_in_uv].offset = offsetof(text_vertex_s, uv);
}
-static void font_context_color(struct sg_color_target_state* color) {
+static void wayc_font_context_color(struct sg_color_target_state* color) {
wayc_notnull(color);
struct sg_blend_state blend = {};
@@ -42,18 +43,15 @@ static void font_context_color(struct sg_color_target_state* color) {
color->blend = blend;
}
-static void font_context_pipeline_desc(struct sg_pipeline_desc* desc) {
+static void font_context_pipeline_desc(struct sg_pipeline_desc* desc,
+ struct sg_shader shader) {
wayc_notnull(desc);
- const struct sg_shader_desc* shader_desc =
- text_shader_desc(sg_query_backend());
- struct sg_shader shader = sg_make_shader(shader_desc);
-
struct sg_vertex_layout_state vertex_layout = {};
- font_context_layout(&vertex_layout);
+ wayc_font_context_layout(&vertex_layout);
struct sg_color_target_state color = {};
- font_context_color(&color);
+ wayc_font_context_color(&color);
desc->layout = vertex_layout;
desc->shader = shader;
@@ -62,27 +60,49 @@ static void font_context_pipeline_desc(struct sg_pipeline_desc* desc) {
desc->color_count = 1;
}
-enum font_context_error_e font_context_init(struct font_context_s* context) {
+enum font_context_error_e wayc_font_context_init(
+ struct font_context_s* context) {
wayc_notnull(context);
+ bool success = false;
+
FT_Library ft;
FT_Error fterr = FT_Init_FreeType(&ft);
if (fterr) return FONT_CONTEXT_ERROR_LOAD_LIBRARY;
+ wayc_defer_cond(FT_Done_FreeType(ft), success, true);
+
+ struct sg_shader shader =
+ sg_make_shader(text_shader_desc(sg_query_backend()));
+ if (sg_query_shader_state(shader) != SG_RESOURCESTATE_VALID)
+ return FONT_CONTEXT_ERROR_CREATE_SHADER;
+ wayc_defer_cond(sg_destroy_shader(shader), success, true);
struct sg_sampler_desc sampler_desc = {};
- font_context_sampler_desc(&sampler_desc);
+ wayc_font_context_sampler_desc(&sampler_desc);
struct sg_pipeline_desc pipeline_desc = {};
- font_context_pipeline_desc(&pipeline_desc);
+ font_context_pipeline_desc(&pipeline_desc, shader);
+
+ struct sg_sampler sampler = sg_make_sampler(&sampler_desc);
+ if (sg_query_sampler_state(sampler) != SG_RESOURCESTATE_VALID)
+ return FONT_CONTEXT_ERROR_CREATE_SAMPLER;
+
+ wayc_defer_cond(sg_destroy_sampler(sampler), success, true);
+
+ struct sg_pipeline pipeline = sg_make_pipeline(&pipeline_desc);
+ if (sg_query_pipeline_state(pipeline) != SG_RESOURCESTATE_VALID)
+ return FONT_CONTEXT_ERROR_CREATE_PIPELINE;
context->ft = ft;
- context->sampler = sg_make_sampler(&sampler_desc);
- context->pipeline = sg_make_pipeline(&pipeline_desc);
+ context->sampler = sampler;
+ context->pipeline = pipeline;
+
+ success = true;
return FONT_CONTEXT_ERROR_NONE;
}
-void font_context_deinit(struct font_context_s* context) {
+void wayc_font_context_deinit(struct font_context_s* context) {
wayc_notnull(context);
sg_destroy_pipeline(context->pipeline);