From 2a0c338cdf36a0e4df915a25ea2365f772b0d8d0 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Thu, 9 Aug 2012 21:17:48 -0500 Subject: [PATCH] Removed multithreaded image loader. --- src/components/GuiImage.cpp | 181 +++++++++++------------------------- src/components/GuiImage.h | 16 +--- 2 files changed, 57 insertions(+), 140 deletions(-) diff --git a/src/components/GuiImage.cpp b/src/components/GuiImage.cpp index 48c6182e0..850d55956 100644 --- a/src/components/GuiImage.cpp +++ b/src/components/GuiImage.cpp @@ -21,159 +21,86 @@ GuiImage::GuiImage(int offsetX, int offsetY, std::string path, unsigned int maxW mMaxWidth = maxWidth; 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()) setImage(path); } GuiImage::~GuiImage() { - mDeleting = true; - if(mLoadThread) - SDL_WaitThread(mLoadThread, NULL); - if(mSurface) SDL_FreeSurface(mSurface); } - - -std::string GuiImage::getPathThreadSafe() +void GuiImage::loadImage(std::string path) { - std::string ret; - - 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) + if(boost::filesystem::exists(path)) { - std::string path = getPathThreadSafe(); + //start loading the image + SDL_Surface* newSurf = IMG_Load(path.c_str()); - if(path != mLoadedPath && path != "" && boost::filesystem::exists(path)) + //if we started loading something else or failed to load, don't bother resizing + if(newSurf == NULL) { - //start loading the image - SDL_Surface* newSurf = IMG_Load(path.c_str()); - - //if we started loading something else or failed to load, don't bother resizing - if(path != getPathThreadSafe() || newSurf == NULL) - { - if(newSurf) - SDL_FreeSurface(newSurf); - - continue; - } - - - //std::cout << "Loading complete, checking for resize\n"; - - //resize it - if(mMaxWidth && newSurf->w > mMaxWidth) - { - double scale = (double)mMaxWidth / (double)newSurf->w; - - SDL_Surface* resSurf = zoomSurface(newSurf, scale, scale, SMOOTHING_OFF); - SDL_FreeSurface(newSurf); - newSurf = resSurf; - } - - if(mMaxHeight && newSurf->h > mMaxHeight) - { - double scale = (double)mMaxHeight / (double)newSurf->h; - - SDL_Surface* resSurf = zoomSurface(newSurf, scale, scale, SMOOTHING_OFF); - SDL_FreeSurface(newSurf); - 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 - SDL_mutexP(mSurfaceMutex); - if(mSurface) - SDL_FreeSurface(mSurface); - - mSurface = newSurf; - - //Also update the rect - mRect.x = mOffsetX - (mSurface->w / 2); - mRect.y = mOffsetY; - mRect.w = 0; - mRect.h = 0; - - mLoadedPath = path; - SDL_mutexV(mSurfaceMutex); + std::cerr << "Error loading image.\n"; + return; } + + + //resize it + if(mMaxWidth && newSurf->w > mMaxWidth) + { + double scale = (double)mMaxWidth / (double)newSurf->w; + + SDL_Surface* resSurf = zoomSurface(newSurf, scale, scale, SMOOTHING_OFF); + SDL_FreeSurface(newSurf); + newSurf = resSurf; + } + + if(mMaxHeight && newSurf->h > mMaxHeight) + { + double scale = (double)mMaxHeight / (double)newSurf->h; + + SDL_Surface* resSurf = zoomSurface(newSurf, scale, scale, SMOOTHING_OFF); + SDL_FreeSurface(newSurf); + newSurf = resSurf; + } + + //finally set the image and delete the old one + if(mSurface) + SDL_FreeSurface(mSurface); + + mSurface = newSurf; + + //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"; } - - std::cout << "Finishing image loader thread.\n"; - - return 0; -} - -int startImageLoadThread(void* img) -{ - return ((GuiImage*)img)->runImageLoadThread(); } void GuiImage::setImage(std::string path) { - setPathThreadSafe(path); + if(mPath == path) + return; - if(path.empty()) + mPath = path; + + if(mSurface) { - if(mSurface) - { - SDL_mutexP(mSurfaceMutex); - SDL_FreeSurface(mSurface); - mSurface = NULL; - SDL_mutexV(mSurfaceMutex); - } + SDL_FreeSurface(mSurface); + mSurface = NULL; } + + if(!path.empty()) + loadImage(path); + } void GuiImage::onRender() { if(mSurface) - { - SDL_mutexP(mSurfaceMutex); - SDL_BlitSurface(mSurface, NULL, Renderer::screen, &mRect); - SDL_mutexV(mSurfaceMutex); - }else if(!getPathThreadSafe().empty()) - { - Renderer::drawCenteredText("Loading...", -(Renderer::getScreenWidth() - mOffsetX)/*-mOffsetX * 3*/, mOffsetY, 0x000000); - } + SDL_BlitSurface(mSurface, NULL, Renderer::screen, &mRect); } - diff --git a/src/components/GuiImage.h b/src/components/GuiImage.h index 84bc7727f..f259137fa 100644 --- a/src/components/GuiImage.h +++ b/src/components/GuiImage.h @@ -19,26 +19,16 @@ public: 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: int mMaxWidth, mMaxHeight; - std::string mPath, mLoadedPath; + void loadImage(std::string path); + + std::string mPath; SDL_Surface* mSurface; int mOffsetX, mOffsetY; SDL_Rect mRect; - - SDL_Thread* mLoadThread; - void setPathThreadSafe(std::string path); - std::string getPathThreadSafe(); - SDL_mutex* mPathMutex; - SDL_mutex* mSurfaceMutex; - bool mDeleting; }; #endif