From b9d66c41246fef56e41f600b5523a5ff1a29a1c8 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 9 Apr 2011 18:49:05 +0300 Subject: Improve map loading speed. --- src/map.cpp | 65 ++++++++++++++++++++++++++++++++++++++------- src/map.h | 7 +++++ src/opengl1graphics.cpp | 4 +-- src/openglgraphics.cpp | 12 ++++----- src/resources/image.h | 3 ++- src/resources/mapreader.cpp | 4 ++- 6 files changed, 75 insertions(+), 20 deletions(-) diff --git a/src/map.cpp b/src/map.cpp index eafdc88ec..4437b55a2 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -378,7 +378,10 @@ Map::Map(int width, int height, int tileWidth, int tileHeight): mLastScrollX(0.0f), mLastScrollY(0.0f), mOverlayDetail(config.getIntValue("OverlayDetail")), mOpacity(config.getFloatValue("guialpha")), - mPvp(0) + mPvp(0), + mTilesetsIndexed(false), + mIndexedTilesets(0), + mIndexedTilesetsSize(0) // mSpritesUpdated(true) { const int size = mWidth * mHeight; @@ -758,15 +761,10 @@ void Map::drawAmbientLayers(Graphics *graphics, LayerType type, Tileset *Map::getTilesetWithGid(int gid) const { - Tileset *s = NULL; - for (Tilesets::const_iterator it = mTilesets.begin(), - it_end = mTilesets.end(); it < it_end && (*it)->getFirstGid() <= gid; - ++it) - { - s = *it; - } - - return s; + if (gid < mIndexedTilesetsSize) + return mIndexedTilesets[gid]; + else + return 0; } void Map::blockTile(int x, int y, BlockType type) @@ -1409,6 +1407,9 @@ MapItem *Map::findPortalXY(int x, int y) TileAnimation *Map::getAnimationForGid(int gid) const { + if (mTileAnimations.empty()) + return 0; + std::map::const_iterator i = mTileAnimations.find(gid); return (i == mTileAnimations.end()) ? NULL : i->second; @@ -1465,6 +1466,50 @@ std::string Map::getObjectData(unsigned x, unsigned y, int type) return ""; } +void Map::indexTilesets() +{ + if (mTilesetsIndexed) + return; + + mTilesetsIndexed = true; + + Tileset *s = 0; + for (Tilesets::const_iterator it = mTilesets.begin(), + it_end = mTilesets.end(); it < it_end; + ++it) + { + if (!s || s->getFirstGid() < (*it)->getFirstGid()) + s = *it; + } + if (!s) + return; + + const int size = s->getFirstGid() + s->size(); + mIndexedTilesetsSize = size; + mIndexedTilesets = new Tileset*[size]; + std::fill_n(mIndexedTilesets, size, static_cast(0)); + for (Tilesets::const_iterator it = mTilesets.begin(), + it_end = mTilesets.end(); it < it_end; + ++it) + { + s = *it; + const int start = s->getFirstGid(); + const int end = start + s->size(); + for (int f = start; f < end; f ++) + mIndexedTilesets[f] = s; + } +} + +void Map::clearIndexedTilesets() +{ + if (!mTilesetsIndexed) + return; + + mTilesetsIndexed = false; + delete[] mIndexedTilesets; + mIndexedTilesetsSize = 0; +} + SpecialLayer::SpecialLayer(int width, int height, bool drawSprites): mWidth(width), mHeight(height) { diff --git a/src/map.h b/src/map.h index 79c9e8b2b..0c1c5b098 100644 --- a/src/map.h +++ b/src/map.h @@ -433,6 +433,10 @@ class Map : public Properties, public ConfigListener std::string getObjectData(unsigned x, unsigned y, int type); + void indexTilesets(); + + void clearIndexedTilesets(); + protected: friend class Actor; @@ -516,6 +520,9 @@ class Map : public Properties, public ConfigListener float mOpacity; int mOpenGL; int mPvp; + bool mTilesetsIndexed; + Tileset** mIndexedTilesets; + int mIndexedTilesetsSize; SpecialLayer *mSpecialLayer; SpecialLayer *mTempLayer; diff --git a/src/opengl1graphics.cpp b/src/opengl1graphics.cpp index b2742e360..ad488d3cd 100644 --- a/src/opengl1graphics.cpp +++ b/src/opengl1graphics.cpp @@ -121,7 +121,7 @@ static inline void drawQuad(Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height) { - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { // Find OpenGL normalized texture coordinates. float texX1 = static_cast(srcX) @@ -159,7 +159,7 @@ static inline void drawRescaledQuad(Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height, int desiredWidth, int desiredHeight) { - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { // Find OpenGL normalized texture coordinates. float texX1 = static_cast(srcX) diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index aa3dc6667..f65553a15 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -130,7 +130,7 @@ static inline void drawQuad(Image *image, int srcX, int srcY, int dstX, int dstY, int width, int height) { - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { // Find OpenGL normalized texture coordinates. const float texX1 = static_cast(srcX) / @@ -192,7 +192,7 @@ static inline void drawRescaledQuad(Image *image, int width, int height, int desiredWidth, int desiredHeight) { - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { // Find OpenGL normalized texture coordinates. const float texX1 = static_cast(srcX) / @@ -379,7 +379,7 @@ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h) unsigned int vp = 0; const unsigned int vLimit = vertexBufSize * 4; // Draw a set of textured rectangles - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { float texX1 = static_cast(srcX) / tw; float texY1 = static_cast(srcY) / th; @@ -515,7 +515,7 @@ void OpenGLGraphics::drawRescaledImagePattern(Image *image, int x, int y, float texY1 = static_cast(srcY) / th; // Draw a set of textured rectangles - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { for (int py = 0; py < h; py += ih) { @@ -634,7 +634,7 @@ void OpenGLGraphics::drawImagePattern2(GraphicsVertexes *vert, Image *image) std::vector::iterator ivp; // Draw a set of textured rectangles - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { std::vector &floatTexPool = ogl->getFloatTexPool(); std::vector::iterator ft; @@ -701,7 +701,7 @@ void OpenGLGraphics::calcImagePattern(GraphicsVertexes* vert, Image *image, ogl->init(); // Draw a set of textured rectangles - if (image->getTextureType() == GL_TEXTURE_2D) + if (image->mTextureType == GL_TEXTURE_2D) { float texX1 = static_cast(srcX) / tw; float texY1 = static_cast(srcY) / th; diff --git a/src/resources/image.h b/src/resources/image.h index 585b3d62b..beaba7b0e 100644 --- a/src/resources/image.h +++ b/src/resources/image.h @@ -209,6 +209,8 @@ class Image : public Resource static int getTextureType() { return mTextureType; } + + static int mTextureType; #endif protected: @@ -268,7 +270,6 @@ class Image : public Resource int mTexWidth, mTexHeight; static int mUseOpenGL; - static int mTextureType; static int mTextureSize; #endif }; diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 25e9750de..d2bcde06d 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -370,7 +370,7 @@ Map *MapReader::readMap(xmlNodePtr node, const std::string &path) } map->initializeAmbientLayers(); - + map->clearIndexedTilesets(); return map; } @@ -424,6 +424,8 @@ void MapReader::readLayer(xmlNodePtr node, Map *map) const bool isFringeLayer = (name.substr(0, 6) == "fringe"); const bool isCollisionLayer = (name.substr(0, 9) == "collision"); + map->indexTilesets(); + MapLayer *layer = 0; if (!isCollisionLayer) -- cgit v1.2.3-60-g2f50