Skip to content

Commit 360866b

Browse files
RolandRoland
Roland
authored and
Roland
committed
initial commit with first implementations of tmx parser
1 parent 26fd477 commit 360866b

32 files changed

+935
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
*.lai
1212
*.la
1313
*.a
14+
15+
# Build directory
16+
build

CMakeLists.txt

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
3+
project(demo-rpg)
4+
set(target demo-rpg)
5+
6+
IF(NOT MSVC)
7+
ADD_DEFINITIONS(-std=c++0x -Werror=return-type)
8+
ENDIF(NOT MSVC)
9+
10+
11+
find_package(Qt4 COMPONENTS QtCore QtGui REQUIRED)
12+
include(${QT_USE_FILE})
13+
14+
include_directories("include" "lib/tmx/include")
15+
16+
FILE(GLOB_RECURSE headers "include" *.h *.hpp)
17+
18+
set(sources
19+
src/main.cpp
20+
)
21+
22+
#QT4_WRAP_CPP(moc_sources ...)
23+
#QT4_WRAP_UI(ui_headers ...)
24+
25+
add_subdirectory("lib")
26+
27+
add_executable(${target} ${sources} ${headers})
28+
29+
target_link_libraries(${target} tmx ${QT_LIBRARIES})
30+
31+
32+
#copy data
33+
add_custom_command(
34+
TARGET ${target} POST_BUILD
35+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/data ${CMAKE_BINARY_DIR}/data
36+
)

data/ExteriorTest-760306.jpg

121 KB
Loading

data/example.tmx

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32">
3+
<tileset firstgid="1" name="ExteriorTest-760306" tilewidth="32" tileheight="32">
4+
<image source="ExteriorTest-760306.jpg" width="640" height="480"/>
5+
<terraintypes>
6+
<terrain name="myterrain" tile="-1"/>
7+
<terrain name="stone" tile="135"/>
8+
<terrain name="grass" tile="214"/>
9+
</terraintypes>
10+
<tile id="193" terrain=",,,2"/>
11+
<tile id="194" terrain=",,2,2"/>
12+
<tile id="195" terrain=",,2,"/>
13+
<tile id="213" terrain=",2,,2"/>
14+
<tile id="215" terrain="2,,2,"/>
15+
<tile id="233" terrain=",2,,"/>
16+
<tile id="234" terrain="2,2,,"/>
17+
<tile id="235" terrain="2,,,"/>
18+
</tileset>
19+
<layer name="Tile Layer 1" width="100" height="100">
20+
<data encoding="base64" compression="zlib">
21+
eJzt0TENADAMBLGURfmTCp0OBZFX5MH7SddV1QAAAMAa93zTHfiRyI8sfmTxI4sfAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAw6wEpe8dp
22+
</data>
23+
</layer>
24+
<objectgroup name="Object Layer 1" width="100" height="100" visible="0">
25+
<object gid="229" x="904" y="287"/>
26+
<object gid="250" x="712" y="381"/>
27+
<object gid="250" x="967" y="245"/>
28+
<object gid="250" x="959" y="429"/>
29+
<object gid="250" x="1016" y="383"/>
30+
<object gid="250" x="749" y="470"/>
31+
<object gid="250" x="803" y="319"/>
32+
<object name="test" type="mytype" x="442" y="272" width="94" height="79">
33+
<ellipse/>
34+
</object>
35+
<object x="446" y="665" width="147" height="150">
36+
<ellipse/>
37+
</object>
38+
</objectgroup>
39+
</map>

include/main.h

Whitespace-only changes.

lib/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
3+
add_subdirectory("tmx")
4+

lib/tmx/CMakeLists.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
3+
project(tmx)
4+
set(target tmx)
5+
6+
IF(NOT MSVC)
7+
ADD_DEFINITIONS(-std=c++0x -Werror=return-type)
8+
ENDIF(NOT MSVC)
9+
10+
11+
find_package(Qt4 COMPONENTS QtCore QtXml REQUIRED)
12+
include(${QT_USE_FILE})
13+
14+
include_directories("include")
15+
16+
FILE(GLOB_RECURSE headers "include" *.h *.hpp)
17+
18+
set(sources
19+
src/Parser.cpp
20+
src/XmlHandler.cpp
21+
src/Format.cpp
22+
src/Builder.cpp
23+
src/Base.cpp
24+
src/Layer.cpp
25+
src/TileLayer.cpp
26+
src/ObjectLayer.cpp
27+
src/Tile.cpp
28+
src/Map.cpp
29+
src/Tileset.cpp
30+
src/Object.cpp
31+
)
32+
33+
add_library(${target} SHARED ${sources} ${headers})
34+
35+
target_link_libraries(${target} ${QT_LIBRARIES})

lib/tmx/include/tmx/Base.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <QString>
4+
5+
namespace tmx {
6+
7+
class Base
8+
{
9+
public:
10+
void setProperty(const QString& property, const QString& value) {}
11+
protected:
12+
};
13+
14+
} // namespace tmx

