diff options
Diffstat (limited to 'src/resources/image.cpp')
-rw-r--r-- | src/resources/image.cpp | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 3a6f7236..975bd647 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -326,31 +326,52 @@ void Image::setAlpha(float alpha) } } -Image* Image::SDLgetScaledImage(int width, int height) +Image *Image::SDLgetScaledImage(int width, int height) { - // No scaling on incorrect new values. if (width == 0 || height == 0) - return NULL; + return 0; - // No scaling when there is ... no different given size ... + // Increase our reference count and return ourselves in case of same size if (width == getWidth() && height == getHeight()) - return NULL; + { + incRef(); + return this; + } - Image* scaledImage = NULL; - SDL_Surface* scaledSurface = NULL; + if (!mSDLSurface) + return 0; - if (mSDLSurface) + ResourceManager *resman = ResourceManager::getInstance(); + + // Generate a unique ID path for storing the scaled version in the + // resource manager. + std::string idPath = getIdPath(); + idPath += ":scaled:"; + idPath += toString(width); + idPath += "x"; + idPath += toString(height); + + // Try whether a scaled version is already available + Image *scaledImage = static_cast<Image*>(resman->get(idPath)); + + if (!scaledImage) { - scaledSurface = zoomSurface(mSDLSurface, - (double) width / getWidth(), - (double) height / getHeight(), - 1); + // No scaled version with this size exists already, so create one + SDL_Surface *scaledSurface = zoomSurface(mSDLSurface, + (double) width / getWidth(), + (double) height / getHeight(), + 1); - // The load function takes care of the SDL<->OpenGL implementation - // and about freeing the given SDL_surface*. if (scaledSurface) + { scaledImage = load(scaledSurface); + SDL_FreeSurface(scaledSurface); + + // Place the scaled image in the resource manager + resman->addResource(idPath, scaledImage); + } } + return scaledImage; } |