summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-03-14 21:02:01 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-03-14 21:02:01 -0400
commitb563953a4846bab720cae17ef4ab5a8296730c7c (patch)
treee585bda1e4a8b979c8864cd25a84f663122c83b7
parent6b1dcc5df51df4a2d8b724187eb1bcdb4fd9df8b (diff)
downloadtherapy-b563953a4846bab720cae17ef4ab5a8296730c7c.tar.gz
therapy-b563953a4846bab720cae17ef4ab5a8296730c7c.tar.bz2
therapy-b563953a4846bab720cae17ef4ab5a8296730c7c.zip
Started writing map editor
-rw-r--r--src/components/map_collision.cpp2
-rw-r--r--src/components/map_render.cpp2
-rw-r--r--src/game.h2
-rw-r--r--src/map.cpp8
-rw-r--r--tools/mapedit/CMakeLists.txt45
-rw-r--r--tools/mapedit/src/main.cpp54
-rw-r--r--tools/mapedit/src/map.cpp94
-rw-r--r--tools/mapedit/src/map.h44
8 files changed, 244 insertions, 7 deletions
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)
7 addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport); 7 addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport);
8 addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport); 8 addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport);
9 9
10 for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) 10 for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++)
11 { 11 {
12 int x = i % MAP_WIDTH; 12 int x = i % MAP_WIDTH;
13 int y = i / MAP_WIDTH; 13 int y = i / MAP_WIDTH;
diff --git a/src/components/map_render.cpp b/src/components/map_render.cpp index d93afe6..6fdfcc3 100644 --- a/src/components/map_render.cpp +++ b/src/components/map_render.cpp
@@ -8,7 +8,7 @@ MapRenderComponent::MapRenderComponent(const Map& map) : screen(GAME_WIDTH, GAME
8 8
9 Texture tiles("../res/tiles.png"); 9 Texture tiles("../res/tiles.png");
10 10
11 for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) 11 for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++)
12 { 12 {
13 int tile = map.getMapdata()[i]; 13 int tile = map.getMapdata()[i];
14 int x = i % MAP_WIDTH; 14 int x = i % MAP_WIDTH;
diff --git a/src/game.h b/src/game.h index a4620d4..1818cec 100644 --- a/src/game.h +++ b/src/game.h
@@ -14,7 +14,7 @@ const int TILE_HEIGHT = 8;
14const int GAME_WIDTH = 320; 14const int GAME_WIDTH = 320;
15const int GAME_HEIGHT = 200; 15const int GAME_HEIGHT = 200;
16const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH; 16const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH;
17const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT; 17const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT - 1;
18 18
19const int FRAMES_PER_SECOND = 60; 19const int FRAMES_PER_SECOND = 60;
20const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; 20const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND;
diff --git a/src/map.cpp b/src/map.cpp index 73eb2b4..6b83442 100644 --- a/src/map.cpp +++ b/src/map.cpp
@@ -50,9 +50,9 @@ Map::Map(const std::string name)
50 } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) 50 } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment"))
51 { 51 {
52 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); 52 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
53 mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); 53 mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
54 mapdata[0] = atoi(strtok((char*) key, ",\n")); 54 mapdata[0] = atoi(strtok((char*) key, ",\n"));
55 for (int i=1; i<(MAP_WIDTH*(MAP_HEIGHT-1)); i++) 55 for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++)
56 { 56 {
57 mapdata[i] = atoi(strtok(NULL, ",\n")); 57 mapdata[i] = atoi(strtok(NULL, ",\n"));
58 } 58 }
@@ -100,8 +100,8 @@ Map::Map(const std::string name)
100 100
101Map::Map(const Map& map) 101Map::Map(const Map& map)
102{ 102{
103 mapdata = (int*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); 103 mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
104 memcpy(mapdata, map.mapdata, MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(int)); 104 memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int));
105 105
106 title = (char*) malloc((MAP_WIDTH+1)*sizeof(char)); 106 title = (char*) malloc((MAP_WIDTH+1)*sizeof(char));
107 strncpy(title, map.title, MAP_WIDTH+1); 107 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 @@
1cmake_minimum_required(VERSION 2.6)
2project(AromatherapyMapEditor)
3
4set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${AromatherapyMapEditor_SOURCE_DIR}/cmake")
5# Set an output directory for our binaries
6set(BIN_DIR ${AromatherapyMapEditor_SOURCE_DIR}/bin)
7
8# Bump up warning levels appropriately for clang, gcc & msvc
9# Also set debug/optimization flags depending on the build type. IDE users choose this when
10# selecting the build mode in their IDE
11if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
12 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -std=c++11")
13 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} -g")
14 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -O2")
15elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
16 if (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
17 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
18 else()
19 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
20 endif()
21endif()
22
23# Look up SDL2 and add the include directory to our include path
24find_package(wxWidgets REQUIRED core base)
25include(${wxWidgets_USE_FILE})
26
27find_package(libxml2 REQUIRED)
28
29set(ALL_LIBS
30 ${wxWidgets_LIBRARIES}
31 ${LIBXML2_LIBRARIES}
32)
33
34include_directories(
35 ${LIBXML2_INCLUDE_DIR}
36 src
37)
38
39set(CMAKE_BUILD_TYPE Debug)
40add_executable(AromatherapyMapEditor
41 src/main.cpp
42 src/map.cpp
43)
44target_link_libraries(AromatherapyMapEditor ${ALL_LIBS})
45install(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 @@
1#include <wx/wxprec.h>
2
3#ifndef WX_PRECOMP
4#include <wx/wx.h>
5#endif
6
7#include "map.h"
8
9class MapeditApp : public wxApp {
10 public:
11 virtual bool OnInit();
12};
13
14class MapeditFrame : public wxFrame {
15 public:
16 MapeditFrame() : MapeditFrame(Map()) {}
17 MapeditFrame(Map map);
18
19 private:
20 void OnExit(wxCommandEvent& event);
21
22 Map map;
23
24 wxDECLARE_EVENT_TABLE();
25};
26
27wxBEGIN_EVENT_TABLE(MapeditFrame, wxFrame)
28 EVT_MENU(wxID_EXIT, MapeditFrame::OnExit)
29wxEND_EVENT_TABLE()
30
31wxIMPLEMENT_APP(MapeditApp);
32
33bool MapeditApp::OnInit()
34{
35 MapeditFrame* frame = new MapeditFrame();
36 frame->Show(true);
37 return true;
38}
39
40MapeditFrame::MapeditFrame(Map map) : wxFrame(NULL, wxID_ANY, "Map Editor", wxPoint(50, 50), wxSize(450, 340)), map(map)
41{
42 wxMenu* menuFile = new wxMenu;
43 menuFile->Append(wxID_EXIT);
44
45 wxMenuBar* menuBar = new wxMenuBar;
46 menuBar->Append(menuFile, "&File");
47
48 SetMenuBar(menuBar);
49}
50
51void MapeditFrame::OnExit(wxCommandEvent& event)
52{
53 Close(true);
54}
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 @@
1#include "map.h"
2#include <libxml/parser.h>
3
4Map::Map()
5{
6 mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int));
7}
8
9Map::Map(const std::string filename)
10{
11 xmlDocPtr doc = xmlParseFile(filename.c_str());
12 if (doc == nullptr)
13 {
14 throw MapLoadException(filename);
15 }
16
17 xmlNodePtr top = xmlDocGetRootElement(doc);
18 if (top == nullptr)
19 {
20 throw MapLoadException(filename);
21 }
22
23 if (xmlStrcmp(top->name, (const xmlChar*) "map-def"))
24 {
25 throw MapLoadException(filename);
26 }
27
28 for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
29 {
30 if (!xmlStrcmp(node->name, (const xmlChar*) "name"))
31 {
32 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
33 title = (char*) key;
34 xmlFree(key);
35 } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment"))
36 {
37 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
38 mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
39 mapdata[0] = atoi(strtok((char*) key, ",\n"));
40 for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++)
41 {
42 mapdata[i] = atoi(strtok(NULL, ",\n"));
43 }
44 xmlFree(key);
45 } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap"))
46 {
47 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
48 leftmap = (char*) key;
49 xmlFree(key);
50 } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap"))
51 {
52 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
53 rightmap = (char*) key;
54 xmlFree(key);
55 }
56 }
57
58 xmlFreeDoc(doc);
59}
60
61Map::Map(const Map& map)
62{
63 mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int));
64 memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int));
65
66 title = map.title;
67 leftmap = map.leftmap;
68 rightmap = map.rightmap;
69}
70
71Map::Map(Map&& map) : Map()
72{
73 swap(*this, map);
74}
75
76Map::~Map()
77{
78 free(mapdata);
79}
80
81Map& Map::operator= (Map map)
82{
83 swap(*this, map);
84
85 return *this;
86}
87
88void swap(Map& first, Map& second)
89{
90 std::swap(first.mapdata, second.mapdata);
91 std::swap(first.title, second.title);
92 std::swap(first.leftmap, second.leftmap);
93 std::swap(first.rightmap, second.rightmap);
94} \ 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 @@
1#ifndef MAP_H
2#define MAP_H
3
4#include <string>
5#include <exception>
6
7const int TILE_WIDTH = 8;
8const int TILE_HEIGHT = 8;
9const int GAME_WIDTH = 320;
10const int GAME_HEIGHT = 200;
11const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH;
12const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT - 1;
13
14class MapLoadException: public std::exception
15{
16 public:
17 MapLoadException(std::string mapname) : mapname(mapname) {}
18
19 virtual const char* what() const throw()
20 {
21 return ("An error occured loading map " + mapname).c_str();
22 }
23
24 private:
25 std::string mapname;
26};
27
28class Map {
29 public:
30 Map();
31 Map(const std::string name);
32 Map(const Map& map);
33 Map(Map&& map);
34 ~Map();
35 Map& operator= (Map other);
36 friend void swap(Map& first, Map& second);
37
38 int* mapdata;
39 std::string title;
40 std::string leftmap;
41 std::string rightmap;
42};
43
44#endif