From fdbd1a4c521c537597b63cd4f2262517cab1f81a Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sat, 6 Oct 2012 15:26:12 +0300
Subject: Improve map draw speed in fast opengl.

---
 src/graphics.cpp             |  5 +++++
 src/graphics.h               |  3 +++
 src/maplayer.cpp             |  7 +++++--
 src/normalopenglgraphics.cpp |  4 ++--
 src/normalopenglgraphics.h   |  3 ++-
 src/resources/image.cpp      | 10 ++++------
 src/resources/image.h        |  5 ++---
 src/safeopenglgraphics.cpp   |  1 +
 src/safeopenglgraphics.h     |  3 ++-
 9 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/graphics.cpp b/src/graphics.cpp
index 19c53c83a..8b83880a7 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -785,6 +785,11 @@ void Graphics::calcImagePattern(GraphicsVertexes* const vert,
     vert->incPtr(1);
 }
 
+void Graphics::calcTile(ImageVertexes *const vert, const Image *const image,
+                        int x, int y)
+{
+}
+
 void Graphics::calcTile(ImageVertexes *const vert, int x, int y)
 {
     // Check that preconditions for blitting are met.
diff --git a/src/graphics.h b/src/graphics.h
index cdfc092fe..b95bb3779 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -206,6 +206,9 @@ class Graphics : public gcn::SDLGraphics
                                       const int x, const int y,
                                       const int w, const int h);
 
+        virtual void calcTile(ImageVertexes *const vert,
+                              const Image *const image, int x, int y);
+
         virtual void calcTile(ImageVertexes *const vert, int x, int y);
 
         virtual void drawTile(const ImageVertexes *const vert);
diff --git a/src/maplayer.cpp b/src/maplayer.cpp
index cf40cfae7..f79c8c042 100644
--- a/src/maplayer.cpp
+++ b/src/maplayer.cpp
@@ -232,6 +232,7 @@ void MapLayer::updateOGL(Graphics *const graphics, int startX, int startY,
                          const int scrollX, const int scrollY,
                          const int debugFlags)
 {
+#ifdef USE_OPENGL
     delete_all(mTempRows);
     mTempRows.clear();
 
@@ -276,7 +277,7 @@ void MapLayer::updateOGL(Graphics *const graphics, int startX, int startY,
                 const int py = py0 - img->mBounds.h;
                 if (flag || img->mBounds.h <= 32)
                 {
-                    if (lastImage != img)
+                    if (!lastImage || lastImage->mGLImage != img->mGLImage)
                     {
                         if (img->mBounds.w > 32)
                             imgSet.clear();
@@ -294,11 +295,13 @@ void MapLayer::updateOGL(Graphics *const graphics, int startX, int startY,
                         }
                         lastImage = img;
                     }
-                    graphics->calcTile(imgVert, px, py);
+                    lastImage = img;
+                    graphics->calcTile(imgVert, lastImage, px, py);
                 }
             }
         }
     }
+#endif
 }
 
 void MapLayer::drawOGL(Graphics *const graphics)
diff --git a/src/normalopenglgraphics.cpp b/src/normalopenglgraphics.cpp
index 4f3fd2aa2..b0e09b9eb 100644
--- a/src/normalopenglgraphics.cpp
+++ b/src/normalopenglgraphics.cpp
@@ -802,12 +802,12 @@ void NormalOpenGLGraphics::calcImagePattern(GraphicsVertexes *const vert,
 }
 
 void NormalOpenGLGraphics::calcTile(ImageVertexes *const vert,
+                                    const Image *const image,
                                     int dstX, int dstY)
 {
-    if (!vert)
+    if (!vert || !image)
         return;
 
-    Image *image = vert->image;
     const int srcX = image->mBounds.x;
     const int srcY = image->mBounds.y;
 
diff --git a/src/normalopenglgraphics.h b/src/normalopenglgraphics.h
index b2cb90e67..64649f5c8 100644
--- a/src/normalopenglgraphics.h
+++ b/src/normalopenglgraphics.h
@@ -87,7 +87,8 @@ class NormalOpenGLGraphics final : public Graphics
                               const int x, const int y,
                               const int w, const int h) override;
 