lib/tmx/include/tmx/Builder.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <tmx/Format.h>
4+
#include <tmx/Map.h>
5+
#include <tmx/Tileset.h>
6+
7+
#include <QHash>
8+
#include <QStack>
9+
#include <QList>
10+
11+
namespace tmx {
12+
13+
class Builder
14+
{
15+
public:
16+
Builder();
17+
18+
void create(const QString& type);
19+
void finish(const QString& type);
20+
void setAttribute(const QString& name, const QString& value);
21+
22+
Map* map() const;
23+
protected:
24+
QStack<Format::Element> _elementStack;
25+
QStack<Map*> _mapStack;
26+
QStack<Tileset*> _tilesetStack;
27+
28+
QList<Map*> _maps;
29+
30+
Format::Element currentElementType() const;
31+
Map* currentMap();
32+
Tileset* currentTileset();
33+
34+
void setMapAttribute(const QString& name, const QString& value);
35+
void setTilesetAttribute(const QString& name, const QString& value);
36+
void setTileOffsetAttribute(const QString& name, const QString& value);
37+
};
38+
39+
40+
} // namespace tmx

lib/tmx/include/tmx/Format.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <QString>
4+
#include <QHash>
5+
6+
namespace tmx {
7+
8+
class Format
9+
{
10+
public:
11+
enum Element {
12+
UnknownElement,
13+
Map,
14+
Tileset,
15+
Layer,
16+
Tile,
17+
Image,
18+
ObjectGroup,
19+
Object,
20+
Data,
21+
Source,
22+
TileOffset
23+
};
24+
enum Attribute {
25+
UnknownAttribute,
26+
Id,
27+
Gid,
28+
FirstGid,
29+
Name,
30+
Orientation,
31+
Width,
32+
Height,
33+
X,
34+
Y,
35+
TileWidth,
36+
TileHeight,
37+
Encoding,
38+
Compression,
39+
Version,
40+
Spacing,
41+
Margin
42+
};
43+
44+
static Element element(const QString& name);
45+
static Attribute attribute(const QString& name);
46+
protected:
47+
static QHash<QString, Element> _elementMap;
48+
static QHash<QString, Attribute> _attributeMap;
49+
50+
static QHash<QString, Element> createElementMap();
51+
static QHash<QString, Attribute> createAttributeMap();
52+
private:
53+
Format();
54+
};
55+
56+
} // namespace tmx

lib/tmx/include/tmx/Layer.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
namespace tmx {
4+
5+
class Tmx
6+
{
7+
public:
8+
9+
protected:
10+
11+
};
12+
13+
} // namespace tmx

lib/tmx/include/tmx/Map.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#include <tmx/Base.h>
4+
#include <tmx/Tileset.h>
5+
6+
#include <QSize>
7+
#include <QList>
8+
9+
namespace tmx {
10+
11+
class Map : public Base
12+
{
13+
public:
14+
enum Orientation {
15+
Invalid,
16+
Orthogonal,
17+
Isometric,
18+
Staggered
19+
};
20+
21+
Map();
22+
Map(Orientation orientation, const QSize& size, const QSize& tileSize);
23+
24+
~Map();
25+
26+
Orientation orientation() const;
27+
void setOrientation(Orientation orientation);
28+
29+
QSize size() const;
30+
int width() const;
31+
int height() const;
32+
void setSize(const QSize& size);
33+
void setWidth(int width);
34+
void setHeight(int height);
35+
36+
QSize tileSize() const;
37+
int tileWidth() const;
38+
int tileHeight() const;
39+
void setTileSize(const QSize& size);
40+
void setTileWidth(int width);
41+
void setTileHeight(int height);
42+
43+
void addTileset(Tileset* tileset);
44+
45+
QString toString() const;
46+
47+
static Orientation orientationFromString(const QString& name);
48+
protected:
49+
Orientation _orientation;
50+
QSize _size;
51+
QSize _tileSize;
52+
QList<Tileset*> _tilesets;
53+
};
54+
55+
} // namespace tmx

lib/tmx/include/tmx/Object.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
namespace tmx {
4+
5+
class Tmx
6+
{
7+
public:
8+
9+
protected:
10+
11+
};
12+
13+
} // namespace tmx

lib/tmx/include/tmx/ObjectLayer.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
namespace tmx {
4+
5+
class Tmx
6+
{
7+
public:
8+
9+
protected:
10+
11+
};
12+
13+
} // namespace tmx

lib/tmx/include/tmx/Parser.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <QString>
4+
5+
namespace tmx {
6+
7+
class Parser
8+
{
9+
public:
10+
//Parser();
11+
12+
void parseFile(const QString& filename);
13+
14+
protected:
15+
16+
};
17+
18+
} // namespace tmx

lib/tmx/include/tmx/Tile.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
namespace tmx {
4+
5+
class Tmx
6+
{
7+
public:
8+
9+
protected:
10+
11+
};
12+
13+
} // namespace tmx

lib/tmx/include/tmx/TileLayer.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
namespace tmx {
4+
5+
class Tmx
6+
{
7+
public:
8+
9+
protected:
10+
11+
};
12+
13+
} // namespace tmx

0 commit comments

Comments
 (0)