2024-09-16 06:07:01 +00:00
|
|
|
#ifndef TWN_TEXTURES_H
|
|
|
|
#define TWN_TEXTURES_H
|
2024-07-12 18:10:25 +00:00
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_util.h"
|
|
|
|
#include "twn_texture_modes.h"
|
|
|
|
#include "twn_engine_api.h"
|
|
|
|
#include "twn_gpu_texture.h"
|
2024-07-14 13:04:12 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <stb_rect_pack.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
struct texture {
|
2024-07-30 15:09:21 +00:00
|
|
|
t_frect srcrect; /* position in atlas */
|
2024-07-12 18:10:25 +00:00
|
|
|
SDL_Surface *data; /* original image data */
|
2024-07-14 13:04:12 +00:00
|
|
|
int atlas_index;
|
2024-09-16 06:07:01 +00:00
|
|
|
gpu_texture loner_texture; /* stored directly for loners, == 0 means atlas_index should be used */
|
|
|
|
gpu_texture repeating_texture; /* separately allocated texture, for loners == loner_texture */
|
2024-07-28 20:59:23 +00:00
|
|
|
enum texture_mode mode;
|
2024-07-12 18:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct texture_cache_item {
|
|
|
|
char *key;
|
|
|
|
struct texture value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct texture_cache {
|
2024-07-14 15:36:48 +00:00
|
|
|
SDL_Window *window; /* from context */
|
2024-07-12 18:10:25 +00:00
|
|
|
|
|
|
|
struct texture_cache_item *hash;
|
|
|
|
|
2024-07-14 13:04:12 +00:00
|
|
|
stbrp_node *node_buffer; /* used internally by stb_rect_pack */
|
2024-07-12 18:10:25 +00:00
|
|
|
|
|
|
|
SDL_Surface **atlas_surfaces;
|
2024-09-16 06:07:01 +00:00
|
|
|
gpu_texture *atlas_textures; /* shared by atlas textures */
|
2024-07-14 15:36:48 +00:00
|
|
|
int atlas_index; /* atlas that is currently being built */
|
2024-07-12 18:10:25 +00:00
|
|
|
|
2024-07-14 13:04:12 +00:00
|
|
|
bool is_dirty; /* current atlas needs to be recreated */
|
2024-07-12 18:10:25 +00:00
|
|
|
};
|
|
|
|
|
2024-07-30 15:31:38 +00:00
|
|
|
/* 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)
|
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
void textures_cache_init(struct texture_cache *cache, SDL_Window *window);
|
|
|
|
void textures_cache_deinit(struct texture_cache *cache);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* for debugging */
|
2024-09-16 06:07:01 +00:00
|
|
|
void textures_dump_atlases(struct texture_cache *cache);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2024-09-16 06:07:01 +00:00
|
|
|
void textures_update_atlas(struct texture_cache *cache);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns a persistent handle to some texture in cache, loading it if needed */
|
|
|
|
/* check the result with m_texture_key_is_valid() */
|
2024-09-16 06:07:01 +00:00
|
|
|
t_texture_key textures_get_key(struct texture_cache *cache, const char *path);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns a rect in a texture cache of the given key */
|
2024-09-16 06:07:01 +00:00
|
|
|
t_frect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns a rect of dimensions of the whole texture (whole atlas) */
|
2024-09-16 06:07:01 +00:00
|
|
|
t_frect textures_get_dims(const struct texture_cache *cache, t_texture_key key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns an identifier that is equal for all textures placed in the same atlas */
|
2024-09-16 06:07:01 +00:00
|
|
|
int32_t textures_get_atlas_id(const struct texture_cache *cache, t_texture_key key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
void textures_bind(const struct texture_cache *cache, t_texture_key key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
2024-09-16 06:07:01 +00:00
|
|
|
void textures_bind_repeating(const struct texture_cache *cache, t_texture_key key);
|
2024-07-31 21:23:32 +00:00
|
|
|
|
2024-07-30 15:31:38 +00:00
|
|
|
/* returns helpful information about contents of alpha channel in given texture */
|
2024-09-16 06:07:01 +00:00
|
|
|
enum texture_mode textures_get_mode(const struct texture_cache *cache, t_texture_key key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns the number of atlases in the cache */
|
2024-09-16 06:07:01 +00:00
|
|
|
size_t textures_get_num_atlases(const struct texture_cache *cache);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
2024-08-26 21:33:37 +00:00
|
|
|
/* TODO: should recieve texture_cache, get_key optimization cache should be cleared some other way */
|
2024-09-16 06:07:01 +00:00
|
|
|
void textures_reset_state(void);
|
2024-08-21 15:00:27 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
#endif
|