diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-11 21:27:25 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-11 21:27:25 +0100 |
| commit | 600c704e09dc24ea259fc72c63287d07a0a4bd39 (patch) | |
| tree | 7c866dc5cedacbf176d4014f69bc0cdd1f098715 /src/rendering.cc | |
| parent | f6a9ffd92b8470a32fb4ebd6342dea4b9539f586 (diff) | |
working on atlas
Diffstat (limited to 'src/rendering.cc')
| -rw-r--r-- | src/rendering.cc | 53 |
1 files changed, 48 insertions, 5 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); +} |
