summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/rendering.cc53
-rw-r--r--src/rendering.h13
-rw-r--r--src/wayclock.cc16
3 files changed, 69 insertions, 13 deletions
diff --git a/src/rendering.cc b/src/rendering.cc
index 2c05c8e..e12026a 100644
--- a/src/rendering.cc
+++ b/src/rendering.cc
@@ -2,10 +2,15 @@
#include <glad.h>
+#include <cstring>
+
+#include "cglm/ivec2.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;
@@ -24,6 +29,8 @@ bool wayc_image_init(texture_t* texture, image_type_e type,
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, const u8* data) {
+ wayc_notnull(data);
+
switch (type) {
case IMAGE_TYPE_2D:
// TODO: add error checking
@@ -41,10 +48,15 @@ void wayc_image_use(texture_t texture, u32 slot) {
glBindTextureUnit(slot, texture);
}
-void wayc_image_deinit(texture_t* texture) { glDeleteTextures(1, 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;
@@ -62,9 +74,40 @@ void wayc_sampler_use(sampler_t sampler, u32 slot) {
void wayc_sampler_deinit(sampler_t* sampler) { glDeleteSamplers(1, sampler); }
-bool wayc_atlas_init(atlas_t* atlas, image_format_e format, u32 width,
- u32 height) {
- return wayc_image_init(atlas, IMAGE_TYPE_2D, format, width, height);
+bool wayc_atlas_init(atlas_s* atlas, 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, IMAGE_FORMAT_RGBA, 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;
+ glm_ivec2_zero(atlas->cursor);
+
+ success = true;
+ return true;
}
-void wayc_atlas_deinit(atlas_t* atlas) { wayc_image_deinit(atlas); }
+void wayc_atlas_use(atlas_s atlas, u32 slot) {
+ wayc_image_use(atlas.texture, slot);
+ wayc_sampler_use(atlas.sampler, slot);
+}
+
+void wayc_atlas_deinit(atlas_s* atlas) {
+ wayc_notnull(atlas);
+ wayc_sampler_deinit(&atlas->sampler);
+ wayc_image_deinit(&atlas->texture);
+}
diff --git a/src/rendering.h b/src/rendering.h
index 5896697..7609b8a 100644
--- a/src/rendering.h
+++ b/src/rendering.h
@@ -52,8 +52,13 @@ bool wayc_sampler_init(sampler_t* sampler, sample_filter_e filter,
void wayc_sampler_use(sampler_t sampler, u32 slot);
void wayc_sampler_deinit(sampler_t* sampler);
-typedef texture_t atlas_t;
+struct atlas_s {
+ texture_t texture;
+ sampler_t sampler;
+ ivec2 cursor;
+ u32 width, height;
+};
-bool wayc_atlas_init(atlas_t* atlas, image_format_e format, u32 width,
- u32 height);
-void wayc_atlas_deinit(atlas_t* atlas);
+bool wayc_atlas_init(atlas_s* atlas, u32 width, u32 height);
+void wayc_atlas_use(atlas_s atlas, u32 slot);
+void wayc_atlas_deinit(atlas_s* atlas);
diff --git a/src/wayclock.cc b/src/wayclock.cc
index 0852176..84a188e 100644
--- a/src/wayclock.cc
+++ b/src/wayclock.cc
@@ -3,6 +3,7 @@
#include "events.h"
#include "glad.h"
#include "graphics.h"
+#include "rendering.h"
#include "text.h"
#include "utils.h"
#include "window.h"
@@ -11,10 +12,14 @@
#define WAYC_APP_WIDTH 1280
#define WAYC_APP_HEIGHT 1080
#define WAYC_FONT "AdwaitaMono-Regular.ttf"
+#define WAYC_ATLAS_WIDTH 512
+#define WAYC_ATLAS_HEIGHT 512
struct app_s {
struct renderer_s* renderer;
struct font_context_s* fctx;
+ struct font_s* font;
+ struct atlas_s* atlas;
};
void wayc_frame(struct renderer_s* renderer, struct window_s* window,
@@ -68,6 +73,12 @@ int main() {
wayc_defer(wayc_font_deinit(&font));
+ struct atlas_s atlas;
+ if (!wayc_atlas_init(&atlas, WAYC_ATLAS_WIDTH, WAYC_ATLAS_HEIGHT))
+ wayc_panic("Failed to initialize atlas");
+
+ wayc_defer(wayc_atlas_deinit(&atlas));
+
struct eventloop_s loop;
if (wayc_eventloop_init(&loop, wayc_handle) != EVENTLOOP_ERROR_NONE)
wayc_panic("Failed to initialize event loop");
@@ -102,10 +113,7 @@ int main() {
wayc_renderer_use(&renderer);
wayc_window_redraw(&window, &loop);
- struct app_s app = {
- &renderer,
- &fctx,
- };
+ struct app_s app = {&renderer, &fctx, &font, &atlas};
while (wayc_eventloop_running(&loop)) {
wayc_eventloop_update(&loop, (u8*)&app);