mirror of
https://github.com/isledecomp/isle-portable.git
synced 2026-02-03 04:21:15 +00:00
3DS: Account for sprite scaling when resizing UI
This commit is contained in:
parent
77c832c237
commit
fa049eadb6
@ -115,31 +115,30 @@ static int NearestPowerOfTwoClamp(int val)
|
||||
return 512;
|
||||
}
|
||||
|
||||
static SDL_Surface* ConvertAndResizeSurface(SDL_Surface* original, bool isUI, float scale)
|
||||
static SDL_Surface* ConvertAndResizeSurface(SDL_Surface* original, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
SDL_Surface* converted = SDL_ConvertSurface(original, SDL_PIXELFORMAT_RGBA8888);
|
||||
if (!converted) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!isUI) {
|
||||
return converted;
|
||||
return SDL_ConvertSurface(original, SDL_PIXELFORMAT_RGBA8888);
|
||||
}
|
||||
|
||||
int scaledW = static_cast<int>(converted->w * scale);
|
||||
int scaledH = static_cast<int>(converted->h * scale);
|
||||
int scaledW = static_cast<int>(original->w * scaleX);
|
||||
int scaledH = static_cast<int>(original->h * scaleY);
|
||||
|
||||
int paddedW = NearestPowerOfTwoClamp(scaledW);
|
||||
int paddedH = NearestPowerOfTwoClamp(scaledH);
|
||||
|
||||
SDL_Surface* padded = SDL_CreateSurface(paddedW, paddedH, SDL_PIXELFORMAT_RGBA8888);
|
||||
if (!padded) {
|
||||
SDL_DestroySurface(converted);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SDL_Rect dstRect = {0, 0, scaledW, scaledH};
|
||||
SDL_BlitSurfaceScaled(converted, nullptr, padded, &dstRect, SDL_SCALEMODE_LINEAR);
|
||||
SDL_DestroySurface(converted);
|
||||
if (scaleX == 1.0f && scaleY == 1.0f) {
|
||||
SDL_BlitSurface(original, nullptr, padded, nullptr);
|
||||
}
|
||||
else {
|
||||
SDL_Rect dstRect = {0, 0, scaledW, scaledH};
|
||||
SDL_BlitSurfaceScaled(original, nullptr, padded, &dstRect, SDL_SCALEMODE_LINEAR);
|
||||
}
|
||||
|
||||
return padded;
|
||||
}
|
||||
@ -181,9 +180,9 @@ static void EncodeTextureLayout(const u8* src, u8* dst, int width, int height)
|
||||
}
|
||||
}
|
||||
|
||||
static bool ConvertAndUploadTexture(C3D_Tex* tex, SDL_Surface* originalSurface, bool isUI, float scale)
|
||||
static bool ConvertAndUploadTexture(C3D_Tex* tex, SDL_Surface* originalSurface, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
SDL_Surface* resized = ConvertAndResizeSurface(originalSurface, isUI, scale);
|
||||
SDL_Surface* resized = ConvertAndResizeSurface(originalSurface, isUI, scaleX, scaleY);
|
||||
if (!resized) {
|
||||
return false;
|
||||
}
|
||||
@ -198,18 +197,24 @@ static bool ConvertAndUploadTexture(C3D_Tex* tex, SDL_Surface* originalSurface,
|
||||
params.maxLevel = isUI ? 0 : 4;
|
||||
params.type = GPU_TEX_2D;
|
||||
if (!C3D_TexInitWithParams(tex, nullptr, params)) {
|
||||
SDL_DestroySurface(resized);
|
||||
if (resized != originalSurface) {
|
||||
SDL_DestroySurface(resized);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t* tiledData = (uint8_t*) malloc(width * height * 4);
|
||||
if (!tiledData) {
|
||||
SDL_DestroySurface(resized);
|
||||
if (resized != originalSurface) {
|
||||
SDL_DestroySurface(resized);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
EncodeTextureLayout((const u8*) resized->pixels, tiledData, width, height);
|
||||
SDL_DestroySurface(resized);
|
||||
if (resized != originalSurface) {
|
||||
SDL_DestroySurface(resized);
|
||||
}
|
||||
|
||||
C3D_TexUpload(tex, tiledData);
|
||||
free(tiledData);
|
||||
@ -228,7 +233,7 @@ static bool ConvertAndUploadTexture(C3D_Tex* tex, SDL_Surface* originalSurface,
|
||||
return true;
|
||||
}
|
||||
|
||||
Uint32 Citro3DRenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
Uint32 Citro3DRenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
|
||||
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
|
||||
@ -242,7 +247,13 @@ Uint32 Citro3DRenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
if (tex.texture == texture) {
|
||||
if (tex.version != texture->m_version) {
|
||||
C3D_TexDelete(&tex.c3dTex);
|
||||
if (!ConvertAndUploadTexture(&tex.c3dTex, originalSurface, isUi, m_viewportTransform.scale)) {
|
||||
if (!ConvertAndUploadTexture(
|
||||
&tex.c3dTex,
|
||||
originalSurface,
|
||||
isUI,
|
||||
scaleX * m_viewportTransform.scale,
|
||||
scaleY * m_viewportTransform.scale
|
||||
)) {
|
||||
return NO_TEXTURE_ID;
|
||||
}
|
||||
|
||||
@ -260,7 +271,13 @@ Uint32 Citro3DRenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
entry.width = NearestPowerOfTwoClamp(originalW * m_viewportTransform.scale);
|
||||
entry.height = NearestPowerOfTwoClamp(originalH * m_viewportTransform.scale);
|
||||
|
||||
if (!ConvertAndUploadTexture(&entry.c3dTex, originalSurface, isUi, m_viewportTransform.scale)) {
|
||||
if (!ConvertAndUploadTexture(
|
||||
&entry.c3dTex,
|
||||
originalSurface,
|
||||
isUI,
|
||||
scaleX * m_viewportTransform.scale,
|
||||
scaleY * m_viewportTransform.scale
|
||||
)) {
|
||||
return NO_TEXTURE_ID;
|
||||
}
|
||||
|
||||
|
||||
@ -76,7 +76,7 @@ void DirectX9Renderer::AddTextureDestroyCallback(Uint32 id, IDirect3DRMTexture*
|
||||
);
|
||||
}
|
||||
|
||||
Uint32 DirectX9Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
Uint32 DirectX9Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
|
||||
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
|
||||
|
||||
@ -49,12 +49,12 @@ int GL11_GetMaxTextureSize()
|
||||
return maxTextureSize;
|
||||
}
|
||||
|
||||
GLuint GL11_UploadTextureData(void* pixels, int width, int height, bool isUi)
|
||||
GLuint GL11_UploadTextureData(void* pixels, int width, int height, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
GLuint texId;
|
||||
glGenTextures(1, &texId);
|
||||
glBindTexture(GL_TEXTURE_2D, texId);
|
||||
if (isUi) {
|
||||
if (isUI) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
|
||||
@ -65,7 +65,7 @@ void GL11_InitState();
|
||||
void GL11_LoadExtensions();
|
||||
void GL11_DestroyTexture(GLuint texId);
|
||||
int GL11_GetMaxTextureSize();
|
||||
GLuint GL11_UploadTextureData(void* pixels, int width, int height, bool isUI);
|
||||
GLuint GL11_UploadTextureData(void* pixels, int width, int height, bool isUI, float scaleX, float scaleY);
|
||||
void GL11_UploadMesh(GLMeshCacheEntry& cache, bool hasTexture);
|
||||
void GL11_DestroyMesh(GLMeshCacheEntry& cache);
|
||||
void GL11_BeginFrame(const Matrix4x4* projection);
|
||||
|
||||
@ -119,7 +119,7 @@ static int NextPowerOfTwo(int v)
|
||||
return power;
|
||||
}
|
||||
|
||||
static Uint32 UploadTextureData(SDL_Surface* src, bool useNPOT, bool isUi)
|
||||
static Uint32 UploadTextureData(SDL_Surface* src, bool useNPOT, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
SDL_Surface* working = src;
|
||||
if (src->format != SDL_PIXELFORMAT_RGBA32) {
|
||||
@ -166,14 +166,14 @@ static Uint32 UploadTextureData(SDL_Surface* src, bool useNPOT, bool isUi)
|
||||
finalSurface = resized;
|
||||
}
|
||||
|
||||
Uint32 texId = GL11_UploadTextureData(finalSurface->pixels, finalSurface->w, finalSurface->h, isUi);
|
||||
Uint32 texId = GL11_UploadTextureData(finalSurface->pixels, finalSurface->w, finalSurface->h, isUI, scaleX, scaleY);
|
||||
if (finalSurface != src) {
|
||||
SDL_DestroySurface(finalSurface);
|
||||
}
|
||||
return texId;
|
||||
}
|
||||
|
||||
Uint32 OpenGL1Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
Uint32 OpenGL1Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
|
||||
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
|
||||
@ -183,7 +183,7 @@ Uint32 OpenGL1Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
if (tex.texture == texture) {
|
||||
if (tex.version != texture->m_version) {
|
||||
GL11_DestroyTexture(tex.glTextureId);
|
||||
tex.glTextureId = UploadTextureData(surface->m_surface, m_useNPOT, isUi);
|
||||
tex.glTextureId = UploadTextureData(surface->m_surface, m_useNPOT, isUI, scaleX, scaleY);
|
||||
tex.version = texture->m_version;
|
||||
tex.width = surface->m_surface->w;
|
||||
tex.height = surface->m_surface->h;
|
||||
@ -192,7 +192,7 @@ Uint32 OpenGL1Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
}
|
||||
}
|
||||
|
||||
GLuint texId = UploadTextureData(surface->m_surface, m_useNPOT, isUi);
|
||||
GLuint texId = UploadTextureData(surface->m_surface, m_useNPOT, isUI, scaleX, scaleY);
|
||||
|
||||
for (Uint32 i = 0; i < m_textures.size(); ++i) {
|
||||
auto& tex = m_textures[i];
|
||||
|
||||
@ -321,7 +321,7 @@ void OpenGLES2Renderer::AddTextureDestroyCallback(Uint32 id, IDirect3DRMTexture*
|
||||
);
|
||||
}
|
||||
|
||||
bool UploadTexture(SDL_Surface* source, GLuint& outTexId, bool isUi)
|
||||
bool UploadTexture(SDL_Surface* source, GLuint& outTexId, bool isUI)
|
||||
{
|
||||
SDL_Surface* surf = source;
|
||||
if (source->format != SDL_PIXELFORMAT_RGBA32) {
|
||||
@ -335,7 +335,7 @@ bool UploadTexture(SDL_Surface* source, GLuint& outTexId, bool isUi)
|
||||
glBindTexture(GL_TEXTURE_2D, outTexId);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf->w, surf->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf->pixels);
|
||||
|
||||
if (isUi) {
|
||||
if (isUI) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
@ -362,7 +362,7 @@ bool UploadTexture(SDL_Surface* source, GLuint& outTexId, bool isUi)
|
||||
return true;
|
||||
}
|
||||
|
||||
Uint32 OpenGLES2Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
Uint32 OpenGLES2Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
|
||||
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
|
||||
@ -372,7 +372,7 @@ Uint32 OpenGLES2Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
if (tex.texture == texture) {
|
||||
if (tex.version != texture->m_version) {
|
||||
glDeleteTextures(1, &tex.glTextureId);
|
||||
if (UploadTexture(surface->m_surface, tex.glTextureId, isUi)) {
|
||||
if (UploadTexture(surface->m_surface, tex.glTextureId, isUI)) {
|
||||
tex.version = texture->m_version;
|
||||
}
|
||||
}
|
||||
@ -381,7 +381,7 @@ Uint32 OpenGLES2Renderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
}
|
||||
|
||||
GLuint texId;
|
||||
if (!UploadTexture(surface->m_surface, texId, isUi)) {
|
||||
if (!UploadTexture(surface->m_surface, texId, isUI)) {
|
||||
return NO_TEXTURE_ID;
|
||||
}
|
||||
|
||||
|
||||
@ -499,7 +499,7 @@ SDL_GPUTexture* Direct3DRMSDL3GPURenderer::CreateTextureFromSurface(SDL_Surface*
|
||||
return texptr;
|
||||
}
|
||||
|
||||
Uint32 Direct3DRMSDL3GPURenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
Uint32 Direct3DRMSDL3GPURenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
|
||||
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
|
||||
|
||||
@ -554,7 +554,7 @@ void Direct3DRMSoftwareRenderer::AddTextureDestroyCallback(Uint32 id, IDirect3DR
|
||||
);
|
||||
}
|
||||
|
||||
Uint32 Direct3DRMSoftwareRenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUi)
|
||||
Uint32 Direct3DRMSoftwareRenderer::GetTextureId(IDirect3DRMTexture* iTexture, bool isUI, float scaleX, float scaleY)
|
||||
{
|
||||
auto texture = static_cast<Direct3DRMTextureImpl*>(iTexture);
|
||||
auto surface = static_cast<DirectDrawSurfaceImpl*>(texture->m_surface);
|
||||
|
||||
@ -78,11 +78,13 @@ HRESULT FrameBufferImpl::Blt(
|
||||
if (!surface) {
|
||||
return DDERR_GENERIC;
|
||||
}
|
||||
Uint32 textureId = DDRenderer->GetTextureId(surface->ToTexture(), true);
|
||||
SDL_Rect srcRect =
|
||||
lpSrcRect ? ConvertRect(lpSrcRect) : SDL_Rect{0, 0, surface->m_surface->w, surface->m_surface->h};
|
||||
SDL_Rect dstRect =
|
||||
lpDestRect ? ConvertRect(lpDestRect) : SDL_Rect{0, 0, (int) m_virtualWidth, (int) m_virtualHeight};
|
||||
float scaleX = (float) dstRect.w / (float) srcRect.w;
|
||||
float scaleY = (float) dstRect.h / (float) srcRect.h;
|
||||
Uint32 textureId = DDRenderer->GetTextureId(surface->ToTexture(), true, scaleX, scaleY);
|
||||
DDRenderer->Draw2DImage(textureId, srcRect, dstRect, {1.0f, 1.0f, 1.0f, 1.0f});
|
||||
|
||||
return DD_OK;
|
||||
|
||||
@ -31,7 +31,7 @@ class Direct3DRMRenderer : public IDirect3DDevice2 {
|
||||
virtual void PushLights(const SceneLight* vertices, size_t count) = 0;
|
||||
virtual void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) = 0;
|
||||
virtual void SetFrustumPlanes(const Plane* frustumPlanes) = 0;
|
||||
virtual Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi = false) = 0;
|
||||
virtual Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI = false, float scaleX = 0, float scaleY = 0) = 0;
|
||||
virtual Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) = 0;
|
||||
int GetWidth() { return m_width; }
|
||||
int GetHeight() { return m_height; }
|
||||
|
||||
@ -32,7 +32,7 @@ class Citro3DRenderer : public Direct3DRMRenderer {
|
||||
void PushLights(const SceneLight* lightsArray, size_t count) override;
|
||||
void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) override;
|
||||
void SetFrustumPlanes(const Plane* frustumPlanes) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI, float scaleX, float scaleY) override;
|
||||
Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) override;
|
||||
HRESULT BeginFrame() override;
|
||||
void EnableTransparency() override;
|
||||
|
||||
@ -18,7 +18,7 @@ class DirectX9Renderer : public Direct3DRMRenderer {
|
||||
void PushLights(const SceneLight* lightsArray, size_t count) override;
|
||||
void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) override;
|
||||
void SetFrustumPlanes(const Plane* frustumPlanes) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI, float scaleX, float scaleY) override;
|
||||
Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) override;
|
||||
HRESULT BeginFrame() override;
|
||||
void EnableTransparency() override;
|
||||
|
||||
@ -18,7 +18,7 @@ class OpenGL1Renderer : public Direct3DRMRenderer {
|
||||
void PushLights(const SceneLight* lightsArray, size_t count) override;
|
||||
void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) override;
|
||||
void SetFrustumPlanes(const Plane* frustumPlanes) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI, float scaleX, float scaleY) override;
|
||||
Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) override;
|
||||
HRESULT BeginFrame() override;
|
||||
void EnableTransparency() override;
|
||||
|
||||
@ -39,7 +39,7 @@ class OpenGLES2Renderer : public Direct3DRMRenderer {
|
||||
void PushLights(const SceneLight* lightsArray, size_t count) override;
|
||||
void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) override;
|
||||
void SetFrustumPlanes(const Plane* frustumPlanes) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI, float scaleX, float scaleY) override;
|
||||
Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) override;
|
||||
HRESULT BeginFrame() override;
|
||||
void EnableTransparency() override;
|
||||
|
||||
@ -47,7 +47,7 @@ class Direct3DRMSDL3GPURenderer : public Direct3DRMRenderer {
|
||||
static Direct3DRMRenderer* Create(DWORD width, DWORD height);
|
||||
~Direct3DRMSDL3GPURenderer() override;
|
||||
void PushLights(const SceneLight* vertices, size_t count) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI, float scaleX, float scaleY) override;
|
||||
Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) override;
|
||||
void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) override;
|
||||
void SetFrustumPlanes(const Plane* frustumPlanes) override;
|
||||
|
||||
@ -29,7 +29,7 @@ class Direct3DRMSoftwareRenderer : public Direct3DRMRenderer {
|
||||
Direct3DRMSoftwareRenderer(DWORD width, DWORD height);
|
||||
~Direct3DRMSoftwareRenderer() override;
|
||||
void PushLights(const SceneLight* vertices, size_t count) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUi) override;
|
||||
Uint32 GetTextureId(IDirect3DRMTexture* texture, bool isUI, float scaleX, float scaleY) override;
|
||||
Uint32 GetMeshId(IDirect3DRMMesh* mesh, const MeshGroup* meshGroup) override;
|
||||
void SetProjection(const D3DRMMATRIX4D& projection, D3DVALUE front, D3DVALUE back) override;
|
||||
void SetFrustumPlanes(const Plane* frustumPlanes) override;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user