From 62e2ccbb158f146e2c3ceab14ff5581d3f3975ef Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 1 Jul 2014 21:57:46 +0300 Subject: Add to image helper function to copy surface into part of image. --- src/resources/openglimagehelper.cpp | 54 ++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 10 deletions(-) (limited to 'src/resources/openglimagehelper.cpp') diff --git a/src/resources/openglimagehelper.cpp b/src/resources/openglimagehelper.cpp index d083b22b5..5f1ec643c 100644 --- a/src/resources/openglimagehelper.cpp +++ b/src/resources/openglimagehelper.cpp @@ -133,19 +133,12 @@ int OpenGLImageHelper::powerOfTwo(const int input) return value >= mTextureSize ? mTextureSize : value; } -Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, - int width, int height) +SDL_Surface *OpenGLImageHelper::convertSurface(SDL_Surface *tmpImage, + int width, int height) { if (!tmpImage) return nullptr; - // Flush current error flag. - graphicsManager.getLastError(); - - if (!width) - width = tmpImage->w; - if (!height) - height = tmpImage->h; int realWidth = powerOfTwo(width); int realHeight = powerOfTwo(height); @@ -197,6 +190,28 @@ Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, } SDL_BlitSurface(oldImage, nullptr, tmpImage, nullptr); } + return tmpImage; +} + +Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, + int width, int height) +{ + if (!tmpImage) + return nullptr; + + // Flush current error flag. + graphicsManager.getLastError(); + + if (!width) + width = tmpImage->w; + if (!height) + height = tmpImage->h; + + SDL_Surface *oldImage = tmpImage; + tmpImage = convertSurface(tmpImage, width, height); + + const int realWidth = tmpImage->w; + const int realHeight = tmpImage->h; const GLuint texture = getNewTexture(); switch (mUseOpenGL) @@ -281,7 +296,7 @@ Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, if (SDL_MUSTLOCK(tmpImage)) SDL_UnlockSurface(tmpImage); - if (oldImage) + if (oldImage != tmpImage) MSDL_FreeSurface(tmpImage); GLenum error = graphicsManager.getLastError(); @@ -358,4 +373,23 @@ void OpenGLImageHelper::invalidate(const GLuint textureId) } } +void OpenGLImageHelper::copySurfaceToImage(Image *const image, + const int x, const int y, + SDL_Surface *surface) const +{ + if (!surface) + return; + + SDL_Surface *const oldSurface = surface; + surface = convertSurface(surface, surface->w, surface->h); + + glTexSubImage2D(mTextureType, 0, + x, y, + surface->w, surface->h, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + + if (surface != oldSurface) + MSDL_FreeSurface(surface); +} + #endif -- cgit v1.2.3-60-g2f50