From 250e3d94f047de6b8fa2a49909ba60e8dccdfae4 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 26 Aug 2013 18:24:02 +0300 Subject: improve draw speed in sofrware mode in SDL2. --- src/sdl2graphics.cpp | 219 ++++++++++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 107 deletions(-) diff --git a/src/sdl2graphics.cpp b/src/sdl2graphics.cpp index 533b44179..e24978e37 100644 --- a/src/sdl2graphics.cpp +++ b/src/sdl2graphics.cpp @@ -99,22 +99,22 @@ bool SDLGraphics::drawRescaledImage(const Image *const image, int srcX, int srcY if (!image->mTexture) return false; - dstX += mClipStack.top().xOffset; - dstY += mClipStack.top().yOffset; - - srcX += image->mBounds.x; - srcY += image->mBounds.y; - - SDL_Rect dstRect; - SDL_Rect srcRect; - dstRect.x = static_cast(dstX); - dstRect.y = static_cast(dstY); - srcRect.x = static_cast(srcX); - srcRect.y = static_cast(srcY); - srcRect.w = static_cast(width); - srcRect.h = static_cast(height); - dstRect.w = static_cast(desiredWidth); - dstRect.h = static_cast(desiredHeight); + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect &bounds = image->mBounds; + const SDL_Rect srcRect + { + static_cast(srcX + bounds.x), + static_cast(srcY + bounds.y), + static_cast(width), + static_cast(height) + }; + const SDL_Rect dstRect + { + static_cast(dstX + top.xOffset), + static_cast(dstY + top.yOffset), + static_cast(desiredWidth), + static_cast(desiredHeight) + }; return (MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect) < 0); @@ -129,22 +129,22 @@ bool SDLGraphics::drawImage2(const Image *const image, int srcX, int srcY, if (!mWindow || !image || !image->mTexture) return false; - dstX += mClipStack.top().xOffset; - dstY += mClipStack.top().yOffset; - - srcX += image->mBounds.x; - srcY += image->mBounds.y; - - SDL_Rect dstRect; - SDL_Rect srcRect; - dstRect.x = static_cast(dstX); - dstRect.y = static_cast(dstY); - srcRect.x = static_cast(srcX); - srcRect.y = static_cast(srcY); - srcRect.w = static_cast(width); - srcRect.h = static_cast(height); - dstRect.w = static_cast(width); - dstRect.h = static_cast(height); + const gcn::ClipRectangle &top = mClipStack.top(); + const SDL_Rect &bounds = image->mBounds; + const SDL_Rect srcRect + { + static_cast(srcX + bounds.x), + static_cast(srcY + bounds.y), + static_cast(width), + static_cast(height) + }; + const SDL_Rect dstRect + { + static_cast(dstX + top.xOffset), + static_cast(dstY + top.yOffset), + static_cast(width), + static_cast(height) + }; return !MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); } @@ -160,34 +160,33 @@ void SDLGraphics::drawImagePattern(const Image *const image, if (!image->mTexture) return; - const int iw = image->mBounds.w; - const int ih = image->mBounds.h; - + const SDL_Rect &bounds = image->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; if (iw == 0 || ih == 0) return; - for (int py = 0; py < h; py += ih) // Y position on pattern plane + const gcn::ClipRectangle &top = mClipStack.top(); + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + + SDL_Rect dstRect; + SDL_Rect srcRect; + srcRect.x = static_cast(bounds.x); + srcRect.y = static_cast(bounds.y); + for (int py = 0; py < h; py += ih) { const int dh = (py + ih >= h) ? h - py : ih; - const int srcY = image->mBounds.y; - const int dstY = y + py + mClipStack.top().yOffset; + dstRect.y = static_cast(py + yOffset); + srcRect.h = static_cast(dh); + dstRect.h = static_cast(dh); - for (int px = 0; px < w; px += iw) // X position on pattern plane + for (int px = 0; px < w; px += iw) { const int dw = (px + iw >= w) ? w - px : iw; - const int srcX = image->mBounds.x; - const int dstX = x + px + mClipStack.top().xOffset; - - SDL_Rect dstRect; - SDL_Rect srcRect; - dstRect.x = static_cast(dstX); - dstRect.y = static_cast(dstY); - srcRect.x = static_cast(srcX); - srcRect.y = static_cast(srcY); + dstRect.x = static_cast(px + xOffset); srcRect.w = static_cast(dw); - srcRect.h = static_cast(dh); dstRect.w = static_cast(dw); - dstRect.h = static_cast(dh); MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); } @@ -214,34 +213,33 @@ void SDLGraphics::drawRescaledImagePattern(const Image *const image, if (!tmpImage) return; - const int iw = tmpImage->mBounds.w; - const int ih = tmpImage->mBounds.h; - + const SDL_Rect &bounds = tmpImage->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; if (iw == 0 || ih == 0) return; - for (int py = 0; py < h; py += ih) // Y position on pattern plane + const gcn::ClipRectangle &top = mClipStack.top(); + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + + SDL_Rect dstRect; + SDL_Rect srcRect; + srcRect.x = static_cast(bounds.x); + srcRect.y = static_cast(bounds.y); + for (int py = 0; py < h; py += ih) { const int dh = (py + ih >= h) ? h - py : ih; - const int srcY = tmpImage->mBounds.y; - const int dstY = y + py + mClipStack.top().yOffset; + dstRect.y = static_cast(py + yOffset); + srcRect.h = static_cast(dh); + dstRect.h = static_cast(dh); - for (int px = 0; px < w; px += iw) // X position on pattern plane + for (int px = 0; px < w; px += iw) { const int dw = (px + iw >= w) ? w - px : iw; - const int srcX = tmpImage->mBounds.x; - const int dstX = x + px + mClipStack.top().xOffset; - - SDL_Rect dstRect; - SDL_Rect srcRect; - dstRect.x = static_cast(dstX); - dstRect.y = static_cast(dstY); - srcRect.x = static_cast(srcX); - srcRect.y = static_cast(srcY); + dstRect.x = static_cast(px + xOffset); srcRect.w = static_cast(dw); - srcRect.h = static_cast(dh); dstRect.w = static_cast(dw); - dstRect.h = static_cast(dh); MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); } @@ -259,33 +257,36 @@ void SDLGraphics::calcImagePattern(ImageVertexes* const vert, if (!vert || !mWindow || !image || !image->mTexture) return; - const int iw = image->mBounds.w; - const int ih = image->mBounds.h; - + const SDL_Rect &bounds = image->mBounds; + const int iw = bounds.w; + const int ih = bounds.h; if (iw == 0 || ih == 0) return; - for (int py = 0; py < h; py += ih) // Y position on pattern plane + const gcn::ClipRectangle &top = mClipStack.top(); + const int xOffset = top.xOffset + x; + const int yOffset = top.yOffset + y; + const int srcX = bounds.x; + const int srcY = bounds.y; + for (int py = 0; py < h; py += ih) { const int dh = (py + ih >= h) ? h - py : ih; - const int srcY = image->mBounds.y; - const int dstY = y + py + mClipStack.top().yOffset; + const int dstY = py + yOffset; - for (int px = 0; px < w; px += iw) // X position on pattern plane + for (int px = 0; px < w; px += iw) { const int dw = (px + iw >= w) ? w - px : iw; - const int srcX = image->mBounds.x; - const int dstX = x + px + mClipStack.top().xOffset; + const int dstX = px + xOffset; DoubleRect *const r = new DoubleRect(); SDL_Rect &dstRect = r->dst; SDL_Rect &srcRect = r->src; - dstRect.x = static_cast(dstX); - dstRect.y = static_cast(dstY); srcRect.x = static_cast(srcX); srcRect.y = static_cast(srcY); srcRect.w = static_cast(dw); srcRect.h = static_cast(dh); + dstRect.x = static_cast(dstX); + dstRect.y = static_cast(dstY); dstRect.w = static_cast(dw); dstRect.h = static_cast(dh); @@ -331,21 +332,24 @@ void SDLGraphics::calcTileSDL(ImageVertexes *const vert, int x, int y) const return; const Image *const image = vert->image; + const SDL_Rect &bounds = image->mBounds; const gcn::ClipRectangle &top = mClipStack.top(); x += top.xOffset; y += top.yOffset; DoubleRect *rect = new DoubleRect(); - - rect->dst.x = static_cast(x); - rect->dst.y = static_cast(y); - rect->src.x = static_cast(image->mBounds.x); - rect->src.y = static_cast(image->mBounds.y); - rect->src.w = static_cast(image->mBounds.w); - rect->src.h = static_cast(image->mBounds.h); - rect->dst.w = static_cast(image->mBounds.w); - rect->dst.h = static_cast(image->mBounds.h); + SDL_Rect &dstRect = rect->dst; + SDL_Rect &srcRect = rect->src; + + srcRect.x = static_cast(bounds.x); + srcRect.y = static_cast(bounds.y); + srcRect.w = static_cast(bounds.w); + srcRect.h = static_cast(bounds.h); + dstRect.x = static_cast(x); + dstRect.y = static_cast(y); + dstRect.w = static_cast(bounds.w); + dstRect.h = static_cast(bounds.h); vert->sdl.push_back(rect); } @@ -406,10 +410,7 @@ void SDLGraphics::drawTile(const ImageVertexes *const vert) void SDLGraphics::updateScreen() { BLOCK_START("Graphics::updateScreen") - SDL_RenderPresent(mRenderer); - -// SDL_SetRenderDrawColor(mRenderer, 255, 0, 0, 255); // SDL_RenderClear(mRenderer); BLOCK_END("Graphics::updateScreen") } @@ -470,10 +471,11 @@ bool SDLGraphics::calcWindow(ImageCollection *const vertCol, vert = vertCol->currentVert; } + const Image *const *const grid = &imgRect.grid[0]; return calcImageRect(vert, x, y, w, h, - imgRect.grid[0], imgRect.grid[2], imgRect.grid[6], imgRect.grid[8], - imgRect.grid[1], imgRect.grid[5], imgRect.grid[7], imgRect.grid[3], - imgRect.grid[4]); + grid[0], grid[2], grid[6], grid[8], + grid[1], grid[5], grid[7], grid[3], + grid[4]); } void SDLGraphics::fillRectangle(const gcn::Rectangle &rectangle) @@ -481,8 +483,8 @@ void SDLGraphics::fillRectangle(const gcn::Rectangle &rectangle) const gcn::ClipRectangle &top = mClipStack.top(); const SDL_Rect rect { - static_cast(rectangle.x) + top.xOffset, - static_cast(rectangle.y) + top.yOffset, + static_cast(rectangle.x + top.xOffset), + static_cast(rectangle.y + top.yOffset), static_cast(rectangle.width), static_cast(rectangle.height) }; @@ -503,16 +505,17 @@ void SDLGraphics::_endDraw() bool SDLGraphics::pushClipArea(gcn::Rectangle area) { - SDL_Rect rect; const bool result = gcn::Graphics::pushClipArea(area); const gcn::ClipRectangle &carea = mClipStack.top(); - rect.x = static_cast(carea.x); - rect.y = static_cast(carea.y); - rect.w = static_cast(carea.width); - rect.h = static_cast(carea.height); + const SDL_Rect rect + { + static_cast(carea.x), + static_cast(carea.y), + static_cast(carea.width), + static_cast(carea.height) + }; SDL_RenderSetClipRect(mRenderer, &rect); - return result; } @@ -524,11 +527,13 @@ void SDLGraphics::popClipArea() return; const gcn::ClipRectangle &carea = mClipStack.top(); - SDL_Rect rect; - rect.x = static_cast(carea.x); - rect.y = static_cast(carea.y); - rect.w = static_cast(carea.width); - rect.h = static_cast(carea.height); + const SDL_Rect rect + { + static_cast(carea.x), + static_cast(carea.y), + static_cast(carea.width), + static_cast(carea.height) + }; SDL_RenderSetClipRect(mRenderer, &rect); } -- cgit v1.2.3-70-g09d2