From 1f1e02d2b0143dd1f89720cc204bb0da36b302e9 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 12 Jun 2014 01:41:07 +0300 Subject: In modernopengl add draw rescaled image. --- src/render/modernopenglgraphics.cpp | 63 +++++++++++++++++++++++++++++++++---- src/render/modernopenglgraphics.h | 7 +++++ src/test/testlauncher.cpp | 2 ++ 3 files changed, 66 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/render/modernopenglgraphics.cpp b/src/render/modernopenglgraphics.cpp index 6955571eb..91b393ed9 100644 --- a/src/render/modernopenglgraphics.cpp +++ b/src/render/modernopenglgraphics.cpp @@ -271,13 +271,40 @@ void ModernOpenGLGraphics::drawQuad(const Image *const image, glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } -static inline void drawRescaledQuad(const Image *const image, - const int srcX, const int srcY, - const int dstX, const int dstY, - const int width, const int height, - const int desiredWidth, - const int desiredHeight) +void ModernOpenGLGraphics::drawRescaledQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight) { + const float tw = static_cast(image->mTexWidth); + const float th = static_cast(image->mTexHeight); + // Find OpenGL normalized texture coordinates. + const float texX1 = static_cast(srcX) / tw; + const float texY1 = static_cast(srcY) / th; + const float texX2 = static_cast(srcX + width) / tw; + const float texY2 = static_cast(srcY + height) / th; + + const float x1 = static_cast(dstX); + const float y1 = static_cast(dstY); + const float x2 = x1 + static_cast(desiredWidth); + const float y2 = y1 + static_cast(desiredHeight); + + GLfloat vertices[] = + { + x1, y1, texX1, texY1, + x2, y1, texX2, texY1, + x1, y2, texX1, texY2, + x2, y2, texX2, texY2 + }; + + mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices), + vertices, GL_DYNAMIC_DRAW); +#ifdef DEBUG_DRAW_CALLS + mDrawCalls ++; +#endif + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } bool ModernOpenGLGraphics::drawImage(const Image *const image, @@ -329,6 +356,30 @@ bool ModernOpenGLGraphics::drawRescaledImage(const Image *const image, const int desiredWidth, const int desiredHeight) { + if (!image) + return false; + + const SDL_Rect &imageRect = image->mBounds; + + // Just draw the image normally when no resizing is necessary, + if (imageRect.w == desiredWidth && imageRect.h == desiredHeight) + return drawImageInline(image, dstX, dstY); + + setColorAlpha(image->mAlpha); +#ifdef DEBUG_BIND_TEXTURE + debugBindTexture(image); +#endif + bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage); + setTexturingAndBlending(true); + + const ClipRect &clipArea = mClipStack.top(); + // Draw a textured quad. + drawRescaledQuad(image, + imageRect.x, imageRect.y, + dstX + clipArea.xOffset, dstY + clipArea.yOffset, + imageRect.w, imageRect.h, + desiredWidth, desiredHeight); + return true; } diff --git a/src/render/modernopenglgraphics.h b/src/render/modernopenglgraphics.h index 56e949aab..fec209a7e 100644 --- a/src/render/modernopenglgraphics.h +++ b/src/render/modernopenglgraphics.h @@ -78,6 +78,13 @@ class ModernOpenGLGraphics final : public Graphics const int dstX, const int dstY, const int width, const int height); + inline void drawRescaledQuad(const Image *const image, + const int srcX, const int srcY, + const int dstX, const int dstY, + const int width, const int height, + const int desiredWidth, + const int desiredHeight); + inline void drawTriangleArray(const int size); GLfloat *mFloatArray; diff --git a/src/test/testlauncher.cpp b/src/test/testlauncher.cpp index de51ce970..d5aaa4c0e 100644 --- a/src/test/testlauncher.cpp +++ b/src/test/testlauncher.cpp @@ -398,6 +398,8 @@ int TestLauncher::testDraw() mainGraphics->calcPattern(col, img[1], 500, 400, 150, 100); mainGraphics->drawTileCollection(col); + mainGraphics->drawRescaledImage(img[0], 250, 350, 35, 90); + mainGraphics->setColor(Color(0x00U, 0xFFU, 0x00U, 0x90U)); mainGraphics->drawNet(450, 10, 600, 300, 32, 20); -- cgit v1.2.3-60-g2f50