From 570afe946e478a78ac2c64d2c11335ef6a03ae66 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Sat, 22 Jun 2024 11:06:29 +1000 Subject: [PATCH] fix scrolling texture interp integer overflow (#72) --- src/game/behaviors/texscroll.inc.c | 8 ++++---- src/game/scroll_targets.c | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/game/behaviors/texscroll.inc.c b/src/game/behaviors/texscroll.inc.c index 533b973e..fc02b225 100644 --- a/src/game/behaviors/texscroll.inc.c +++ b/src/game/behaviors/texscroll.inc.c @@ -154,13 +154,13 @@ void uv_update_scroll(void) { // Prepare for interpolation if (bhv < SCROLL_UV_X) { u8 bhvIndex = MIN(bhv, 2); - for (u16 k = 0; k < scroll->size; k++) { - scroll->prevF32[k] = verts[k]->n.ob[bhvIndex]; + for (u16 i = 0; i < scroll->size; i++) { + scroll->prevF32[i] = verts[i]->n.ob[bhvIndex]; } } else { u8 bhvIndex = MIN(bhv-SCROLL_UV_X, 1); - for (u16 k = 0; k < scroll->size; k++) { - scroll->prevS16[k] = verts[k]->n.tc[bhvIndex]; + for (u16 i = 0; i < scroll->size; i++) { + scroll->prevS16[i] = verts[i]->n.tc[bhvIndex]; } } scroll->needInterp = true; diff --git a/src/game/scroll_targets.c b/src/game/scroll_targets.c index f869992b..c0e564df 100644 --- a/src/game/scroll_targets.c +++ b/src/game/scroll_targets.c @@ -1,4 +1,5 @@ #include "scroll_targets.h" +#include "pc/lua/utils/smlua_math_utils.h" static struct ScrollTarget *sScrollTargets = NULL; @@ -21,6 +22,7 @@ struct ScrollTarget *get_scroll_targets(u32 id, u16 size, u16 offset) { // If we need to, realloc the block of vertices if ((!scroll->hasOffset && offset > 0) || size < scroll->size) { + if (scroll->hasOffset) { return NULL; } if (size > scroll->size) { size = scroll->size; } // Don't use an invalid size scroll->hasOffset = true; Vtx* *newVtx = calloc(size, sizeof(Vtx*)); @@ -137,6 +139,8 @@ void patch_scroll_targets_before(void) { } } +#define SHORT_RANGE 32767 + void patch_scroll_targets_interpolated(f32 delta) { f32 antiDelta = 1.0f - delta; struct ScrollTarget *scroll = sScrollTargets; @@ -152,7 +156,7 @@ void patch_scroll_targets_interpolated(f32 delta) { } else { u8 bhvIndex = MIN(scroll->bhv-SCROLL_UV_X, 1); for (u16 k = 0; k < scroll->size; k++) { - verts[k]->n.tc[bhvIndex] = (int) scroll->prevS16[k] * antiDelta + scroll->interpS16[k] * delta; + verts[k]->n.tc[bhvIndex] = clampf(scroll->prevS16[k] * antiDelta + scroll->interpS16[k] * delta, -SHORT_RANGE, SHORT_RANGE); } } }