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 --- src/components/map_collision.cpp | 2 +- src/components/map_render.cpp | 2 +- src/game.h | 2 +- src/map.cpp | 8 ++-- tools/mapedit/CMakeLists.txt | 45 +++++++++++++++++++ tools/mapedit/src/main.cpp | 54 +++++++++++++++++++++++ tools/mapedit/src/map.cpp | 94 ++++++++++++++++++++++++++++++++++++++++ tools/mapedit/src/map.h | 44 +++++++++++++++++++ 8 files changed, 244 insertions(+), 7 deletions(-) create mode 100644 tools/mapedit/CMakeLists.txt create mode 100644 tools/mapedit/src/main.cpp create mode 100644 tools/mapedit/src/map.cpp create mode 100644 tools/mapedit/src/map.h diff --git a/src/components/map_collision.cpp b/src/components/map_collision.cpp index f385320..9afa6f8 100644 --- a/src/components/map_collision.cpp +++ b/src/components/map_collision.cpp @@ -7,7 +7,7 @@ MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport); addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport); - for (int i=0; iname, (const xmlChar*) "environment")) { xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); + 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-1)); i++) + for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) { mapdata[i] = atoi(strtok(NULL, ",\n")); } @@ -100,8 +100,8 @@ Map::Map(const std::string name) Map::Map(const Map& map) { - mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); - memcpy(mapdata, map.mapdata, MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); + mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); title = (char*) malloc((MAP_WIDTH+1)*sizeof(char)); strncpy(title, map.title, MAP_WIDTH+1); diff --git a/tools/mapedit/CMakeLists.txt b/tools/mapedit/CMakeLists.txt new file mode 100644 index 0000000..9a024c8 --- /dev/null +++ b/tools/mapedit/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 2.6) +project(AromatherapyMapEditor) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${AromatherapyMapEditor_SOURCE_DIR}/cmake") +# Set an output directory for our binaries +set(BIN_DIR ${AromatherapyMapEditor_SOURCE_DIR}/bin) + +# Bump up warning levels appropriately for clang, gcc & msvc +# Also set debug/optimization flags depending on the build type. IDE users choose this when +# selecting the build mode in their IDE +if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -std=c++11") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} -g") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -O2") +elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC") + if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() +endif() + +# Look up SDL2 and add the include directory to our include path +find_package(wxWidgets REQUIRED core base) +include(${wxWidgets_USE_FILE}) + +find_package(libxml2 REQUIRED) + +set(ALL_LIBS + ${wxWidgets_LIBRARIES} + ${LIBXML2_LIBRARIES} +) + +include_directories( + ${LIBXML2_INCLUDE_DIR} + src +) + +set(CMAKE_BUILD_TYPE Debug) +add_executable(AromatherapyMapEditor + src/main.cpp + src/map.cpp +) +target_link_libraries(AromatherapyMapEditor ${ALL_LIBS}) +install(TARGETS AromatherapyMapEditor RUNTIME DESTINATION ${BIN_DIR}) 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