diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-02 00:15:20 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-02 00:15:20 +0000 |
commit | 8501e358510dac20b7aa457408e36c48fe387df0 (patch) | |
tree | 46d9baeae1ed466d79b1b0b33151dac8c9ba1157 /src | |
parent | b7cded07732a85204b7e251d51cf4538a58ce374 (diff) | |
download | mana-client-8501e358510dac20b7aa457408e36c48fe387df0.tar.gz mana-client-8501e358510dac20b7aa457408e36c48fe387df0.tar.bz2 mana-client-8501e358510dac20b7aa457408e36c48fe387df0.tar.xz mana-client-8501e358510dac20b7aa457408e36c48fe387df0.zip |
Moved map drawing loop into the Map class and made it safer (it doesn't try to
draw tiles outside of the map area). The tmw.ini file was removed, we're using
config.xml now.
Diffstat (limited to 'src')
-rw-r--r-- | src/graphic/graphic.cpp | 49 | ||||
-rw-r--r-- | src/graphic/graphic.h | 10 | ||||
-rw-r--r-- | src/map.cpp | 24 | ||||
-rw-r--r-- | src/map.h | 6 |
4 files changed, 59 insertions, 30 deletions
diff --git a/src/graphic/graphic.cpp b/src/graphic/graphic.cpp index 0d8a926b..1602e13b 100644 --- a/src/graphic/graphic.cpp +++ b/src/graphic/graphic.cpp @@ -162,6 +162,16 @@ Graphics::~Graphics() _endDraw(); } +int Graphics::getWidth() +{ + return screen->w; +} + +int Graphics::getHeight() +{ + return screen->h; +} + void Graphics::drawImageRect( int x, int y, int w, int h, Image *topLeft, Image *topRight, @@ -367,8 +377,8 @@ void Engine::draw() map_x = (player_node->x - 13) * 32 + get_x_offset(player_node); map_y = (player_node->y - 9) * 32 + get_y_offset(player_node); - camera_x = map_x >> 5; - camera_y = map_y >> 5; + camera_x = map_x / 32; + camera_y = map_y / 32; int offset_x = map_x & 31; int offset_y = map_y & 31; @@ -378,20 +388,11 @@ void Engine::draw() frame++; // Draw tiles below nodes - for (int j = 0; j < 20; j++) { - for (int i = 0; i < 26; i++) { - Image *tile0 = tiledMap->getTile(i + camera_x, j + camera_y, 0); - Image *tile1 = tiledMap->getTile(i + camera_x, j + camera_y, 1); - - if (tile0) { - tile0->draw(screen, i * 32 - offset_x, j * 32 - offset_y); - } - if (tile1) { - tile1->draw(screen, i * 32 - offset_x, j * 32 - offset_y); - } - } + if (tiledMap) { + tiledMap->draw(guiGraphics, map_x, map_y, 0); + tiledMap->draw(guiGraphics, map_x, map_y, 1); } - + // Draw items std::list<FloorItem*>::iterator floorItemIterator = floorItems.begin(); while (floorItemIterator != floorItems.end()) @@ -531,21 +532,9 @@ void Engine::draw() // last drawed for fringe layer, draw the missing lines } - // Draw tiles above nodes - for (int j = 0; j < 20; j++) { - for (int i = 0; i < 26; i++) { - Image *tile = tiledMap->getTile(i + camera_x, j + camera_y, 2); - - if (tile) { - tile->draw(screen, i * 32 - offset_x, j * 32 - offset_y); - -#ifdef DEBUG - guiGraphics->setColor(gcn::Color(0, 0, 0)); - guiGraphics->drawRectangle(gcn::Rectangle( - i * 32 - offset_x, j * 32 - offset_y, 32, 32)); -#endif - } - } + // Draw tiles below nodes + if (tiledMap) { + tiledMap->draw(guiGraphics, map_x, map_y, 2); } // Find a path from the player to the mouse, and draw it. This is for debug diff --git a/src/graphic/graphic.h b/src/graphic/graphic.h index 413c9d33..f8f7bb12 100644 --- a/src/graphic/graphic.h +++ b/src/graphic/graphic.h @@ -151,6 +151,16 @@ class Graphics : public gcn::SDLGraphics { */ void updateScreen(); + /** + * Returns the width of the screen. + */ + int getWidth(); + + /** + * Returns the height of the screen. + */ + int getHeight(); + private: Image *mouseCursor; }; diff --git a/src/map.cpp b/src/map.cpp index 8b90df06..ede85d00 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -182,6 +182,30 @@ void Map::setSize(int width, int height) tiles = new Image*[width * height * 3]; } +void Map::draw(Graphics *graphics, int scrollX, int scrollY, int layer) +{ + int startX = scrollX / 32; + int startY = scrollY / 32; + int endX = (graphics->getWidth() + scrollX + 31) / 32; + int endY = (graphics->getHeight() + scrollY + 31) / 32; + + if (startX < 0) startX = 0; + if (startY < 0) startY = 0; + if (endX >= width) endX = width - 1; + if (endY >= height) endY = height - 1; + + for (int y = startY; y < endY; y++) + { + for (int x = startX; x < endX; x++) + { + Image *img = getTile(x, y, layer); + if (img) { + img->draw(screen, x * 32 - scrollX, y * 32 - scrollY); + } + } + } +} + void Map::setWalk(int x, int y, bool walkable) { metaTiles[x + y * width].walkable = walkable; @@ -25,6 +25,7 @@ #define _TMW_MAP_H #include "being.h" +#include "graphic/graphic.h" #include "resources/image.h" /** @@ -97,6 +98,11 @@ class Map static Map *load(const std::string &mapFile); /** + * Draws the map to the given graphics output. + */ + void draw(Graphics *graphics, int scrollX, int scrollY, int layer); + + /** * Sets the size of the map. This will destroy any existing map data. */ void setSize(int width, int height); |