fixed ratio image drawing support

This commit is contained in:
Eduardo Bart
2011-10-28 21:56:45 -02:00
parent 6c200bd764
commit f4f0d7e960
10 changed files with 66 additions and 68 deletions

View File

@@ -36,8 +36,9 @@ BorderImage::BorderImage(TexturePtr texture,
const Rect& topRight,
const Rect& bottomLeft,
const Rect& bottomRight,
const Rect& center) : Image(texture)
const Rect& center)
{
m_texture = texture;
m_leftBorderTexCoords = left;
m_rightBorderTexCoords = right;
m_topBorderTexCoords = top;
@@ -102,7 +103,7 @@ BorderImagePtr BorderImage::loadFromOTML(const OTMLNodePtr& borderImageNode)
bottomBorder = Rect(subRect.left() + left, subRect.bottom() - bottom + 1, subRect.width() - right - left, bottom);
topLeftCorner = Rect(subRect.left(), subRect.top(), left, top);
topRightCorner = Rect(subRect.right() - right + 1, subRect.top(), right, top);
bottomLeftCorner = Rect(subRect.left(), subRect.bottom() - bottom, left, bottom);
bottomLeftCorner = Rect(subRect.left(), subRect.bottom() - bottom + 1, left, bottom);
bottomRightCorner = Rect(subRect.right() - right + 1, subRect.bottom() - bottom + 1, right, bottom);
center = Rect(subRect.left() + left, subRect.top() + top, subRect.width() - right - left, subRect.height() - top - bottom);
@@ -184,7 +185,7 @@ void BorderImage::draw(const Rect& screenCoords)
// bottom left corner
rectCoords = Rect(screenCoords.left(),
screenCoords.top() + m_topBorderTexCoords.height() + centerSize.height(),
screenCoords.top() + m_topLeftCornerTexCoords.height() + centerSize.height(),
m_bottomLeftCornerTexCoords.size());
g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomLeftCornerTexCoords);

View File

@@ -27,37 +27,45 @@
#include <framework/otml/otml.h>
Image::Image(TexturePtr texture, Rect textureCoords)
Image::Image()
{
m_texture = texture;
if(!textureCoords.isValid())
m_textureCoords = Rect(0, 0, m_texture->getSize());
else
m_textureCoords = textureCoords;
m_fixedRatio = false;
}
ImagePtr Image::loadFromOTML(const OTMLNodePtr& imageNode)
void Image::loadFromOTML(const OTMLNodePtr& imageNode)
{
// load configs from otml node
std::string source = imageNode->hasValue() ? imageNode->value() : imageNode->valueAt("source");
bool smooth = imageNode->valueAt("smooth", false);
Rect textureCoords = imageNode->valueAt("coords", Rect());
m_textureCoords = imageNode->valueAt("coords", Rect());
m_fixedRatio = imageNode->valueAt("fixed ratio", false);
// load texture
TexturePtr texture = g_textures.getTexture(source);
if(!texture)
m_texture = g_textures.getTexture(source);
if(!m_texture)
throw OTMLException(imageNode, "could not load image texture");
// enable texture bilinear filter
if(smooth)
texture->enableBilinearFilter();
// create image
return ImagePtr(new Image(texture, textureCoords));
m_texture->enableBilinearFilter();
}
void Image::draw(const Rect& screenCoords)
{
if(m_texture)
g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords);
if(m_texture) {
if(m_fixedRatio) {
const Size& texSize = m_texture->getSize();
Size texCoordsSize = screenCoords.size();
texCoordsSize.scale(texSize, Fw::KeepAspectRatio);
Point texCoordsOffset;
if(texSize.height() > texCoordsSize.height())
texCoordsOffset.y = (texSize.height() - texCoordsSize.height())/2;
else if(texSize.width() > texCoordsSize.width())
texCoordsOffset.x = (texSize.width() - texCoordsSize.width())/2;
g_graphics.drawTexturedRect(screenCoords, m_texture, Rect(texCoordsOffset, texCoordsSize));
} else {
g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords);
}
}
}

View File

@@ -30,15 +30,16 @@
class Image
{
public:
Image(TexturePtr texture, Rect textureCoords = Rect());
Image();
static ImagePtr loadFromOTML(const OTMLNodePtr& imageNode);
void loadFromOTML(const OTMLNodePtr& imageNode);
virtual void draw(const Rect& screenCoords);
protected:
TexturePtr m_texture;
Rect m_textureCoords;
bool m_fixedRatio;
};
#endif

View File

@@ -44,7 +44,7 @@ public:
int getWidth() const { return m_size.width(); }
int getHeight() const { return m_size.height(); }
const Size getSize() const { return m_size; }
const Size& getSize() const { return m_size; }
const Size& getGlSize() const { return m_glSize; }
bool isEmpty() const { return m_textureId == 0; }