use t_frect for texture dimentions
This commit is contained in:
parent
ea29f2c5f0
commit
0b215acbdf
@ -168,7 +168,7 @@ static void render_sprites(const struct primitive_2d primitives[],
|
|||||||
NULL,
|
NULL,
|
||||||
GL_STREAM_DRAW);
|
GL_STREAM_DRAW);
|
||||||
|
|
||||||
const t_rect dims =
|
const t_frect dims =
|
||||||
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
|
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
|
||||||
|
|
||||||
/* vertex population over a mapped buffer */
|
/* 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 size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
|
||||||
const struct sprite_primitive sprite = primitives[cur].sprite;
|
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);
|
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
|
||||||
|
|
||||||
const float wr = (float)srcrect.w / (float)dims.w;
|
const float wr = srcrect.w / dims.w;
|
||||||
const float hr = (float)srcrect.h / (float)dims.h;
|
const float hr = srcrect.h / dims.h;
|
||||||
const float xr = (float)srcrect.x / (float)dims.w;
|
const float xr = srcrect.x / dims.w;
|
||||||
const float yr = (float)srcrect.y / (float)dims.h;
|
const float yr = srcrect.y / dims.h;
|
||||||
|
|
||||||
t_fvec2 uv0 = { xr + wr * sprite.flip_x, yr + hr * sprite.flip_y };
|
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 };
|
t_fvec2 uv1 = { xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y };
|
||||||
|
@ -64,22 +64,22 @@ static void draw_uncolored_space_traingle_batch(struct mesh_batch *batch,
|
|||||||
struct uncolored_space_triangle_payload *payload =
|
struct uncolored_space_triangle_payload *payload =
|
||||||
&((union uncolored_space_triangle *)batch->primitives)[i].payload;
|
&((union uncolored_space_triangle *)batch->primitives)[i].payload;
|
||||||
|
|
||||||
t_rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
|
t_frect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
|
||||||
t_rect dims = textures_get_dims(&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? */
|
/* TODO: fast path for uvs mapped directly on srcrect corners? */
|
||||||
|
|
||||||
const float wr = (float)srcrect.w / (float)dims.w;
|
const float wr = srcrect.w / dims.w;
|
||||||
const float hr = (float)srcrect.h / (float)dims.h;
|
const float hr = srcrect.h / dims.h;
|
||||||
const float xr = (float)srcrect.x / (float)dims.w;
|
const float xr = srcrect.x / dims.w;
|
||||||
const float yr = (float)srcrect.y / (float)dims.h;
|
const float yr = srcrect.y / dims.h;
|
||||||
|
|
||||||
payload->uv0.x = xr + ((float)payload->uv0.x / (float)srcrect.w) * wr;
|
payload->uv0.x = xr + ((float)payload->uv0.x / srcrect.w) * wr;
|
||||||
payload->uv0.y = yr + ((float)payload->uv0.y / (float)srcrect.h) * hr;
|
payload->uv0.y = yr + ((float)payload->uv0.y / srcrect.h) * hr;
|
||||||
payload->uv1.x = xr + ((float)payload->uv1.x / (float)srcrect.w) * wr;
|
payload->uv1.x = xr + ((float)payload->uv1.x / srcrect.w) * wr;
|
||||||
payload->uv1.y = yr + ((float)payload->uv1.y / (float)srcrect.h) * hr;
|
payload->uv1.y = yr + ((float)payload->uv1.y / srcrect.h) * hr;
|
||||||
payload->uv2.x = xr + ((float)payload->uv2.x / (float)srcrect.w) * wr;
|
payload->uv2.x = xr + ((float)payload->uv2.x / srcrect.w) * wr;
|
||||||
payload->uv2.y = yr + ((float)payload->uv2.y / (float)srcrect.h) * hr;
|
payload->uv2.y = yr + ((float)payload->uv2.y / srcrect.h) * hr;
|
||||||
}
|
}
|
||||||
|
|
||||||
textures_bind(&ctx.texture_cache, texture_key, GL_TEXTURE_2D);
|
textures_bind(&ctx.texture_cache, texture_key, GL_TEXTURE_2D);
|
||||||
|
@ -146,10 +146,10 @@ static void recreate_current_atlas_texture(struct texture_cache *cache) {
|
|||||||
NULL,
|
NULL,
|
||||||
atlas_surface,
|
atlas_surface,
|
||||||
&(SDL_Rect){
|
&(SDL_Rect){
|
||||||
.x = cache->hash[i].value.srcrect.x,
|
.x = (int)cache->hash[i].value.srcrect.x,
|
||||||
.y = cache->hash[i].value.srcrect.y,
|
.y = (int)cache->hash[i].value.srcrect.y,
|
||||||
.w = cache->hash[i].value.srcrect.w,
|
.w = (int)cache->hash[i].value.srcrect.w,
|
||||||
.h = cache->hash[i].value.srcrect.h,
|
.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 */
|
/* updates the atlas location of every rect in the cache */
|
||||||
static void update_texture_rects_in_atlas(struct texture_cache *cache, stbrp_rect *rects) {
|
static void update_texture_rects_in_atlas(struct texture_cache *cache, stbrp_rect *rects) {
|
||||||
for (size_t i = 0; i < arrlenu(rects); ++i) {
|
for (size_t i = 0; i < arrlenu(rects); ++i) {
|
||||||
cache->hash[i].value.srcrect = (t_rect) {
|
cache->hash[i].value.srcrect = (t_frect) {
|
||||||
.x = rects[i].x,
|
.x = (float)rects[i].x,
|
||||||
.y = rects[i].y,
|
.y = (float)rects[i].y,
|
||||||
.w = rects[i].w,
|
.w = (float)rects[i].w,
|
||||||
.h = rects[i].h,
|
.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) {
|
if (surface->w >= TEXTURE_ATLAS_SIZE || surface->h >= TEXTURE_ATLAS_SIZE) {
|
||||||
new_texture.loner_texture = new_gl_texture();
|
new_texture.loner_texture = new_gl_texture();
|
||||||
upload_texture_from_surface(new_texture.loner_texture, surface);
|
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);
|
shput(cache->hash, path, new_texture);
|
||||||
return (t_texture_key){ (uint16_t)shgeti(cache->hash, path) };
|
return (t_texture_key){ (uint16_t)shgeti(cache->hash, path) };
|
||||||
} else {
|
} 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)) {
|
if (m_texture_key_is_valid(key)) {
|
||||||
return cache->hash[key.id].value.srcrect;
|
return cache->hash[key.id].value.srcrect;
|
||||||
} else {
|
} else {
|
||||||
CRY("Texture lookup failed.",
|
CRY("Texture lookup failed.",
|
||||||
"Tried to get texture that isn't loaded.");
|
"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 (m_texture_key_is_valid(key)) {
|
||||||
if (cache->hash[key.id].value.loner_texture != 0)
|
if (cache->hash[key.id].value.loner_texture != 0)
|
||||||
return cache->hash[key.id].value.srcrect;
|
return cache->hash[key.id].value.srcrect;
|
||||||
else
|
else
|
||||||
return (t_rect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE };
|
return (t_frect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE };
|
||||||
} else {
|
} else {
|
||||||
CRY("Texture lookup failed.",
|
CRY("Texture lookup failed.",
|
||||||
"Tried to get texture that isn't loaded.");
|
"Tried to get texture that isn't loaded.");
|
||||||
return (t_rect){ 0, 0, 0, 0 };
|
return (t_frect){ 0, 0, 0, 0 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
t_texture_key textures_get_key(struct texture_cache *cache, const char *path);
|
||||||
|
|
||||||
/* returns a rect in a texture cache of the given key */
|
/* 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) */
|
/* 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 */
|
/* 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);
|
int32_t textures_get_atlas_id(const struct texture_cache *cache, t_texture_key key);
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct texture {
|
struct texture {
|
||||||
t_rect srcrect; /* position in atlas */
|
t_frect srcrect; /* position in atlas */
|
||||||
SDL_Surface *data; /* original image data */
|
SDL_Surface *data; /* original image data */
|
||||||
int atlas_index;
|
int atlas_index;
|
||||||
GLuint loner_texture; /* stored directly for loners, == 0 means atlas_index should be used */
|
GLuint loner_texture; /* stored directly for loners, == 0 means atlas_index should be used */
|
||||||
|
Loading…
Reference in New Issue
Block a user