Removed multithreaded image loader.

This commit is contained in:
Aloshi 2012-08-09 21:17:48 -05:00
parent 9c86241cf8
commit 2a0c338cdf
2 changed files with 57 additions and 140 deletions

View file

@ -21,77 +21,31 @@ GuiImage::GuiImage(int offsetX, int offsetY, std::string path, unsigned int maxW
mMaxWidth = maxWidth; mMaxWidth = maxWidth;
mMaxHeight = maxHeight; mMaxHeight = maxHeight;
mPathMutex = SDL_CreateMutex();
mSurfaceMutex = SDL_CreateMutex();
mDeleting = false;
mLoadThread = SDL_CreateThread(&startImageLoadThread, this);
if(!mLoadThread)
{
std::cerr << "Error - could not create image load thread!\n";
std::cerr << " " << SDL_GetError() << "\n";
}
if(!path.empty()) if(!path.empty())
setImage(path); setImage(path);
} }
GuiImage::~GuiImage() GuiImage::~GuiImage()
{ {
mDeleting = true;
if(mLoadThread)
SDL_WaitThread(mLoadThread, NULL);
if(mSurface) if(mSurface)
SDL_FreeSurface(mSurface); SDL_FreeSurface(mSurface);
} }
void GuiImage::loadImage(std::string path)
std::string GuiImage::getPathThreadSafe()
{ {
std::string ret; if(boost::filesystem::exists(path))
SDL_mutexP(mPathMutex);
ret = mPath;
SDL_mutexV(mPathMutex);
return ret;
}
void GuiImage::setPathThreadSafe(std::string path)
{
SDL_mutexP(mPathMutex);
mPath = path;
if(mPath.empty())
mLoadedPath = "";
SDL_mutexV(mPathMutex);
}
int GuiImage::runImageLoadThread()
{
while(!mDeleting)
{
std::string path = getPathThreadSafe();
if(path != mLoadedPath && path != "" && boost::filesystem::exists(path))
{ {
//start loading the image //start loading the image
SDL_Surface* newSurf = IMG_Load(path.c_str()); SDL_Surface* newSurf = IMG_Load(path.c_str());
//if we started loading something else or failed to load, don't bother resizing //if we started loading something else or failed to load, don't bother resizing
if(path != getPathThreadSafe() || newSurf == NULL) if(newSurf == NULL)
{ {
if(newSurf) std::cerr << "Error loading image.\n";
SDL_FreeSurface(newSurf); return;
continue;
} }
//std::cout << "Loading complete, checking for resize\n";
//resize it //resize it
if(mMaxWidth && newSurf->w > mMaxWidth) if(mMaxWidth && newSurf->w > mMaxWidth)
{ {
@ -111,17 +65,7 @@ int GuiImage::runImageLoadThread()
newSurf = resSurf; newSurf = resSurf;
} }
//again, make sure we're still good to go
if(path != getPathThreadSafe() || newSurf == NULL)
{
if(newSurf)
SDL_FreeSurface(newSurf);
continue;
}
//finally set the image and delete the old one //finally set the image and delete the old one
SDL_mutexP(mSurfaceMutex);
if(mSurface) if(mSurface)
SDL_FreeSurface(mSurface); SDL_FreeSurface(mSurface);
@ -132,48 +76,31 @@ int GuiImage::runImageLoadThread()
mRect.y = mOffsetY; mRect.y = mOffsetY;
mRect.w = 0; mRect.w = 0;
mRect.h = 0; mRect.h = 0;
}else{
mLoadedPath = path; std::cerr << "File \"" << path << "\" not found!\n";
SDL_mutexV(mSurfaceMutex);
} }
} }
std::cout << "Finishing image loader thread.\n";
return 0;
}
int startImageLoadThread(void* img)
{
return ((GuiImage*)img)->runImageLoadThread();
}
void GuiImage::setImage(std::string path) void GuiImage::setImage(std::string path)
{ {
setPathThreadSafe(path); if(mPath == path)
return;
mPath = path;
if(path.empty())
{
if(mSurface) if(mSurface)
{ {
SDL_mutexP(mSurfaceMutex);
SDL_FreeSurface(mSurface); SDL_FreeSurface(mSurface);
mSurface = NULL; mSurface = NULL;
SDL_mutexV(mSurfaceMutex);
}
} }
if(!path.empty())
loadImage(path);
} }
void GuiImage::onRender() void GuiImage::onRender()
{ {
if(mSurface) if(mSurface)
{
SDL_mutexP(mSurfaceMutex);
SDL_BlitSurface(mSurface, NULL, Renderer::screen, &mRect); SDL_BlitSurface(mSurface, NULL, Renderer::screen, &mRect);
SDL_mutexV(mSurfaceMutex);
}else if(!getPathThreadSafe().empty())
{
Renderer::drawCenteredText("Loading...", -(Renderer::getScreenWidth() - mOffsetX)/*-mOffsetX * 3*/, mOffsetY, 0x000000);
} }
}

View file

@ -19,26 +19,16 @@ public:
void onRender(); void onRender();
//this should really never be called by anything except setImage
//but it was either make this function public or make mSurface public
//so just don't use this, okay?
int runImageLoadThread();
private: private:
int mMaxWidth, mMaxHeight; int mMaxWidth, mMaxHeight;
std::string mPath, mLoadedPath; void loadImage(std::string path);
std::string mPath;
SDL_Surface* mSurface; SDL_Surface* mSurface;
int mOffsetX, mOffsetY; int mOffsetX, mOffsetY;
SDL_Rect mRect; SDL_Rect mRect;
SDL_Thread* mLoadThread;
void setPathThreadSafe(std::string path);
std::string getPathThreadSafe();
SDL_mutex* mPathMutex;
SDL_mutex* mSurfaceMutex;
bool mDeleting;
}; };
#endif #endif