2012-08-02 01:43:55 +00:00
|
|
|
#include "GuiImage.h"
|
|
|
|
#include <iostream>
|
2012-08-09 21:19:07 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
int GuiImage::getWidth() { return mWidth; }
|
|
|
|
int GuiImage::getHeight() { return 100; }
|
2012-08-09 21:19:07 +00:00
|
|
|
|
2012-08-10 19:28:34 +00:00
|
|
|
GuiImage::GuiImage(int offsetX, int offsetY, std::string path, unsigned int maxWidth, unsigned int maxHeight, bool resizeExact)
|
2012-08-02 01:43:55 +00:00
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
mTextureID = 0;
|
2012-08-09 21:19:07 +00:00
|
|
|
|
|
|
|
mOffsetX = offsetX;
|
|
|
|
mOffsetY = offsetY;
|
|
|
|
|
2012-08-13 18:32:53 +00:00
|
|
|
//default origin (center of image)
|
2012-08-15 06:18:06 +00:00
|
|
|
mOriginX = 0.5;
|
|
|
|
mOriginY = 0.5;
|
2012-08-13 18:32:53 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
mWidth = 0;
|
|
|
|
mHeight = 0;
|
|
|
|
|
2012-08-13 18:32:53 +00:00
|
|
|
mTiled = false;
|
|
|
|
|
2012-08-09 21:19:07 +00:00
|
|
|
mMaxWidth = maxWidth;
|
|
|
|
mMaxHeight = maxHeight;
|
2012-08-13 18:32:53 +00:00
|
|
|
|
2012-08-10 19:28:34 +00:00
|
|
|
mResizeExact = resizeExact;
|
2012-08-13 18:32:53 +00:00
|
|
|
mUseAlpha = false;
|
2012-08-09 21:19:07 +00:00
|
|
|
|
2012-08-02 01:43:55 +00:00
|
|
|
if(!path.empty())
|
|
|
|
setImage(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiImage::~GuiImage()
|
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
unloadImage();
|
2012-08-02 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 02:17:48 +00:00
|
|
|
void GuiImage::loadImage(std::string path)
|
2012-08-09 21:19:07 +00:00
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
//make sure the file *exists*
|
|
|
|
if(!boost::filesystem::exists(path))
|
2012-08-02 01:43:55 +00:00
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
std::cerr << "File \"" << path << "\" not found!\n";
|
|
|
|
return;
|
|
|
|
}
|
2012-08-09 21:19:07 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
FREE_IMAGE_FORMAT format = FIF_UNKNOWN;
|
|
|
|
FIBITMAP* image = NULL;
|
|
|
|
BYTE* imageData = NULL;
|
|
|
|
unsigned int width, height;
|
2012-08-09 21:19:07 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
//detect the filetype
|
|
|
|
//format = FreeImage_GetFileType(path.c_str(), 0);
|
|
|
|
if(format == FIF_UNKNOWN)
|
|
|
|
format = FreeImage_GetFIFFromFilename(path.c_str());
|
|
|
|
if(format == FIF_UNKNOWN)
|
|
|
|
{
|
|
|
|
std::cerr << "Error - could not detect filetype for image \"" << path << "\"!\n";
|
|
|
|
return;
|
|
|
|
}
|
2012-08-09 21:19:07 +00:00
|
|
|
|
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
//make sure we can read this filetype first, then load it
|
|
|
|
if(FreeImage_FIFSupportsReading(format))
|
|
|
|
{
|
|
|
|
std::cout << "Loading image...";
|
|
|
|
image = FreeImage_Load(format, path.c_str());
|
|
|
|
std::cout << "success\n";
|
|
|
|
}else{
|
|
|
|
std::cerr << "Error - file format reading not supported for image \"" << path << "\"!\n";
|
|
|
|
return;
|
|
|
|
}
|
2012-08-13 18:32:53 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
//make sure it loaded properly
|
|
|
|
if(!image)
|
|
|
|
{
|
|
|
|
std::cerr << "Error loading image \"" << path << "\"!\n";
|
|
|
|
return;
|
|
|
|
}
|
2012-08-10 19:28:34 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
imageData = FreeImage_GetBits(image);
|
|
|
|
if(!imageData)
|
|
|
|
{
|
|
|
|
std::cerr << "Error retriving bits from image \"" << path << "\"!\n";
|
|
|
|
return;
|
|
|
|
}
|
2012-08-02 01:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
width = FreeImage_GetWidth(image);
|
|
|
|
height = FreeImage_GetHeight(image);
|
2012-08-13 18:32:53 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
if(!width || !height)
|
|
|
|
{
|
|
|
|
std::cerr << "Width or height are zero for image \"" << path << "\"!\n";
|
|
|
|
return;
|
2012-08-10 02:17:48 +00:00
|
|
|
}
|
2012-08-29 18:53:53 +00:00
|
|
|
|
|
|
|
//force power of two for testing
|
|
|
|
width = 512; height = 512;
|
|
|
|
|
|
|
|
//now for the openGL texture stuff
|
|
|
|
glGenTextures(1, &mTextureID);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mTextureID);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
|
|
|
|
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
|
|
|
|
//free the image data
|
|
|
|
FreeImage_Unload(image);
|
|
|
|
|
|
|
|
std::cout << "Image load successful, w: " << mWidth << " h: " << mHeight << " texID: " << mTextureID << "\n";
|
2012-08-09 21:19:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
void GuiImage::unloadImage()
|
2012-08-13 18:32:53 +00:00
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
if(mTextureID)
|
2012-08-13 18:32:53 +00:00
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
std::cout << "deleting texture\n";
|
|
|
|
glDeleteTextures(1, &mTextureID);
|
2012-08-13 18:32:53 +00:00
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
mTextureID = 0;
|
2012-08-13 18:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 21:19:07 +00:00
|
|
|
void GuiImage::setImage(std::string path)
|
|
|
|
{
|
2012-08-10 02:17:48 +00:00
|
|
|
if(mPath == path)
|
|
|
|
return;
|
2012-08-09 21:19:07 +00:00
|
|
|
|
2012-08-10 02:17:48 +00:00
|
|
|
mPath = path;
|
|
|
|
|
2012-08-29 18:53:53 +00:00
|
|
|
unloadImage();
|
2012-08-10 02:17:48 +00:00
|
|
|
if(!path.empty())
|
|
|
|
loadImage(path);
|
|
|
|
|
2012-08-02 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 18:32:53 +00:00
|
|
|
void GuiImage::setOrigin(float originX, float originY)
|
|
|
|
{
|
|
|
|
mOriginX = originX;
|
|
|
|
mOriginY = originY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiImage::setTiling(bool tile)
|
|
|
|
{
|
|
|
|
mTiled = tile;
|
|
|
|
|
|
|
|
if(mTiled)
|
|
|
|
mResizeExact = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiImage::setAlpha(bool useAlpha)
|
|
|
|
{
|
|
|
|
mUseAlpha = useAlpha;
|
|
|
|
}
|
|
|
|
|
2012-08-02 01:43:55 +00:00
|
|
|
void GuiImage::onRender()
|
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
if(mTextureID)
|
2012-08-13 18:32:53 +00:00
|
|
|
{
|
2012-08-29 18:53:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, mTextureID);
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
|
|
|
|
GLfloat points[12];
|
|
|
|
points[0] = mOffsetX - (mWidth * mOriginX); points[1] = mOffsetY - (mHeight * mOriginY);
|
|
|
|
points[2] = mOffsetX - (mWidth * mOriginX); points[3] = mOffsetY + (mHeight * (1 - mOriginY));
|
|
|
|
points[4] = mOffsetX + (mWidth * (1 - mOriginX)); points[5] = mOffsetY - (mHeight * mOriginY);
|
|
|
|
|
|
|
|
points[6] = mOffsetX + (mWidth * (1 - mOriginX)); points[7] = mOffsetY - (mHeight * mOriginY);
|
|
|
|
points[8] = mOffsetX - (mWidth * mOriginX); points[9] = mOffsetY + (mHeight * (1 - mOriginY));
|
|
|
|
points[10] = mOffsetX + (mWidth * (1 -mOriginX)); points[11] = mOffsetY + (mHeight * (1 - mOriginY));
|
|
|
|
|
|
|
|
//std::cout << "x: " << points[0] << " y: " << points[1] << " to x: " << points[10] << " y: " << points[11] << std::endl;
|
|
|
|
//std::cout << "(w: " << mWidth << " h: " << mHeight << ")" << std::endl;
|
|
|
|
|
|
|
|
GLfloat texs[12];
|
|
|
|
texs[0] = 0; texs[1] = 0;
|
|
|
|
texs[2] = 0; texs[3] = 1;
|
|
|
|
texs[4] = 1; texs[5] = 0;
|
|
|
|
|
|
|
|
texs[6] = 1; texs[7] = 0;
|
|
|
|
texs[8] = 0; texs[9] = 1;
|
|
|
|
texs[10] = 1; texs[11] = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, points);
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 0, texs);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
2012-08-13 18:32:53 +00:00
|
|
|
}
|
2012-08-02 01:43:55 +00:00
|
|
|
}
|