From a4cb50687e687eb6e986d3125b14fbb0b4ffd02f Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 30 Jul 2024 18:31:38 +0300 Subject: [PATCH] private the texture.c/.h, remove vestigial circle hash table from ctx --- CMakeLists.txt | 2 +- src/context.h | 2 +- src/main.c | 4 +-- src/rendering.c | 2 +- src/rendering.h | 6 ---- src/rendering/internal_api.h | 9 +++++- src/rendering/sprites.h | 2 +- src/rendering/triangles.h | 2 +- src/textures.h | 55 ----------------------------------- src/textures/internal_api.h | 44 ++++++++++++++++++++++++++++ src/{ => textures}/textures.c | 9 +++--- 11 files changed, 63 insertions(+), 74 deletions(-) delete mode 100644 src/textures.h rename src/{ => textures}/textures.c (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4ca5ef..67f9c0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,10 +44,10 @@ set(TOWNENGINE_SOURCE_FILES src/audio.c src/audio.h src/util.c src/util.h src/rendering.c src/rendering.h - src/textures.c src/textures.h src/input.c src/input.h src/text.c src/text.h src/camera.c src/camera.h + src/textures/textures.c ${SYSTEM_SOURCE_FILES} ) diff --git a/src/context.h b/src/context.h index a36e59a..cbc3193 100644 --- a/src/context.h +++ b/src/context.h @@ -2,7 +2,7 @@ #define CONTEXT_H -#include "textures.h" +#include "textures/internal_api.h" #include "input.h" #include diff --git a/src/main.c b/src/main.c index 5da23bb..9e88fca 100644 --- a/src/main.c +++ b/src/main.c @@ -2,9 +2,10 @@ #include "rendering.h" #include "input.h" #include "util.h" -#include "textures.h" #include "game_api.h" #include "audio/internal_api.h" +#include "textures/internal_api.h" +#include "rendering/internal_api.h" #include #include @@ -299,7 +300,6 @@ static bool initialize(void) { /* rendering */ /* these are dynamic arrays and will be allocated lazily by stb_ds */ ctx.render_queue_2d = NULL; - ctx.circle_radius_hash = NULL; textures_cache_init(&ctx.texture_cache, ctx.window); if (TTF_Init() < 0) { diff --git a/src/rendering.c b/src/rendering.c index e561e7b..973afa3 100644 --- a/src/rendering.c +++ b/src/rendering.c @@ -2,8 +2,8 @@ #include "rendering/sprites.h" #include "rendering/triangles.h" #include "rendering/circles.h" +#include "textures/internal_api.h" #include "context.h" -#include "textures.h" #include #include diff --git a/src/rendering.h b/src/rendering.h index 48221b6..e56387e 100644 --- a/src/rendering.h +++ b/src/rendering.h @@ -20,9 +20,6 @@ typedef struct push_sprite_args { bool, flip_y ) } t_push_sprite_args; -/* clears all render queues */ -void render_queue_clear(void); - /* pushes a sprite onto the sprite render queue */ /* this is a simplified version of push_sprite_ex for the most common case. */ /* it assumes you want no color modulation, no rotation, no flip */ @@ -68,7 +65,4 @@ void unfurl_triangle(const char *path, /* pushes a camera state to be used for all future unfurl_* commands */ void set_camera(const t_camera *camera); -/* renders the background, then the primitives in all render queues */ -void render(void); - #endif diff --git a/src/rendering/internal_api.h b/src/rendering/internal_api.h index 36abc6c..b3c8b03 100644 --- a/src/rendering/internal_api.h +++ b/src/rendering/internal_api.h @@ -1,7 +1,8 @@ #ifndef RENDERING_INTERNAL_API_H #define RENDERING_INTERNAL_API_H -#include "../textures.h" + #include "../util.h" +#include "../textures/internal_api.h" #include #include @@ -78,4 +79,10 @@ struct mesh_batch_item { struct mesh_batch value; }; +/* renders the background, then the primitives in all render queues */ +void render(void); + +/* clears all render queues */ +void render_queue_clear(void); + #endif diff --git a/src/rendering/sprites.h b/src/rendering/sprites.h index 2eb9842..849422f 100644 --- a/src/rendering/sprites.h +++ b/src/rendering/sprites.h @@ -2,10 +2,10 @@ #ifndef SPRITES_H #define SPRITES_H -#include "../textures.h" #include "../rendering.h" #include "../context.h" #include "../util.h" +#include "../textures/internal_api.h" #include "quad_element_buffer.h" #include "internal_api.h" diff --git a/src/rendering/triangles.h b/src/rendering/triangles.h index 4d8eeca..e26c1b2 100644 --- a/src/rendering/triangles.h +++ b/src/rendering/triangles.h @@ -2,9 +2,9 @@ #ifndef TRIANGLES_H #define TRIANGLES_H -#include "../textures.h" #include "../context.h" #include "internal_api.h" +#include "../textures/internal_api.h" #include diff --git a/src/textures.h b/src/textures.h deleted file mode 100644 index c5f91a0..0000000 --- a/src/textures.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef TEXTURES_H -#define TEXTURES_H - -#include "textures/internal_api.h" -#include "textures/modes.h" -#include "util.h" - -#include -#include - -/* type safe structure for persistent texture handles */ -typedef struct { uint16_t id; } t_texture_key; - -/* tests whether given key structure corresponds to any texture */ -#define m_texture_key_is_valid(p_key) ((p_key).id != (uint16_t)-1) - -void textures_cache_init(struct texture_cache *cache, SDL_Window *window); -void textures_cache_deinit(struct texture_cache *cache); - -/* for debugging */ -void textures_dump_atlases(struct texture_cache *cache); - -/* loads an image if it isn't in the cache, otherwise a no-op. */ -/* can be called from anywhere at any time after init, useful if you want to */ -/* preload textures you know will definitely be used */ -// void textures_load(struct texture_cache *cache, const char *path); - -/* repacks the current texture atlas based on the texture cache if needed */ -/* any previously returned srcrect results are invalidated after that */ -/* call it every time before rendering */ -void textures_update_atlas(struct texture_cache *cache); - -/* returns a persistent handle to some texture in cache, loading it if needed */ -/* check the result with m_texture_key_is_valid() */ -t_texture_key textures_get_key(struct texture_cache *cache, const char *path); - -/* returns a rect in a texture cache of the given key */ -t_frect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key); - -/* returns a rect of dimensions of the whole texture (whole atlas) */ -t_frect textures_get_dims(const struct texture_cache *cache, t_texture_key key); - -/* returns an identifier that is equal for all textures placed in the same atlas */ -int32_t textures_get_atlas_id(const struct texture_cache *cache, t_texture_key key); - -/* binds atlas texture in opengl state */ -void textures_bind(const struct texture_cache *cache, t_texture_key key, GLenum target); - -/* returns helpful information about contents of alpha channel in given texture */ -enum texture_mode textures_get_mode(const struct texture_cache *cache, t_texture_key key); - -/* returns the number of atlases in the cache */ -size_t textures_get_num_atlases(const struct texture_cache *cache); - -#endif diff --git a/src/textures/internal_api.h b/src/textures/internal_api.h index f3688da..312a3bd 100644 --- a/src/textures/internal_api.h +++ b/src/textures/internal_api.h @@ -39,4 +39,48 @@ struct texture_cache { bool is_dirty; /* current atlas needs to be recreated */ }; +/* type safe structure for persistent texture handles */ +typedef struct { uint16_t id; } t_texture_key; + +/* tests whether given key structure corresponds to any texture */ +#define m_texture_key_is_valid(p_key) ((p_key).id != (uint16_t)-1) + +void textures_cache_init(struct texture_cache *cache, SDL_Window *window); +void textures_cache_deinit(struct texture_cache *cache); + +/* for debugging */ +void textures_dump_atlases(struct texture_cache *cache); + +/* loads an image if it isn't in the cache, otherwise a no-op. */ +/* can be called from anywhere at any time after init, useful if you want to */ +/* preload textures you know will definitely be used */ +// void textures_load(struct texture_cache *cache, const char *path); + +/* repacks the current texture atlas based on the texture cache if needed */ +/* any previously returned srcrect results are invalidated after that */ +/* call it every time before rendering */ +void textures_update_atlas(struct texture_cache *cache); + +/* returns a persistent handle to some texture in cache, loading it if needed */ +/* check the result with m_texture_key_is_valid() */ +t_texture_key textures_get_key(struct texture_cache *cache, const char *path); + +/* returns a rect in a texture cache of the given key */ +t_frect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key); + +/* returns a rect of dimensions of the whole texture (whole atlas) */ +t_frect textures_get_dims(const struct texture_cache *cache, t_texture_key key); + +/* returns an identifier that is equal for all textures placed in the same atlas */ +int32_t textures_get_atlas_id(const struct texture_cache *cache, t_texture_key key); + +/* binds atlas texture in opengl state */ +void textures_bind(const struct texture_cache *cache, t_texture_key key, GLenum target); + +/* returns helpful information about contents of alpha channel in given texture */ +enum texture_mode textures_get_mode(const struct texture_cache *cache, t_texture_key key); + +/* returns the number of atlases in the cache */ +size_t textures_get_num_atlases(const struct texture_cache *cache); + #endif diff --git a/src/textures.c b/src/textures/textures.c similarity index 99% rename from src/textures.c rename to src/textures/textures.c index 652dfeb..5c3581a 100644 --- a/src/textures.c +++ b/src/textures/textures.c @@ -1,7 +1,6 @@ -#include "textures/internal_api.h" -#include "config.h" -#include "util.h" -#include "textures.h" +#include "internal_api.h" +#include "../config.h" +#include "../util.h" #include #include @@ -396,7 +395,7 @@ void textures_update_atlas(struct texture_cache *cache) { /* EXPERIMANTAL: LIKELY TO BE REMOVED! */ #ifdef __linux__ /* use rodata elf section for fast lookups of repeating textures */ -#include "system/linux/elf.h" +#include "../system/linux/elf.h" static const char *rodata_start; static const char *rodata_stop;