From 58ee835c3763e7bf088fa6c7e31dda1d687589cc Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Tue, 10 Mar 2009 11:50:27 -0600 Subject: Extended window layout to take relative positions, as well as offsets to that position. This makes it so that when resolutions are changed, the default locations stay relative to the window's position, and not the 800x600 screen resolution. Signed-off-by: Ira Rice --- src/graphics.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'src/graphics.cpp') diff --git a/src/graphics.cpp b/src/graphics.cpp index 3c507f4b..48fd1340 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -50,30 +50,25 @@ bool Graphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel) mFullscreen = fs; mHWAccel = hwaccel; - if (fs) { + if (fs) displayFlags |= SDL_FULLSCREEN; - } - if (hwaccel) { + if (hwaccel) displayFlags |= SDL_HWSURFACE | SDL_DOUBLEBUF; - } else { + else displayFlags |= SDL_SWSURFACE; - } mScreen = SDL_SetVideoMode(w, h, bpp, displayFlags); - if (!mScreen) { + if (!mScreen) return false; - } char videoDriverName[64]; - if (SDL_VideoDriverName(videoDriverName, 64)) { + if (SDL_VideoDriverName(videoDriverName, 64)) logger->log("Using video driver: %s", videoDriverName); - } - else { + else logger->log("Using video driver: unknown"); - } const SDL_VideoInfo *vi = SDL_GetVideoInfo(); @@ -104,9 +99,8 @@ bool Graphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel) bool Graphics::setFullscreen(bool fs) { - if (mFullscreen == fs) { + if (mFullscreen == fs) return true; - } return setVideoMode(mScreen->w, mScreen->h, mScreen->format->BitsPerPixel, fs, mHWAccel); @@ -128,7 +122,7 @@ bool Graphics::drawImage(Image *image, int x, int y) } bool Graphics::drawImage(Image *image, int srcX, int srcY, int dstX, int dstY, - int width, int height, bool) + int width, int height, bool) { // Check that preconditions for blitting are met. if (!mScreen || !image || !image->mImage) return false; @@ -150,7 +144,7 @@ bool Graphics::drawImage(Image *image, int srcX, int srcY, int dstX, int dstY, } void Graphics::drawImage(gcn::Image const *image, int srcX, int srcY, - int dstX, int dstY, int width, int height) + int dstX, int dstY, int width, int height) { ProxyImage const *srcImage = dynamic_cast< ProxyImage const * >(image); @@ -167,8 +161,10 @@ void Graphics::drawImagePattern(Image *image, int x, int y, int w, int h) int px = 0; // X position on pattern plane int py = 0; // Y position on pattern plane - while (py < h) { - while (px < w) { + while (py < h) + { + while (px < w) + { int dw = (px + iw >= w) ? w - px : iw; int dh = (py + ih >= h) ? h - py : ih; drawImage(image, 0, 0, x + px, y + py, dw, dh); -- cgit v1.2.3-60-g2f50 From a1eb62126bae54557e03682cac70c8331e359e01 Mon Sep 17 00:00:00 2001 From: Ira Rice Date: Mon, 16 Mar 2009 22:45:49 -0600 Subject: Applied a similar optimization as in commit d654758ef63f6515d678ceaf77d63a2693e08fb7, but for SDL instead. This currently doesn't buy too much, but it's a little better than it used to be. TODO: Find out why SDL is bottlenecked, and try to bring its performance up to OpenGL levels, if possible. Signed-off-by: Ira Rice --- src/graphics.cpp | 34 ++++++++++++++++++++------------ src/openglgraphics.cpp | 53 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 29 deletions(-) (limited to 'src/graphics.cpp') diff --git a/src/graphics.cpp b/src/graphics.cpp index 48fd1340..ca86f536 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -154,24 +154,34 @@ void Graphics::drawImage(gcn::Image const *image, int srcX, int srcY, void Graphics::drawImagePattern(Image *image, int x, int y, int w, int h) { - int iw = image->getWidth(); - int ih = image->getHeight(); - if (iw == 0 || ih == 0) return; + // Check that preconditions for blitting are met. + if (!mScreen || !image || !image->mImage) return; - int px = 0; // X position on pattern plane - int py = 0; // Y position on pattern plane + const int iw = image->getWidth(); + const int ih = image->getHeight(); + + if (iw == 0 || ih == 0) return; - while (py < h) + for (int py = 0; py < h; py += ih) // Y position on pattern plane { - while (px < w) + int dh = (py + ih >= h) ? h - py : ih; + int srcY = image->mBounds.y; + int dstY = y + py + mClipStack.top().yOffset; + + for (int px = 0; px < w; px += iw) // X position on pattern plane { int dw = (px + iw >= w) ? w - px : iw; - int dh = (py + ih >= h) ? h - py : ih; - drawImage(image, 0, 0, x + px, y + py, dw, dh); - px += iw; + int srcX = image->mBounds.x; + int dstX = x + px + mClipStack.top().xOffset; + + SDL_Rect dstRect; + SDL_Rect srcRect; + dstRect.x = dstX; dstRect.y = dstY; + srcRect.x = srcX; srcRect.y = srcY; + srcRect.w = dw; srcRect.h = dh; + + SDL_BlitSurface(image->mImage, &srcRect, mScreen, &dstRect); } - py += ih; - px = 0; } } diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index d436e7a5..78b502ea 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -110,6 +110,8 @@ bool OpenGLGraphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel) bool OpenGLGraphics::drawImage(Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height, bool useColor) { + if (!image) return false; + srcX += image->mBounds.x; srcY += image->mBounds.y; @@ -166,16 +168,14 @@ bool OpenGLGraphics::drawImage(Image *image, int srcX, int srcY, * so that glBegin...glEnd are outside the main loop. */ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h) { - if (Image::mTextureType == GL_TEXTURE_2D) - { - /* I'm not seeing this get called at all, so no point in optimising it. */ - Graphics::drawImagePattern(image, x, y, w, h); - return; - } + if (!image) return; const int srcX = image->mBounds.x; const int srcY = image->mBounds.y; + const float texX1 = srcX / (float)image->mTexWidth; + const float texY1 = srcY / (float)image->mTexHeight; + int iw = image->getWidth(); int ih = image->getHeight(); if (iw == 0 || ih == 0) @@ -184,6 +184,7 @@ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h) glColor4f(1.0f, 1.0f, 1.0f, image->mAlpha); glBindTexture(Image::mTextureType, image->mGLImage); + setTexturingAndBlending(true); // Draw a set of textured rectangles @@ -192,20 +193,38 @@ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h) for (int py = 0; py < h; py += ih) { int height = (py + ih >= h) ? h - py : ih; - int dstY = y+py; + int dstY = y + py; for (int px = 0; px < w; px += iw) { int width = (px + iw >= w) ? w - px : iw; - int dstX = x+px; - - 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); + int dstX = x + px; + + if (Image::mTextureType == GL_TEXTURE_2D) + { + // Find OpenGL normalized texture coordinates. + 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); + } } } -- cgit v1.2.3-60-g2f50