summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2005-02-20 17:16:37 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2005-02-20 17:16:37 +0000
commit38a97bac830d6afe82fe3f68f3d8a0831bb5b643 (patch)
treeae8f26697caeee74da81c7b87cda0ac90faf5fb7 /src
parentd332272741e8382409453b975c66235d45e66cc7 (diff)
downloadmana-38a97bac830d6afe82fe3f68f3d8a0831bb5b643.tar.gz
mana-38a97bac830d6afe82fe3f68f3d8a0831bb5b643.tar.bz2
mana-38a97bac830d6afe82fe3f68f3d8a0831bb5b643.tar.xz
mana-38a97bac830d6afe82fe3f68f3d8a0831bb5b643.zip
More progress towards loading XML maps.
Diffstat (limited to 'src')
-rw-r--r--src/game.cpp17
-rw-r--r--src/graphic/graphic.cpp10
-rw-r--r--src/graphic/spriteset.h2
-rw-r--r--src/main.cpp47
-rw-r--r--src/main.h1
-rw-r--r--src/map.cpp33
-rw-r--r--src/map.h9
-rw-r--r--src/resources/mapreader.cpp63
-rw-r--r--src/resources/mapreader.h28
9 files changed, 125 insertions, 85 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 01a155a6..be2eacb8 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -122,7 +122,8 @@ void game() {
void do_init()
{
- if (!tiledMap.load(map_path)) {
+ tiledMap = Map::load(map_path);
+ if (!tiledMap) {
error("Could not find map file");
}
@@ -310,7 +311,7 @@ void do_input()
if (keys[SDLK_UP] || keys[SDLK_KP8])
{
// UP
- if (tiledMap.getWalk(x, y - 1) != 0)
+ if (tiledMap->getWalk(x, y - 1) != 0)
{
walk(x, y-1, NORTH);
walk_status = 1;
@@ -326,7 +327,7 @@ void do_input()
else if (keys[SDLK_DOWN] || keys[SDLK_KP2])
{
// Down
- if (tiledMap.getWalk(x, y + 1) != 0)
+ if (tiledMap->getWalk(x, y + 1) != 0)
{
walk(x, y + 1, SOUTH);
walk_status = 1;
@@ -341,7 +342,7 @@ void do_input()
}
else if (keys[SDLK_LEFT] || keys[SDLK_KP4])
{
- if (tiledMap.getWalk(x - 1, y) != 0) {
+ if (tiledMap->getWalk(x - 1, y) != 0) {
walk(x - 1, y, WEST);
walk_status = 1;
src_x = x;
@@ -355,7 +356,7 @@ void do_input()
}
else if (keys[SDLK_RIGHT] || keys[SDLK_KP6])
{
- if (tiledMap.getWalk(x + 1, y) != 0) {
+ if (tiledMap->getWalk(x + 1, y) != 0) {
walk(x + 1, y, EAST);
walk_status = 1;
src_x = x;
@@ -552,7 +553,7 @@ void do_parse() {
being->job = RFIFOW(14);
add_node(being);
}
- being->setPath(tiledMap.findPath(
+ being->setPath(tiledMap->findPath(
get_src_x(RFIFOP(50)),
get_src_y(RFIFOP(50)),
get_dest_x(RFIFOP(50)),
@@ -635,7 +636,9 @@ void do_parse() {
memset(map_path, '\0', 480);
strcat(map_path, "./data/map/");
strncat(map_path, RFIFOP(2), 497 - strlen(map_path));
- if (tiledMap.load(map_path)) {
+ if (tiledMap) delete tiledMap;
+ tiledMap = Map::load(map_path);
+ if (tiledMap) {
Being *temp;
temp = new Being();
memcpy(temp, player_node, sizeof(Being));
diff --git a/src/graphic/graphic.cpp b/src/graphic/graphic.cpp
index 9fdda40b..83ed22bc 100644
--- a/src/graphic/graphic.cpp
+++ b/src/graphic/graphic.cpp
@@ -379,8 +379,8 @@ void Engine::draw()
// 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);
+ 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);
@@ -509,7 +509,7 @@ void Engine::draw()
// 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);
+ Image *tile = tiledMap->getTile(i + camera_x, j + camera_y, 2);
if (tile) {
tile->draw(screen, i * 32 - offset_x, j * 32 - offset_y);
@@ -527,7 +527,7 @@ void Engine::draw()
// purposes.
if (displayPathToMouse)
{
- PATH_NODE *debugPath = tiledMap.findPath(
+ PATH_NODE *debugPath = tiledMap->findPath(
player_node->x, player_node->y,
mouseX / 32 + camera_x, mouseY / 32 + camera_y);
@@ -538,7 +538,7 @@ void Engine::draw()
guiGraphics->setColor(gcn::Color(255, 0, 0));
guiGraphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8));
- MetaTile *tile = tiledMap.getMetaTile(debugPath->x, debugPath->y);
+ MetaTile *tile = tiledMap->getMetaTile(debugPath->x, debugPath->y);
std::stringstream cost;
cost << tile->Gcost;
diff --git a/src/graphic/spriteset.h b/src/graphic/spriteset.h
index d7b72a0e..42d6a82a 100644
--- a/src/graphic/spriteset.h
+++ b/src/graphic/spriteset.h
@@ -41,7 +41,7 @@ class Spriteset {
/*
* Cuts the passed image in a grid of sub images.
*/
- Spriteset::Spriteset(Image *img, int w, int h);
+ Spriteset(Image *img, int w, int h);
/**
* Destructor.
diff --git a/src/main.cpp b/src/main.cpp
index 4163b239..686763ba 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -55,7 +55,8 @@ PLAYER_INFO *char_info = new PLAYER_INFO;
Spriteset *hairset = NULL, *playerset = NULL;
Image *login_wallpaper = NULL;
-Graphics* graphics;
+Graphics *graphics;
+Map *tiledMap;
char username[LEN_USERNAME];
char password[LEN_PASSWORD];
@@ -394,27 +395,27 @@ int main(int argc, char *argv[])
// ---by Kyokai
int PLAYER_INFO::GetSkill(int n_ID, int n_XP, int n_base)
{
- if(n_ID>N_SKILLS||n_ID<0) // out of cheese error, abort function
- return 0;
- // 1. raise the exp value
- m_Skill[n_ID].exp+=(n_XP*m_Skill[n_ID].mod);
-
- // 2. Check for level up
- if(m_Skill[n_ID].exp >= 20 * ((m_Skill[n_ID].level)^(6/5)))
- {
- m_Skill[n_ID].level += 1;
- m_Skill[n_ID].exp = 0;
- // TO DO: send the user a message that tells him his
- // skill just leveled up!
- }
-
- // 3. getting the return value
- int r = m_Skill[n_ID].level;
- if(n_base)
- {
- // TO DO: alter values based on equipment bonuses
- }
-
- return r; // return the value
+ if (n_ID > N_SKILLS || n_ID < 0) // out of cheese error, abort function
+ return 0;
+ // 1. raise the exp value
+ m_Skill[n_ID].exp += (short)(n_XP * m_Skill[n_ID].mod);
+
+ // 2. Check for level up
+ if (m_Skill[n_ID].exp >= 20 * ((m_Skill[n_ID].level)^(6/5)))
+ {
+ m_Skill[n_ID].level += 1;
+ m_Skill[n_ID].exp = 0;
+ // TO DO: send the user a message that tells him his
+ // skill just leveled up!
+ }
+
+ // 3. getting the return value
+ int r = m_Skill[n_ID].level;
+ if (n_base)
+ {
+ // TO DO: alter values based on equipment bonuses
+ }
+
+ return r; // return the value
}
diff --git a/src/main.h b/src/main.h
index 3b02a45c..dfc0c7ff 100644
--- a/src/main.h
+++ b/src/main.h
@@ -103,5 +103,6 @@ extern unsigned short x, y;
extern unsigned char direction;
extern Configuration config;
extern Sound sound;
+extern Map *tiledMap;
#endif
diff --git a/src/map.cpp b/src/map.cpp
index ee029af8..8b90df06 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -25,14 +25,11 @@
#include "map.h"
#include "log.h"
#include "resources/resourcemanager.h"
+#include "resources/mapreader.h"
#include "graphic/spriteset.h"
-#include <libxml/parser.h>
-#include <libxml/tree.h>
#include <queue>
-Map tiledMap;
-
#define OLD_MAP_WIDTH 200
#define OLD_MAP_HEIGHT 200
@@ -112,20 +109,20 @@ Map::~Map()
delete[] tiles;
}
-bool Map::load(const std::string &mapFile)
+Map *Map::load(const std::string &mapFile)
{
FILE *file = fopen(mapFile.c_str(), "r");
if (!file) {
log("Warning: %s", mapFile.c_str());
- return false;
+ return NULL;
}
MAP oldMap;
fread(&oldMap, sizeof(MAP), 1, file);
fclose(file);
- setSize(OLD_MAP_WIDTH, OLD_MAP_HEIGHT);
+ Map *map = new Map(OLD_MAP_WIDTH, OLD_MAP_HEIGHT);
// Load the default tileset
ResourceManager *resman = ResourceManager::getInstance();
@@ -160,33 +157,19 @@ bool Map::load(const std::string &mapFile)
}
if (id < tileset->spriteset.size() && (a == 0 || id > 0)) {
- setTile(x, y, a, tileset->spriteset[id]);
+ map->setTile(x, y, a, tileset->spriteset[id]);
}
else {
- setTile(x, y, a, NULL);
+ map->setTile(x, y, a, NULL);
}
}
// Walkability
- setWalk(x, y, (oldMap.tiles[x][y].data[3] & 0x0002) > 0);
+ map->setWalk(x, y, (oldMap.tiles[x][y].data[3] & 0x0002) > 0);
}
}
- return true;
-}
-
-bool loadXmlMap(const std::string &mapFile)
-{
- xmlDocPtr doc = xmlReadFile(mapFile.c_str(), NULL, 0);
-
- if (!doc) {
- log("Warning: %s", mapFile.c_str());
- return false;
- }
-
- xmlFreeDoc(doc);
-
- return false;
+ return map;
}
void Map::setSize(int width, int height)
diff --git a/src/map.h b/src/map.h
index ce5725da..2f03dcbe 100644
--- a/src/map.h
+++ b/src/map.h
@@ -94,12 +94,7 @@ class Map
/**
* Loads a map file (gat).
*/
- bool load(const std::string &mapFile);
-
- /**
- * Loads an XML map file (tmx).
- */
- bool loadXmlMap(const std::string &mapFile);
+ static Map *load(const std::string &mapFile);
/**
* Sets the size of the map. This will destroy any existing map data.
@@ -166,6 +161,4 @@ class Map
int onClosedList, onOpenList;
};
-extern Map tiledMap;
-
#endif
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp
index a3f42829..98160ff8 100644
--- a/src/resources/mapreader.cpp
+++ b/src/resources/mapreader.cpp
@@ -31,6 +31,19 @@
#define DEFAULT_TILE_WIDTH 32
#define DEFAULT_TILE_HEIGHT 32
+std::vector<Tileset*> MapReader::tilesets;
+
+Tileset::Tileset(Image *img, int w, int h, int firstGid):
+ Spriteset(img, w, h),
+ firstGid(firstGid)
+{
+}
+
+int Tileset::getFirstGid()
+{
+ return firstGid;
+}
+
Map *MapReader::readMap(const std::string &filename)
{
@@ -98,7 +111,10 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path)
{
if (xmlStrEqual(node->name, BAD_CAST "tileset"))
{
- readTileset(node, path, map);
+ Tileset *tileset = readTileset(node, path, map);
+ if (tileset) {
+ tilesets.push_back(tileset);
+ }
}
else if (xmlStrEqual(node->name, BAD_CAST "layer"))
{
@@ -125,9 +141,28 @@ void MapReader::readLayer(xmlNodePtr node, Map *map, int layer)
{
if (xmlStrEqual(node->name, BAD_CAST "tile") && y < h)
{
- //int gid = getProperty(node, "gid", -1);
- // TODO: Convert gid to Image* and set the tile
- //map->setTile(x, y, layer, (gid > -1) ? gid : 0);
+ int gid = getProperty(node, "gid", -1);
+ Image *img = NULL;
+
+ if (gid > -1) {
+ std::vector<Tileset*>::iterator i;
+ Tileset *set = NULL;
+
+ // Find the tileset with the highest firstGid below/eq to gid
+ for (i = tilesets.begin(); i != tilesets.end(); ++i) {
+ if ((*i)->getFirstGid() <= gid) {
+ set = (*i);
+ }
+ }
+
+ if (set && (gid - set->getFirstGid()) <
+ (int)set->spriteset.size())
+ {
+ img = set->spriteset[gid - set->getFirstGid()];
+ }
+ }
+
+ map->setTile(x, y, layer, img);
x++;
if (x == w) {x = 0; y++;}
@@ -141,18 +176,15 @@ void MapReader::readLayer(xmlNodePtr node, Map *map, int layer)
}
}
-void MapReader::readTileset(xmlNodePtr node, const std::string &path, Map *map)
+Tileset* MapReader::readTileset(
+ xmlNodePtr node, const std::string &path, Map *map)
{
- xmlChar* prop = xmlGetProp(node, BAD_CAST "source");
- if (prop) {
+ if (xmlHasProp(node, BAD_CAST "source")) {
log("Warning: External tilesets not supported yet.");
-#ifndef WIN32
- xmlFree(prop);
-#endif
- return;
+ return NULL;
}
- int firstgid = getProperty(node, "firstgid", 0);
+ int firstGid = getProperty(node, "firstgid", 0);
int tw = getProperty(node, "tilewidth", map->getTileWidth());
int th = getProperty(node, "tileheight", map->getTileHeight());
@@ -172,12 +204,11 @@ void MapReader::readTileset(xmlNodePtr node, const std::string &path, Map *map)
if (tilebmp)
{
- Spriteset *set = new Spriteset(tilebmp, tw, th);
- //set->setFirstGid(firstgid);
- // TODO: Like uhm, do something with this set!
+ Tileset *set = new Tileset(tilebmp, tw, th, firstGid);
#ifndef WIN32
xmlFree(source);
#endif
+ return set;
}
else {
log("Warning: Failed to load tileset (%s)\n", source);
@@ -189,6 +220,8 @@ void MapReader::readTileset(xmlNodePtr node, const std::string &path, Map *map)
node = node->next;
}
+
+ return NULL;
}
int MapReader::getProperty(xmlNodePtr node, const char* name, int def)
diff --git a/src/resources/mapreader.h b/src/resources/mapreader.h
index d58fdf90..b2a076ec 100644
--- a/src/resources/mapreader.h
+++ b/src/resources/mapreader.h
@@ -25,8 +25,29 @@
#define _INCLUDED_MAPREADER_H
#include "../map.h"
+#include "../graphic/spriteset.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
+#include <vector>
+
+/**
+ * A tileset, which is basically just a spriteset but it stores a firstgid.
+ */
+class Tileset : public Spriteset {
+ public:
+ /**
+ * Constructor.
+ */
+ Tileset(Image *img, int w, int h, int firstGid);
+
+ /**
+ * Returns the first gid.
+ */
+ int getFirstGid();
+
+ private:
+ int firstGid;
+};
/**
* Reader for XML map files (*.tmx)
@@ -54,10 +75,15 @@ class MapReader
/**
* Helper function that handles reading a tile set.
*/
- static void readTileset(xmlNodePtr node, const std::string &path,
+ static Tileset *readTileset(xmlNodePtr node, const std::string &path,
Map *map);
+ /**
+ * Helper function to get an integer property.
+ */
static int getProperty(xmlNodePtr node, const char* name, int def);
+
+ static std::vector<Tileset*> tilesets;
};
#endif