-        void calcTile(ImageVertexes *const vert, int x, int y) override;
+        void calcTile(ImageVertexes *const vert, const Image *const image,
+                      int x, int y) override;
 
         void drawTile(const ImageVertexes *const vert) override;
 
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index 0e2423bf9..6fa0b5bf5 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -46,6 +46,9 @@
 Image::Image(SDL_Surface *const image, const bool hasAlphaChannel0,
              uint8_t *const alphaChannel) :
     Resource(),
+#ifdef USE_OPENGL
+    mGLImage(0),
+#endif
     mLoaded(false),
     mAlpha(1.0f),
     mHasAlphaChannel(hasAlphaChannel0),
@@ -53,12 +56,7 @@ Image::Image(SDL_Surface *const image, const bool hasAlphaChannel0,
     mAlphaChannel(alphaChannel),
     mUseAlphaCache(SDLImageHelper::mEnableAlphaCache),
     mIsAlphaVisible(hasAlphaChannel0),
-#ifdef USE_OPENGL
-    mIsAlphaCalculated(false),
-    mGLImage(0)
-#else
     mIsAlphaCalculated(false)
-#endif
 {
     mBounds.x = 0;
     mBounds.y = 0;
@@ -83,6 +81,7 @@ Image::Image(SDL_Surface *const image, const bool hasAlphaChannel0,
 Image::Image(const GLuint glimage, const int width, const int height,
              const int texWidth, const int texHeight) :
     Resource(),
+    mGLImage(glimage),
     mLoaded(false),
     mAlpha(1.0f),
     mHasAlphaChannel(true),
@@ -91,7 +90,6 @@ Image::Image(const GLuint glimage, const int width, const int height,
     mUseAlphaCache(false),
     mIsAlphaVisible(true),
     mIsAlphaCalculated(false),
-    mGLImage(glimage),
     mTexWidth(texWidth),
     mTexHeight(texHeight)
 {
diff --git a/src/resources/image.h b/src/resources/image.h
index 8fafc87aa..26498e988 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -143,8 +143,6 @@ class Image : public Resource
         void SDLTerminateAlphaCache();
 
 #ifdef USE_OPENGL
-        // OpenGL only public functions
-
         int getTextureWidth() const
         { return mTexWidth; }
 
@@ -153,6 +151,8 @@ class Image : public Resource
 
         GLuint getGLImage() const
         { return mGLImage; }
+
+        GLuint mGLImage;
 #endif
 
         bool isHasAlphaChannel() const
@@ -213,7 +213,6 @@ class Image : public Resource
         Image(const GLuint glimage, const int width, const int height,
               const int texWidth, const int texHeight);
 
-        GLuint mGLImage;
         int mTexWidth, mTexHeight;
 #endif
 };
diff --git a/src/safeopenglgraphics.cpp b/src/safeopenglgraphics.cpp
index 93cee86db..457ef6dfe 100644
--- a/src/safeopenglgraphics.cpp
+++ b/src/safeopenglgraphics.cpp
@@ -356,6 +356,7 @@ bool SafeOpenGLGraphics::calcImageRect(GraphicsVertexes *const vert,
 }
 
 void SafeOpenGLGraphics::calcTile(ImageVertexes *const vert A_UNUSED,
+                                  const Image *const image A_UNUSED,
                                   int x A_UNUSED, int y A_UNUSED)
 {
 
diff --git a/src/safeopenglgraphics.h b/src/safeopenglgraphics.h
index efa9da832..1a0d06f46 100644
--- a/src/safeopenglgraphics.h
+++ b/src/safeopenglgraphics.h
@@ -94,7 +94,8 @@ class SafeOpenGLGraphics final : public Graphics
         void drawImageRect2(GraphicsVertexes *const vert,
                             const ImageRect &imgRect) override;
 
-        void calcTile(ImageVertexes *const vert, int x, int y) override;
+        void calcTile(ImageVertexes *const vert, const Image *const image,
+                      int x, int y) override;
 
         void drawTile(const ImageVertexes *const vert) override;
 
-- 
cgit v1.2.3-70-g09d2