summaryrefslogtreecommitdiff
path: root/src/openglgraphics.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openglgraphics.cpp')
-rw-r--r--src/openglgraphics.cpp113
1 files changed, 65 insertions, 48 deletions
diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp
index f918a5c0d..2ec5db490 100644
--- a/src/openglgraphics.cpp
+++ b/src/openglgraphics.cpp
@@ -486,24 +486,26 @@ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h)
static_cast<GLubyte>(mColor.a));
}
-void OpenGLGraphics::drawRescaledImagePattern(Image *image, int x, int y,
- int w, int h, int scaledWidth,
+void OpenGLGraphics::drawRescaledImagePattern(Image *image,
+ int x, int y,
+ int w, int h,
+ int scaledWidth,
int scaledHeight)
{
if (!image)
return;
+ if (scaledWidth == 0 || scaledHeight == 0)
+ return;
+
const int srcX = image->mBounds.x;
const int srcY = image->mBounds.y;
- const int iw = scaledWidth;
- const int ih = scaledHeight;
+ const int iw = image->getWidth();
+ const int ih = image->getHeight();
if (iw == 0 || ih == 0)
return;
- const float tw = static_cast<float>(image->getTextureWidth());
- const float th = static_cast<float>(image->getTextureHeight());
-
glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha);
bindTexture(Image::mTextureType, image->mGLImage);
@@ -513,23 +515,32 @@ void OpenGLGraphics::drawRescaledImagePattern(Image *image, int x, int y,
unsigned int vp = 0;
const unsigned int vLimit = vertexBufSize * 4;
- float texX1 = static_cast<float>(srcX) / tw;
- float texY1 = static_cast<float>(srcY) / th;
-
// Draw a set of textured rectangles
if (image->mTextureType == GL_TEXTURE_2D)
{
- for (int py = 0; py < h; py += ih)
+ const float tw = static_cast<float>(image->getTextureWidth());
+ const float th = static_cast<float>(image->getTextureHeight());
+
+ const float texX1 = static_cast<float>(srcX) / tw;
+ const float texY1 = static_cast<float>(srcY) / th;
+
+ const float tFractionW = iw / tw;
+ const float tFractionH = ih / th;
+
+ for (int py = 0; py < h; py += scaledHeight)
{
- const int height = (py + ih >= h) ? h - py : ih;
+ const int height = (py + scaledHeight >= h)
+ ? h - py : scaledHeight;
const int dstY = y + py;
- for (int px = 0; px < w; px += iw)
+ for (int px = 0; px < w; px += scaledWidth)
{
- int width = (px + iw >= w) ? w - px : iw;
+ int width = (px + scaledWidth >= w) ? w - px : scaledWidth;
int dstX = x + px;
+ const float visibleFractionW = (float) width / scaledWidth;
+ const float visibleFractionH = (float) height / scaledHeight;
- float texX2 = static_cast<float>(srcX + width) / tw;
- float texY2 = static_cast<float>(srcY + height) / th;
+ const float texX2 = texX1 + tFractionW * visibleFractionW;
+ const float texY2 = texY1 + tFractionH * visibleFractionH;
mFloatTexArray[vp + 0] = texX1;
mFloatTexArray[vp + 1] = texY1;
@@ -546,14 +557,14 @@ void OpenGLGraphics::drawRescaledImagePattern(Image *image, int x, int y,
mIntVertArray[vp + 0] = dstX;
mIntVertArray[vp + 1] = dstY;
- mIntVertArray[vp + 2] = dstX + scaledWidth;
+ mIntVertArray[vp + 2] = dstX + width;
mIntVertArray[vp + 3] = dstY;
- mIntVertArray[vp + 4] = dstX + scaledWidth;
- mIntVertArray[vp + 5] = dstY + scaledHeight;
+ mIntVertArray[vp + 4] = dstX + width;
+ mIntVertArray[vp + 5] = dstY + height;
mIntVertArray[vp + 6] = dstX;
- mIntVertArray[vp + 7] = dstY + scaledHeight;
+ mIntVertArray[vp + 7] = dstY + height;
vp += 8;
if (vp >= vLimit)
@@ -568,38 +579,42 @@ void OpenGLGraphics::drawRescaledImagePattern(Image *image, int x, int y,
}
else
{
- for (int py = 0; py < h; py += ih)
+ const float scaleFactorW = (float) scaledWidth / iw;
+ const float scaleFactorH = (float) scaledHeight / ih;
+
+ for (int py = 0; py < h; py += scaledHeight)
{
- const int height = (py + ih >= h) ? h - py : ih;
+ const int height = (py + scaledHeight >= h)
+ ? h - py : scaledHeight;
const int dstY = y + py;
- for (int px = 0; px < w; px += iw)
+ for (int px = 0; px < w; px += scaledWidth)
{
- int width = (px + iw >= w) ? w - px : iw;
+ int width = (px + scaledWidth >= w) ? w - px : scaledWidth;
int dstX = x + px;
mIntTexArray[vp + 0] = srcX;
mIntTexArray[vp + 1] = srcY;
- mIntTexArray[vp + 2] = srcX + width;
+ mIntTexArray[vp + 2] = srcX + width / scaleFactorW;
mIntTexArray[vp + 3] = srcY;
- mIntTexArray[vp + 4] = srcX + width;
- mIntTexArray[vp + 5] = srcY + height;
+ mIntTexArray[vp + 4] = srcX + width / scaleFactorW;
+ mIntTexArray[vp + 5] = srcY + height / scaleFactorH;
mIntTexArray[vp + 6] = srcX;
- mIntTexArray[vp + 7] = srcY + height;
+ mIntTexArray[vp + 7] = srcY + height / scaleFactorH;
mIntVertArray[vp + 0] = dstX;
mIntVertArray[vp + 1] = dstY;
- mIntVertArray[vp + 2] = dstX + scaledWidth;
+ mIntVertArray[vp + 2] = dstX + width;
mIntVertArray[vp + 3] = dstY;
- mIntVertArray[vp + 4] = dstX + scaledWidth;
- mIntVertArray[vp + 5] = dstY + scaledHeight;
+ mIntVertArray[vp + 4] = dstX + width;
+ mIntVertArray[vp + 5] = dstY + height;
mIntVertArray[vp + 6] = dstX;
- mIntVertArray[vp + 7] = dstY + scaledHeight;
+ mIntVertArray[vp + 7] = dstY + height;
vp += 8;
if (vp >= vLimit)
@@ -631,15 +646,15 @@ void OpenGLGraphics::drawImagePattern2(GraphicsVertexes *vert, Image *image)
setTexturingAndBlending(true);
std::vector<GLint*> *intVertPool = ogl->getIntVertPool();
- std::vector<GLint*>::iterator iv;
+ std::vector<GLint*>::const_iterator iv;
std::vector<int> *vp = ogl->getVp();
- std::vector<int>::iterator ivp;
+ std::vector<int>::const_iterator ivp;
// Draw a set of textured rectangles
if (image->mTextureType == GL_TEXTURE_2D)
{
std::vector<GLfloat*> *floatTexPool = ogl->getFloatTexPool();
- std::vector<GLfloat*>::iterator ft;
+ std::vector<GLfloat*>::const_iterator ft;
for (iv = intVertPool->begin(), ft = floatTexPool->begin(),
ivp = vp->begin();
@@ -653,7 +668,7 @@ void OpenGLGraphics::drawImagePattern2(GraphicsVertexes *vert, Image *image)
else
{
std::vector<GLint*> *intTexPool = ogl->getIntTexPool();
- std::vector<GLint*>::iterator it;
+ std::vector<GLint*>::const_iterator it;
for (iv = intVertPool->begin(), it = intTexPool->begin(),
ivp = vp->begin();
@@ -1010,6 +1025,9 @@ SDL_Surface* OpenGLGraphics::getScreenshot()
w, h, 24,
0xff0000, 0x00ff00, 0x0000ff, 0x000000);
+ if (!screenshot)
+ return 0;
+
if (SDL_MUSTLOCK(screenshot))
SDL_LockSurface(screenshot);
@@ -1048,22 +1066,22 @@ bool OpenGLGraphics::pushClipArea(gcn::Rectangle area)
if (!mClipStack.empty())
{
- transX = -mClipStack.top().xOffset;
- transY = -mClipStack.top().yOffset;
+ const gcn::ClipRectangle &clipArea = mClipStack.top();
+ transX = -clipArea.xOffset;
+ transY = -clipArea.yOffset;
}
bool result = gcn::Graphics::pushClipArea(area);
- transX += mClipStack.top().xOffset;
- transY += mClipStack.top().yOffset;
+ const gcn::ClipRectangle &clipArea = mClipStack.top();
+ transX += clipArea.xOffset;
+ transY += clipArea.yOffset;
glPushMatrix();
glTranslatef(static_cast<GLfloat>(transX),
static_cast<GLfloat>(transY), 0);
- glScissor(mClipStack.top().x,
- mTarget->h - mClipStack.top().y - mClipStack.top().height,
- mClipStack.top().width,
- mClipStack.top().height);
+ glScissor(clipArea.x, mTarget->h - clipArea.y - clipArea.height,
+ clipArea.width, clipArea.height);
return result;
}
@@ -1076,10 +1094,9 @@ void OpenGLGraphics::popClipArea()
return;
glPopMatrix();
- glScissor(mClipStack.top().x,
- mTarget->h - mClipStack.top().y - mClipStack.top().height,
- mClipStack.top().width,
- mClipStack.top().height);
+ const gcn::ClipRectangle &clipArea = mClipStack.top();
+ glScissor(clipArea.x, mTarget->h - clipArea.y - clipArea.height,
+ clipArea.width, clipArea.height);
}
void OpenGLGraphics::setColor(const gcn::Color& color)