diff --git a/apps/testgame/scenes/ingame.c b/apps/testgame/scenes/ingame.c index a8ed7e9..c50e0d1 100644 --- a/apps/testgame/scenes/ingame.c +++ b/apps/testgame/scenes/ingame.c @@ -55,11 +55,9 @@ static void ingame_tick(struct state *state) { input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input)); } - m_sprite(m_set(path, "/assets/player/baron-walk.png"), - m_set(rect, ((t_frect){ 256, 256, 48, 48 })), - m_opt(color, ((t_color){ 255, 255, 255, 255 })), - 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/9slice.png"), + m_set(rect, ((t_frect){ 16, 16, 128, 128 })), + m_opt(texture_region, ((t_frect){ 0, 0, (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) }))); m_sprite(m_set(path, "/assets/light.png"), m_set(rect, ((t_frect){ 48, 64, 64, 64 })), diff --git a/include/twn_rendering.h b/include/twn_rendering.h index 4eb6631..0ee9d09 100644 --- a/include/twn_rendering.h +++ b/include/twn_rendering.h @@ -15,7 +15,7 @@ typedef struct push_sprite_args { t_frect rect; m_option_list( - t_fvec2, texture_origin, + t_frect, texture_region, t_color, color, float, rotation, bool, flip_x, diff --git a/src/rendering/twn_rendering_c.h b/src/rendering/twn_rendering_c.h index f069bce..3782c5d 100644 --- a/src/rendering/twn_rendering_c.h +++ b/src/rendering/twn_rendering_c.h @@ -40,7 +40,7 @@ struct sprite_primitive { bool repeat; m_option_list( - t_fvec2, texture_origin ) + t_frect, texture_region ) }; struct rect_primitive { diff --git a/src/rendering/twn_sprites.c b/src/rendering/twn_sprites.c index 5be3149..e31a842 100644 --- a/src/rendering/twn_sprites.c +++ b/src/rendering/twn_sprites.c @@ -26,7 +26,7 @@ void push_sprite(const t_push_sprite_args args) { .flip_x = m_or(args, flip_x, false), .flip_y = m_or(args, flip_y, false), .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 = { @@ -127,13 +127,22 @@ void render_sprites(const struct primitive_2d primitives[], const float xr = srcrect.x / dims.w; const float yr = srcrect.y / dims.h; - 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 }; - 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 }; - - /* TODO: texture_origin support */ + if (!m_is_set(sprite, texture_region)) { + 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 }; + 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 }; + } 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); + 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 { /* 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 }; 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 */ - const float ax = sprite.texture_origin_opt.x / srcrect.w; - const float ay = sprite.texture_origin_opt.y / srcrect.h; + const float ax = sprite.texture_region_opt.x / srcrect.w; + const float ay = sprite.texture_region_opt.y / srcrect.h; uv0.x += ax; uv1.x += ax; uv2.x += ax; uv3.x += ax; uv0.y += ay; uv1.y += ay; uv2.y += ay; uv3.y += ay;