From 14c82e5b5d5547bcf9c3b06c68cf20a100600d0b Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Sat, 25 Aug 2007 16:56:52 +0000 Subject: Removed dependency on Guichan's OpenGL library. Taken over image loading and handling. Reduced memory usage when using OpenGL. (Up to 95% reduction for some textures.) --- src/openglgraphics.cpp | 115 ++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 59 deletions(-) (limited to 'src/openglgraphics.cpp') diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index f4cda0b9..b1e6ef81 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -27,6 +27,7 @@ #include "openglgraphics.h" +#include #include #ifdef __APPLE__ @@ -83,6 +84,23 @@ bool OpenGLGraphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel) logger->log("Using OpenGL %s double buffering.", (gotDoubleBuffer ? "with" : "without")); + char const *glExtensions = (char const *)glGetString(GL_EXTENSIONS); + int texSize; + bool rectTex = strstr(glExtensions, "GL_ARB_texture_rectangle"); + if (rectTex) + { + Image::mTextureType = GL_TEXTURE_RECTANGLE_ARB; + glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &texSize); + } + else + { + Image::mTextureType = GL_TEXTURE_2D; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize); + } + Image::mTextureSize = texSize; + logger->log("OpenGL texture size: %d pixels%s", Image::mTextureSize, + rectTex ? " (rectangle textures)" : ""); + return true; } @@ -92,17 +110,44 @@ bool OpenGLGraphics::drawImage(Image *image, int srcX, int srcY, srcX += image->mBounds.x; srcY += image->mBounds.y; - // Find OpenGL texture coordinates - float texX1 = srcX / (float)image->mTexWidth; - float texY1 = srcY / (float)image->mTexHeight; - float texX2 = (srcX + width) / (float)image->mTexWidth; - float texY2 = (srcY + height) / (float)image->mTexHeight; - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); - glBindTexture(GL_TEXTURE_2D, image->mGLImage); + glBindTexture(Image::mTextureType, image->mGLImage); + + setTexturingAndBlending(true); + + // Draw a textured quad. + glBegin(GL_QUADS); - drawTexedQuad(dstX, dstY, width, height, texX1, texY1, texX2, texY2); + if (Image::mTextureType == GL_TEXTURE_2D) + { + // Find OpenGL normalized texture coordinates. + float texX1 = srcX / (float)image->mTexWidth; + float texY1 = srcY / (float)image->mTexHeight; + float texX2 = (srcX + width) / (float)image->mTexWidth; + float texY2 = (srcY + height) / (float)image->mTexHeight; + + glTexCoord2f(texX1, texY1); + glVertex2i(dstX, dstY); + glTexCoord2f(texX2, texY1); + glVertex2i(dstX + width, dstY); + glTexCoord2f(texX2, texY2); + glVertex2i(dstX + width, dstY + height); + glTexCoord2f(texX1, texY2); + glVertex2i(dstX, dstY + height); + } + else + { + glTexCoord2i(srcX, srcY); + glVertex2i(dstX, dstY); + glTexCoord2i(srcX + width, srcY); + glVertex2i(dstX + width, dstY); + glTexCoord2i(srcX + width, srcY + height); + glVertex2i(dstX + width, dstY + height); + glTexCoord2i(srcX, srcY + height); + glVertex2i(dstX, dstY + height); + } + glEnd(); glColor4ub(mColor.r, mColor.g, mColor.b, mColor.a); return true; @@ -229,33 +274,6 @@ void OpenGLGraphics::setColor(const gcn::Color& color) mColorAlpha = (color.a != 255); } -void OpenGLGraphics::drawImage(const gcn::Image* image, - int srcX, int srcY, - int dstX, int dstY, - int width, int height) -{ - const gcn::OpenGLImage* srcImage = - dynamic_cast(image); - - if (srcImage == NULL) - { - throw GCN_EXCEPTION("Trying to draw an image of unknown format, " - "must be an SDLImage."); - } - - // Find OpenGL texture coordinates - float texX1 = srcX / (float)srcImage->getTextureWidth(); - float texY1 = srcY / (float)srcImage->getTextureHeight(); - float texX2 = (srcX + width) / (float)srcImage->getTextureWidth(); - float texY2 = (srcY + height) / (float)srcImage->getTextureHeight(); - - // Please dont look too closely at the next line, it is not pretty. - // It uses the image data as a pointer to a GLuint - glBindTexture(GL_TEXTURE_2D, srcImage->getTextureHandle()); - - drawTexedQuad(dstX, dstY, width, height, texX1, texY1, texX2, texY2); -} - void OpenGLGraphics::drawPoint(int x, int y) { setTexturingAndBlending(false); @@ -297,9 +315,9 @@ void OpenGLGraphics::setTexturingAndBlending(bool enable) { if (enable) { if (!mTexture) { - glEnable(GL_TEXTURE_2D); + glEnable(Image::mTextureType); mTexture = true; - }; + } if (!mAlpha) { @@ -316,7 +334,7 @@ void OpenGLGraphics::setTexturingAndBlending(bool enable) } if (mTexture) { - glDisable(GL_TEXTURE_2D); + glDisable(Image::mTextureType); mTexture = false; } } @@ -336,25 +354,4 @@ void OpenGLGraphics::drawRectangle(const gcn::Rectangle& rect, bool filled) glEnd(); } -void OpenGLGraphics::drawTexedQuad(int x, int y, int w, int h, - float texX1, float texY1, float texX2, float texY2) -{ - setTexturingAndBlending(true); - - // Draw a textured quad - glBegin(GL_QUADS); - glTexCoord2f(texX1, texY1); - glVertex2i(x, y); - - glTexCoord2f(texX2, texY1); - glVertex2i(x + w, y); - - glTexCoord2f(texX2, texY2); - glVertex2i(x + w, y + h); - - glTexCoord2f(texX1, texY2); - glVertex2i(x, y + h); - glEnd(); -} - #endif // USE_OPENGL -- cgit v1.2.3-60-g2f50