From 889ea19ddd5b4240375793d5030a04ed4642ec66 Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sat, 24 Aug 2013 16:12:12 +0300
Subject: extract surface draw into surfacegraphics class.

---
 src/CMakeLists.txt             |   2 +
 src/Makefile.am                |   2 +
 src/compoundsprite.cpp         |   8 +-
 src/resources/atlasmanager.cpp |   6 +-
 src/resources/image.h          |   1 +
 src/sdl2graphics.cpp           |  13 +--
 src/sdl2graphics.h             |  13 ---
 src/sdlgraphics.cpp            |  13 +--
 src/sdlgraphics.h              |  13 ---
 src/surfacegraphics.cpp        |  89 ++++++++++++++++++++
 src/surfacegraphics.h          | 185 +++++++++++++++++++++++++++++++++++++++++
 11 files changed, 290 insertions(+), 55 deletions(-)
 create mode 100644 src/surfacegraphics.cpp
 create mode 100644 src/surfacegraphics.h

(limited to 'src')

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index fc7bfa8c8..cd4be5cd6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -708,6 +708,8 @@ SET(SRCS
     sprite.h
     statuseffect.cpp
     statuseffect.h
+    surfacegraphics.cpp
+    surfacegraphics.h
     text.cpp
     text.h
     textmanager.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 120f06e82..927d854f2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -717,6 +717,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \
 	      sprite.h \
 	      statuseffect.cpp \
 	      statuseffect.h \
+	      surfacegraphics.cpp \
+	      surfacegraphics.h \
 	      text.cpp \
 	      text.h \
 	      textmanager.cpp \
diff --git a/src/compoundsprite.cpp b/src/compoundsprite.cpp
index 24e8306c3..6e8d47373 100644
--- a/src/compoundsprite.cpp
+++ b/src/compoundsprite.cpp
@@ -30,8 +30,8 @@
 #endif
 
 #include "map.h"
-#include "sdlgraphics.h"
 #include "sdlshared.h"
+#include "surfacegraphics.h"
 
 #include "resources/image.h"
 #include "resources/imagehelper.h"
@@ -336,9 +336,9 @@ void CompoundSprite::redraw() const
     if (!surface)
         return;
 
-    SDLGraphics *graphics = new SDLGraphics();
-    graphics->setBlitMode(SDLGraphics::BLIT_GFX);
-    graphics->setWindow(surface, BUFFER_WIDTH, BUFFER_HEIGHT);
+    SurfaceGraphics *graphics = new SurfaceGraphics();
+    graphics->setBlitMode(SurfaceGraphics::BLIT_GFX);
+    graphics->setTarget(surface);
     graphics->_beginDraw();
 
     int tileX = 32 / 2;
diff --git a/src/resources/atlasmanager.cpp b/src/resources/atlasmanager.cpp
index 5f4897147..853f79743 100644
--- a/src/resources/atlasmanager.cpp
+++ b/src/resources/atlasmanager.cpp
@@ -27,7 +27,7 @@
 #include "client.h"
 #include "graphicsmanager.h"
 #include "logger.h"
-#include "sdlgraphics.h"
+#include "surfacegraphics.h"
 
 #include "utils/mathutils.h"
 #include "utils/physfsrwops.h"
@@ -232,8 +232,8 @@ SDL_Surface *AtlasManager::createSDLAtlas(TextureAtlas *const atlas)
     if (!surface)
         return nullptr;
 
-    SDLGraphics *const graphics = new SDLGraphics();
-    graphics->setWindow(surface, width, height);
+    SurfaceGraphics *const graphics = new SurfaceGraphics();
+    graphics->setTarget(surface);
     graphics->_beginDraw();
 
     // drawing SDL images to surface
diff --git a/src/resources/image.h b/src/resources/image.h
index a0df54926..b31ecb29e 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -57,6 +57,7 @@ class Image : public Resource
     friend class OpenGLImageHelper;
     friend class SDLGraphics;
     friend class SDLImageHelper;
+    friend class SurfaceGraphics;
     friend class TestLauncher;
 #ifdef USE_OPENGL
     friend class AtlasManager;
diff --git a/src/sdl2graphics.cpp b/src/sdl2graphics.cpp
index 71a3ebbae..bdd06d3cf 100644
--- a/src/sdl2graphics.cpp
+++ b/src/sdl2graphics.cpp
@@ -49,7 +49,6 @@ static unsigned int *cB = nullptr;
 
 SDLGraphics::SDLGraphics() :
     Graphics(),
-    mBlitMode(BLIT_NORMAL),
     mOldPixel(0),
     mOldAlpha(0)
 {
@@ -131,16 +130,8 @@ bool SDLGraphics::drawImage2(const Image *const image, int srcX, int srcY,
 
     return true;
 /*
-    if (mBlitMode == BLIT_NORMAL)
-    {
-        return !(SDL_BlitSurface(image->mSDLSurface, &srcRect,
-                                 mWindow, &dstRect) < 0);
-    }
-    else
-    {
-        return !(SDL_gfxBlitRGBA(image->mSDLSurface, &srcRect,
-                                 mWindow, &dstRect) < 0);
-    }
+    return !(SDL_BlitSurface(image->mSDLSurface, &srcRect,
+                             mWindow, &dstRect) < 0);
 */
 }
 
diff --git a/src/sdl2graphics.h b/src/sdl2graphics.h
index 583be5e8a..35659161d 100644
--- a/src/sdl2graphics.h
+++ b/src/sdl2graphics.h
@@ -42,12 +42,6 @@ struct SDL_Surface;
 class SDLGraphics : public Graphics
 {
     public:
-        enum BlitMode
-        {
-            BLIT_NORMAL = 0,
-            BLIT_GFX
-        };
-
         /**
          * Constructor.
          */
@@ -122,12 +116,6 @@ class SDLGraphics : public Graphics
                                 const int w, const int h,
                                 const ImageRect &imgRect);
 
-        void setBlitMode(const BlitMode mode)
-        { mBlitMode = mode; }
-
-        BlitMode getBlitMode() const A_WARN_UNUSED
-        { return mBlitMode; }
-
         void fillRectangle(const gcn::Rectangle &rect) override;
 
         void drawRectangle(const gcn::Rectangle &rect) override;
@@ -156,7 +144,6 @@ class SDLGraphics : public Graphics
 
         void drawVLine(int x, int y1, int y2);
 
-        BlitMode mBlitMode;
         uint32_t mOldPixel;
         int mOldAlpha;
 };
diff --git a/src/sdlgraphics.cpp b/src/sdlgraphics.cpp
index a77076c26..f62bca7c8 100644
--- a/src/sdlgraphics.cpp
+++ b/src/sdlgraphics.cpp
@@ -47,7 +47,6 @@ static unsigned int *cB = nullptr;
 
 SDLGraphics::SDLGraphics() :
     Graphics(),
-    mBlitMode(BLIT_NORMAL),
     mOldPixel(0),
     mOldAlpha(0)
 {
@@ -126,16 +125,8 @@ bool SDLGraphics::drawImage2(const Image *const image, int srcX, int srcY,
     srcRect.w = static_cast<uint16_t>(width);
     srcRect.h = static_cast<uint16_t>(height);
 
-    if (mBlitMode == BLIT_NORMAL)
-    {
-        return !(SDL_BlitSurface(image->mSDLSurface, &srcRect,
-                                 mWindow, &dstRect) < 0);
-    }
-    else
-    {
-        return !(SDL_gfxBlitRGBA(image->mSDLSurface, &srcRect,
-                                 mWindow, &dstRect) < 0);
-    }
+    return !(SDL_BlitSurface(image->mSDLSurface, &srcRect,
+        mWindow, &dstRect) < 0);
 }
 
 void SDLGraphics::drawImagePattern(const Image *const image,
diff --git a/src/sdlgraphics.h b/src/sdlgraphics.h
index b0a076b26..db2ad93a0 100644
--- a/src/sdlgraphics.h
+++ b/src/sdlgraphics.h
@@ -45,12 +45,6 @@ struct SDL_Surface;
 class SDLGraphics : public Graphics
 {
     public:
-        enum BlitMode
-        {
-            BLIT_NORMAL = 0,
-            BLIT_GFX
-        };
-
         /**
          * Constructor.
          */
@@ -125,12 +119,6 @@ class SDLGraphics : public Graphics
                                 const int w, const int h,
                                 const ImageRect &imgRect);
 
-        void setBlitMode(const BlitMode mode)
-        { mBlitMode = mode; }
-
-        BlitMode getBlitMode() const A_WARN_UNUSED
-        { return mBlitMode; }
-
         void fillRectangle(const gcn::Rectangle &rect) override;
 
         void drawRectangle(const gcn::Rectangle &rect) override;
@@ -159,7 +147,6 @@ class SDLGraphics : public Graphics
 
         void drawVLine(int x, int y1, int y2);
 
-        BlitMode mBlitMode;
         uint32_t mOldPixel;
         int mOldAlpha;
 };
diff --git a/src/surfacegraphics.cpp b/src/surfacegraphics.cpp
new file mode 100644
index 000000000..b1c8dd148
--- /dev/null
+++ b/src/surfacegraphics.cpp
@@ -0,0 +1,89 @@
+/*
+ *  The ManaPlus Client
+ *  Copyright (C) 2004-2009  The Mana World Development Team
+ *  Copyright (C) 2009-2010  The Mana Developers
+ *  Copyright (C) 2011-2013  The ManaPlus Developers
+ *
+ *  This file is part of The ManaPlus Client.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "surfacegraphics.h"
+
+#include "main.h"
+
+#include "configuration.h"
+#include "graphicsmanager.h"
+#include "graphicsvertexes.h"
+#include "logger.h"
+
+#include "resources/imagehelper.h"
+
+#include <guichan/sdl/sdlpixel.hpp>
+
+#ifndef USE_SDL2
+#include <SDL_gfxBlitFunc.h>
+#endif
+
+#include "debug.h"
+
+SurfaceGraphics::SurfaceGraphics() :
+    Graphics(),
+    mBlitMode(BLIT_NORMAL),
+    mTarget(nullptr)
+{
+}
+
+SurfaceGraphics::~SurfaceGraphics()
+{
+}
+
+bool SurfaceGraphics::drawImage2(const Image *const image, int srcX, int srcY,
+                                 int dstX, int dstY, const int width,
+                                 const int height, const bool useColor A_UNUSED)
+{
+    FUNC_BLOCK("Graphics::drawImage2", 1)
+    // Check that preconditions for blitting are met.
+    if (!mTarget || !image || !image->mSDLSurface)
+        return false;
+
+    srcX += image->mBounds.x;
+    srcY += image->mBounds.y;
+
+    SDL_Rect dstRect;
+    SDL_Rect srcRect;
+    dstRect.x = static_cast<int16_t>(dstX);
+    dstRect.y = static_cast<int16_t>(dstY);
+    srcRect.x = static_cast<int16_t>(srcX);
+    srcRect.y = static_cast<int16_t>(srcY);
+    srcRect.w = static_cast<uint16_t>(width);
+    srcRect.h = static_cast<uint16_t>(height);
+
+#ifdef USE_SDL2
+    return !(SDL_BlitSurface(image->mSDLSurface, &srcRect,
+                             mTarget, &dstRect) < 0);
+#else
+    if (mBlitMode == BLIT_NORMAL)
+    {
+        return !(SDL_BlitSurface(image->mSDLSurface, &srcRect,
+                                 mTarget, &dstRect) < 0);
+    }
+    else
+    {
+        return !(SDL_gfxBlitRGBA(image->mSDLSurface, &srcRect,
+                                 mTarget, &dstRect) < 0);
+    }
+#endif
+}
diff --git a/src/surfacegraphics.h b/src/surfacegraphics.h
new file mode 100644
index 000000000..5cce65ddd
--- /dev/null
+++ b/src/surfacegraphics.h
@@ -0,0 +1,185 @@
+/*
+ *  The ManaPlus Client
+ *  Copyright (C) 2004-2009  The Mana World Development Team
+ *  Copyright (C) 2009-2010  The Mana Developers
+ *  Copyright (C) 2011-2013  The ManaPlus Developers
+ *
+ *  This file is part of The ManaPlus Client.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SURFACEGRAPHICS_H
+#define SURFACEGRAPHICS_H
+
+#include "graphics.h"
+
+#include "localconsts.h"
+
+class Image;
+class ImageCollection;
+class ImageVertexes;
+class MapLayer;
+
+struct SDL_Surface;
+
+/**
+ * A central point of control for graphics.
+ */
+class SurfaceGraphics : public Graphics
+{
+    public:
+        enum BlitMode
+        {
+            BLIT_NORMAL = 0,
+            BLIT_GFX
+        };
+
+        SurfaceGraphics();
+
+        A_DELETE_COPY(SurfaceGraphics)
+
+        virtual ~SurfaceGraphics();
+
+        void setWindow(SDL_Surface *const target)
+        { mTarget = target; }
+
+        SDL_Surface *getTarget() const
+        { return mTarget; }
+
+        void _beginDraw()
+        { }
+
+        void _endDraw()
+        { }
+
+        bool pushClipArea(gcn::Rectangle rect A_UNUSED)
+        { return true; }
+
+        void popClipArea()
+        { }
+
+        bool drawRescaledImage(const Image *const image A_UNUSED,
+                               int srcX A_UNUSED, int srcY A_UNUSED,
+                               int dstX A_UNUSED, int dstY A_UNUSED,
+                               const int width A_UNUSED,
+                               const int height A_UNUSED,
+                               const int desiredWidth A_UNUSED,
+                               const int desiredHeight A_UNUSED,
+                               const bool useColor A_UNUSED = false)
+        { return false; }
+
+        void drawImagePattern(const Image *const image A_UNUSED,
+                              const int x A_UNUSED, const int y A_UNUSED,
+                              const int w A_UNUSED, const int h A_UNUSED)
+        { }
+
+        void drawRescaledImagePattern(const Image *const image A_UNUSED,
+                                      const int x A_UNUSED,
+                                      const int y A_UNUSED,
+                                      const int w A_UNUSED,
+                                      const int h A_UNUSED,
+                                      const int scaledWidth A_UNUSED,
+                                      const int scaledHeight A_UNUSED)
+        { }
+
+        void calcImagePattern(ImageVertexes *const vert A_UNUSED,
+                                      const Image *const image A_UNUSED,
+                                      const int x A_UNUSED,
+                                      const int y A_UNUSED,
+                                      const int w A_UNUSED,
+                                      const int h A_UNUSED) const
+        { }
+
+        void calcImagePattern(ImageCollection *const vert A_UNUSED,
+                              const Image *const image A_UNUSED,
+                              const int x A_UNUSED, const int y A_UNUSED,
+                              const int w A_UNUSED, const int h A_UNUSED) const
+        { }
+
+        void calcTile(ImageVertexes *const vert A_UNUSED,
+                      const Image *const image A_UNUSED,
+                      int x A_UNUSED, int y A_UNUSED) const
+        { }
+
+        void calcTileSDL(ImageVertexes *const vert A_UNUSED,
+                         int x A_UNUSED, int y A_UNUSED) const
+        { }
+
+        void calcTile(ImageCollection *const vertCol A_UNUSED,
+                      const Image *const image A_UNUSED,
+                      int x A_UNUSED, int y A_UNUSED)
+        { }
+
+        void drawTile(const ImageVertexes *const vert A_UNUSED)
+        { }
+
+        void drawTile(const ImageCollection *const vertCol A_UNUSED)
+        { }
+
+        void updateScreen()
+        { }
+
+        SDL_Surface *getScreenshot() A_WARN_UNUSED
+        { return nullptr; }
+
+        bool drawNet(const int x1 A_UNUSED, const int y1 A_UNUSED,
+                     const int x2 A_UNUSED, const int y2 A_UNUSED,
+                     const int width A_UNUSED, const int height A_UNUSED)
+        { return false; }
+
+        bool calcWindow(ImageCollection *const vertCol A_UNUSED,
+                        const int x A_UNUSED, const int y A_UNUSED,
+                        const int w A_UNUSED, const int h A_UNUSED,
+                        const ImageRect &imgRect A_UNUSED)
+        { return false; }
+
+        void setBlitMode(const BlitMode mode)
+        { mBlitMode = mode; }
+
+        BlitMode getBlitMode() const A_WARN_UNUSED
+        { return mBlitMode; }
+
+        void fillRectangle(const gcn::Rectangle &rect A_UNUSED) override
+        { }
+
+        void drawRectangle(const gcn::Rectangle &rect A_UNUSED) override
+        { }
+
+        void drawPoint(int x A_UNUSED, int y A_UNUSED) override
+        { }
+
+        void drawLine(int x1 A_UNUSED, int y1 A_UNUSED,
+                      int x2 A_UNUSED, int y2 A_UNUSED) override
+        { }
+
+        bool setVideoMode(const int w A_UNUSED, const int h A_UNUSED,
+                          const int bpp A_UNUSED,
+                          const bool fs A_UNUSED, const bool hwaccel A_UNUSED,
+                          const bool resize A_UNUSED,
+                          const bool noFrame A_UNUSED)
+        { return false; }
+
+    protected:
+        bool drawImage2(const Image *const image,
+                        int srcX, int srcY,
+                        int dstX, int dstY,
+                        const int width, const int height,
+                        const bool useColor);
+
+        BlitMode mBlitMode;
+        SDL_Surface *mTarget;
+};
+
+#endif  // SURFACEGRAPHICS_H
-- 
cgit v1.2.3-70-g09d2