From b27a517af7b8b99733622193a380bb3ef6674bcc Mon Sep 17 00:00:00 2001 From: neoraider Date: Fri, 11 Apr 2008 10:04:04 +0000 Subject: zoomedit: * Instance: Added createLevel and loadLevel methods. --- src/Data/Info.cpp | 6 ++++-- src/Data/Info.h | 4 ++-- src/Instance.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++--------- src/Instance.h | 7 +++++-- src/zoomedit.cpp | 2 +- 5 files changed, 61 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/Data/Info.cpp b/src/Data/Info.cpp index 28b85db..f0ce024 100644 --- a/src/Data/Info.cpp +++ b/src/Data/Info.cpp @@ -24,10 +24,12 @@ namespace Data { Info::Info(xmlpp::Element *node) { nameNode = dynamic_cast(node->get_children("name").front()); - name = nameNode->get_child_text()->get_content(); + if(nameNode->has_child_text()) + name = nameNode->get_child_text()->get_content(); descNode = dynamic_cast(node->get_children("desc").front()); - desc = descNode->get_child_text()->get_content(); + if(descNode->has_child_text()) + desc = descNode->get_child_text()->get_content(); } } diff --git a/src/Data/Info.h b/src/Data/Info.h index 1678b81..f9343d2 100644 --- a/src/Data/Info.h +++ b/src/Data/Info.h @@ -43,7 +43,7 @@ class Info { void setName(const Glib::ustring &n) { name = n; - nameNode->get_child_text()->set_content(n); + nameNode->set_child_text(n); } const Glib::ustring& getDescription() const { @@ -52,7 +52,7 @@ class Info { void setDescription(const Glib::ustring &d) { desc = d; - descNode->get_child_text()->set_content(d); + descNode->set_child_text(d); } }; diff --git a/src/Instance.cpp b/src/Instance.cpp index c3ac17c..eb4d9dd 100644 --- a/src/Instance.cpp +++ b/src/Instance.cpp @@ -26,7 +26,7 @@ namespace ZoomEdit { guint Instance::instances = 0; -Instance::Instance() : window(NULL), levelXml(NULL), level(NULL) { +Instance::Instance(const Glib::ustring &file) : window(NULL), levelXml(NULL), level(NULL) { instances++; #ifdef GLIBMM_EXCEPTIONS_ENABLED @@ -57,11 +57,10 @@ 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()); + if(file.empty()) + createLevel(); + else + loadLevel(file); window->show(); } @@ -81,10 +80,51 @@ Instance::~Instance() { Gtk::Main::quit(); } -bool Instance::create() { - Instance *instance = new Instance(); +void Instance::createLevel() { + if(level) + delete level; + + if(levelXml) + delete levelXml; + + levelXml = new xmlpp::DomParser; + xmlpp::Document *doc = levelXml->get_document(); + + xmlpp::Element *root = doc->create_root_node("level"); + + xmlpp::Element *info = root->add_child("info"); + info->add_child("name"); + info->add_child("desc"); + xmlpp::Element *start = info->add_child("start"); + start->set_attribute("x", "0"); + start->set_attribute("y", "0"); + start->set_attribute("z", "0"); + + root->add_child("rooms"); + root->add_child("gates"); + root->add_child("textures"); +} + +bool Instance::loadLevel(const Glib::ustring &file) { + if(level) + delete level; + + if(levelXml) + delete levelXml; + + levelXml = new xmlpp::DomParser(file); + xmlpp::Document *doc = levelXml->get_document(); + + if(doc && doc->get_root_node()) + level = new Data::Level(doc->get_root_node()); + + return (level != NULL); +} + +bool Instance::create(const Glib::ustring &file) { + Instance *instance = new Instance(file); - if(!instance->xml) { + if(!instance->level) { delete instance; return false; } diff --git a/src/Instance.h b/src/Instance.h index 384abdf..d9c0509 100644 --- a/src/Instance.h +++ b/src/Instance.h @@ -38,7 +38,10 @@ class Instance { public: virtual ~Instance(); - static bool create(); + void createLevel(); + bool loadLevel(const Glib::ustring &file); + + static bool create(const Glib::ustring &file = Glib::ustring()); private: static guint instances; @@ -49,7 +52,7 @@ class Instance { xmlpp::DomParser *levelXml; Data::Level *level; - Instance(); + Instance(const Glib::ustring &file); void destroy(); diff --git a/src/zoomedit.cpp b/src/zoomedit.cpp index 7c0e73b..c0d70b0 100644 --- a/src/zoomedit.cpp +++ b/src/zoomedit.cpp @@ -26,7 +26,7 @@ int main(int argc, char *argv[]) { Gtk::Main gtk(&argc, &argv); gtk_gl_init(&argc, &argv); - if(!ZoomEdit::Instance::create()) + if(!ZoomEdit::Instance::create("level.lvl")) return 1; gtk.run(); -- cgit v1.2.3