twn_rendering: m_sprite texture_origin changed for texture_region, stretched subregion work now

This commit is contained in:
veclav talica 2024-09-21 20:07:05 +03:00
parent ddf0ef6a0c
commit 5caf736370
4 changed files with 24 additions and 17 deletions

View File

@ -55,11 +55,9 @@ static void ingame_tick(struct state *state) {
input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input)); input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input));
} }
m_sprite(m_set(path, "/assets/player/baron-walk.png"), m_sprite(m_set(path, "/assets/9slice.png"),
m_set(rect, ((t_frect){ 256, 256, 48, 48 })), m_set(rect, ((t_frect){ 16, 16, 128, 128 })),
m_opt(color, ((t_color){ 255, 255, 255, 255 })), m_opt(texture_region, ((t_frect){ 0, 0, (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) })));
m_opt(stretch, false ),
m_opt(texture_origin, ((t_fvec2){ (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) })));
m_sprite(m_set(path, "/assets/light.png"), m_sprite(m_set(path, "/assets/light.png"),
m_set(rect, ((t_frect){ 48, 64, 64, 64 })), m_set(rect, ((t_frect){ 48, 64, 64, 64 })),

View File

@ -15,7 +15,7 @@ typedef struct push_sprite_args {
t_frect rect; t_frect rect;
m_option_list( m_option_list(
t_fvec2, texture_origin, t_frect, texture_region,
t_color, color, t_color, color,
float, rotation, float, rotation,
bool, flip_x, bool, flip_x,

View File

@ -40,7 +40,7 @@ struct sprite_primitive {
bool repeat; bool repeat;
m_option_list( m_option_list(
t_fvec2, texture_origin ) t_frect, texture_region )
}; };
struct rect_primitive { struct rect_primitive {

View File

@ -26,7 +26,7 @@ void push_sprite(const t_push_sprite_args args) {
.flip_x = m_or(args, flip_x, false), .flip_x = m_or(args, flip_x, false),
.flip_y = m_or(args, flip_y, false), .flip_y = m_or(args, flip_y, false),
.repeat = !m_or(args, stretch, true), .repeat = !m_or(args, stretch, true),
m_opt_from(texture_origin, args, texture_origin) m_opt_from(texture_region, args, texture_region)
}; };
struct primitive_2d primitive = { struct primitive_2d primitive = {
@ -127,13 +127,22 @@ void render_sprites(const struct primitive_2d primitives[],
const float xr = srcrect.x / dims.w; const float xr = srcrect.x / dims.w;
const float yr = srcrect.y / dims.h; const float yr = srcrect.y / dims.h;
if (!m_is_set(sprite, texture_region)) {
uv0 = (t_fvec2){ xr + wr * sprite.flip_x, yr + hr * sprite.flip_y }; uv0 = (t_fvec2){ xr + wr * sprite.flip_x, yr + hr * sprite.flip_y };
uv1 = (t_fvec2){ xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y }; uv1 = (t_fvec2){ xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y };
uv2 = (t_fvec2){ xr + wr * !sprite.flip_x, yr + hr * !sprite.flip_y }; uv2 = (t_fvec2){ xr + wr * !sprite.flip_x, yr + hr * !sprite.flip_y };
uv3 = (t_fvec2){ xr + wr * !sprite.flip_x, yr + hr * sprite.flip_y }; uv3 = (t_fvec2){ xr + wr * !sprite.flip_x, yr + hr * sprite.flip_y };
} else {
const float offx = (sprite.texture_region_opt.x / srcrect.w) * (srcrect.w / dims.w);
const float offy = (sprite.texture_region_opt.y / srcrect.h) * (srcrect.h / dims.h);
const float offw = (1.0f - (sprite.texture_region_opt.w / srcrect.w)) * (srcrect.w / dims.w);
const float offh = (1.0f - (sprite.texture_region_opt.h / srcrect.h)) * (srcrect.h / dims.h);
/* TODO: texture_origin support */ uv0 = (t_fvec2){ xr + offx + wr * sprite.flip_x, yr + offy + hr * sprite.flip_y };
uv1 = (t_fvec2){ xr + offx + wr * sprite.flip_x, yr - offh + hr * !sprite.flip_y };
uv2 = (t_fvec2){ xr - offw + wr * !sprite.flip_x, yr - offh + hr * !sprite.flip_y };
uv3 = (t_fvec2){ xr - offw + wr * !sprite.flip_x, yr + offy + hr * sprite.flip_y };
}
} else { } else {
/* try fitting texture into supplied destination rectangle */ /* try fitting texture into supplied destination rectangle */
@ -145,11 +154,11 @@ void render_sprites(const struct primitive_2d primitives[],
uv2 = (t_fvec2){ rx * !sprite.flip_x, ry * !sprite.flip_y }; uv2 = (t_fvec2){ rx * !sprite.flip_x, ry * !sprite.flip_y };
uv3 = (t_fvec2){ rx * !sprite.flip_x, ry * sprite.flip_y }; uv3 = (t_fvec2){ rx * !sprite.flip_x, ry * sprite.flip_y };
if (m_is_set(sprite, texture_origin)) { if (m_is_set(sprite, texture_region)) {
/* displace origin */ /* displace origin */
const float ax = sprite.texture_origin_opt.x / srcrect.w; const float ax = sprite.texture_region_opt.x / srcrect.w;
const float ay = sprite.texture_origin_opt.y / srcrect.h; const float ay = sprite.texture_region_opt.y / srcrect.h;
uv0.x += ax; uv1.x += ax; uv2.x += ax; uv3.x += ax; uv0.x += ax; uv1.x += ax; uv2.x += ax; uv3.x += ax;
uv0.y += ay; uv1.y += ay; uv2.y += ay; uv3.y += ay; uv0.y += ay; uv1.y += ay; uv2.y += ay; uv3.y += ay;