Refactored ImageComponent to use Vector2.

This commit is contained in:
Aloshi 2013-06-02 16:05:29 -05:00
parent 24512c0c9f
commit 826624481a
3 changed files with 62 additions and 86 deletions

View file

@ -5,29 +5,27 @@
#include "../Log.h" #include "../Log.h"
#include "../Renderer.h" #include "../Renderer.h"
unsigned int ImageComponent::getWidth() { return mDrawWidth; } unsigned int ImageComponent::getWidth() { return mSize.x; }
unsigned int ImageComponent::getHeight() { return mDrawHeight; } unsigned int ImageComponent::getHeight() { return mSize.y; }
ImageComponent::ImageComponent(Window* window, int offsetX, int offsetY, std::string path, unsigned int resizeWidth, unsigned int resizeHeight, bool resizeExact) : GuiComponent(window) ImageComponent::ImageComponent(Window* window, int offsetX, int offsetY, std::string path, unsigned int resizeWidth, unsigned int resizeHeight, bool allowUpscale) : GuiComponent(window)
{ {
mTextureID = 0; mTextureID = 0;
setOffset(Vector2i(offsetX, offsetY)); setOffset(Vector2i(offsetX, offsetY));
//default origin is the center of image //default origin is the center of image
mOriginX = 0.5; mOrigin.x = 0.5;
mOriginY = 0.5; mOrigin.y = 0.5;
mOpacity = 255;
mWidth = mDrawWidth = 0; mOpacity = 255;
mHeight = mDrawHeight = 0;
mTiled = false; mTiled = false;
mResizeWidth = resizeWidth; mTargetSize.x = resizeWidth;
mResizeHeight = resizeHeight; mTargetSize.y = resizeHeight;
mResizeExact = resizeExact; mAllowUpscale = allowUpscale;
mFlipX = false; mFlipX = false;
mFlipY = false; mFlipY = false;
@ -46,7 +44,7 @@ void ImageComponent::loadImage(std::string path)
//make sure the file *exists* //make sure the file *exists*
if(!boost::filesystem::exists(path)) if(!boost::filesystem::exists(path))
{ {
LOG(LogError) << "File \"" << path << "\" not found!"; LOG(LogError) << "Image \"" << path << "\" not found!";
return; return;
} }
@ -112,29 +110,6 @@ void ImageComponent::loadImage(std::string path)
return; return;
} }
/*
//set width/height to powers of 2 for OpenGL
for(unsigned int i = 0; i < 22; i++)
{
unsigned int pwrOf2 = pow(2, i);
if(!widthPwrOf2 && pwrOf2 >= width)
widthPwrOf2 = pwrOf2;
if(!heightPwrOf2 && pwrOf2 >= height)
heightPwrOf2 = pwrOf2;
if(widthPwrOf2 && heightPwrOf2)
break;
}
if(!widthPwrOf2 || !heightPwrOf2)
{
LOG(LogError) << "Error assigning power of two for width or height of image!";
FreeImage_Unload(image);
return;
}*/
//convert from BGRA to RGBA //convert from BGRA to RGBA
GLubyte* imageRGBA = new GLubyte[4*width*height]; GLubyte* imageRGBA = new GLubyte[4*width*height];
for(unsigned int i = 0; i < width*height; i++) for(unsigned int i = 0; i < width*height; i++)
@ -159,8 +134,8 @@ void ImageComponent::loadImage(std::string path)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
mWidth = width; mTextureSize.x = width;
mHeight = height; mTextureSize.y = height;
//free the image data //free the image data
FreeImage_Unload(image); FreeImage_Unload(image);
@ -173,36 +148,32 @@ void ImageComponent::loadImage(std::string path)
void ImageComponent::resize() void ImageComponent::resize()
{ {
mDrawWidth = mWidth; mSize.x = mTextureSize.x;
mDrawHeight = mHeight; mSize.y = mTextureSize.y;
//(we don't resize tiled images) //(we don't resize tiled images)
if(!mTiled) if(!mTiled && (mTargetSize.x || mTargetSize.y))
{ {
float resizeScaleX = 0, resizeScaleY = 0; Vector2f resizeScale;
if(mResizeExact)
{
if(mResizeWidth)
resizeScaleX = (float)mResizeWidth / mWidth;
if(mResizeHeight)
resizeScaleY = (float)mResizeHeight / mHeight;
}else{
if(mResizeWidth && mWidth > mResizeWidth)
resizeScaleX = (float)mResizeWidth / mWidth;
if(mResizeHeight && mHeight > mResizeHeight) if(mTargetSize.x && (mAllowUpscale || mSize.x > mTargetSize.x))
resizeScaleY = (float)mResizeHeight / mHeight; {
resizeScale.x = (float)mTargetSize.x / mSize.x;
}
if(mTargetSize.y && (mAllowUpscale || mSize.y > mTargetSize.y))
{
resizeScale.y = (float)mTargetSize.y / mSize.y;
} }
if(resizeScaleX && !resizeScaleY) if(resizeScale.x && !resizeScale.y)
resizeScaleY = resizeScaleX; resizeScale.y = resizeScale.x;
if(resizeScaleY && !resizeScaleX) if(resizeScale.y && !resizeScale.x)
resizeScaleX = resizeScaleY; resizeScale.x = resizeScale.y;
if(resizeScaleX) if(resizeScale.x)
mDrawWidth = (int)(mDrawWidth * resizeScaleX); mSize.x = (int)(mSize.x * resizeScale.x);
if(resizeScaleY) if(resizeScale.y)
mDrawHeight = (int)(mDrawHeight * resizeScaleY); mSize.y = (int)(mSize.y * resizeScale.y);
} }
} }
@ -231,8 +202,8 @@ void ImageComponent::setImage(std::string path)
void ImageComponent::setOrigin(float originX, float originY) void ImageComponent::setOrigin(float originX, float originY)
{ {
mOriginX = originX; mOrigin.x = originX;
mOriginY = originY; mOrigin.y = originY;
} }
void ImageComponent::setTiling(bool tile) void ImageComponent::setTiling(bool tile)
@ -240,14 +211,14 @@ void ImageComponent::setTiling(bool tile)
mTiled = tile; mTiled = tile;
if(mTiled) if(mTiled)
mResizeExact = false; mAllowUpscale = false;
} }
void ImageComponent::setResize(unsigned int width, unsigned int height, bool resizeExact) void ImageComponent::setResize(unsigned int width, unsigned int height, bool allowUpscale)
{ {
mResizeWidth = width; mTargetSize.x = width;
mResizeHeight = height; mTargetSize.y = height;
mResizeExact = resizeExact; mAllowUpscale = allowUpscale;
resize(); resize();
} }
@ -270,9 +241,9 @@ void ImageComponent::onRender()
if(mTiled) if(mTiled)
{ {
float xCount = ((float)mResizeWidth/mWidth); float xCount = (float)mTargetSize.x / mTextureSize.x;
float yCount = ((float)mResizeHeight/mHeight); float yCount = (float)mTargetSize.y / mTextureSize.y;
Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6); Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6);
buildImageArray(0, 0, points, texs, xCount, yCount); buildImageArray(0, 0, points, texs, xCount, yCount);
}else{ }else{
@ -288,13 +259,13 @@ void ImageComponent::onRender()
void ImageComponent::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs, float px, float py) void ImageComponent::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs, float px, float py)
{ {
points[0] = posX - (mDrawWidth * mOriginX) * px; points[1] = posY - (mDrawHeight * mOriginY) * py; points[0] = posX - (mSize.x * mOrigin.x) * px; points[1] = posY - (mSize.y * mOrigin.y) * py;
points[2] = posX - (mDrawWidth * mOriginX) * px; points[3] = posY + (mDrawHeight * (1 - mOriginY)) * py; points[2] = posX - (mSize.x * mOrigin.x) * px; points[3] = posY + (mSize.y * (1 - mOrigin.y)) * py;
points[4] = posX + (mDrawWidth * (1 - mOriginX)) * px; points[5] = posY - (mDrawHeight * mOriginY) * py; points[4] = posX + (mSize.x * (1 - mOrigin.x)) * px; points[5] = posY - (mSize.y * mOrigin.y) * py;
points[6] = posX + (mDrawWidth * (1 - mOriginX)) * px; points[7] = posY - (mDrawHeight * mOriginY) * py; points[6] = posX + (mSize.x * (1 - mOrigin.x)) * px; points[7] = posY - (mSize.y * mOrigin.y) * py;
points[8] = posX - (mDrawWidth * mOriginX) * px; points[9] = posY + (mDrawHeight * (1 - mOriginY)) * py; points[8] = posX - (mSize.x * mOrigin.x) * px; points[9] = posY + (mSize.y * (1 - mOrigin.y)) * py;
points[10] = posX + (mDrawWidth * (1 -mOriginX)) * px; points[11] = posY + (mDrawHeight * (1 - mOriginY)) * py; points[10] = posX + (mSize.x * (1 -mOrigin.x)) * px; points[11] = posY + (mSize.y * (1 - mOrigin.y)) * py;
@ -359,11 +330,15 @@ void ImageComponent::init()
{ {
if(!mPath.empty()) if(!mPath.empty())
loadImage(mPath); loadImage(mPath);
GuiComponent::init();
} }
void ImageComponent::deinit() void ImageComponent::deinit()
{ {
unloadImage(); unloadImage();
GuiComponent::deinit();
} }
bool ImageComponent::hasImage() bool ImageComponent::hasImage()

View file

@ -13,15 +13,15 @@ class ImageComponent : public GuiComponent
{ {
public: public:
//Creates a new GuiImage at the given location. If given an image, it will be loaded. If maxWidth and/or maxHeight are nonzero, the image will be //Creates a new GuiImage at the given location. If given an image, it will be loaded. If maxWidth and/or maxHeight are nonzero, the image will be
//resized to fix. If only one axis is specified, the other will be resized in accordance with the image's aspect ratio. If resizeExact is false, //resized to fit. If only one axis is specified, the other will be set in accordance with the image's aspect ratio. If allowUpscale is false,
//the image will only be downscaled, never upscaled (the image's size must surpass at least one nonzero bound). //the image will only be downscaled, never upscaled (the image's size must surpass at least one nonzero bound).
ImageComponent(Window* window, int offsetX = 0, int offsetY = 0, std::string path = "", unsigned int maxWidth = 0, unsigned int maxHeight = 0, bool resizeExact = false); ImageComponent(Window* window, int offsetX = 0, int offsetY = 0, std::string path = "", unsigned int maxWidth = 0, unsigned int maxHeight = 0, bool allowUpscale = false);
virtual ~ImageComponent(); virtual ~ImageComponent();
void setImage(std::string path); //Loads the image at the given filepath. void setImage(std::string path); //Loads the image at the given filepath.
void setOrigin(float originX, float originY); //Sets the origin as a percentage of this image (e.g. (0, 0) is top left, (0.5, 0.5) is the center) void setOrigin(float originX, float originY); //Sets the origin as a percentage of this image (e.g. (0, 0) is top left, (0.5, 0.5) is the center)
void setTiling(bool tile); //Enables or disables tiling. Must be called before loading an image or resizing will be weird. void setTiling(bool tile); //Enables or disables tiling. Must be called before loading an image or resizing will be weird.
void setResize(unsigned int width, unsigned int height, bool resizeExact); void setResize(unsigned int width, unsigned int height, bool allowUpscale);
void setFlipX(bool flip); void setFlipX(bool flip);
void setFlipY(bool flip); void setFlipY(bool flip);
@ -42,9 +42,12 @@ protected:
void onRender(); void onRender();
private: private:
unsigned int mResizeWidth, mResizeHeight; Vector2<unsigned int> mSize;
float mOriginX, mOriginY; Vector2<unsigned int> mTargetSize;
bool mResizeExact, mTiled, mFlipX, mFlipY; Vector2<unsigned int> mTextureSize;
Vector2f mOrigin;
bool mAllowUpscale, mTiled, mFlipX, mFlipY;
unsigned char mOpacity; unsigned char mOpacity;
@ -56,9 +59,6 @@ private:
std::string mPath; std::string mPath;
unsigned int mWidth, mHeight; //Our rendered size.
unsigned int mDrawWidth, mDrawHeight;
GLuint mTextureID; GLuint mTextureID;
}; };

View file

@ -1,4 +1,5 @@
//EmulationStation, a graphical front-end for ROM browsing. Created by Alec "Aloshi" Lofquist. //EmulationStation, a graphical front-end for ROM browsing. Created by Alec "Aloshi" Lofquist.
//http://www.aloshi.com
#include <SDL.h> #include <SDL.h>
#include <iostream> #include <iostream>
@ -82,7 +83,7 @@ int main(int argc, char* argv[])
std::cout << "--debug even more logging\n"; std::cout << "--debug even more logging\n";
std::cout << "--dimtime [seconds] time to wait before dimming the screen (default 30, use 0 for never)\n"; std::cout << "--dimtime [seconds] time to wait before dimming the screen (default 30, use 0 for never)\n";
#ifdef _DESKTOP_ #ifdef USE_OPENGL_DESKTOP
std::cout << "--windowed not fullscreen\n"; std::cout << "--windowed not fullscreen\n";
#endif #endif