From e513b3e1d7334258c82febb873ccab88c9f8bb83 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 31 Aug 2012 00:46:31 +0300 Subject: Improve OpenGL change color speed. --- src/normalopenglgraphics.cpp | 86 +++++++++++++++++++------------------------- src/normalopenglgraphics.h | 12 ++++++- src/safeopenglgraphics.cpp | 67 +++++++++++++++++----------------- src/safeopenglgraphics.h | 10 +++++- 4 files changed, 89 insertions(+), 86 deletions(-) (limited to 'src') diff --git a/src/normalopenglgraphics.cpp b/src/normalopenglgraphics.cpp index b8b3eb89d..257fb7171 100644 --- a/src/normalopenglgraphics.cpp +++ b/src/normalopenglgraphics.cpp @@ -55,6 +55,8 @@ NormalOpenGLGraphics::NormalOpenGLGraphics(): mIntVertArray(new GLint[vertexBufSize * 4 + 30]), mAlpha(false), mTexture(false), + mIsByteColor(false), + mFloatColor(1.0f), #ifdef DEBUG_BIND_TEXTURE mColorAlpha(false), mOldTextureId(0) @@ -220,8 +222,7 @@ bool NormalOpenGLGraphics::drawImage2(const Image *const image, srcY += image->mBounds.y; if (!useColor) - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); -// glColor4ub(255, 255, 255, image->mAlpha * 255); + setColorAlpha(image->mAlpha); #ifdef DEBUG_BIND_TEXTURE debugBindTexture(image); @@ -232,14 +233,6 @@ bool NormalOpenGLGraphics::drawImage2(const Image *const image, drawQuad(image, srcX, srcY, dstX, dstY, width, height); - if (!useColor) - { - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); - } - return true; } @@ -284,7 +277,7 @@ bool NormalOpenGLGraphics::drawRescaledImage(Image *const image, int srcX, srcY += image->mBounds.y; if (!useColor) - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); #ifdef DEBUG_BIND_TEXTURE debugBindTexture(image); @@ -299,7 +292,7 @@ bool NormalOpenGLGraphics::drawRescaledImage(Image *const image, int srcX, if (smooth) // A basic smooth effect... { - glColor4f(1.0f, 1.0f, 1.0f, 0.2f); + setColorAlpha(0.2f); drawRescaledQuad(image, srcX, srcY, dstX - 1, dstY - 1, width, height, desiredWidth + 1, desiredHeight + 1); drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY + 1, width, height, @@ -311,14 +304,6 @@ bool NormalOpenGLGraphics::drawRescaledImage(Image *const image, int srcX, desiredWidth, desiredHeight - 1); } - if (!useColor) - { - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); - } - return true; } @@ -341,7 +326,7 @@ void NormalOpenGLGraphics::drawImagePattern(const Image *const image, const float tw = static_cast(image->getTextureWidth()); const float th = static_cast(image->getTextureHeight()); - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); #ifdef DEBUG_BIND_TEXTURE debugBindTexture(image); @@ -451,11 +436,6 @@ void NormalOpenGLGraphics::drawImagePattern(const Image *const image, if (vp > 0) drawQuadArrayii(vp); } - - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); } void NormalOpenGLGraphics::drawRescaledImagePattern(const Image *const image, @@ -478,7 +458,7 @@ void NormalOpenGLGraphics::drawRescaledImagePattern(const Image *const image, if (iw == 0 || ih == 0) return; - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); #ifdef DEBUG_BIND_TEXTURE debugBindTexture(image); @@ -607,11 +587,6 @@ void NormalOpenGLGraphics::drawRescaledImagePattern(const Image *const image, if (vp > 0) drawQuadArrayii(vp); } - - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); } void NormalOpenGLGraphics::drawImagePattern2(GraphicsVertexes *const vert, @@ -622,7 +597,7 @@ void NormalOpenGLGraphics::drawImagePattern2(GraphicsVertexes *const vert, NormalOpenGLGraphicsVertexes *ogl = vert->getOGL(); - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); #ifdef DEBUG_BIND_TEXTURE debugBindTexture(image); #endif @@ -667,12 +642,6 @@ void NormalOpenGLGraphics::drawImagePattern2(GraphicsVertexes *const vert, drawQuadArrayii(*iv, *it, *ivp); } } - - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); - } void NormalOpenGLGraphics::calcImagePattern(GraphicsVertexes *const vert, @@ -942,7 +911,7 @@ void NormalOpenGLGraphics::drawTile(const ImageVertexes *const vert) NormalOpenGLGraphicsVertexes *ogl = vert->ogl; - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); #ifdef DEBUG_BIND_TEXTURE debugBindTexture(image); #endif @@ -953,11 +922,6 @@ void NormalOpenGLGraphics::drawTile(const ImageVertexes *const vert) drawQuadArrayfi(ogl->mIntVertArray, ogl->mFloatTexArray, ogl->ptr); else drawQuadArrayii(ogl->mIntVertArray, ogl->mIntTexArray, ogl->ptr); - - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); } void NormalOpenGLGraphics::updateScreen() @@ -1113,17 +1077,13 @@ void NormalOpenGLGraphics::popClipArea() void NormalOpenGLGraphics::setColor(const gcn::Color& color) { mColor = color; - glColor4ub(static_cast(color.r), - static_cast(color.g), - static_cast(color.b), - static_cast(color.a)); - mColorAlpha = (color.a != 255); } void NormalOpenGLGraphics::drawPoint(int x, int y) { setTexturingAndBlending(false); + restoreColor(); glBegin(GL_POINTS); glVertex2i(x, y); @@ -1134,6 +1094,7 @@ void NormalOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) { setTexturingAndBlending(false); glDisableClientState(GL_TEXTURE_COORD_ARRAY); + restoreColor(); mFloatTexArray[0] = static_cast(x1) + 0.5f; mFloatTexArray[1] = static_cast(y1) + 0.5f; @@ -1209,6 +1170,7 @@ void NormalOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect, setTexturingAndBlending(false); glDisableClientState(GL_TEXTURE_COORD_ARRAY); + restoreColor(); GLfloat vert[] = { @@ -1233,6 +1195,7 @@ bool NormalOpenGLGraphics::drawNet(const int x1, const int y1, setTexturingAndBlending(false); glDisableClientState(GL_TEXTURE_COORD_ARRAY); + restoreColor(); const float xf1 = static_cast(x1); const float xf2 = static_cast(x2); @@ -1356,6 +1319,29 @@ void NormalOpenGLGraphics::dumpSettings() } } +void NormalOpenGLGraphics::setColorAlpha(const float alpha) +{ + if (!mIsByteColor && mFloatColor == alpha) + return; + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + mIsByteColor = false; + mFloatColor = alpha; +} + +void NormalOpenGLGraphics::restoreColor() +{ + if (mIsByteColor && mByteColor == mColor) + return; + + glColor4ub(static_cast(mColor.r), + static_cast(mColor.g), + static_cast(mColor.b), + static_cast(mColor.a)); + mIsByteColor = true; + mByteColor = mColor; +} + #ifdef DEBUG_BIND_TEXTURE void NormalOpenGLGraphics::debugBindTexture(const Image *image) { diff --git a/src/normalopenglgraphics.h b/src/normalopenglgraphics.h index 45973941a..972656cc4 100644 --- a/src/normalopenglgraphics.h +++ b/src/normalopenglgraphics.h @@ -166,10 +166,20 @@ class NormalOpenGLGraphics : public Graphics void debugBindTexture(const Image *image); private: + void inline setColorAlpha(float alpha); + + void inline restoreColor(); + GLfloat *mFloatTexArray; GLint *mIntTexArray; GLint *mIntVertArray; - bool mAlpha, mTexture; + bool mAlpha; + bool mTexture; + + bool mIsByteColor; + gcn::Color mByteColor; + float mFloatColor; + bool mColorAlpha; FBOInfo mFbo; #ifdef DEBUG_BIND_TEXTURE diff --git a/src/safeopenglgraphics.cpp b/src/safeopenglgraphics.cpp index 5463c648c..93cee86db 100644 --- a/src/safeopenglgraphics.cpp +++ b/src/safeopenglgraphics.cpp @@ -47,6 +47,8 @@ GLuint SafeOpenGLGraphics::mLastImage = 0; SafeOpenGLGraphics::SafeOpenGLGraphics(): mAlpha(false), mTexture(false), + mIsByteColor(false), + mFloatColor(1.0f), mColorAlpha(false) { mOpenGL = 2; @@ -157,7 +159,7 @@ bool SafeOpenGLGraphics::drawImage2(const Image *const image, srcY += image->mBounds.y; if (!useColor) - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); @@ -168,14 +170,6 @@ bool SafeOpenGLGraphics::drawImage2(const Image *const image, drawQuad(image, srcX, srcY, dstX, dstY, width, height); glEnd(); - if (!useColor) - { - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); - } - return true; } @@ -220,7 +214,7 @@ bool SafeOpenGLGraphics::drawRescaledImage(Image *const image, int srcX, srcY += image->mBounds.y; if (!useColor) - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); @@ -233,7 +227,7 @@ bool SafeOpenGLGraphics::drawRescaledImage(Image *const image, int srcX, if (smooth) // A basic smooth effect... { - glColor4f(1.0f, 1.0f, 1.0f, 0.2f); + setColorAlpha(0.2f); drawRescaledQuad(image, srcX, srcY, dstX - 1, dstY - 1, width, height, desiredWidth + 1, desiredHeight + 1); drawRescaledQuad(image, srcX, srcY, dstX + 1, dstY + 1, width, height, @@ -247,14 +241,6 @@ bool SafeOpenGLGraphics::drawRescaledImage(Image *const image, int srcX, glEnd(); - if (!useColor) - { - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); - } - return true; } @@ -275,7 +261,7 @@ void SafeOpenGLGraphics::drawImagePattern(const Image *const image, if (iw == 0 || ih == 0) return; - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); @@ -298,11 +284,6 @@ void SafeOpenGLGraphics::drawImagePattern(const Image *const image, } glEnd(); - - glColor4ub(static_cast(mColor.r), - static_cast(mColor.g), - static_cast(mColor.b), - static_cast(mColor.a)); } void SafeOpenGLGraphics::drawRescaledImagePattern(const Image *const image, @@ -322,7 +303,7 @@ void SafeOpenGLGraphics::drawRescaledImagePattern(const Image *const image, if (iw == 0 || ih == 0) return; - glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); + setColorAlpha(image->mAlpha); bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); @@ -352,9 +333,6 @@ void SafeOpenGLGraphics::drawRescaledImagePattern(const Image *const image, } glEnd(); - - glColor4ub(static_cast(mColor.r), static_cast(mColor.g), - static_cast(mColor.b), static_cast(mColor.a)); } bool SafeOpenGLGraphics::calcImageRect(GraphicsVertexes *const vert, @@ -533,17 +511,13 @@ void SafeOpenGLGraphics::popClipArea() void SafeOpenGLGraphics::setColor(const gcn::Color& color) { mColor = color; - glColor4ub(static_cast(color.r), - static_cast(color.g), - static_cast(color.b), - static_cast(color.a)); - mColorAlpha = (color.a != 255); } void SafeOpenGLGraphics::drawPoint(int x, int y) { setTexturingAndBlending(false); + restoreColor(); glBegin(GL_POINTS); glVertex2i(x, y); @@ -553,6 +527,7 @@ void SafeOpenGLGraphics::drawPoint(int x, int y) void SafeOpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) { setTexturingAndBlending(false); + restoreColor(); glBegin(GL_LINES); glVertex2f(static_cast(x1) + 0.5f, static_cast(y1) + 0.5f); @@ -622,6 +597,7 @@ void SafeOpenGLGraphics::drawRectangle(const gcn::Rectangle& rect, bool filled) const float offset = filled ? 0 : 0.5f; setTexturingAndBlending(false); + restoreColor(); glBegin(filled ? GL_QUADS : GL_LINE_LOOP); glVertex2f(static_cast(rect.x) + offset, @@ -644,4 +620,27 @@ void SafeOpenGLGraphics::bindTexture(GLenum target, GLuint texture) } } +void SafeOpenGLGraphics::setColorAlpha(const float alpha) +{ + if (!mIsByteColor && mFloatColor == alpha) + return; + + glColor4f(1.0f, 1.0f, 1.0f, alpha); + mIsByteColor = false; + mFloatColor = alpha; +} + +void SafeOpenGLGraphics::restoreColor() +{ + if (mIsByteColor && mByteColor == mColor) + return; + + glColor4ub(static_cast(mColor.r), + static_cast(mColor.g), + static_cast(mColor.b), + static_cast(mColor.a)); + mIsByteColor = true; + mByteColor = mColor; +} + #endif // USE_OPENGL diff --git a/src/safeopenglgraphics.h b/src/safeopenglgraphics.h index a1d744282..a6f22182c 100644 --- a/src/safeopenglgraphics.h +++ b/src/safeopenglgraphics.h @@ -141,7 +141,15 @@ class SafeOpenGLGraphics : public Graphics void setTexturingAndBlending(bool enable); private: - bool mAlpha, mTexture; + void inline setColorAlpha(float alpha); + + void inline restoreColor(); + + bool mAlpha; + bool mTexture; + bool mIsByteColor; + gcn::Color mByteColor; + float mFloatColor; bool mColorAlpha; FBOInfo mFbo; }; -- cgit v1.2.3-70-g09d2