summaryrefslogtreecommitdiff
path: root/src/sdlgraphics.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sdlgraphics.cpp')
-rw-r--r--src/sdlgraphics.cpp123
1 files changed, 55 insertions, 68 deletions
diff --git a/src/sdlgraphics.cpp b/src/sdlgraphics.cpp
index 0fec9ab7..b0980675 100644
--- a/src/sdlgraphics.cpp
+++ b/src/sdlgraphics.cpp
@@ -30,39 +30,6 @@
#include <cmath>
-class SetColorAlphaMod
-{
-public:
- SetColorAlphaMod(SDL_Texture *texture, gcn::Color color, bool enabled)
- : mTexture(texture)
- , mEnabled(texture != nullptr && enabled)
- {
- if (mEnabled)
- {
- SDL_GetTextureColorMod(texture, &mOriginal.r, &mOriginal.g, &mOriginal.b);
- SDL_GetTextureAlphaMod(texture, &mOriginal.a);
-
- SDL_SetTextureColorMod(texture, color.r, color.g, color.b);
- SDL_SetTextureAlphaMod(texture, color.a * mOriginal.a / 255);
- }
- }
-
- ~SetColorAlphaMod()
- {
- if (mEnabled)
- {
- SDL_SetTextureAlphaMod(mTexture, mOriginal.a);
- SDL_SetTextureColorMod(mTexture, mOriginal.r, mOriginal.g, mOriginal.b);
- }
- }
-
-private:
- SDL_Texture *mTexture = nullptr;
- SDL_Color mOriginal;
- const bool mEnabled;
-};
-
-
std::unique_ptr<Graphics> SDLGraphics::create(SDL_Window *window, const VideoSettings &settings)
{
int rendererFlags = 0;
@@ -72,16 +39,17 @@ std::unique_ptr<Graphics> SDLGraphics::create(SDL_Window *window, const VideoSet
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, rendererFlags);
if (!renderer)
{
- logger->error(strprintf("Failed to create renderer: %s",
+ Log::critical(strprintf("Failed to create renderer: %s",
SDL_GetError()));
return {};
}
- return std::make_unique<SDLGraphics>(renderer);
+ return std::make_unique<SDLGraphics>(window, renderer);
}
-SDLGraphics::SDLGraphics(SDL_Renderer *renderer)
- : mRenderer(renderer)
+SDLGraphics::SDLGraphics(SDL_Window *window, SDL_Renderer *renderer)
+ : mWindow(window)
+ , mRenderer(renderer)
{
Image::setRenderer(mRenderer);
@@ -90,25 +58,25 @@ SDLGraphics::SDLGraphics(SDL_Renderer *renderer)
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
if (const char *driver = SDL_GetCurrentVideoDriver())
- logger->log("Using video driver: %s", driver);
+ Log::info("Using video driver: %s", driver);
else
- logger->log("Using video driver: not initialized");
+ Log::info("Using video driver: not initialized");
SDL_RendererInfo info;
if (SDL_GetRendererInfo(renderer, &info) == 0) {
- logger->log("Using renderer: %s", info.name);
-
- logger->log("The renderer is a software fallback: %s",
- (info.flags & SDL_RENDERER_SOFTWARE) ? "yes" : "no");
- logger->log("The renderer is hardware accelerated: %s",
- (info.flags & SDL_RENDERER_ACCELERATED) ? "yes" : "no");
- logger->log("Vsync: %s",
- (info.flags & SDL_RENDERER_PRESENTVSYNC) ? "on" : "off");
- logger->log("Renderer supports rendering to texture: %s",
- (info.flags & SDL_RENDERER_TARGETTEXTURE) ? "yes" : "no");
- logger->log("Max texture size: %dx%d",
- info.max_texture_width, info.max_texture_height);
+ Log::info("Using renderer: %s", info.name);
+
+ Log::info("The renderer is a software fallback: %s",
+ (info.flags & SDL_RENDERER_SOFTWARE) ? "yes" : "no");
+ Log::info("The renderer is hardware accelerated: %s",
+ (info.flags & SDL_RENDERER_ACCELERATED) ? "yes" : "no");
+ Log::info("Vsync: %s",
+ (info.flags & SDL_RENDERER_PRESENTVSYNC) ? "on" : "off");
+ Log::info("Renderer supports rendering to texture: %s",
+ (info.flags & SDL_RENDERER_TARGETTEXTURE) ? "yes" : "no");
+ Log::info("Max texture size: %dx%d",
+ info.max_texture_width, info.max_texture_height);
}
}
@@ -167,7 +135,7 @@ bool SDLGraphics::drawRescaledImage(const Image *image,
dstRect.w = desiredWidth;
dstRect.h = desiredHeight;
- SetColorAlphaMod mod(image->mTexture, mColor, useColor);
+ setColorAlphaMod(image, useColor);
return SDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect) != 0;
}
@@ -198,14 +166,16 @@ bool SDLGraphics::drawRescaledImageF(const Image *image,
dstRect.w = desiredWidth;
dstRect.h = desiredHeight;
- SetColorAlphaMod mod(image->mTexture, mColor, useColor);
+ setColorAlphaMod(image, useColor);
return SDL_RenderCopyF(mRenderer, image->mTexture, &srcRect, &dstRect) == 0;
}
#endif
void SDLGraphics::drawRescaledImagePattern(const Image *image,
- int x, int y,
- int w, int h,
+ int srcX, int srcY,
+ int srcW, int srcH,
+ int dstX, int dstY,
+ int dstW, int dstH,
int scaledWidth,
int scaledHeight)
{
@@ -216,25 +186,25 @@ void SDLGraphics::drawRescaledImagePattern(const Image *image,
if (scaledHeight <= 0 || scaledWidth <= 0)
return;
+ setColorAlphaMod(image, false);
+
SDL_Rect srcRect;
- srcRect.x = image->mBounds.x;
- srcRect.y = image->mBounds.y;
+ srcRect.x = image->mBounds.x + srcX;
+ srcRect.y = image->mBounds.y + srcY;
- for (int py = 0; py < h; py += scaledHeight) // Y position on pattern plane
+ for (int py = 0; py < dstH; py += scaledHeight) // Y position on pattern plane
{
- int dh = (py + scaledHeight >= h) ? h - py : scaledHeight;
- int dstY = y + py + mClipStack.top().yOffset;
+ SDL_Rect dstRect;
+ dstRect.h = (py + scaledHeight >= dstH) ? dstH - py : scaledHeight;
+ dstRect.y = dstY + py + mClipStack.top().yOffset;
- for (int px = 0; px < w; px += scaledWidth) // X position on pattern plane
+ for (int px = 0; px < dstW; px += scaledWidth) // X position on pattern plane
{
- int dw = (px + scaledWidth >= w) ? w - px : scaledWidth;
- int dstX = x + px + mClipStack.top().xOffset;
+ dstRect.x = dstX + px + mClipStack.top().xOffset;
+ dstRect.w = (px + scaledWidth >= dstW) ? dstW - px : scaledWidth;
- SDL_Rect dstRect;
- dstRect.x = dstX; dstRect.y = dstY;
- dstRect.w = dw; dstRect.h = dh;
- srcRect.w = image->mBounds.w * dw / scaledWidth;
- srcRect.h = image->mBounds.h * dh / scaledHeight;
+ srcRect.w = srcW * dstRect.w / scaledWidth;
+ srcRect.h = srcH * dstRect.h / scaledHeight;
if (SDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect))
return;
@@ -411,3 +381,20 @@ void SDLGraphics::fillRectangle(const gcn::Rectangle &rectangle)
(Uint8)(mColor.a));
SDL_RenderFillRect(mRenderer, &rect);
}
+
+void SDLGraphics::setColorAlphaMod(const Image *image, bool useColor) const
+{
+ SDL_Color color = { 255, 255, 255, 255 };
+ if (useColor)
+ {
+ color.r = static_cast<uint8_t>(mColor.r);
+ color.g = static_cast<uint8_t>(mColor.g);
+ color.b = static_cast<uint8_t>(mColor.b);
+ color.a = static_cast<uint8_t>(mColor.a);
+ }
+ color.a *= image->getAlpha();
+
+ SDL_Texture *texture = image->mTexture;
+ SDL_SetTextureColorMod(texture, color.r, color.g, color.b);
+ SDL_SetTextureAlphaMod(texture, color.a);
+}