diff options
-rw-r--r-- | src/Data/Level.cpp | 16 | ||||
-rw-r--r-- | src/Data/Level.h | 3 | ||||
-rw-r--r-- | src/Data/Room.cpp | 11 | ||||
-rw-r--r-- | src/Data/Room.h | 3 | ||||
-rw-r--r-- | src/Data/TexCoords.h | 12 | ||||
-rw-r--r-- | src/Data/Triangle.cpp | 70 | ||||
-rw-r--r-- | src/Data/Triangle.h | 42 | ||||
-rw-r--r-- | src/Instance.cpp | 12 | ||||
-rw-r--r-- | src/Instance.h | 4 |
9 files changed, 135 insertions, 38 deletions
diff --git a/src/Data/Level.cpp b/src/Data/Level.cpp index 4da62ea..e3c69fd 100644 --- a/src/Data/Level.cpp +++ b/src/Data/Level.cpp @@ -22,7 +22,21 @@ namespace ZoomEdit { namespace Data { - +Level::Level(xmlpp::Element *levelNode) { + roomsNode = dynamic_cast<xmlpp::Element*>(levelNode->get_children("rooms").front()); + + if(!roomsNode) + return; + + xmlpp::Node::NodeList roomList = roomsNode->get_children("room"); + + for(xmlpp::Node::NodeList::iterator room = roomList.begin(); room != roomList.end(); ++room) { + xmlpp::Element *roomNode = dynamic_cast<xmlpp::Element*>(*room); + + if(roomNode) + rooms.push_front(Room(roomNode)); + } +} } } diff --git a/src/Data/Level.h b/src/Data/Level.h index 4a9481a..621263e 100644 --- a/src/Data/Level.h +++ b/src/Data/Level.h @@ -29,9 +29,10 @@ namespace Data { class Level { private: std::list<Room> rooms; + xmlpp::Element *roomsNode; public: - Level() {} + Level(xmlpp::Element *levelNode); }; } diff --git a/src/Data/Room.cpp b/src/Data/Room.cpp index 58a7c31..95003f4 100644 --- a/src/Data/Room.cpp +++ b/src/Data/Room.cpp @@ -22,7 +22,16 @@ namespace ZoomEdit { namespace Data { - +Room::Room(xmlpp::Element *node) : roomNode(node) { + xmlpp::Node::NodeList triangleList = roomNode->get_children("triangle"); + + for(xmlpp::Node::NodeList::iterator t = triangleList.begin(); t != triangleList.end(); ++t) { + xmlpp::Element *tNode = dynamic_cast<xmlpp::Element*>(*t); + + if(tNode) + triangles.push_front(Triangle(tNode)); + } +} } } diff --git a/src/Data/Room.h b/src/Data/Room.h index 3d7ddbd..dea433d 100644 --- a/src/Data/Room.h +++ b/src/Data/Room.h @@ -29,9 +29,10 @@ namespace Data { class Room { private: std::list<Triangle> triangles; + xmlpp::Element *roomNode; public: - Room() {} + Room(xmlpp::Element *node); }; } diff --git a/src/Data/TexCoords.h b/src/Data/TexCoords.h index caf98a1..679c41c 100644 --- a/src/Data/TexCoords.h +++ b/src/Data/TexCoords.h @@ -29,6 +29,18 @@ class TexCoords { public: TexCoords(float s0 = 0, float t0 = 0, float r0 = 0, float q0 = 0) : s(s0), t(t0), r(r0), q(q0) {} + + float getS() const {return s;} + void setS(float s0) {s = s0;} + + float getT() const {return t;} + void setT(float t0) {t = t0;} + + float getR() const {return r;} + void setR(float r0) {r = r0;} + + float getQ() const {return q;} + void setQ(float q0) {q = q0;} }; } diff --git a/src/Data/Triangle.cpp b/src/Data/Triangle.cpp index 9adbaba..c7038c0 100644 --- a/src/Data/Triangle.cpp +++ b/src/Data/Triangle.cpp @@ -18,11 +18,81 @@ */ #include "Triangle.h" +#include <cstdlib> +#include <iostream> namespace ZoomEdit { namespace Data { +Vertex Triangle::loadVertex(xmlpp::Element *node) const { + Vertex v; + + v.setX(std::atof(node->get_attribute_value("x").c_str())); + v.setY(std::atof(node->get_attribute_value("y").c_str())); + v.setZ(std::atof(node->get_attribute_value("z").c_str())); + + return v; +} + +Vector Triangle::loadVector(xmlpp::Element *node) const { + Vector v; + + v.setX(std::atof(node->get_attribute_value("x").c_str())); + v.setY(std::atof(node->get_attribute_value("y").c_str())); + v.setZ(std::atof(node->get_attribute_value("z").c_str())); + + return v; +} +TexCoords Triangle::loadTexCoords(xmlpp::Element *node) const { + TexCoords t; + + t.setS(std::atof(node->get_attribute_value("s").c_str())); + t.setT(std::atof(node->get_attribute_value("t").c_str())); + t.setR(std::atof(node->get_attribute_value("r").c_str())); + t.setQ(std::atof(node->get_attribute_value("q").c_str())); + + return t; +} + +Triangle::Triangle(xmlpp::Element *node) : triangleNode(node) { + xmlpp::Node::NodeList nodeList = node->get_children(); + + int i = -1; + + std::memset(vertexNodes, 0, sizeof(vertexNodes)); + + for(xmlpp::Node::NodeList::iterator n = nodeList.begin(); n != nodeList.end(); ++n) { + xmlpp::Element *e = dynamic_cast<xmlpp::Element*>(*n); + if(!e) + continue; + + if(i >= 0) { + if(e->get_name() == "normal") { + normalNodes[i] = e; + normals[i] = loadVector(e); + continue; + } + else if(e->get_name() == "texcoords") { + texCoordsNodes[i] = e; + texCoords[i] = loadTexCoords(e); + + std::cout << "TexCoords (" << texCoords[i].getS() << ", " << texCoords[i].getT() << ", " << texCoords[i].getR() << ", " << texCoords[i].getQ() << ")" << std::endl; + + continue; + } + } + + if(e->get_name() == "vertex") { + vertexNodes[++i] = e; + vertices[i] = loadVertex(e); + + std::cout << "Vertex (" << vertices[i].getX() << ", " << vertices[i].getY() << ", " << vertices[i].getZ() << ")" << std::endl; + } + } + + visibleNode = node->get_attribute("visible"); +} } } diff --git a/src/Data/Triangle.h b/src/Data/Triangle.h index e3e6130..0c17834 100644 --- a/src/Data/Triangle.h +++ b/src/Data/Triangle.h @@ -22,66 +22,50 @@ #include "Vertex.h" #include "TexCoords.h" +#include <libxml++/nodes/element.h> namespace ZoomEdit { namespace Data { class Triangle { private: + xmlpp::Element *triangleNode; + xmlpp::Element *vertexNodes[3]; + xmlpp::Element *normalNodes[3]; + xmlpp::Element *texCoordsNodes[3]; + xmlpp::Attribute *visibleNode; + Vertex vertices[3]; Vector normals[3]; TexCoords texCoords[3]; bool visible; - public: - Triangle(bool vis = true) : visible(vis) {} - - Triangle(Vertex v0, Vertex v1, Vertex v2, bool vis = true) : visible(vis) { - vertices[0] = v0; vertices[1] = v1; vertices[2] = v2; - } - - Triangle(Vertex v[3], bool vis = true) : visible(vis) { - vertices[0] = v[0]; vertices[1] = v[1]; vertices[2] = v[2]; - } - - Triangle(Vertex v[3], Vector n[3], bool vis = true) : visible(vis) { - vertices[0] = v[0]; vertices[1] = v[1]; vertices[2] = v[2]; - normals[0] = n[0]; normals[1] = n[1]; normals[2] = n[2]; - } - - Triangle(Vertex v[3], TexCoords t[3], bool vis = true) : visible(vis) { - vertices[0] = v[0]; vertices[1] = v[1]; vertices[2] = v[2]; - texCoords[0] = t[0]; texCoords[1] = t[1]; texCoords[2] = t[2]; - } + Vertex loadVertex(xmlpp::Element *node) const; + Vector loadVector(xmlpp::Element *node) const; + TexCoords loadTexCoords(xmlpp::Element *node) const; - Triangle(Vertex v[3], Vector n[3], TexCoords t[3], bool vis = true) : visible(vis) { - vertices[0] = v[0]; vertices[1] = v[1]; vertices[2] = v[2]; - normals[0] = n[0]; normals[1] = n[1]; normals[2] = n[2]; - texCoords[0] = t[0]; texCoords[1] = t[1]; texCoords[2] = t[2]; - } + public: + Triangle(xmlpp::Element *node); - Vertex& getVertex(unsigned int i) {return vertices[i%3];} const Vertex& getVertex(unsigned int i) const {return vertices[i%3];} void setVertex(unsigned int i, Vertex v) {vertices[i%3] = v;} void setVertices(Vertex v[3]) { vertices[0] = v[0]; vertices[1] = v[1]; vertices[2] = v[2]; } - Vector& getNormal(unsigned int i) {return normals[i%3];} const Vector& getNormal(unsigned int i) const {return normals[i%3];} void setNormal(unsigned int i, Vector n) {normals[i%3] = n;} void setNormals(Vector n[3]) { normals[0] = n[0]; normals[1] = n[1]; normals[2] = n[2]; } - TexCoords& getTexCoords(unsigned int i) {return texCoords[i%3];} const TexCoords& getTexCoords(unsigned int i) const {return texCoords[i%3];} void getTexCoords(unsigned int i, TexCoords t) {texCoords[i%3] = t;} void getTexCoords(TexCoords t[3]) { texCoords[0] = t[0]; texCoords[1] = t[1]; texCoords[2] = t[2]; } - bool isVisible() {return visible;} + bool isVisible() const {return visible;} void setVisible(bool vis) {visible = vis;} }; diff --git a/src/Instance.cpp b/src/Instance.cpp index ead9cac..c3ac17c 100644 --- a/src/Instance.cpp +++ b/src/Instance.cpp @@ -57,6 +57,12 @@ Instance::Instance() : window(NULL), levelXml(NULL), level(NULL) { window->signal_hide().connect(sigc::mem_fun(this, &Instance::destroy)); + levelXml = new xmlpp::DomParser("level.lvl"); + xmlpp::Document *doc = levelXml->get_document(); + + if(doc && doc->get_root_node()) + level = new Data::Level(doc->get_root_node()); + window->show(); } @@ -64,12 +70,12 @@ Instance::~Instance() { if(window) delete window; - if(levelXml) - delete levelXml; - if(level) delete level; + if(levelXml) + delete levelXml; + instances--; if(!instances) Gtk::Main::quit(); diff --git a/src/Instance.h b/src/Instance.h index e8a0007..384abdf 100644 --- a/src/Instance.h +++ b/src/Instance.h @@ -22,7 +22,7 @@ #include <gtkmm/main.h> #include <libglademm/xml.h> -#include <libxml++/document.h> +#include <libxml++/parsers/domparser.h> namespace ZoomEdit { @@ -46,7 +46,7 @@ class Instance { Glib::RefPtr<Gnome::Glade::Xml> xml; Gui::Window *window; - xmlpp::Document *levelXml; + xmlpp::DomParser *levelXml; Data::Level *level; Instance(); |