diff options
author | Philipp Sehmisch <tmw@crushnet.org> | 2006-08-21 16:06:50 +0000 |
---|---|---|
committer | Philipp Sehmisch <tmw@crushnet.org> | 2006-08-21 16:06:50 +0000 |
commit | 7eef0aaedefc5d3c73301bcb02834e165124c7e6 (patch) | |
tree | 3ac39dbb0bc92db2cdf9d90db700662627f6d8df | |
parent | e8c3de13e9f850498b9ab2dfaca758c851566d3a (diff) | |
download | mana-7eef0aaedefc5d3c73301bcb02834e165124c7e6.tar.gz mana-7eef0aaedefc5d3c73301bcb02834e165124c7e6.tar.bz2 mana-7eef0aaedefc5d3c73301bcb02834e165124c7e6.tar.xz mana-7eef0aaedefc5d3c73301bcb02834e165124c7e6.zip |
added overlays and smooth scrolling. (someone who knows what he is doing has to create the makefiles for the unix users)
-rw-r--r-- | ChangeLog | 16 | ||||
-rw-r--r-- | src/engine.cpp | 81 | ||||
-rw-r--r-- | src/engine.h | 5 | ||||
-rw-r--r-- | src/map.cpp | 86 | ||||
-rw-r--r-- | src/map.h | 28 | ||||
-rw-r--r-- | src/resources/mapreader.cpp | 27 |
6 files changed, 217 insertions, 26 deletions
@@ -1,3 +1,19 @@ +2006-08-21 Philipp Sehmisch <tmw@crushnet.org> + + * src/mapreader.cpp: added logic for initialisation of the overlays + * src/map.cpp, src/map.h: code for updating and drawing the overlays + * src/engine.cpp, src/engine.h: added smooth scrolling and the call + to draw the overlays + * data/maps/new_1-1.tmx.gz, data/graphics/new_5-1.tmx.gz, + data/graphics/images/ambient/sandstorm.png: Added sandstorm effect + to the desert maps outside of the city (feedback, please) + * data/maps/new_2-1.tmx.gz, data/maps/new_4-1.tmx.gz, + data/maps/new_5-1.tmx.gz, data/graphics/images/ambient/spotlight.png + Added spotlight effect to the cave maps (feedback, please) + * data/graphics/maps/new_9-1.tmx.gz, + data/graphics/images/ambient/clouds.png + Added cloudshadow effect to the woodland map (feedback, please) + 2006-08-20 Bjørn Lindeijer <bjorn@lindeijer.nl> * configure.ac: Moved libpng check before SDL_image check, since the diff --git a/src/engine.cpp b/src/engine.cpp index eb36a9fc..9a93b03e 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -27,6 +27,7 @@ #include "being.h" #include "beingmanager.h" +#include "configuration.h" #include "flooritemmanager.h" #include "game.h" #include "graphics.h" @@ -160,36 +161,72 @@ void Engine::logic() void Engine::draw(Graphics *graphics) { + // calculate viewpoint int midTileX = graphics->getWidth() / 32 / 2; int midTileY = graphics->getHeight() / 32 / 2; - int map_x = (player_node->mX - midTileX) * 32 + player_node->getXOffset(); - int map_y = (player_node->mY - midTileY) * 32 + player_node->getYOffset(); + int player_x = (player_node->mX - midTileX) * 32 + player_node->getXOffset(); + int player_y = (player_node->mY - midTileY) * 32 + player_node->getYOffset(); + + scrollLaziness = (int)config.getValue("ScrollLaziness", 32); + scrollRadius = (int)config.getValue("ScrollRadius", 32); + + if (scrollLaziness < 1) + scrollLaziness = 1; //avoids division by zero + + if (player_x > view_x + scrollRadius) + { + view_x += (player_x - view_x - scrollRadius) / scrollLaziness; + }; + if (player_x < view_x - scrollRadius) + { + view_x += (player_x - view_x + scrollRadius) / scrollLaziness; + }; + if (player_y > view_y + scrollRadius) + { + view_y += (player_y - view_y - scrollRadius) / scrollLaziness; + }; + if (player_y < view_y - scrollRadius) + { + view_y += (player_y - view_y + scrollRadius) / scrollLaziness; + }; + + //auto center when player is off screen + if ( player_x - view_x > graphics->getWidth() / 2 + || view_x - player_x > graphics->getWidth() / 2 + || view_y - player_y > graphics->getHeight() / 2 + || player_y - view_y > graphics->getHeight() / 2 + ) + { + view_x = player_x; + view_y = player_y; + }; if (mCurrentMap) { - if (map_x < 0) { - map_x = 0; + if (view_x < 0) { + view_x = 0; } - if (map_y < 0) { - map_y = 0; + if (view_y < 0) { + view_y = 0; } - if (map_x > (mCurrentMap->getWidth() - midTileX) * 32) { - map_x = (mCurrentMap->getWidth() - midTileX) * 32; + if (view_x > (mCurrentMap->getWidth() - midTileX) * 32) { + view_x = (mCurrentMap->getWidth() - midTileX) * 32; } - if (map_y > (mCurrentMap->getHeight() - midTileY) * 32) { - map_y = (mCurrentMap->getHeight() - midTileY) * 32; + if (view_y > (mCurrentMap->getHeight() - midTileY) * 32) { + view_y = (mCurrentMap->getHeight() - midTileY) * 32; } } - camera_x = map_x / 32; - camera_y = map_y / 32; + camera_x = int(view_x + 16) / 32; + camera_y = int(view_y + 16) / 32; // Draw tiles and sprites if (mCurrentMap != NULL) { - mCurrentMap->draw(graphics, map_x, map_y, 0); - mCurrentMap->draw(graphics, map_x, map_y, 1); - mCurrentMap->draw(graphics, map_x, map_y, 2); + mCurrentMap->draw(graphics, (int)view_x, (int)view_y, 0); + mCurrentMap->draw(graphics, (int)view_x, (int)view_y, 1); + mCurrentMap->draw(graphics, (int)view_x, (int)view_y, 2); + mCurrentMap->drawOverlay(graphics, view_x, view_y); } // Find a path from the player to the mouse, and draw it. This is for debug @@ -210,8 +247,8 @@ void Engine::draw(Graphics *graphics) graphics->setColor(gcn::Color(255, 0, 0)); for (PathIterator i = debugPath.begin(); i != debugPath.end(); i++) { - int squareX = i->x * 32 - map_x + 12; - int squareY = i->y * 32 - map_y + 12; + int squareX = i->x * 32 - int(view_x) + 12; + int squareY = i->y * 32 - int(view_y) + 12; graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); graphics->drawText( @@ -224,9 +261,9 @@ void Engine::draw(Graphics *graphics) Beings *beings = beingManager->getAll(); for (BeingIterator i = beings->begin(); i != beings->end(); i++) { - (*i)->drawSpeech(graphics, -map_x, -map_y); - (*i)->drawName(graphics, -map_x, -map_y); - (*i)->drawEmotion(graphics, -map_x, -map_y); + (*i)->drawSpeech(graphics, -(int)view_x, -(int)view_y); + (*i)->drawName(graphics, -(int)view_x, -(int)view_y); + (*i)->drawEmotion(graphics, -(int)view_x, -(int)view_y); } // Draw target marker if needed @@ -237,8 +274,8 @@ void Engine::draw(Graphics *graphics) graphics->setColor(gcn::Color(255, 255, 255)); int dy = (target->getType() == Being::PLAYER) ? 90 : 52; - graphics->drawText("[TARGET]", target->getPixelX() - map_x + 15, - target->getPixelY() - map_y - dy, gcn::Graphics::CENTER); + graphics->drawText("[TARGET]", target->getPixelX() - (int)view_x + 15, + target->getPixelY() - (int)view_y - dy, gcn::Graphics::CENTER); } gui->draw(); diff --git a/src/engine.h b/src/engine.h index ff39cfba..3ae380d6 100644 --- a/src/engine.h +++ b/src/engine.h @@ -78,6 +78,11 @@ class Engine Map *mCurrentMap; Network *mNetwork; + + int scrollRadius; + int scrollLaziness; + float view_x; // current viewpoint in pixels + float view_y; // current viewpoint in pixels }; extern Engine *engine; diff --git a/src/map.cpp b/src/map.cpp index 531b0f15..6eec4865 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -60,7 +60,8 @@ struct Location Map::Map(int width, int height, int tileWidth, int tileHeight): mWidth(width), mHeight(height), mTileWidth(tileWidth), mTileHeight(tileHeight), - mOnClosedList(1), mOnOpenList(2) + mOnClosedList(1), mOnOpenList(2), + mLastScrollX(0.0f), mLastScrollY(0.0f) { mMetaTiles = new MetaTile[mWidth * mHeight]; mTiles = new Image*[mWidth * mHeight * 3]; @@ -68,12 +69,18 @@ Map::Map(int width, int height, int tileWidth, int tileHeight): Map::~Map() { + // clean up map data delete[] mMetaTiles; delete[] mTiles; - - // Clean up tilesets + // clean up tilesets for_each(mTilesets.begin(), mTilesets.end(), make_dtor(mTilesets)); mTilesets.clear(); + // clean up overlays + std::list<AmbientOverlay>::iterator i; + for (i = mOverlays.begin(); i != mOverlays.end(); i++) + { + (*i).image->decRef(); + } } void @@ -160,6 +167,79 @@ Map::draw(Graphics *graphics, int scrollX, int scrollY, int layer) } void +Map::drawOverlay(Graphics *graphics, float scrollX, float scrollY) +{ + std::list<AmbientOverlay>::iterator i; + + if (mLastScrollX == 0.0f && mLastScrollY == 0.0f) + { + mLastScrollX = scrollX; + mLastScrollY = scrollY; + } + + for (i = mOverlays.begin(); i != mOverlays.end(); i++) + { + + if ((*i).image != NULL) + { + //apply self scrolling + (*i).scrollX -= (*i).scrollSpeedX; + (*i).scrollY -= (*i).scrollSpeedY; + + //apply parallaxing + (*i).scrollX += (scrollX - mLastScrollX) * (*i).parallax; + (*i).scrollY += (scrollY - mLastScrollY) * (*i).parallax; + + //keep the image pattern on the screen + while ((*i).scrollX > (*i).image->getWidth()) + { + (*i).scrollX -= (*i).image->getWidth(); + } + while ((*i).scrollY > (*i).image->getHeight()) + { + (*i).scrollY -= (*i).image->getHeight(); + } + while ((*i).scrollX < 0) + { + (*i).scrollX += (*i).image->getWidth(); + } + while ((*i).scrollY < 0) + { + (*i).scrollY += (*i).image->getHeight(); + } + + graphics->drawImagePattern ( (*i).image, + 0 - (int)(*i).scrollX, + 0 - (int)(*i).scrollY, + graphics->getWidth() + (int)(*i).scrollX, + graphics->getHeight() + (int)(*i).scrollY + ); + } + } + + mLastScrollX = scrollX; + mLastScrollY = scrollY; +} + +void +Map::setOverlay(Image *image, float speedX, float speedY, float parallax) +{ + if (image != NULL) + { + AmbientOverlay newOverlay; + + newOverlay.image = image; + newOverlay.parallax = parallax; + newOverlay.scrollSpeedX = speedX; + newOverlay.scrollSpeedY = speedY; + newOverlay.scrollX = 0; + newOverlay.scrollY = 0; + + mOverlays.push_back(newOverlay); + } +} + +void Map::setTileWithGid(int x, int y, int layer, int gid) { if (layer == 3) @@ -63,6 +63,16 @@ struct MetaTile bool walkable; /**< Can beings walk on this tile */ }; +struct AmbientOverlay +{ + Image *image; + float parallax; + float scrollX; + float scrollY; + float scrollSpeedX; + float scrollSpeedY; +}; + /** * A tile map. */ @@ -80,11 +90,21 @@ class Map : public Properties ~Map(); /** - * Draws the map to the given graphics output. + * Draws a map layer to the given graphics output. */ void draw(Graphics *graphics, int scrollX, int scrollY, int layer); /** + * Sets Overlay Graphic and Scrollspeed + */ + void setOverlay(Image *image, float speedX, float speedY, float parallax); + + /** + * Draws the overlay graphic to the given graphics output. + */ + void drawOverlay(Graphics *graphics, float scrollX, float scrollY); + + /** * Sets the size of the map. This will destroy any existing map data. */ void setSize(int width, int height); @@ -198,6 +218,12 @@ class Map : public Properties // Pathfinding members int mOnClosedList, mOnOpenList; + + //overlay Data + AmbientOverlay mFoo; + std::list<AmbientOverlay> mOverlays; + float mLastScrollX; + float mLastScrollY; }; #endif diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 382b0797..c1ae911c 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -34,6 +34,7 @@ #include "../map.h" #include "../tileset.h" +#include "../utils/tostring.h" const unsigned int DEFAULT_TILE_WIDTH = 32; const unsigned int DEFAULT_TILE_HEIGHT = 32; @@ -213,6 +214,32 @@ MapReader::readMap(xmlNodePtr node, const std::string &path) } } + //set Overlays + int i = 0; + ResourceManager *resman = ResourceManager::getInstance(); + + while (map->hasProperty("overlay" + toString(i) + "image")) + { + Image *overlayImage = resman->getImage(map->getProperty("overlay" + toString(i) + "image")); + float scrollX = 0.0f; + float scrollY = 0.0f; + float parallax = 0.0f; + if (map->hasProperty("overlay" + toString(i) + "scrollX")) + { + scrollX = atof(map->getProperty("overlay" + toString(i) + "scrollX").c_str()); + } + if (map->hasProperty("overlay" + toString(i) + "scrollY")) + { + scrollY = atof(map->getProperty("overlay" + toString(i) + "scrollY").c_str()); + } + if (map->hasProperty("overlay" + toString(i) + "parallax")) + { + parallax = atof(map->getProperty("overlay" + toString(i) + "parallax").c_str()); + } + map->setOverlay (overlayImage, scrollX, scrollY, parallax); + i++; + } + return map; } |