Rescale save-loaded textures to runtime texture dimensions

`LegoTextureInfo::LoadBits` sized its memcpy from the destination
DirectDraw surface and assumed the source bits matched. They don't
when the save was made under a different renderer than the current
one: HW mode squares non-square assets in `LegoImage::Read` (e.g.
128x32 -> 128x128 via row duplication), SW mode keeps the original
dimensions, and `ReadNamedTexture` always loads with `p_square=0`,
so saved bits keep whatever size they had at save time. SW save
loaded under HW caused an OOB read in `LoadBits`; HW save loaded
under SW silently truncated the source and rendered a distorted
texture.

Add `LegoImage::Resize(width, height)` (nearest-neighbor, handles
both up- and down-scale) and call it in `LoadFromNamedTexture` to
match the runtime `LegoTextureInfo` surface dimensions before
binding. Save format unchanged; `LoadBits` signature unchanged.
Square textures and matching-mode loads short-circuit on the
fast-path.
This commit is contained in:
Christian Semmler 2026-05-14 18:10:09 -07:00
parent 8c8559452d
commit 5d9f92a029
No known key found for this signature in database
GPG Key ID: 086DAA1360BEEE5C
3 changed files with 46 additions and 0 deletions

View File

@ -803,6 +803,17 @@ void LoadFromNamedTexture(LegoNamedTexture* p_namedTexture)
LegoTextureInfo* textureInfo = TextureContainer()->Get(p_namedTexture->GetName()->GetData());
if (textureInfo != NULL) {
DDSURFACEDESC desc;
memset(&desc, 0, sizeof(desc));
desc.dwSize = sizeof(desc);
if (textureInfo->m_surface->GetSurfaceDesc(&desc) == DD_OK) {
LegoImage* image = p_namedTexture->GetTexture()->GetImage();
if (image->GetWidth() != desc.dwWidth || image->GetHeight() != desc.dwHeight) {
image->Resize(desc.dwWidth, desc.dwHeight);
}
}
textureInfo->LoadBits(p_namedTexture->GetTexture()->GetImage()->GetBits());
}
}

View File

@ -179,3 +179,37 @@ LegoResult LegoImage::Write(LegoStorage* p_storage)
}
return SUCCESS;
}
LegoResult LegoImage::Resize(LegoU32 p_width, LegoU32 p_height)
{
if (m_surface == NULL) {
return FAILURE;
}
if (m_surface->w == (int) p_width && m_surface->h == (int) p_height) {
return SUCCESS;
}
SDL_Surface* newSurface = SDL_CreateSurface(p_width, p_height, SDL_PIXELFORMAT_INDEX8);
if (newSurface == NULL) {
return FAILURE;
}
LegoU32 srcW = m_surface->w;
LegoU32 srcH = m_surface->h;
const LegoU8* src = (const LegoU8*) m_surface->pixels;
LegoU8* dst = (LegoU8*) newSurface->pixels;
for (LegoU32 dy = 0; dy < p_height; dy++) {
LegoU32 sy = dy * srcH / p_height;
const LegoU8* srcRow = src + sy * srcW;
LegoU8* dstRow = dst + dy * p_width;
for (LegoU32 dx = 0; dx < p_width; dx++) {
LegoU32 sx = dx * srcW / p_width;
dstRow[dx] = srcRow[sx];
}
}
SDL_DestroySurface(m_surface);
m_surface = newSurface;
return SUCCESS;
}

View File

@ -49,6 +49,7 @@ class LegoImage {
LegoU8* GetBits() const { return (LegoU8*) m_surface->pixels; }
LegoResult Read(LegoStorage* p_storage, LegoU32 p_square);
LegoResult Write(LegoStorage* p_storage);
LegoResult Resize(LegoU32 p_width, LegoU32 p_height);
protected:
SDL_Surface* m_surface;