From 8195e73cb801d56240beb966554a864729d9f20c Mon Sep 17 00:00:00 2001 From: Bertram Date: Fri, 24 Jul 2009 22:59:13 +0200 Subject: Made the wallpaper be rescaled when necessary under SDL and OpenGL. The SDL methods to rescale the wallpaper has been optimized to permit rescaling at load time while OpenGL draws directly rescaled. Does someone know how to smooth the rescaled image under OpenGL? --- src/openglgraphics.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/openglgraphics.cpp') diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index b9d837ad..5c9e2049 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -140,6 +140,42 @@ static inline void drawQuad(Image *image, } } +static inline void drawRescaledQuad(Image *image, + int srcX, int srcY, int dstX, int dstY, + int width, int height, + int desiredWidth, int desiredHeight) +{ + if (image->getTextureType() == GL_TEXTURE_2D) + { + // Find OpenGL normalized texture coordinates. + float texX1 = srcX / (float) image->getTextureWidth(); + float texY1 = srcY / (float) image->getTextureHeight(); + float texX2 = (srcX + width) / (float) image->getTextureWidth(); + float texY2 = (srcY + height) / (float) image->getTextureHeight(); + + glTexCoord2f(texX1, texY1); + glVertex2i(dstX, dstY); + glTexCoord2f(texX2, texY1); + glVertex2i(dstX + desiredWidth, dstY); + glTexCoord2f(texX2, texY2); + glVertex2i(dstX + desiredWidth, dstY + desiredHeight); + glTexCoord2f(texX1, texY2); + glVertex2i(dstX, dstY + desiredHeight); + } + else + { + glTexCoord2i(srcX, srcY); + glVertex2i(dstX, dstY); + glTexCoord2i(srcX + width, srcY); + glVertex2i(dstX + desiredWidth, dstY); + glTexCoord2i(srcX + width, srcY + height); + glVertex2i(dstX + desiredWidth, dstY + desiredHeight); + glTexCoord2i(srcX, srcY + height); + glVertex2i(dstX, dstY + desiredHeight); + } +} + + bool OpenGLGraphics::drawImage(Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height, bool useColor) @@ -168,6 +204,37 @@ bool OpenGLGraphics::drawImage(Image *image, int srcX, int srcY, return true; } +bool OpenGLGraphics::drawRescaledImage(Image *image, int srcX, int srcY, + int dstX, int dstY, + int width, int height, + int desiredWidth, int desiredHeight, + bool useColor) +{ + if (!image) + return false; + + srcX += image->mBounds.x; + srcY += image->mBounds.y; + + if (!useColor) + glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + + glBindTexture(Image::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + glBegin(GL_QUADS); + drawRescaledQuad(image, srcX, srcY, dstX, dstY, width, height, + desiredWidth, desiredHeight); + glEnd(); + + if (!useColor) + glColor4ub(mColor.r, mColor.g, mColor.b, mColor.a); + + return true; +} + /* Optimising the functions that Graphics::drawImagePattern would call, * so that glBegin...glEnd are outside the main loop. */ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h) -- cgit v1.2.3-70-g09d2