2024-09-16 13:17:00 +00:00
|
|
|
#ifndef TWN_TEXTURES_C_H
|
|
|
|
#define TWN_TEXTURES_C_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"
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "rendering/twn_gpu_texture_c.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>
|
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct Texture {
|
|
|
|
Rect 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-23 17:43:16 +00:00
|
|
|
GPUTexture loner_texture; /* stored directly for loners, == 0 means atlas_index should be used */
|
|
|
|
GPUTexture repeating_texture; /* separately allocated Texture, for loners == loner_texture */
|
|
|
|
enum TextureMode mode;
|
|
|
|
} Texture;
|
2024-07-12 18:10:25 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct TextureCacheItem {
|
2024-07-12 18:10:25 +00:00
|
|
|
char *key;
|
2024-09-23 17:43:16 +00:00
|
|
|
struct Texture value;
|
|
|
|
} TextureCacheItem;
|
2024-07-12 18:10:25 +00:00
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct TextureCache {
|
2024-07-14 15:36:48 +00:00
|
|
|
SDL_Window *window; /* from context */
|
2024-07-12 18:10:25 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
struct TextureCacheItem *hash;
|
2024-07-12 18:10:25 +00:00
|
|
|
|
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-23 17:43:16 +00:00
|
|
|
GPUTexture *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-09-23 17:43:16 +00:00
|
|
|
} TextureCache;
|
2024-07-12 18:10:25 +00:00
|
|
|
|
2024-07-30 15:31:38 +00:00
|
|
|
/* type safe structure for persistent texture handles */
|
2024-09-23 17:43:16 +00:00
|
|
|
typedef struct TextureKey { uint16_t id; } TextureKey;
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* 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-23 17:43:16 +00:00
|
|
|
void textures_cache_init(struct TextureCache *cache, SDL_Window *window);
|
|
|
|
void textures_cache_deinit(struct TextureCache *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-23 17:43:16 +00:00
|
|
|
void textures_update_atlas(TextureCache *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-23 17:43:16 +00:00
|
|
|
TextureKey textures_get_key(TextureCache *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-23 17:43:16 +00:00
|
|
|
Rect textures_get_srcrect(const TextureCache *cache, TextureKey key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns a rect of dimensions of the whole texture (whole atlas) */
|
2024-09-23 17:43:16 +00:00
|
|
|
Rect textures_get_dims(const TextureCache *cache, TextureKey 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-23 17:43:16 +00:00
|
|
|
int32_t textures_get_atlas_id(const TextureCache *cache, TextureKey key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void textures_bind(const TextureCache *cache, TextureKey key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void textures_bind_repeating(const TextureCache *cache, TextureKey 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-23 17:43:16 +00:00
|
|
|
TextureMode textures_get_mode(const TextureCache *cache, TextureKey key);
|
2024-07-30 15:31:38 +00:00
|
|
|
|
|
|
|
/* returns the number of atlases in the cache */
|
2024-09-23 17:43:16 +00:00
|
|
|
size_t textures_get_num_atlases(const TextureCache *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-09-26 18:02:56 +00:00
|
|
|
/* uncached low-level loading */
|
|
|
|
/* warn: surface->pixels must be freed along side the surface itself */
|
|
|
|
SDL_Surface *textures_load_surface(const char *path);
|
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
#endif
|