From b563953a4846bab720cae17ef4ab5a8296730c7c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 14 Mar 2015 21:02:01 -0400 Subject: Started writing map editor --- tools/mapedit/src/main.cpp | 54 ++++++++++++++++++++++++++ tools/mapedit/src/map.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++ tools/mapedit/src/map.h | 44 ++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 tools/mapedit/src/main.cpp create mode 100644 tools/mapedit/src/map.cpp create mode 100644 tools/mapedit/src/map.h (limited to 'tools/mapedit/src') diff --git a/tools/mapedit/src/main.cpp b/tools/mapedit/src/main.cpp new file mode 100644 index 0000000..09be151 --- /dev/null +++ b/tools/mapedit/src/main.cpp @@ -0,0 +1,54 @@ +#include + +#ifndef WX_PRECOMP +#include +#endif + +#include "map.h" + +class MapeditApp : public wxApp { + public: + virtual bool OnInit(); +}; + +class MapeditFrame : public wxFrame { + public: + MapeditFrame() : MapeditFrame(Map()) {} + MapeditFrame(Map map); + + private: + void OnExit(wxCommandEvent& event); + + Map map; + + wxDECLARE_EVENT_TABLE(); +}; + +wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame) + EVT_MENU(wxID_EXIT, MapeditFrame::OnExit) +wxEND_EVENT_TABLE() + +wxIMPLEMENT_APP(MapeditApp); + +bool MapeditApp::OnInit() +{ + MapeditFrame* frame = new MapeditFrame(); + frame->Show(true); + return true; +} + +MapeditFrame::MapeditFrame(Map map) : wxFrame(NULL, wxID_ANY, "Map Editor", wxPoint(50, 50), wxSize(450, 340)), map(map) +{ + wxMenu* menuFile = new wxMenu; + menuFile->Append(wxID_EXIT); + + wxMenuBar* menuBar = new wxMenuBar; + menuBar->Append(menuFile, "&File"); + + SetMenuBar(menuBar); +} + +void MapeditFrame::OnExit(wxCommandEvent& event) +{ + Close(true); +} diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp new file mode 100644 index 0000000..52a2096 --- /dev/null +++ b/tools/mapedit/src/map.cpp @@ -0,0 +1,94 @@ +#include "map.h" +#include + +Map::Map() +{ + mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); +} + +Map::Map(const std::string filename) +{ + xmlDocPtr doc = xmlParseFile(filename.c_str()); + if (doc == nullptr) + { + throw MapLoadException(filename); + } + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) + { + throw MapLoadException(filename); + } + + if (xmlStrcmp(top->name, (const xmlChar*) "map-def")) + { + throw MapLoadException(filename); + } + + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) + { + if (!xmlStrcmp(node->name, (const xmlChar*) "name")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + title = (char*) key; + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + mapdata[0] = atoi(strtok((char*) key, ",\n")); + for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) + { + mapdata[i] = atoi(strtok(NULL, ",\n")); + } + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + leftmap = (char*) key; + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + rightmap = (char*) key; + xmlFree(key); + } + } + + xmlFreeDoc(doc); +} + +Map::Map(const Map& map) +{ + mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + + title = map.title; + leftmap = map.leftmap; + rightmap = map.rightmap; +} + +Map::Map(Map&& map) : Map() +{ + swap(*this, map); +} + +Map::~Map() +{ + free(mapdata); +} + +Map& Map::operator= (Map map) +{ + swap(*this, map); + + return *this; +} + +void swap(Map& first, Map& second) +{ + std::swap(first.mapdata, second.mapdata); + std::swap(first.title, second.title); + std::swap(first.leftmap, second.leftmap); + std::swap(first.rightmap, second.rightmap); +} \ No newline at end of file diff --git a/tools/mapedit/src/map.h b/tools/mapedit/src/map.h new file mode 100644 index 0000000..83244f3 --- /dev/null +++ b/tools/mapedit/src/map.h @@ -0,0 +1,44 @@ +#ifndef MAP_H +#define MAP_H + +#include +#include + +const int TILE_WIDTH = 8; +const int TILE_HEIGHT = 8; +const int GAME_WIDTH = 320; +const int GAME_HEIGHT = 200; +const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH; +const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT - 1; + +class MapLoadException: public std::exception +{ + public: + MapLoadException(std::string mapname) : mapname(mapname) {} + + virtual const char* what() const throw() + { + return ("An error occured loading map " + mapname).c_str(); + } + + private: + std::string mapname; +}; + +class Map { + public: + Map(); + Map(const std::string name); + Map(const Map& map); + Map(Map&& map); + ~Map(); + Map& operator= (Map other); + friend void swap(Map& first, Map& second); + + int* mapdata; + std::string title; + std::string leftmap; + std::string rightmap; +}; + +#endif -- cgit 1.4.1