zoomedit:

* Level can load Room data from the XML tree now.
This commit is contained in:
neoraider 2008-04-10 12:00:05 +00:00
parent 3c72a44fa4
commit 2271ef709f
9 changed files with 135 additions and 38 deletions

View file

@ -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));
}
}
}
}

View file

@ -29,9 +29,10 @@ namespace Data {
class Level {
private:
std::list<Room> rooms;
xmlpp::Element *roomsNode;
public:
Level() {}
Level(xmlpp::Element *levelNode);
};
}

View file

@ -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));
}
}
}
}

View file

@ -29,9 +29,10 @@ namespace Data {
class Room {
private:
std::list<Triangle> triangles;
xmlpp::Element *roomNode;
public:
Room() {}
Room(xmlpp::Element *node);
};
}

View file

@ -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;}
};
}

View file

@ -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");
}
}
}

View file

@ -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;
Vertex loadVertex(xmlpp::Element *node) const;
Vector loadVector(xmlpp::Element *node) const;
TexCoords loadTexCoords(xmlpp::Element *node) const;
public:
Triangle(bool vis = true) : visible(vis) {}
Triangle(xmlpp::Element *node);
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];
}
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];
}
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;}
};

View file

@ -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();

View file

@ -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();