use t_frect for texture dimentions

This commit is contained in:
veclav talica 2024-07-30 18:09:21 +03:00
parent ea29f2c5f0
commit 0b215acbdf
5 changed files with 36 additions and 36 deletions

View File

@ -168,7 +168,7 @@ static void render_sprites(const struct primitive_2d primitives[],
NULL,
GL_STREAM_DRAW);
const t_rect dims =
const t_frect dims =
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
/* vertex population over a mapped buffer */
@ -181,13 +181,13 @@ static void render_sprites(const struct primitive_2d primitives[],
const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
const struct sprite_primitive sprite = primitives[cur].sprite;
const t_rect srcrect =
const t_frect srcrect =
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
const float wr = (float)srcrect.w / (float)dims.w;
const float hr = (float)srcrect.h / (float)dims.h;
const float xr = (float)srcrect.x / (float)dims.w;
const float yr = (float)srcrect.y / (float)dims.h;
const float wr = srcrect.w / dims.w;
const float hr = srcrect.h / dims.h;
const float xr = srcrect.x / dims.w;
const float yr = srcrect.y / dims.h;
t_fvec2 uv0 = { xr + wr * sprite.flip_x, yr + hr * sprite.flip_y };
t_fvec2 uv1 = { xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y };

View File

@ -64,22 +64,22 @@ static void draw_uncolored_space_traingle_batch(struct mesh_batch *batch,
struct uncolored_space_triangle_payload *payload =
&((union uncolored_space_triangle *)batch->primitives)[i].payload;
t_rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
t_rect dims = textures_get_dims(&ctx.texture_cache, texture_key);
t_frect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
t_frect dims = textures_get_dims(&ctx.texture_cache, texture_key);
/* TODO: fast path for uvs mapped directly on srcrect corners? */
const float wr = (float)srcrect.w / (float)dims.w;
const float hr = (float)srcrect.h / (float)dims.h;
const float xr = (float)srcrect.x / (float)dims.w;
const float yr = (float)srcrect.y / (float)dims.h;
const float wr = srcrect.w / dims.w;
const float hr = srcrect.h / dims.h;
const float xr = srcrect.x / dims.w;
const float yr = srcrect.y / dims.h;
payload->uv0.x = xr + ((float)payload->uv0.x / (float)srcrect.w) * wr;
payload->uv0.y = yr + ((float)payload->uv0.y / (float)srcrect.h) * hr;
payload->uv1.x = xr + ((float)payload->uv1.x / (float)srcrect.w) * wr;
payload->uv1.y = yr + ((float)payload->uv1.y / (float)srcrect.h) * hr;
payload->uv2.x = xr + ((float)payload->uv2.x / (float)srcrect.w) * wr;
payload->uv2.y = yr + ((float)payload->uv2.y / (float)srcrect.h) * hr;
payload->uv0.x = xr + ((float)payload->uv0.x / srcrect.w) * wr;
payload->uv0.y = yr + ((float)payload->uv0.y / srcrect.h) * hr;
payload->uv1.x = xr + ((float)payload->uv1.x / srcrect.w) * wr;
payload->uv1.y = yr + ((float)payload->uv1.y / srcrect.h) * hr;
payload->uv2.x = xr + ((float)payload->uv2.x / srcrect.w) * wr;
payload->uv2.y = yr + ((float)payload->uv2.y / srcrect.h) * hr;
}
textures_bind(&ctx.texture_cache, texture_key, GL_TEXTURE_2D);

View File

@ -146,10 +146,10 @@ static void recreate_current_atlas_texture(struct texture_cache *cache) {
NULL,
atlas_surface,
&(SDL_Rect){
.x = cache->hash[i].value.srcrect.x,
.y = cache->hash[i].value.srcrect.y,
.w = cache->hash[i].value.srcrect.w,
.h = cache->hash[i].value.srcrect.h,
.x = (int)cache->hash[i].value.srcrect.x,
.y = (int)cache->hash[i].value.srcrect.y,
.w = (int)cache->hash[i].value.srcrect.w,
.h = (int)cache->hash[i].value.srcrect.h,
});
}
@ -226,11 +226,11 @@ static bool update_rects(struct texture_cache *cache, stbrp_rect *rects, stbrp_r
/* updates the atlas location of every rect in the cache */
static void update_texture_rects_in_atlas(struct texture_cache *cache, stbrp_rect *rects) {
for (size_t i = 0; i < arrlenu(rects); ++i) {
cache->hash[i].value.srcrect = (t_rect) {
.x = rects[i].x,
.y = rects[i].y,
.w = rects[i].w,
.h = rects[i].h,
cache->hash[i].value.srcrect = (t_frect) {
.x = (float)rects[i].x,
.y = (float)rects[i].y,
.w = (float)rects[i].w,
.h = (float)rects[i].h,
};
}
}
@ -337,7 +337,7 @@ static t_texture_key textures_load(struct texture_cache *cache, const char *path
if (surface->w >= TEXTURE_ATLAS_SIZE || surface->h >= TEXTURE_ATLAS_SIZE) {
new_texture.loner_texture = new_gl_texture();
upload_texture_from_surface(new_texture.loner_texture, surface);
new_texture.srcrect = (t_rect) { .w = surface->w, .h = surface->h };
new_texture.srcrect = (t_frect) { .w = (float)surface->w, .h = (float)surface->h };
shput(cache->hash, path, new_texture);
return (t_texture_key){ (uint16_t)shgeti(cache->hash, path) };
} else {
@ -464,27 +464,27 @@ int32_t textures_get_atlas_id(const struct texture_cache *cache, t_texture_key k
}
}
t_rect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key) {
t_frect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key) {
if (m_texture_key_is_valid(key)) {
return cache->hash[key.id].value.srcrect;
} else {
CRY("Texture lookup failed.",
"Tried to get texture that isn't loaded.");
return (t_rect){ 0, 0, 0, 0 };
return (t_frect){ 0, 0, 0, 0 };
}
}
t_rect textures_get_dims(const struct texture_cache *cache, t_texture_key key) {
t_frect textures_get_dims(const struct texture_cache *cache, t_texture_key key) {
if (m_texture_key_is_valid(key)) {
if (cache->hash[key.id].value.loner_texture != 0)
return cache->hash[key.id].value.srcrect;
else
return (t_rect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE };
return (t_frect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE };
} else {
CRY("Texture lookup failed.",
"Tried to get texture that isn't loaded.");
return (t_rect){ 0, 0, 0, 0 };
return (t_frect){ 0, 0, 0, 0 };
}
}

View File

@ -35,10 +35,10 @@ void textures_update_atlas(struct texture_cache *cache);
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_rect textures_get_srcrect(const struct texture_cache *cache, t_texture_key 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_rect textures_get_dims(const struct texture_cache *cache, t_texture_key key);
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);

View File

@ -11,7 +11,7 @@
#include <stdbool.h>
struct texture {
t_rect srcrect; /* position in atlas */
t_frect srcrect; /* position in atlas */
SDL_Surface *data; /* original image data */
int atlas_index;
GLuint loner_texture; /* stored directly for loners, == 0 means atlas_index should be used */