fix redraw at top-left of map
[Tsunagari.git] / src / world.cpp
blob865530a3aaf1068418c2cad439c03aaef0a160dd
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** world.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Utility.hpp>
8 #include <libxml/tree.h>
10 #include "area.h"
11 #include "player.h"
12 #include "log.h"
13 #include "resourcer.h"
14 #include "window.h"
15 #include "world.h"
17 static World* globalWorld = NULL;
19 World* World::getWorld()
21 return globalWorld;
24 World::World(Resourcer* rc, GameWindow* wnd)
25 : rc(rc), wnd(wnd), area(NULL)
27 globalWorld = this;
30 World::~World()
32 delete area;
35 bool World::init()
37 if (!processDescriptor()) // Try to load in descriptor.
38 return false;
40 // FIXME The player entity doesn't have a descriptor yet.
41 player.reset(new Player(rc, NULL));
42 if (!player->init(xml.playerentity))
43 return false;
44 player->setPhase("down");
46 wnd->setCaption(Gosu::widen(xml.name));
47 return loadArea(xml.entry.area, xml.entry.coords);
50 void World::buttonDown(const Gosu::Button btn)
52 area->buttonDown(btn);
55 void World::buttonUp(const Gosu::Button btn)
57 area->buttonUp(btn);
60 void World::draw()
62 area->draw();
65 bool World::needsRedraw() const
67 return area->needsRedraw();
70 void World::update(unsigned long dt)
72 area->update(dt);
75 bool World::processDescriptor()
77 static const std::string descriptor = "world.conf";
78 xmlChar* str;
80 XMLDocRef doc = rc->getXMLDoc(descriptor, "world.dtd");
81 if (!doc)
82 return false;
83 const xmlNode* root = xmlDocGetRootElement(doc.get()); // <world>
84 if (!root)
85 return false;
87 xmlNode* node = root->xmlChildrenNode; // children of <world>
88 for (; node != NULL; node = node->next) {
89 if (!xmlStrncmp(node->name, BAD_CAST("name"), 5)) {
90 str = xmlNodeGetContent(node);
91 xml.name = (char*)str;
93 if (!xmlStrncmp(node->name, BAD_CAST("author"), 7)) {
94 str = xmlNodeGetContent(node);
95 xml.author = (char*)str;
97 if (!xmlStrncmp(node->name, BAD_CAST("player"), 7)) {
98 str = xmlNodeGetContent(node);
99 xml.playerentity = (char*)str;
101 if (!xmlStrncmp(node->name, BAD_CAST("type"), 5)) {
102 str = xmlGetProp(node, BAD_CAST("locality"));
104 if (xmlStrncmp(str, BAD_CAST("local"), 6))
105 xml.locality = LOCAL;
107 else if (xmlStrncmp(str, BAD_CAST("network"), 8))
108 xml.locality = NETWORK;
110 else {
111 Log::err(descriptor, "Invalid <type> value");
112 return false;
115 str = xmlGetProp(node, BAD_CAST("movement"));
117 if (xmlStrncmp(str, BAD_CAST("turn"), 5))
118 xml.movement = TURN;
120 else if (xmlStrncmp(str, BAD_CAST("tile"), 5))
121 xml.movement = TILE;
123 else if (xmlStrncmp(str, BAD_CAST("notile"), 7))
124 xml.movement = NOTILE;
126 else {
127 Log::err(descriptor, "Invalid <locality> value");
128 return false;
131 if (!xmlStrncmp(node->name, BAD_CAST("entrypoint"), 11)) {
132 str = xmlGetProp(node, BAD_CAST("area"));
133 xml.entry.area = (char*)str;
135 str = xmlGetProp(node, BAD_CAST("x"));
136 xml.entry.coords.x = atol((char*)str);
138 str = xmlGetProp(node, BAD_CAST("y"));
139 xml.entry.coords.y = atol((char*)str);
141 str = xmlGetProp(node, BAD_CAST("z"));
142 xml.entry.coords.z = atol((char*)str);
144 if (!xmlStrncmp(node->name, BAD_CAST("scripts"), 13)) {
145 node = node->xmlChildrenNode; // decend
147 if (!xmlStrncmp(node->name, BAD_CAST("script"), 7)) {
148 str = xmlNodeGetContent(node);
149 xml.scripts.push_back((char*)str);
152 return true;
156 bool World::loadArea(const std::string& areaName, coord_t playerPos)
158 Area* newArea = new Area(rc, this, player.get(), areaName);
159 delete area;
160 area = newArea;
161 if (!area->init())
162 return false;
163 player->setArea(area);
164 player->setCoordsByTile(playerPos);
165 return true;