summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--src/geometry.h50
-rw-r--r--src/graphics.cc18
-rw-r--r--src/graphics.h12
-rw-r--r--src/rendering.cc183
-rw-r--r--src/rendering.h138
-rw-r--r--src/text.cc56
-rw-r--r--src/text.h6
-rw-r--r--src/wayclock.cc12
9 files changed, 91 insertions, 385 deletions
diff --git a/Makefile b/Makefile
index 8f78c56..286ffdb 100644
--- a/Makefile
+++ b/Makefile
@@ -84,7 +84,6 @@ SOURCES = \
$(SRC_DIR)/window.c \
$(SRC_DIR)/graphics.cc \
$(SRC_DIR)/text.cc \
- $(SRC_DIR)/rendering.cc \
$(SRC_DIR)/gfx.c \
$(GLAD_DIR)/glad.c \
$(HASHMAP_SOURCE)
diff --git a/src/geometry.h b/src/geometry.h
new file mode 100644
index 0000000..d15b8ae
--- /dev/null
+++ b/src/geometry.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <cglm/cglm.h>
+
+#include "cglm/types.h"
+#include "glad.h"
+#include "utils.h"
+
+struct quad_s {
+ vec2 vertices[WAYC_QUAD_NVERTS];
+ u32 indices[WAYC_QUAD_NINDICES];
+};
+
+static inline void wayc_quad_init(quad_s* quad, f32 left, f32 top, f32 right,
+ f32 bottom) {
+ wayc_notnull(quad);
+
+ /*
+ 0 ---- 1
+ | |
+ | |
+ 3 ---- 2
+ */
+
+ // Vertices (CCW order)
+ WAYC_X(quad->vertices[0]) = left;
+ WAYC_Y(quad->vertices[0]) = top;
+
+ WAYC_X(quad->vertices[1]) = right;
+ WAYC_Y(quad->vertices[1]) = top;
+
+ WAYC_X(quad->vertices[2]) = right;
+ WAYC_Y(quad->vertices[2]) = bottom;
+
+ WAYC_X(quad->vertices[3]) = left;
+ WAYC_Y(quad->vertices[3]) = bottom;
+
+ /*
+ Triangles (CCW):
+ 0 1 2
+ 0 2 3
+ */
+
+ quad->indices[0] = 0;
+ quad->indices[1] = 1;
+ quad->indices[2] = 2;
+ quad->indices[3] = 0;
+ quad->indices[4] = 2;
+ quad->indices[5] = 3;
+}
diff --git a/src/graphics.cc b/src/graphics.cc
index 23a5ee5..a35e9d0 100644
--- a/src/graphics.cc
+++ b/src/graphics.cc
@@ -4,7 +4,6 @@
#include <EGL/eglplatform.h>
#include <glad.h>
-#include <cstdio>
#include <cstring>
#include "cglm/vec4.h"
@@ -45,18 +44,6 @@ static bool wayc_graphics_config(EGLDisplay display, EGLConfig* config) {
return nconfs > 0;
}
-static void wayc_sokol_log(const char* msg, uint32_t x, uint32_t y,
- const char* file, uint32_t line, const char* func,
- void* user) {
- wayc_notnull(msg);
- wayc_notnull(file);
- wayc_notnull(func);
-
- (void)user;
-
- fprintf(stderr, "[%s:%d:%s] %s (%d, %d)\n", file, line, func, msg, x, y);
-}
-
enum graphics_error_e wayc_graphics_init(struct graphics_s* graphics,
struct wlstate_s* state) {
wayc_notnull(graphics);
@@ -144,12 +131,7 @@ enum renderer_error_e wayc_renderer_init(struct renderer_s* renderer,
return RENDERER_ERROR_LOAD_FUNCTIONS;
}
- sg_logger logger = {};
- logger.func = wayc_sokol_log;
-
sg_desc desc = {};
- desc.logger = logger;
-
sg_setup(&desc);
return RENDERER_ERROR_NONE;
diff --git a/src/graphics.h b/src/graphics.h
index f8398a2..f4beae7 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -51,10 +51,14 @@ static inline void wayc_renderer_resize(struct renderer_s* renderer, i32 width,
i32 height) {
wayc_notnull(renderer);
- renderer->width = width;
- renderer->height = height;
- wl_egl_window_resize(renderer->ewindow, width, height, 0, 0);
- glViewport(0, 0, width, height);
+ u32 safe_width = wayc_max(width, 1);
+ u32 safe_height = wayc_max(height, 1);
+
+ renderer->width = safe_width;
+ renderer->height = safe_height;
+
+ wl_egl_window_resize(renderer->ewindow, safe_width, safe_height, 0, 0);
+ glViewport(0, 0, safe_width, safe_height);
}
static inline void wayc_renderer_use(struct renderer_s* renderer) {
diff --git a/src/rendering.cc b/src/rendering.cc
deleted file mode 100644
index 8127c13..0000000
--- a/src/rendering.cc
+++ /dev/null
@@ -1,183 +0,0 @@
-#include "rendering.h"
-
-#include <glad.h>
-
-#include <cstring>
-
-#include "cglm/types.h"
-#include "utils.h"
-
-bool wayc_image_init(texture_t* texture, image_type_e type,
- image_format_e format, u32 width, u32 height) {
- wayc_notnull(texture);
-
- glCreateTextures(type, 1, texture);
- if (*texture == 0) return false;
-
- switch (type) {
- case IMAGE_TYPE_2D:
- // TODO: add error checking
- glTextureStorage2D(*texture, 1, format, width, height);
- break;
- default:
- break;
- }
-
- return true;
-}
-
-bool wayc_image_upload(texture_t texture, ivec2 offset, u32 width, u32 height,
- image_type_e type, image_format_e format,
- image_data_type_e data_type, i32 alignment,
- const u8* data) {
- wayc_notnull(data);
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
-
- switch (type) {
- case IMAGE_TYPE_2D:
- // TODO: add error checking
- glTextureSubImage2D(texture, 0, offset[0], offset[1], width, height,
- format, data_type, data);
- break;
- default:
- break;
- }
-
- return true;
-}
-
-void wayc_image_use(texture_t texture, u32 slot) {
- glBindTextureUnit(slot, texture);
-}
-
-void wayc_image_deinit(texture_t* texture) {
- wayc_notnull(texture);
- glDeleteTextures(1, texture);
-}
-
-bool wayc_sampler_init(sampler_t* sampler, sample_filter_e filter,
- sample_wrap_e wrap) {
- wayc_notnull(sampler);
-
- glCreateSamplers(1, sampler);
- if (*sampler == 0) return false;
-
- glSamplerParameteri(*sampler, GL_TEXTURE_MIN_FILTER, filter);
- glSamplerParameteri(*sampler, GL_TEXTURE_MAG_FILTER, filter);
- glSamplerParameteri(*sampler, GL_TEXTURE_WRAP_S, wrap);
- glSamplerParameteri(*sampler, GL_TEXTURE_WRAP_T, wrap);
-
- return true;
-}
-
-void wayc_sampler_use(sampler_t sampler, u32 slot) {
- glBindSampler(slot, sampler);
-}
-
-void wayc_sampler_deinit(sampler_t* sampler) { glDeleteSamplers(1, sampler); }
-
-bool wayc_atlas_init(atlas_s* atlas, image_format_e format, u32 width,
- u32 height) {
- wayc_notnull(atlas);
- memset(atlas, 0, sizeof(*atlas));
-
- bool success = false;
-
- texture_t texture;
- bool ok = wayc_image_init(&texture, IMAGE_TYPE_2D, format, width, height);
- if (!ok) return false;
- wayc_defer_cond(wayc_image_deinit(&texture);, success, true);
-
- sampler_t sampler;
- ok = wayc_sampler_init(&sampler, SAMPLE_FILTER_NEAREST,
- SAMPLE_WRAP_CLAMP_TO_EDGE);
- if (!ok) return false;
-
- atlas->texture = texture;
- atlas->sampler = sampler;
- atlas->width = width;
- atlas->height = height;
-
- success = true;
- return true;
-}
-
-void wayc_atlas_use(atlas_s* atlas, u32 slot) {
- wayc_image_use(atlas->texture, slot);
- wayc_sampler_use(atlas->sampler, slot);
-}
-
-bool wayc_atlas_set(atlas_s* atlas, image_data_type_e data_type, ivec2 offset,
- u32 width, u32 height, i32 alignment, const u8* data) {
- wayc_notnull(atlas);
- wayc_notnull(data);
-
- u32 final_x = offset[0] + width;
- u32 final_y = offset[1] + height;
- if (final_x > atlas->width || final_y > atlas->height) return false;
-
- wayc_image_upload(atlas->texture, offset, width, height, IMAGE_TYPE_2D,
- atlas->format, data_type, alignment, data);
- return true;
-}
-
-void wayc_atlas_deinit(atlas_s* atlas) {
- wayc_notnull(atlas);
- wayc_sampler_deinit(&atlas->sampler);
- wayc_image_deinit(&atlas->texture);
-}
-
-static inline void wayc_atlas_packer_wrap(struct atlas_packer_s* packer) {
- WAYC_X(packer->cursor) = 0;
- WAYC_Y(packer->cursor) += packer->row_height;
- packer->row_height = 0;
-}
-
-bool wayc_atlas_packer_allocate(struct atlas_packer_s* packer, u32 width,
- u32 height, ivec2 out) {
- wayc_notnull(packer);
-
- struct atlas_s* atlas = packer->atlas;
- wayc_notnull(atlas);
-
- if (width > atlas->width || height > atlas->height) return false;
-
- if (WAYC_X(packer->cursor) + width > atlas->width)
- wayc_atlas_packer_wrap(packer);
-
- if (WAYC_Y(packer->cursor) + height > atlas->height) return false;
-
- glm_ivec2_copy(packer->cursor, out);
- WAYC_X(packer->cursor) += width;
- packer->row_height = wayc_max(packer->row_height, height);
-
- return true;
-}
-
-bool wayc_buffer_init(buffer_t* buffer, usize size) {
- wayc_notnull(buffer);
-
- glCreateBuffers(1, buffer);
- if (*buffer == 0) return false;
-
- glNamedBufferStorage(*buffer, size, nullptr, GL_DYNAMIC_STORAGE_BIT);
- return true;
-}
-
-bool wayc_buffer_set(buffer_t buffer, usize offset, usize size,
- const u8* data) {
- wayc_notnull(data);
-
- glNamedBufferSubData(buffer, offset, size, data); // TODO: add error checking
- return true;
-}
-
-void wayc_buffer_use(buffer_t buffer, buffer_type_e type) {
- glBindBuffer(type, buffer);
-}
-
-void wayc_buffer_deinit(buffer_t* buffer) {
- wayc_notnull(buffer);
- glDeleteBuffers(1, buffer);
-}
diff --git a/src/rendering.h b/src/rendering.h
deleted file mode 100644
index 45a65c4..0000000
--- a/src/rendering.h
+++ /dev/null
@@ -1,138 +0,0 @@
-#pragma once
-
-#include <cglm/cglm.h>
-
-#include "cglm/types.h"
-#include "glad.h"
-#include "utils.h"
-
-struct quad_s {
- vec2 vertices[WAYC_QUAD_NVERTS];
- u32 indices[WAYC_QUAD_NINDICES];
-};
-
-static inline void wayc_quad_init(quad_s* quad, f32 left, f32 top, f32 right,
- f32 bottom) {
- wayc_notnull(quad);
-
- /*
- 0 ---- 1
- | |
- | |
- 3 ---- 2
- */
-
- // Vertices (CCW order)
- WAYC_X(quad->vertices[0]) = left;
- WAYC_Y(quad->vertices[0]) = top;
-
- WAYC_X(quad->vertices[1]) = right;
- WAYC_Y(quad->vertices[1]) = top;
-
- WAYC_X(quad->vertices[2]) = right;
- WAYC_Y(quad->vertices[2]) = bottom;
-
- WAYC_X(quad->vertices[3]) = left;
- WAYC_Y(quad->vertices[3]) = bottom;
-
- /*
- Triangles (CCW):
- 0 1 2
- 0 2 3
- */
-
- quad->indices[0] = 0;
- quad->indices[1] = 1;
- quad->indices[2] = 2;
- quad->indices[3] = 0;
- quad->indices[4] = 2;
- quad->indices[5] = 3;
-}
-
-// TODO: review all structs and use types
-
-enum image_type_e {
- IMAGE_TYPE_2D = GL_TEXTURE_2D,
-};
-
-enum image_format_e {
- IMAGE_FORMAT_RGBA = GL_RGBA,
- IMAGE_FORMAT_RED = GL_RED,
-};
-
-enum image_data_type_e {
- IMAGE_DATA_TYPE_UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
-};
-
-enum {
- IMAGE_FORMAT_RGBA_ALIGNMENT = 4,
- IMAGE_FORMAT_RED_ALIGNMENT = 1,
-};
-
-typedef u32 texture_t;
-
-bool wayc_image_init(texture_t* texture, image_type_e type,
- image_format_e format, u32 width, u32 height);
-
-bool wayc_image_upload(texture_t texture, ivec2 offset, u32 width, u32 height,
- image_type_e type, image_format_e format,
- image_data_type_e data_type, i32 alignment,
- const u8* data);
-
-void wayc_image_use(texture_t texture, u32 slot);
-void wayc_image_deinit(texture_t* texture);
-
-enum sample_filter_e {
- SAMPLE_FILTER_NEAREST = GL_NEAREST,
- SAMPLE_FILTER_LINEAR = GL_LINEAR,
-};
-
-enum sample_wrap_e {
- SAMPLE_WRAP_CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
- SAMPLE_WRAP_REPEAT = GL_REPEAT,
-};
-
-typedef u32 sampler_t;
-
-bool wayc_sampler_init(sampler_t* sampler, sample_filter_e filter,
- sample_wrap_e wrap);
-void wayc_sampler_use(sampler_t sampler, u32 slot);
-void wayc_sampler_deinit(sampler_t* sampler);
-
-struct atlas_s {
- texture_t texture;
- sampler_t sampler;
- u32 width, height;
- image_format_e format;
-};
-
-bool wayc_atlas_init(atlas_s* atlas, image_format_e format, u32 width,
- u32 height);
-void wayc_atlas_use(atlas_s* atlas, u32 slot);
-bool wayc_atlas_set(atlas_s* atlas, image_data_type_e data_type, ivec2 offset,
- u32 width, u32 height, i32 alignment, const u8* data);
-void wayc_atlas_deinit(atlas_s* atlas);
-
-struct atlas_packer_s {
- struct atlas_s* atlas;
- u32 row_height;
- ivec2 cursor;
-};
-
-#define WAYC_ATLAS_PACKER_INIT(atlas) \
- atlas_packer_s { atlas, 0, {0, 0} }
-
-bool wayc_atlas_packer_allocate(struct atlas_packer_s* packer, u32 width,
- u32 height, ivec2 out);
-
-enum buffer_type_e {
- BUFFER_TYPE_VERTEX = GL_ARRAY_BUFFER,
- BUFFER_TYPE_INDEX = GL_ELEMENT_ARRAY_BUFFER,
-};
-
-typedef u32 buffer_t;
-
-bool wayc_buffer_init(buffer_t* buffer, usize size);
-bool wayc_buffer_set(buffer_t buffer, usize offset, usize size, const u8* data);
-void wayc_buffer_use(buffer_t buffer, buffer_type_e type);
-void wayc_buffer_deinit(buffer_t* buffer);
diff --git a/src/text.cc b/src/text.cc
index cac4a42..b52e9fe 100644
--- a/src/text.cc
+++ b/src/text.cc
@@ -7,8 +7,8 @@
#include "cglm/types.h"
#include "freetype/freetype.h"
+#include "geometry.h"
#include "hash.h"
-#include "rendering.h"
#include "utils.h"
static bool wayc_font_cache_lookup(struct font_s* font, codepoint_t codepoint,
@@ -18,12 +18,12 @@ static bool wayc_font_cache_lookup(struct font_s* font, codepoint_t codepoint,
if (ok) *out = glyph;
return ok;
}
-
+/*
static void wayc_font_cache_insert(struct font_s* font, codepoint_t codepoint,
struct glyph_s glyph) {
wayc_notnull(font);
wayc_hashmap_insert(&font->cache, &codepoint, &glyph);
-}
+} */
enum font_context_error_e wayc_font_context_init(struct font_context_s* ctx) {
wayc_notnull(ctx);
@@ -76,13 +76,10 @@ enum font_error_e wayc_font_init(struct font_s* font,
return FONT_ERROR_NONE;
}
-enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas,
- struct atlas_packer_s* packer,
- codepoint_t codepoint, struct glyph_s* out) {
+enum font_error_e wayc_font_lookup(struct font_s* font, codepoint_t codepoint,
+ struct glyph_s* out) {
wayc_notnull(font);
- wayc_notnull(atlas);
wayc_notnull(out);
-
FT_Face face = font->face;
bool found = wayc_font_cache_lookup(font, codepoint, out);
@@ -98,29 +95,32 @@ enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas,
FT_Bitmap bitmap = slot->bitmap;
if (bitmap.pitch != (i32)bitmap.width) wayc_panic("unsupported pitch");
+ /*
ivec2 size = {(i32)bitmap.width, (i32)bitmap.rows};
- ivec2 uv0;
+ ivec2 uv0;
- bool ok = wayc_atlas_packer_allocate(packer, WAYC_X(size), WAYC_Y(size), uv0);
- if (!ok) return FONT_ERROR_ATLAS_FULL;
+ bool ok = wayc_atlas_packer_allocate(packer, WAYC_X(size), WAYC_Y(size),
+ uv0); if (!ok) return FONT_ERROR_ATLAS_FULL;
- ok = wayc_atlas_set(atlas, IMAGE_DATA_TYPE_UNSIGNED_BYTE, uv0, WAYC_X(size),
- WAYC_Y(size), IMAGE_FORMAT_RED_ALIGNMENT, bitmap.buffer);
- if (!ok) return FONT_ERROR_ATLAS_FULL;
+ ok = wayc_atlas_set(atlas, IMAGE_DATA_TYPE_UNSIGNED_BYTE, uv0, WAYC_X(size),
+ WAYC_Y(size), IMAGE_FORMAT_RED_ALIGNMENT,
+ bitmap.buffer); if (!ok) return FONT_ERROR_ATLAS_FULL;
- struct glyph_s glyph = {};
- glyph.bearing_x = slot->bitmap_left;
- glyph.bearing_y = slot->bitmap_top;
- glyph.advance = (f32)slot->advance.x / WAYC_SCALE;
+ struct glyph_s glyph = {};
+ glyph.bearing_x = slot->bitmap_left;
+ glyph.bearing_y = slot->bitmap_top;
+ glyph.advance = (f32)slot->advance.x / WAYC_SCALE;
- glyph.uv0[0] = uv0[0];
- glyph.uv0[1] = uv0[1];
- glyph.uv1[0] = uv0[0] + size[0];
- glyph.uv1[1] = uv0[1] + size[1];
+ glyph.uv0[0] = uv0[0];
+ glyph.uv0[1] = uv0[1];
+ glyph.uv1[0] = uv0[0] + size[0];
+ glyph.uv1[1] = uv0[1] + size[1];
- wayc_font_cache_insert(font, codepoint, glyph);
+ wayc_font_cache_insert(font, codepoint, glyph);
+ *out = glyph;
- *out = glyph;
+ */
+ wayc_panic("Atlas packing not implemented yet");
return FONT_ERROR_NONE;
}
@@ -131,7 +131,7 @@ void wayc_font_deinit(struct font_s* font) {
FT_Done_Face(font->face);
mi_free(font->data);
}
-
+/*
static void wayc_text_assemble_one(const struct glyph_s* glyph, vec2* pen,
f32 atlas_width, f32 atlas_height,
struct text_vertex_s* out_verts,
@@ -167,7 +167,7 @@ static void wayc_text_assemble_one(const struct glyph_s* glyph, vec2* pen,
#pragma GCC unroll 6
for (usize i = 0; i < WAYC_QUAD_NINDICES; ++i)
out_indices[i] = base + (text_index_t)quad.indices[i];
-}
+} */
enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
text_index_t** out_indices,
@@ -177,8 +177,8 @@ enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
wayc_notnull(out_indices);
wayc_notnull(out_verts);
+ /*
bool success = false;
-
usize glyph_count = wayc_string_length(&text->string);
usize vertex_count = glyph_count * WAYC_QUAD_NVERTS;
@@ -225,5 +225,7 @@ enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
*out_verts = vertices;
success = true;
+ */
+ wayc_panic("Text assembly not implemented yet");
return TEXT_ERROR_NONE;
} \ No newline at end of file
diff --git a/src/text.h b/src/text.h
index 30a8893..9ea7d0d 100644
--- a/src/text.h
+++ b/src/text.h
@@ -3,7 +3,6 @@
#include "cglm/types.h"
#include "freetype/freetype.h"
#include "hash.h"
-#include "rendering.h"
#include "utils.h"
typedef u32 codepoint_t;
@@ -55,9 +54,8 @@ struct font_s {
enum font_error_e wayc_font_init(struct font_s* font,
struct font_context_s* ctx, u32 fsize,
const char* path);
-enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas,
- struct atlas_packer_s* packer,
- codepoint_t codepoint, struct glyph_s* out);
+enum font_error_e wayc_font_lookup(struct font_s* font, codepoint_t codepoint,
+ struct glyph_s* out);
void wayc_font_deinit(struct font_s* font);
enum text_error_e {
diff --git a/src/wayclock.cc b/src/wayclock.cc
index 6fa5222..37b4fa9 100644
--- a/src/wayclock.cc
+++ b/src/wayclock.cc
@@ -2,7 +2,6 @@
#include "events.h"
#include "graphics.h"
-#include "rendering.h"
#include "text.h"
#include "utils.h"
#include "window.h"
@@ -107,18 +106,11 @@ int main() {
wayc_defer(wayc_renderer_deinit(&renderer));
- wayc_renderer_use(&renderer); // << make current
-
- struct atlas_s atlas;
- if (!wayc_atlas_init(&atlas, IMAGE_FORMAT_RED, WAYC_ATLAS_WIDTH,
- WAYC_ATLAS_HEIGHT))
- wayc_panic("Failed to initialize atlas");
-
- wayc_defer(wayc_atlas_deinit(&atlas));
+ wayc_renderer_use(&renderer);
wayc_window_redraw(&window, &loop);
- struct app_s app = {&renderer, &fctx, &font, &atlas};
+ struct app_s app = {&renderer, &fctx, &font, nullptr};
while (wayc_eventloop_running(&loop)) {
wayc_eventloop_update(&loop, (u8*)&app);