ES-DE/src/components/GuiImage.cpp

107 lines
2.1 KiB
C++
Raw Normal View History

#include "GuiImage.h"
#include <SDL/SDL_image.h>
#include <SDL/SDL_rotozoom.h>
#include <iostream>
#include <boost/filesystem.hpp>
int GuiImage::getWidth() { if(mSurface) return mSurface->w; else return 0; }
int GuiImage::getHeight() { if(mSurface) return mSurface->h; else return 0; }
int startImageLoadThread(void*);
GuiImage::GuiImage(int offsetX, int offsetY, std::string path, unsigned int maxWidth, unsigned int maxHeight)
{
std::cout << "Creating GuiImage\n";
mSurface = NULL;
mOffsetX = offsetX;
mOffsetY = offsetY;
mMaxWidth = maxWidth;
mMaxHeight = maxHeight;
if(!path.empty())
setImage(path);
}
GuiImage::~GuiImage()
{
if(mSurface)
SDL_FreeSurface(mSurface);
}
2012-08-10 02:17:48 +00:00
void GuiImage::loadImage(std::string path)
{
2012-08-10 02:17:48 +00:00
if(boost::filesystem::exists(path))
{
2012-08-10 02:17:48 +00:00
//start loading the image
SDL_Surface* newSurf = IMG_Load(path.c_str());
2012-08-10 02:17:48 +00:00
//if we started loading something else or failed to load, don't bother resizing
if(newSurf == NULL)
{
2012-08-10 02:17:48 +00:00
std::cerr << "Error loading image.\n";
return;
}
2012-08-10 02:17:48 +00:00
//resize it
if(mMaxWidth && newSurf->w > mMaxWidth)
{
double scale = (double)mMaxWidth / (double)newSurf->w;
2012-08-10 02:17:48 +00:00
SDL_Surface* resSurf = zoomSurface(newSurf, scale, scale, SMOOTHING_OFF);
SDL_FreeSurface(newSurf);
newSurf = resSurf;
}
2012-08-10 02:17:48 +00:00
if(mMaxHeight && newSurf->h > mMaxHeight)
{
double scale = (double)mMaxHeight / (double)newSurf->h;
2012-08-10 02:17:48 +00:00
SDL_Surface* resSurf = zoomSurface(newSurf, scale, scale, SMOOTHING_OFF);
SDL_FreeSurface(newSurf);
newSurf = resSurf;
}
2012-08-10 02:17:48 +00:00
//finally set the image and delete the old one
if(mSurface)
SDL_FreeSurface(mSurface);
2012-08-10 02:17:48 +00:00
mSurface = newSurf;
2012-08-10 02:17:48 +00:00
//Also update the rect
mRect.x = mOffsetX - (mSurface->w / 2);
mRect.y = mOffsetY;
mRect.w = 0;
mRect.h = 0;
}else{
std::cerr << "File \"" << path << "\" not found!\n";
}
}
void GuiImage::setImage(std::string path)
{
2012-08-10 02:17:48 +00:00
if(mPath == path)
return;
2012-08-10 02:17:48 +00:00
mPath = path;
if(mSurface)
{
2012-08-10 02:17:48 +00:00
SDL_FreeSurface(mSurface);
mSurface = NULL;
}
2012-08-10 02:17:48 +00:00
if(!path.empty())
loadImage(path);
}
void GuiImage::onRender()
{
if(mSurface)
2012-08-10 02:17:48 +00:00
SDL_BlitSurface(mSurface, NULL, Renderer::screen, &mRect);
}