Non-power-of-2 padding for background images

* added padding options
 * removed old hardcoded image sizes
dev-ui
Piotr Dziwinski 2013-05-11 23:05:20 +02:00
parent dcf4c8941f
commit cec406ea31
9 changed files with 82 additions and 31 deletions

2
data

@ -1 +1 @@
Subproject commit 660c60beb4076786cbc366fe6d0bbe07a533df10 Subproject commit 6d1ff8c3c48f8492824d27a621ecd8fb5879b182

View File

@ -17,6 +17,8 @@
#include "common/image.h" #include "common/image.h"
#include "math/func.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -199,6 +201,33 @@ void CImage::Fill(Gfx::IntColor color)
SDL_FillRect(m_data->surface, nullptr, c); SDL_FillRect(m_data->surface, nullptr, c);
} }
/**
* Image must be valid.
*
* The dimensions are increased to nearest even power of two values.
* If image is already in power-of-two format, nothing is done.
*/
void CImage::PadToNearestPowerOfTwo()
{
assert(m_data != nullptr);
if (Math::IsPowerOfTwo(m_data->surface->w) && Math::IsPowerOfTwo(m_data->surface->h))
return;
int w = Math::NextPowerOfTwo(m_data->surface->w);
int h = Math::NextPowerOfTwo(m_data->surface->h);
m_data->surface->flags &= (~SDL_SRCALPHA);
SDL_Surface* resizedSurface = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00,
0x000000ff, 0xff000000);
assert(resizedSurface != NULL);
SDL_BlitSurface(m_data->surface, NULL, resizedSurface, NULL);
SDL_FreeSurface(m_data->surface);
m_data->surface = resizedSurface;
}
/** /**
* Image must be valid and pixel coords in valid range. * Image must be valid and pixel coords in valid range.
* *

View File

@ -94,6 +94,9 @@ public:
//! Returns the precise color at given pixel //! Returns the precise color at given pixel
Gfx::IntColor GetPixelInt(Math::IntPoint pixel); Gfx::IntColor GetPixelInt(Math::IntPoint pixel);
//! Pads the image to nearest power of 2 dimensions
void PadToNearestPowerOfTwo();
//! Loads an image from the specified file //! Loads an image from the specified file
bool Load(const std::string &fileName); bool Load(const std::string &fileName);

View File

@ -136,6 +136,8 @@ struct TextureCreateParams
TexMinFilter minFilter; TexMinFilter minFilter;
//! Magnification filter //! Magnification filter
TexMagFilter magFilter; TexMagFilter magFilter;
//! Pad the image to nearest power of 2 dimensions
bool padToNearestPowerOfTwo;
//! Constructor; calls LoadDefault() //! Constructor; calls LoadDefault()
TextureCreateParams() TextureCreateParams()
@ -146,6 +148,7 @@ struct TextureCreateParams
{ {
format = TEX_IMG_RGB; format = TEX_IMG_RGB;
mipmap = false; mipmap = false;
padToNearestPowerOfTwo = false;
minFilter = TEX_MIN_FILTER_NEAREST; minFilter = TEX_MIN_FILTER_NEAREST;
magFilter = TEX_MAG_FILTER_NEAREST; magFilter = TEX_MAG_FILTER_NEAREST;
@ -212,6 +215,8 @@ struct Texture
unsigned int id; unsigned int id;
//! Size of texture //! Size of texture
Math::IntPoint size; Math::IntPoint size;
//! Original size of texture (as loaded from image)
Math::IntPoint originalSize;
//! Whether the texture has alpha channel //! Whether the texture has alpha channel
bool alpha; bool alpha;

View File

@ -94,7 +94,6 @@ CEngine::CEngine(CApplication *app)
m_backgroundCloudUp = Color(); m_backgroundCloudUp = Color();
m_backgroundCloudDown = Color(); m_backgroundCloudDown = Color();
m_backgroundFull = false; m_backgroundFull = false;
m_backgroundScale = Math::Point(1.0f, 1.0f);
m_overFront = true; m_overFront = true;
m_overColor = Color(); m_overColor = Color();
m_overMode = ENG_RSTATE_TCOLOR_BLACK; m_overMode = ENG_RSTATE_TCOLOR_BLACK;
@ -2188,7 +2187,11 @@ bool CEngine::LoadAllTextures()
LoadTexture("map.png"); LoadTexture("map.png");
if (! m_backgroundName.empty()) if (! m_backgroundName.empty())
m_backgroundTex = LoadTexture(m_backgroundName); {
TextureCreateParams params = m_defaultTexParams;
params.padToNearestPowerOfTwo = true;
m_backgroundTex = LoadTexture(m_backgroundName, params);
}
else else
m_backgroundTex.SetInvalid(); m_backgroundTex.SetInvalid();
@ -2609,8 +2612,7 @@ float CEngine::GetFogStart(int rank)
} }
void CEngine::SetBackground(const std::string& name, Color up, Color down, void CEngine::SetBackground(const std::string& name, Color up, Color down,
Color cloudUp, Color cloudDown, Color cloudUp, Color cloudDown, bool full)
bool full, Math::Point scale)
{ {
if (m_backgroundTex.Valid()) if (m_backgroundTex.Valid())
{ {
@ -2624,15 +2626,17 @@ void CEngine::SetBackground(const std::string& name, Color up, Color down,
m_backgroundCloudUp = cloudUp; m_backgroundCloudUp = cloudUp;
m_backgroundCloudDown = cloudDown; m_backgroundCloudDown = cloudDown;
m_backgroundFull = full; m_backgroundFull = full;
m_backgroundScale = scale;
if (! m_backgroundName.empty()) if (! m_backgroundName.empty())
m_backgroundTex = LoadTexture(m_backgroundName); {
TextureCreateParams params = m_defaultTexParams;
params.padToNearestPowerOfTwo = true;
m_backgroundTex = LoadTexture(m_backgroundName, params);
}
} }
void CEngine::GetBackground(std::string& name, Color& up, Color& down, void CEngine::GetBackground(std::string& name, Color& up, Color& down,
Color& cloudUp, Color& cloudDown, Color& cloudUp, Color& cloudDown, bool &full)
bool &full, Math::Point& scale)
{ {
name = m_backgroundName; name = m_backgroundName;
up = m_backgroundColorUp; up = m_backgroundColorUp;
@ -2640,7 +2644,6 @@ void CEngine::GetBackground(std::string& name, Color& up, Color& down,
cloudUp = m_backgroundCloudUp; cloudUp = m_backgroundCloudUp;
cloudDown = m_backgroundCloudDown; cloudDown = m_backgroundCloudDown;
full = m_backgroundFull; full = m_backgroundFull;
scale = m_backgroundScale;
} }
void CEngine::SetForegroundName(const std::string& name) void CEngine::SetForegroundName(const std::string& name)
@ -3900,8 +3903,12 @@ void CEngine::DrawBackgroundImage()
v2 = v1+h; v2 = v1+h;
} }
u2 *= m_backgroundScale.x; Math::Point backgroundScale;
v2 *= m_backgroundScale.y; backgroundScale.x = static_cast<float>(m_backgroundTex.originalSize.x) / static_cast<float>(m_backgroundTex.size.x);
backgroundScale.y = static_cast<float>(m_backgroundTex.originalSize.y) / static_cast<float>(m_backgroundTex.size.y);
u2 *= backgroundScale.x;
v2 *= backgroundScale.y;
SetTexture(m_backgroundTex); SetTexture(m_backgroundTex);
SetState(ENG_RSTATE_OPAQUE_TEXTURE | ENG_RSTATE_WRAP); SetState(ENG_RSTATE_OPAQUE_TEXTURE | ENG_RSTATE_WRAP);

View File

@ -1050,10 +1050,10 @@ public:
//! Management of the background image to use //! Management of the background image to use
void SetBackground(const std::string& name, Color up = Color(), Color down = Color(), void SetBackground(const std::string& name, Color up = Color(), Color down = Color(),
Color cloudUp = Color(), Color cloudDown = Color(), Color cloudUp = Color(), Color cloudDown = Color(),
bool full = false, Math::Point scale = Math::Point(1.0f, 1.0f)); bool full = false);
void GetBackground(std::string& name, Color& up, Color& down, void GetBackground(std::string& name, Color& up, Color& down,
Color& cloudUp, Color& cloudDown, Color& cloudUp, Color& cloudDown,
bool& full, Math::Point& scale); bool& full);
//@} //@}
//! Specifies the name of foreground texture //! Specifies the name of foreground texture
@ -1346,7 +1346,6 @@ protected:
bool m_firstGroundSpot; bool m_firstGroundSpot;
int m_secondTexNum; int m_secondTexNum;
bool m_backgroundFull; bool m_backgroundFull;
Math::Point m_backgroundScale;
std::string m_backgroundName; std::string m_backgroundName;
Texture m_backgroundTex; Texture m_backgroundTex;
Color m_backgroundColorUp; Color m_backgroundColorUp;

View File

@ -423,11 +423,15 @@ Texture CGLDevice::CreateTexture(CImage *image, const TextureCreateParams &param
return Texture(); // invalid texture return Texture(); // invalid texture
} }
Math::IntPoint size = image->GetSize(); Math::IntPoint originalSize = image->GetSize();
if (!Math::IsPowerOfTwo(size.x) || !Math::IsPowerOfTwo(size.y))
GetLogger()->Warn("Creating non-power-of-2 texture (%dx%d)!\n", size.x, size.y);
return CreateTexture(data, params); if (params.padToNearestPowerOfTwo)
image->PadToNearestPowerOfTwo();
Texture tex = CreateTexture(data, params);
tex.originalSize = originalSize;
return tex;
} }
Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &params) Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &params)
@ -437,6 +441,11 @@ Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &par
result.size.x = data->surface->w; result.size.x = data->surface->w;
result.size.y = data->surface->h; result.size.y = data->surface->h;
if (!Math::IsPowerOfTwo(result.size.x) || !Math::IsPowerOfTwo(result.size.y))
GetLogger()->Warn("Creating non-power-of-2 texture (%dx%d)!\n", result.size.x, result.size.y);
result.originalSize = result.size;
// Use & enable 1st texture stage // Use & enable 1st texture stage
if (m_multitextureAvailable) if (m_multitextureAvailable)
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);

View File

@ -1412,9 +1412,8 @@ void CAutoBase::BeginTransit()
m_engine->SetDeepView(2000.0f); // we see very far m_engine->SetDeepView(2000.0f); // we see very far
m_engine->ApplyChange(); m_engine->ApplyChange();
Math::Point scale;
bool full; bool full;
m_engine->GetBackground(m_bgName, m_bgUp, m_bgDown, m_bgCloudUp, m_bgCloudDown, full, scale); m_engine->GetBackground(m_bgName, m_bgUp, m_bgDown, m_bgCloudUp, m_bgCloudDown, full);
m_engine->DeleteTexture(m_bgName); m_engine->DeleteTexture(m_bgName);
m_engine->SetBackground(m_bgBack, Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), m_engine->SetBackground(m_bgBack, Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),

View File

@ -389,7 +389,7 @@ pb->SetState(STATE_SHADOW);
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
@ -509,7 +509,7 @@ pb->SetState(STATE_SHADOW);
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
@ -978,7 +978,7 @@ pb->SetState(STATE_SHADOW);
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
@ -1177,7 +1177,7 @@ pb->SetState(STATE_SHADOW);
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
} }
@ -1702,7 +1702,7 @@ pos.y -= 0.048f;
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
} }
@ -1752,7 +1752,7 @@ pos.y -= 0.048f;
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
m_loadingCounter = 1; // enough time to display! m_loadingCounter = 1; // enough time to display!
@ -1774,7 +1774,7 @@ pos.y -= 0.048f;
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(861.0f / 1024.0f, 646.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
if ( m_phase == PHASE_WELCOME2 ) if ( m_phase == PHASE_WELCOME2 )
@ -1793,7 +1793,7 @@ pos.y -= 0.048f;
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(640.0f / 1024.0f, 480.0f / 512.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
if ( m_phase == PHASE_WELCOME3 ) if ( m_phase == PHASE_WELCOME3 )
@ -1812,7 +1812,7 @@ pos.y -= 0.048f;
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(640.0f / 1024.0f, 480.0f / 512.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }
@ -1948,7 +1948,7 @@ pos.y -= 0.048f;
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f), Gfx::Color(0.0f, 0.0f, 0.0f, 0.0f),
true, Math::Point(1.0f, 768.0f / 1024.0f)); true);
m_engine->SetBackForce(true); m_engine->SetBackForce(true);
} }