1
0
mirror of https://github.com/ScrelliCopter/tmx2gba.git synced 2025-02-21 03:29:25 +11:00

port to tmxlite

This commit is contained in:
2024-03-24 17:53:40 +11:00
parent 7b0979e020
commit 35abaf7121
63 changed files with 24374 additions and 4123 deletions

View File

@@ -0,0 +1,64 @@
/*********************************************************************
(c) Matt Marchant 2016 - 2021
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
//check which platform we're on and create export macros as necessary
#if !defined(TMXLITE_STATIC)
#if defined(_WIN32)
//windows compilers need specific (and different) keywords for export
#define TMXLITE_EXPORT_API __declspec(dllexport)
//for vc compilers we also need to turn off this annoying C4251 warning
#ifdef _MSC_VER
#pragma warning(disable: 4251)
#endif //_MSC_VER
#else //linux, FreeBSD, Mac OS X
#if __GNUC__ >= 4
//gcc 4 has special keywords for showing/hiding symbols,
//the same keyword is used for both importing and exporting
#define TMXLITE_EXPORT_API __attribute__ ((__visibility__ ("default")))
#else
//gcc < 4 has no mechanism to explicitly hide symbols, everything's exported
#define TMXLITE_EXPORT_API
#endif //__GNUC__
#endif //_WIN32
#else
//static build doesn't need import/export macros
#define TMXLITE_EXPORT_API
#endif //TMXLITE_STATIC

View File

@@ -0,0 +1,226 @@
/*********************************************************************
Matt Marchant 2016 - 2021
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
/*********************************************************************
base64_decode
Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*********************************************************************/
#pragma once
#include <tmxlite/detail/Android.hpp>
#include <tmxlite/detail/Log.hpp>
#include <tmxlite/Types.hpp>
#include <string>
#include <sstream>
#include <vector>
#include <functional>
#include <algorithm>
namespace tmx
{
//using inline here just to supress unused warnings on gcc
bool decompress(const char* source, std::vector<unsigned char>& dest, std::size_t inSize, std::size_t expectedSize);
static inline std::string base64_decode(std::string const& encoded_string)
{
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::function<bool(unsigned char)> is_base64 =
[](unsigned char c)->bool
{
return (isalnum(c) || (c == '+') || (c == '/'));
};
auto in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
{
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4)
{
for (i = 0; i < 4; i++)
{
char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
{
ret += char_array_3[i];
}
i = 0;
}
}
if (i)
{
for (j = i; j < 4; j++)
{
char_array_4[j] = 0;
}
for (j = 0; j < 4; j++)
{
char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
{
ret += char_array_3[j];
}
}
return ret;
}
static inline Colour colourFromString(std::string str)
{
//removes preceding #
auto result = str.find_last_of('#');
if (result != std::string::npos)
{
str = str.substr(result + 1);
}
if (str.size() == 6 || str.size() == 8)
{
unsigned int value, r, g, b;
unsigned int a = 255;
std::stringstream input(str);
input >> std::hex >> value;
r = (value >> 16) & 0xff;
g = (value >> 8) & 0xff;
b = value & 0xff;
if (str.size() == 8)
{
a = (value >> 24) & 0xff;
}
return{ std::uint8_t(r), std::uint8_t(g), std::uint8_t(b), std::uint8_t(a) };
}
Logger::log(str + ": not a valid colour string", Logger::Type::Error);
return{};
}
static inline std::string resolveFilePath(std::string path, const std::string& workingDir)
{
static const std::string match("../");
std::size_t result = path.find(match);
std::size_t count = 0;
while (result != std::string::npos)
{
count++;
path = path.substr(result + match.size());
result = path.find(match);
}
if (workingDir.empty()) return path;
std::string outPath = workingDir;
for (auto i = 0u; i < count; ++i)
{
result = outPath.find_last_of('/');
if (result != std::string::npos)
{
outPath = outPath.substr(0, result);
}
}
// this does only work on windows
#ifndef __ANDROID__
return outPath + '/' + path;
#endif
// todo: make resolveFilePath work with subfolders on
// android - currently only the root folder is working
#ifdef __ANDROID__
return path;
#endif
}
static inline std::string getFilePath(const std::string& path)
{
//TODO this doesn't actually check that there is a file at the
//end of the path, or that it's even a valid path...
static auto searchFunc = [](const char separator, const std::string& path)->std::string
{
std::size_t i = path.rfind(separator, path.length());
if (i != std::string::npos)
{
return(path.substr(0, i + 1));
}
return "";
};
#ifdef _WIN32 //try windows formatted paths first
std::string retVal = searchFunc('\\', path);
if (!retVal.empty()) return retVal;
#endif
return searchFunc('/', path);
}
} //namespacec tmx

View File

@@ -0,0 +1,107 @@
/*********************************************************************
Matt Marchant 2016 - 2022
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/Types.hpp>
namespace tmx
{
/*!
\brief Image layers contain a single image which make up that
layer. The parser contains the fully resolved path to the image
relative to the working directory.
*/
class TMXLITE_EXPORT_API ImageLayer final : public Layer
{
public:
explicit ImageLayer(const std::string&);
Type getType() const override { return Layer::Type::Image; }
void parse(const pugi::xml_node&, Map*) override;
/*!
\brief Returns the path, relative to the working directory,
of the image used by the image layer.
*/
const std::string& getImagePath() const { return m_filePath; }
/*!
\brief Returns the colour used by the image to represent transparent
pixels. By default this is (0, 0, 0, 0)
*/
const Colour& getTransparencyColour() const { return m_transparencyColour; }
/*!
\brief Returns true if the image used by this layer specifically states a
colour to use as transparency
*/
bool hasTransparency() const { return m_hasTransparency; }
/*!
\brief Returns the size of the image of the image layer in pixels.
*/
const Vector2u& getImageSize() const { return m_imageSize; }
/*!
\brief Returns true if the image drawn by this layer is repeated along
the X axis.
*/
bool hasRepeatX() const { return m_hasRepeatX; }
/*!
\brief Returns true if the image drawn by this layer is repeated along
the Y axis.
*/
bool hasRepeatY() const { return m_hasRepeatY; }
private:
std::string m_workingDir;
std::string m_filePath;
Colour m_transparencyColour;
bool m_hasTransparency;
Vector2u m_imageSize;
bool m_hasRepeatX;
bool m_hasRepeatY;
};
template <>
inline ImageLayer& Layer::getLayerAs<ImageLayer>()
{
assert(getType() == Type::Image);
return *static_cast<ImageLayer*>(this);
}
template <>
inline const ImageLayer& Layer::getLayerAs<ImageLayer>() const
{
assert(getType() == Type::Image);
return *static_cast<const ImageLayer*>(this);
}
}

View File

@@ -0,0 +1,175 @@
/*********************************************************************
Matt Marchant 2016 - 2023
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Property.hpp>
#include <tmxlite/Types.hpp>
#include <string>
#include <memory>
#include <vector>
namespace pugi
{
class xml_node;
}
namespace tmx
{
class Map;
class TileLayer;
class ObjectGroup;
class ImageLayer;
class LayerGroup;
/*!
\brief Represents a layer of a tmx format tile map.
This is an abstract base class from which all layer
types are derived.
*/
class TMXLITE_EXPORT_API Layer
{
public:
using Ptr = std::unique_ptr<Layer>;
Layer() : m_opacity(1.f), m_visible(true) {};
virtual ~Layer() = default;
/*!
\brief Layer type as returned by getType()
Tile: this layer is a TileLayer type
Object: This layer is an ObjectGroup type
Image: This layer is an ImageLayer type
Group: This layer is a LayerGroup type
*/
enum class Type
{
Tile,
Object,
Image,
Group
};
/*!
\brief Returns a Type value representing the concrete type.
Use this when deciding which conrete layer type to use when
calling the templated function getLayerAs<T>()
*/
virtual Type getType() const = 0;
/*!
\brief Returns the class of the Layer, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }
/*!
\brief Use this to get a reference to the concrete layer type
which this layer points to.
Use getType() to return the type value of this layer and determine
if the concrete type is TileLayer, ObjectGroup, ImageLayer, or LayerGroup
*/
template <typename T>
T& getLayerAs();
template <typename T>
const T& getLayerAs() const;
/*!
\brief Attempts to parse the specific node layer type
*/
virtual void parse(const pugi::xml_node&, Map* = nullptr) = 0;
/*!
\brief Returns the name of the layer
*/
const std::string& getName() const { return m_name; }
/*!
\brief Returns the opacity value for the layer
*/
float getOpacity() const { return m_opacity; }
/*!
\brief Returns whether this layer is visible or not
*/
bool getVisible() const { return m_visible; }
/*!
\brief Returns the offset from the top left corner
of the layer, in pixels
*/
const Vector2i& getOffset() const { return m_offset; }
/*!
\brief Returns the parallax factor
*/
const Vector2f& getParallaxFactor() const { return m_parallaxFactor; }
/*!
\brief Returns the tint colour of the layer.
Defaults to 0xFFFFFFFF - pure white
*/
Colour getTintColour() const { return m_tintColour; }
/*!
\brief Returns the size of the layer, in pixels.
This will be the same as the map size for fixed size maps.
*/
const Vector2u& getSize() const { return m_size; }
/*!
\brief Returns the list of properties of this layer
*/
const std::vector<Property>& getProperties() const { return m_properties; }
protected:
void setName(const std::string& name) { m_name = name; }
void setClass(const std::string& cls) { m_class = cls; }
void setOpacity(float opacity) { m_opacity = opacity; }
void setVisible(bool visible) { m_visible = visible; }
void setOffset(std::int32_t x, std::int32_t y) { m_offset = Vector2i(x, y); }
void setParallaxFactor(float x, float y) { m_parallaxFactor.x = x; m_parallaxFactor.y = y; }
void setTintColour(Colour c) { m_tintColour = c; }
void setSize(std::uint32_t width, std::uint32_t height) { m_size = Vector2u(width, height); }
void addProperty(const pugi::xml_node& node) { m_properties.emplace_back(); m_properties.back().parse(node); }
private:
std::string m_name;
std::string m_class;
float m_opacity;
bool m_visible;
Vector2i m_offset;
Vector2f m_parallaxFactor;
Colour m_tintColour = { 255,255,255,255 };
Vector2u m_size;
std::vector<Property> m_properties;
};
}

View File

@@ -0,0 +1,86 @@
/*********************************************************************
Grant Gangi 2019 - 2022
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/Types.hpp>
#include <vector>
namespace tmx
{
/*!
\brief Layer groups are used to organize the layers of
the map in a hierarchy. They can contain all other layer
types including more layer groups to further nest layers.
*/
class TMXLITE_EXPORT_API LayerGroup final : public Layer
{
public:
LayerGroup(const std::string& workDir, const Vector2u& tileCount);
~LayerGroup() = default;
LayerGroup(const LayerGroup&) = delete;
const LayerGroup& operator = (const LayerGroup&) = delete;
LayerGroup(LayerGroup&&) = default;
LayerGroup& operator = (LayerGroup&&) = default;
Type getType() const override { return Layer::Type::Group; }
void parse(const pugi::xml_node&, Map*) override;
/*!
\brief Returns a reference to the vector containing the layer data.
Layers are pointer-to-baseclass, the concrete type of which can be
found via Layer::getType()
\see Layer
*/
const std::vector<Layer::Ptr>& getLayers() const { return m_layers; }
private:
std::vector<Layer::Ptr> m_layers;
std::string m_workingDir;
Vector2u m_tileCount;
};
template <>
inline LayerGroup& Layer::getLayerAs<LayerGroup>()
{
assert(getType() == Type::Group);
return *static_cast<LayerGroup*>(this);
}
template <>
inline const LayerGroup& Layer::getLayerAs<LayerGroup>() const
{
assert(getType() == Type::Group);
return *static_cast<const LayerGroup*>(this);
}
}

View File

@@ -0,0 +1,282 @@
/*********************************************************************
Matt Marchant 2016 -2021
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Tileset.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/Property.hpp>
#include <tmxlite/Types.hpp>
#include <tmxlite/Object.hpp>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
namespace tmx
{
/*!
\brief Holds the xml version of the loaded map
*/
struct TMXLITE_EXPORT_API Version
{
//major/minor are apparently reserved by gcc
std::uint16_t upper;
std::uint16_t lower;
Version(std::uint16_t maj = 0, std::uint16_t min = 0)
: upper(maj), lower(min) {}
};
enum class Orientation
{
Orthogonal,
Isometric,
Staggered,
Hexagonal,
None
};
enum class RenderOrder
{
RightDown,
RightUp,
LeftDown,
LeftUp,
None
};
enum class StaggerAxis
{
X, Y, None
};
enum class StaggerIndex
{
Even, Odd, None
};
/*!
\brief Parser for TMX format tile maps.
This class can be used to parse the XML format tile maps created
with the Tiled map editor, providing an interface to create drawable and
physics objects. Typical usage would be to create an instance of this
class before calling load() providing a path to the *.tmx file to be
loaded. Then layers or objects can be requested from the Map class
to be interpreted as needed.
\see https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#map
*/
class TMXLITE_EXPORT_API Map final
{
public:
Map();
~Map() = default;
Map(const Map&) = delete;
Map& operator = (const Map&) = delete;
Map(Map&&) = default;
Map& operator = (Map&&) = default;
/*!
\brief Attempts to parse the tilemap at the given location.
\param std::string Path to map file to try to parse
\returns true if map was parsed successfully else returns false.
In debug mode this will attempt to log any errors to the console.
*/
bool load(const std::string&);
/*!
\brief Loads a map from a document stored in a string
\param data A std::string containing the map data to load
\param workingDir A std::string containing the working directory
in which to find assets such as tile sets or images
\returns true if successful, else false
*/
bool loadFromString(const std::string& data, const std::string& workingDir);
/*!
\brief Returns the version of the tile map last parsed.
If no tile map has yet been parsed the version will read 0, 0
*/
const Version& getVersion() const { return m_version; }
/*!
\brief Returns the orientation of the map if one is loaded,
else returns None
*/
Orientation getOrientation() const { return m_orientation; }
/*!
\brief Returns the RenderOrder of the map if one is loaded,
else returns None
*/
RenderOrder getRenderOrder() const { return m_renderOrder; }
/*!
\brief Returns the tile count of the map in the X and Y directions
*/
const Vector2u& getTileCount() const { return m_tileCount; }
/*!
\brief Returns the size of the tile grid in this map.
Actual tile sizes may vary and will be extended / shrunk about
the bottom left corner of the tile.
*/
const Vector2u& getTileSize() const { return m_tileSize; }
/*!
\brief Returns the bounds of the map
*/
FloatRect getBounds() const { return FloatRect(0.f, 0.f, static_cast<float>(m_tileCount.x * m_tileSize.x), static_cast<float>(m_tileCount.y * m_tileSize.y)); }
/*!
\brief Returns the length of an edge of a tile if a Hexagonal
map is loaded.
The length returned is in pixels of the straight edge running
along the axis returned by getStaggerAxis(). If no map is loaded
or the loaded map is not of Hexagonal orientation this function
returns 0.f
*/
float getHexSideLength() const { return m_hexSideLength; }
/*!
\brief Stagger axis of the map.
If either a Staggered or Hexagonal tile map is loaded this returns
which axis the map is staggered along, else returns None.
*/
StaggerAxis getStaggerAxis() const { return m_staggerAxis; }
/*!
\brief Stagger Index of the loaded map.
If a Staggered or Hexagonal map is loaded this returns whether
the even or odd rows of tiles are staggered, otherwise it returns None.
*/
StaggerIndex getStaggerIndex() const { return m_staggerIndex; }
/*!
\brief Returns the background colour of the map.
*/
const Colour& getBackgroundColour() const { return m_backgroundColour; }
/*!
\brief Returns a reference to the vector of tile sets used by the map
*/
const std::vector<Tileset>& getTilesets() const { return m_tilesets; }
/*!
\brief Returns a reference to the vector containing the layer data.
Layers are pointer-to-baseclass, the concrete type of which can be
found via Layer::getType()
\see Layer
*/
const std::vector<Layer::Ptr>& getLayers() const { return m_layers; }
/*!
\brief Returns the class of the Map, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }
/*!
\brief Returns a vector of Property objects loaded by the map
*/
const std::vector<Property>& getProperties() const { return m_properties; }
/*!
\brief Returns a Hashmap of all animated tiles accessible by TileID
*/
const std::map<std::uint32_t, Tileset::Tile>& getAnimatedTiles() const { return m_animTiles; }
/*!
\brief Returns the current working directory of the map. Images and
other resources are loaded relative to this.
*/
const std::string& getWorkingDirectory() const { return m_workingDirectory; }
/*!
\brief Returns an unordered_map of template objects indexed by file name
*/
std::unordered_map<std::string, Object>& getTemplateObjects() { return m_templateObjects; }
const std::unordered_map<std::string, Object>& getTemplateObjects() const { return m_templateObjects; }
/*!
\brief Returns an unordered_map of tilesets used by templated objects.
If Object::getTilesetName() is not empty it can be used to retreive a tileset
from this map. Otherwise the object's tileset can be found from in the map's
global tilesets returned by getTilesets().
*/
std::unordered_map<std::string, Tileset>& getTemplateTilesets() { return m_templateTilesets; }
const std::unordered_map<std::string, Tileset>& getTemplateTilesets() const { return m_templateTilesets; }
/*!
\brief Returns true if this is in infinite tile map.
Infinite maps store their tile data in for tile layers in chunks. If
this is an infinite map use TileLayer::getChunks() to get tile IDs
rather than TileLayer::getTiles().
\see TileLayer
*/
bool isInfinite() const { return m_infinite; }
/*
\brief Returns the origin of each layer's parallax offset value
*/
Vector2f getParallaxOrigin() const { return m_parallaxOrigin; }
private:
Version m_version;
std::string m_class;
Orientation m_orientation;
RenderOrder m_renderOrder;
bool m_infinite;
Vector2u m_tileCount;
Vector2u m_tileSize;
float m_hexSideLength;
StaggerAxis m_staggerAxis;
StaggerIndex m_staggerIndex;
Vector2f m_parallaxOrigin;
Colour m_backgroundColour;
std::string m_workingDirectory;
std::vector<Tileset> m_tilesets;
std::vector<Layer::Ptr> m_layers;
std::vector<Property> m_properties;
std::map<std::uint32_t, Tileset::Tile> m_animTiles;
std::unordered_map<std::string, Object> m_templateObjects;
std::unordered_map<std::string, Tileset> m_templateTilesets;
bool parseMapNode(const pugi::xml_node&);
//always returns false so we can return this
//on load failure
bool reset();
};
}

View File

@@ -0,0 +1,221 @@
/*********************************************************************
(c) Matt Marchant 2016 - 2021
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Property.hpp>
#include <tmxlite/Types.hpp>
#include <string>
#include <vector>
namespace pugi
{
class xml_node;
}
namespace tmx
{
class Map;
/*!
\brief Contains the text information stored in a Text object.
*/
struct TMXLITE_EXPORT_API Text final
{
std::string fontFamily;
std::uint32_t pixelSize = 16; //!< pixels, not points
bool wrap = false;
Colour colour;
bool bold = false;
bool italic = false;
bool underline = false;
bool strikethough = false;
bool kerning = true;
enum class HAlign
{
Left, Centre, Right
}hAlign = HAlign::Left;
enum class VAlign
{
Top, Centre, Bottom
}vAlign = VAlign::Top;
std::string content; //!< actual string content
};
/*!
\brief Objects are stored in ObjectGroup layers.
Objects may be rectangular, elliptical, polygonal or
a polyline. Rectangular and elliptical Objects have their
size determined via the AABB, whereas polygon and polyline
shapes are defined by a list of points. Objects are
rectangular by default. Since version 1.0 Objects also
support Text nodes.
*/
class TMXLITE_EXPORT_API Object final
{
public:
enum class Shape
{
Rectangle,
Ellipse,
Point,
Polygon,
Polyline,
Text
};
Object();
/*!
\brief Attempts to parse the given xml node and
read the Object properties if it is valid.
*/
void parse(const pugi::xml_node&, Map*);
/*!
\brief Returns the unique ID of the Object
*/
std::uint32_t getUID() const { return m_UID; }
/*!
\brief Returns the name of the Object
*/
const std::string& getName() const { return m_name; }
/*!
\brief Returns the type (equal to class) of the Object, as defined in the editor Tiled < 1.9
*/
const std::string& getType() const { return m_class; }
/*!
\brief Returns the class (equal to type) of the Object, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }
/*!
\brief Returns the position of the Object in pixels
*/
const Vector2f& getPosition() const { return m_position; }
/*!
\brief Returns the global Axis Aligned Bounding Box.
The AABB is positioned via the left and top properties, and
define the Object's width and height. This can be used to derive
the shape of the Object if it is rectangular or elliptical.
*/
const FloatRect& getAABB() const { return m_AABB; }
/*!
\brief Returns the rotation of the Object in degrees clockwise
*/
float getRotation() const { return m_rotation; }
/*!
\brief Returns the global tile ID associated with the Object
if there is one. This is used to draw the Object (and therefore
the Object must be rectangular)
*/
std::uint32_t getTileID() const { return m_tileID; }
/*!
\brief Returns the flip flags if the objects uses a TileID to
draw it.
Returns 0 otherwise.
*/
std::uint8_t getFlipFlags() const { return m_flipFlags; }
/*!
\brief Returns whether or not the Object is visible
*/
bool visible() const { return m_visible; }
/*!
\brief Returns the Shape type of the Object
*/
Shape getShape() const { return m_shape; }
/*!
\brief Returns a reference to the vector of points which
make up the Object. If the Object is rectangular or elliptical
then the vector will be empty. Point coordinates are in pixels,
relative to the object position.
*/
const std::vector<Vector2f>& getPoints() const { return m_points; }
/*!
\brief Returns a reference to the vector of properties belonging to
the Object.
*/
const std::vector<Property>& getProperties() const { return m_properties; }
/*!
\brief Returns a Text struct containing information about any text
this object may have, such as font data and formatting.
If an object does not contain any text information this struct will
be populated with default values. Use getShape() to determine
if this object is in fact a text object.
*/
const Text& getText() const { return m_textData; }
Text& getText() { return m_textData; }
/*!
\brief Returns the tileset name used by this object if it is derived
from a template, else returns an empty string.
If the string is not empty use it to index the unordered_map returned
by Map::getTemplateTilesets()
*/
const std::string& getTilesetName() const { return m_tilesetName; }
private:
std::uint32_t m_UID;
std::string m_name;
std::string m_class;
Vector2f m_position;
FloatRect m_AABB;
float m_rotation;
std::uint32_t m_tileID;
std::uint8_t m_flipFlags;
bool m_visible;
Shape m_shape;
std::vector<Vector2f> m_points;
std::vector<Property> m_properties;
Text m_textData;
std::string m_tilesetName;
void parsePoints(const pugi::xml_node&);
void parseText(const pugi::xml_node&);
void parseTemplate(const std::string&, Map*);
};
}

View File

@@ -0,0 +1,99 @@
/*********************************************************************
Matt Marchant 2016 - 2022
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/Object.hpp>
#include <vector>
namespace tmx
{
/*!
\brief ObjectGroup layers contain a series of Objects
which may be made up of shapes or images.
*/
class TMXLITE_EXPORT_API ObjectGroup final : public Layer
{
public:
enum class DrawOrder
{
Index, //< objects should be drawn in the order in which they appear
TopDown //< objects should be drawn sorted by their Y position
};
ObjectGroup();
Type getType() const override { return Layer::Type::Object; }
void parse(const pugi::xml_node&, Map*) override;
/*!
\brief Returns the colour associated with this layer
*/
const Colour& getColour() const { return m_colour; }
/*!
\brief Returns the DrawOrder for the objects in this group.
Defaults to TopDown, where Objects are drawn sorted by Y position
*/
DrawOrder getDrawOrder() const { return m_drawOrder; }
/*!
\brief Returns a reference to the vector of properties for
the ObjectGroup
*/
const std::vector<Property>& getProperties() const { return m_properties; }
/*!
\brief Returns a reference to the vector of Objects which belong to the group
*/
const std::vector<Object>& getObjects() const { return m_objects; }
private:
Colour m_colour;
DrawOrder m_drawOrder;
std::vector<Property> m_properties;
std::vector<Object> m_objects;
};
template <>
inline ObjectGroup& Layer::getLayerAs<ObjectGroup>()
{
assert(getType() == Type::Object);
return *static_cast<ObjectGroup*>(this);
}
template <>
inline const ObjectGroup& Layer::getLayerAs<ObjectGroup>() const
{
assert(getType() == Type::Object);
return *static_cast<const ObjectGroup*>(this);
}
}

View File

@@ -0,0 +1,86 @@
/*********************************************************************
Raphaël Frantz 2021
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Property.hpp>
#include <string>
#include <vector>
namespace tmx
{
/*!
\brief Parser for Tiled object types export format.
Link to the specification: https://doc.mapeditor.org/fr/latest/manual/custom-properties/#predefining-properties.
*/
class TMXLITE_EXPORT_API ObjectTypes final
{
public:
/*!
\brief Types that stores all predefined properties for all objects of this type.
To take less spaces, they are not exported by default into maps.
*/
struct Type
{
std::string name;
Colour colour;
std::vector<Property> properties;
};
/*!
\brief Attempts to parse the object types at the given location.
\param std::string Path to object types file to try to parse
\returns true if object types was parsed successfully else returns false.
In debug mode this will attempt to log any errors to the console.
*/
bool load(const std::string&);
/*!
\brief Loads an object types from a document stored in a string
\param data A std::string containing the object types to load
\param workingDir A std::string containing the working directory
in which to find files.
\returns true if successful, else false
*/
bool loadFromString(const std::string& data, const std::string& workingDir);
/*!
\brief Returns all predefined types and their default values.
*/
const std::vector<Type>& getTypes() const { return m_types; }
private:
std::string m_workingDirectory;
std::vector<Type> m_types;
bool parseObjectTypesNode(const pugi::xml_node&);
//always returns false so we can return this
//on load failure
bool reset();
};
}

View File

@@ -0,0 +1,144 @@
/*********************************************************************
Matt Marchant 2016 - 2021
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Types.hpp>
#include <string>
#include <cassert>
namespace pugi
{
class xml_node;
}
namespace tmx
{
/*!
\brief Represents a custom property.
Tiles, objects and layers of a tmx map may have custom
properties assigned to them. This class represents a
single property and provides access to its value, the
type of which can be determined with getType()
*/
class TMXLITE_EXPORT_API Property final
{
public:
enum class Type
{
Boolean,
Float,
Int,
String,
Colour,
File,
Object,
Undef
};
Property();
static Property fromBoolean(bool value);
static Property fromFloat(float value);
static Property fromInt(int value);
static Property fromString(const std::string& value);
static Property fromColour(const Colour& value);
static Property fromFile(const std::string& value);
static Property fromObject(int value);
/*!
\brief Attempts to parse the given node as a property
\param isObjectTypes Set to true if the parsing is done from an object types files.
*/
void parse(const pugi::xml_node&, bool isObjectTypes = false);
/*!
\brief Returns the type of data stored in the property.
This should generally be called first before trying to
read the proprty value, as reading the incorrect type
will lead to undefined behaviour.
*/
Type getType() const { return m_type; }
/*!
\brief Returns the name of this property
*/
const std::string& getName() const { return m_name; }
/*!
\brief Returns the property's value as a boolean
*/
bool getBoolValue() const { assert(m_type == Type::Boolean); return m_boolValue; }
/*!
\brief Returns the property's value as a float
*/
float getFloatValue() const { assert(m_type == Type::Float); return m_floatValue; }
/*!
\brief Returns the property's value as an integer
*/
int getIntValue() const { assert(m_type == Type::Int || m_type == Type::Object); return m_intValue; }
/*!
\brief Returns the property's value as a string
*/
const std::string& getStringValue() const { assert(m_type == Type::String); return m_stringValue; }
/*!
\brief Returns the property's value as a Colour struct
*/
const Colour& getColourValue() const { assert(m_type == Type::Colour); return m_colourValue; }
/*!
\brief Returns the file path property as a string, relative to the map file
*/
const std::string& getFileValue() const { assert(m_type == Type::File); return m_stringValue; }
/*!
\brief Returns the property's value as an integer object handle
*/
int getObjectValue() const { assert(m_type == Type::Object); return m_intValue; }
private:
union
{
bool m_boolValue;
float m_floatValue;
int m_intValue;
};
std::string m_stringValue;
std::string m_name;
Colour m_colourValue;
Type m_type;
};
}

View File

@@ -0,0 +1,116 @@
/*********************************************************************
Matt Marchant 2016 - 2022
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Layer.hpp>
#include <tmxlite/Types.hpp>
namespace tmx
{
/*!
\brief A layer made up from a series of tile sets
*/
class TMXLITE_EXPORT_API TileLayer final : public Layer
{
public:
/*!
\brief Tile information for a layer
*/
struct Tile final
{
std::uint32_t ID = 0; //!< Global ID of the tile
std::uint8_t flipFlags = 0; //!< Flags marking if the tile should be flipped when drawn
};
/*!
\brief Represents a chunk of tile data, if this is an infinite map
*/
struct Chunk final
{
Vector2i position; //<! coordinate in tiles, not pixels
Vector2i size; //!< size in tiles, not pixels
std::vector<Tile> tiles;
};
/*!
\brief Flags used to tell if a tile is flipped when drawn
*/
enum FlipFlag
{
Horizontal = 0x8,
Vertical = 0x4,
Diagonal = 0x2
};
explicit TileLayer(std::size_t);
Type getType() const override { return Layer::Type::Tile; }
void parse(const pugi::xml_node&, Map*) override;
/*!
\brief Returns the list of tiles used to make up the layer
If this is empty then the map is most likely infinite, in
which case the tile data is stored in chunks.
\see getChunks()
*/
const std::vector<Tile>& getTiles() const { return m_tiles; }
/*!
\brief Returns a vector of chunks which make up this layer
if the map is set to infinite. This will be empty if the map
is not infinite.
\see getTiles()
*/
const std::vector<Chunk>& getChunks() const { return m_chunks; }
private:
std::vector<Tile> m_tiles;
std::vector<Chunk> m_chunks;
std::size_t m_tileCount;
void parseBase64(const pugi::xml_node&);
void parseCSV(const pugi::xml_node&);
void parseUnencoded(const pugi::xml_node&);
void createTiles(const std::vector<std::uint32_t>&, std::vector<Tile>& destination);
};
template <>
inline TileLayer& Layer::getLayerAs<TileLayer>()
{
assert(getType() == Type::Tile);
return *static_cast<TileLayer*>(this);
}
template <>
inline const TileLayer& Layer::getLayerAs<TileLayer>() const
{
assert(getType() == Type::Tile);
return *static_cast<const TileLayer*>(this);
}
}

View File

@@ -0,0 +1,296 @@
/*********************************************************************
Matt Marchant 2016 - 2023
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <tmxlite/Property.hpp>
#include <tmxlite/ObjectGroup.hpp>
#include <string>
#include <vector>
#include <array>
namespace pugi
{
class xml_node;
}
namespace tmx
{
class Map;
/*!
\brief Represents a Tileset node as loaded
from a *.tmx format tile map via the tmx::Map
class.
*/
class TMXLITE_EXPORT_API Tileset final
{
public:
explicit Tileset(const std::string& workingDir);
/*!
\brief Any tiles within a tile set which have special
data associated with them such as animation or terrain
information will have one of these stored in the tile set.
*/
struct Tile final
{
std::uint32_t ID = 0;
std::array<std::int32_t, 4u> terrainIndices{};
std::uint32_t probability = 100;
/*!
\brief a group of frames which make up an animation
*/
struct Animation final
{
/*!
\brief A frame within an animation
*/
struct Frame final
{
std::uint32_t tileID = 0;
std::uint32_t duration = 0;
bool operator == (const Frame& other) const
{
return (this == &other) ||
(tileID == other.tileID && duration == other.duration);
}
bool operator != (const Frame& other) const
{
return !(*this == other);
}
};
std::vector<Frame> frames;
}animation;
std::vector<Property> properties;
ObjectGroup objectGroup;
std::string imagePath;
Vector2u imageSize;
/*!
\brief The position of the tile within the image.
*/
Vector2u imagePosition;
std::string className;
};
/*!
\brief Terrain information with which one
or more tiles may be associated.
*/
struct Terrain final
{
std::string name;
std::uint32_t tileID = -1;
std::vector<Property> properties;
};
/*!
\brief Declares the alignment of tile Objects
*/
enum class ObjectAlignment
{
Unspecified,
TopLeft,
Top,
TopRight,
Left,
Center,
Right,
BottomLeft,
Bottom,
BottomRight
};
/*!
\brief Attempts to parse the given xml node.
If node parsing fails an error is printed in the console
and the Tileset remains in an uninitialised state.
*/
void parse(pugi::xml_node, Map*);
/*!
\brief Returns the first GID of this tile set.
This the ID of the first tile in the tile set, so that
each tile set guarantees a unique set of IDs
*/
std::uint32_t getFirstGID() const { return m_firstGID; }
/*!
\brief Returns the last GID of this tile set.
This is the ID of the last tile in the tile set.
*/
std::uint32_t getLastGID() const;
/*!
\brief Returns the name of this tile set.
*/
const std::string& getName() const { return m_name; }
/*!
\brief Returns the class of the Tileset, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }
/*!
\brief Returns the width and height of a tile in the
tile set, in pixels.
*/
const Vector2u& getTileSize() const { return m_tileSize; }
/*!
\brief Returns the spacing, in pixels, between each tile in the set
*/
std::uint32_t getSpacing() const { return m_spacing; }
/*!
\brief Returns the margin, in pixels, around each tile in the set
*/
std::uint32_t getMargin() const { return m_margin; }
/*!
\brief Returns the number of tiles in the tile set
*/
std::uint32_t getTileCount() const { return m_tileCount; }
/*!
\brief Returns the number of columns which make up the tile set.
This is used when rendering collection of images sets
*/
std::uint32_t getColumnCount() const { return m_columnCount; }
/*!
\brief Returns the alignment of tile objects.
The default value is ObjectAlignment::Unspecified for compatibility.
When the alignment is Unspecified tile objects use BottomLeft in
orthogonal mode and Bottom in isometric mode.
\see ObjectAlignment
*/
ObjectAlignment getObjectAlignment() const { return m_objectAlignment; }
/*!
\brief Returns the tile offset in pixels.
Tile will draw tiles offset from the top left using this value.
*/
const Vector2u& getTileOffset() const { return m_tileOffset; }
/*!
\brief Returns a reference to the list of Property objects for this
tile set
*/
const std::vector<Property>& getProperties() const { return m_properties; }
/*!
\brief Returns the file path to the tile set image, relative to the
working directory. Use this to load the texture required by whichever
method you choose to render the map.
*/
const std::string& getImagePath() const { return m_imagePath; }
/*!
\brief Returns the size of the tile set image in pixels.
*/
const Vector2u& getImageSize() const { return m_imageSize; }
/*!
\brief Returns the colour used by the tile map image to represent transparency.
By default this is a transparent colour (0, 0, 0, 0)
*/
const Colour& getTransparencyColour() const { return m_transparencyColour; }
/*!
\brief Returns true if the image used by this tileset specifically requests
a colour to use as transparency.
*/
bool hasTransparency() const { return m_hasTransparency; }
/*!
\brief Returns a vector of Terrain types associated with one
or more tiles within this tile set
*/
const std::vector<Terrain>& getTerrainTypes() const { return m_terrainTypes; }
/*!
\brief Returns a reference to the vector of tile data used by
tiles which make up this tile set.
*/
const std::vector<Tile>& getTiles() const { return m_tiles; }
/*!
\brief Checks if a tiled ID is in the range of the first ID and the last ID
\param id Tile ID
\return
*/
bool hasTile(std::uint32_t id) const { return id >= m_firstGID && id <= getLastGID(); };
/*!
\brief queries tiles and returns a tile with the given ID. Checks if the TileID is part of the Tileset with `hasTile(id)`
\param id Tile ID. The Tile ID will be corrected internally.
\return In case of a success it returns the correct tile. In terms of failure it will return a nullptr.
*/
const Tile* getTile(std::uint32_t id) const;
private:
std::string m_workingDir;
std::uint32_t m_firstGID;
std::string m_source;
std::string m_name;
std::string m_class;
Vector2u m_tileSize;
std::uint32_t m_spacing;
std::uint32_t m_margin;
std::uint32_t m_tileCount;
std::uint32_t m_columnCount;
ObjectAlignment m_objectAlignment;
Vector2u m_tileOffset;
std::vector<Property> m_properties;
std::string m_imagePath;
Vector2u m_imageSize;
Colour m_transparencyColour;
bool m_hasTransparency;
std::vector<Terrain> m_terrainTypes;
std::vector<std::uint32_t> m_tileIndex;
std::vector<Tile> m_tiles;
void reset();
void parseOffsetNode(const pugi::xml_node&);
void parsePropertyNode(const pugi::xml_node&);
void parseTerrainNode(const pugi::xml_node&);
Tile& newTile(std::uint32_t ID);
void parseTileNode(const pugi::xml_node&, Map*);
void createMissingTile(std::uint32_t ID);
};
}

View File

@@ -0,0 +1,150 @@
/*********************************************************************
Matt Marchant 2016 - 2023
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#pragma once
#include <tmxlite/Config.hpp>
#include <cstdint>
#include <ostream>
namespace tmx
{
/*!
\brief Two dimensional vector used to store points and positions
*/
template <class T>
struct Vector2 final
{
Vector2() : x(0), y(0) {}
Vector2(T x, T y) :x(x), y(y) {}
T x, y;
};
using Vector2f = Vector2<float>;
using Vector2i = Vector2<int>;
using Vector2u = Vector2<unsigned>;
template <typename T>
Vector2<T> operator + (const Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T>& operator += (Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T> operator - (const Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T>& operator -= (Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T> operator * (const Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T>& operator *= (Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T> operator * (const Vector2<T>& l, T r);
template <typename T>
Vector2<T>& operator *= (Vector2<T>& l, T r);
template <typename T>
Vector2<T> operator / (const Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T>& operator /= (Vector2<T>& l, const Vector2<T>& r);
template <typename T>
Vector2<T> operator / (const Vector2<T>& l, T r);
template <typename T>
Vector2<T>& operator /= (Vector2<T>& l, T r);
#include "Types.inl"
/*!
\brief Describes a rectangular area, such as an AABB (axis aligned bounding box)
*/
template <class T>
struct Rectangle final
{
Rectangle() : left(0), top(0), width(0), height(0) {}
Rectangle(T l, T t, T w, T h) : left(l), top(t), width(w), height(h) {}
Rectangle(Vector2<T> position, Vector2<T> size) : left(position.x), top(position.y), width(size.x), height(size.y) {}
T left, top, width, height;
};
using FloatRect = Rectangle<float>;
using IntRect = Rectangle<int>;
/*!
\brief Contains the red, green, blue and alpha values of a colour
in the range 0 - 255.
*/
struct TMXLITE_EXPORT_API Colour final
{
Colour(std::uint8_t red = 0, std::uint8_t green = 0, std::uint8_t blue = 0, std::uint8_t alpha = 255)
: r(red), g(green), b(blue), a(alpha) {}
std::uint8_t r, g, b, a;
bool operator == (const Colour& other)
{
return other.r == r
&& other.g == g
&& other.b == b
&& other.a == a;
}
bool operator != (const Colour& other)
{
return !(*this == other);
}
explicit operator std::uint32_t() const
{
return (r << 24) | (g << 16) | (b << 8) | a;
}
};
}
template <typename T>
std::ostream& operator << (std::ostream& os, const tmx::Vector2<T>& t)
{
os << "{" << t.x << ", " << t.y << "}";
return os;
}
template <typename T>
std::ostream& operator << (std::ostream& os, const tmx::Rectangle<T>& t)
{
os << "{" << t.left << ", " << t.top << ", " << t.width << ", " << t.height << "}";
return os;
}
std::ostream& operator << (std::ostream& os, const tmx::Colour& c);

View File

@@ -0,0 +1,110 @@
/*********************************************************************
Matt Marchant 2016 - 2023
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
template <typename T>
Vector2<T> operator + (const Vector2<T>& l, const Vector2<T>& r)
{
return { l.x + r.x, l.y + r.y };
}
template <typename T>
Vector2<T>& operator += (Vector2<T>& l, const Vector2<T>& r)
{
l.x += r.x;
l.y += r.y;
return l;
}
template <typename T>
Vector2<T> operator - (const Vector2<T>& l, const Vector2<T>& r)
{
return { l.x - r.x, l.y - r.y };
}
template <typename T>
Vector2<T>& operator -= (Vector2<T>& l, const Vector2<T>& r)
{
l.x -= r.x;
l.y -= r.y;
return l;
}
template <typename T>
Vector2<T> operator * (const Vector2<T>& l, const Vector2<T>& r)
{
return { l.x * r.x, l.y * r.y };
}
template <typename T>
Vector2<T>& operator *= (Vector2<T>& l, const Vector2<T>& r)
{
l.x *= r.x;
l.y *= r.y;
return l;
}
template <typename T>
Vector2<T> operator * (const Vector2<T>& l, T r)
{
return { l.x * r, l.y * r };
}
template <typename T>
Vector2<T>& operator *= (Vector2<T>& l, T r)
{
l.x *= r;
l.y *= r;
return l;
}
template <typename T>
Vector2<T> operator / (const Vector2<T>& l, const Vector2<T>& r)
{
return { l.x / r.x, l.y / r.y };
}
template <typename T>
Vector2<T>& operator /= (Vector2<T>& l, const Vector2<T>& r)
{
l.x /= r.x;
l.y /= r.y;
return l;
}
template <typename T>
Vector2<T> operator / (const Vector2<T>& l, T r)
{
return { l.x / r, l.y / r };
}
template <typename T>
Vector2<T>& operator /= (Vector2<T>& l, T r)
{
l.x /= r;
l.y /= r;
return l;
}

View File

@@ -0,0 +1,53 @@
/*********************************************************************
Matt Marchant 2016
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
#ifndef ANDROID_INC_HPP_
#define ANDROID_INC_HPP_
#ifdef __ANDROID__
#include <string>
#include <sstream>
#include <cstdlib>
namespace std
{
template <typename T>
std::string to_string(T value)
{
std::ostringstream os;
os << value;
return os.str();
}
}
#define STOI(str) std::strtol(str.c_str(), 0, 10)
#else
#define STOI(str) std::stoi(str)
#endif // __ANDROID__
#endif // ANDROID_INC_HPP_

View File

@@ -0,0 +1,190 @@
/*********************************************************************
Matt Marchant 2016 - 2021
http://trederia.blogspot.com
tmxlite - Zlib license.
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but
is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any
source distribution.
*********************************************************************/
//flexible logging class, based on code at https://github.com/fallahn/xygine
#ifndef TMXLITE_LOGGER_HPP_
#define TMXLITE_LOGGER_HPP_
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <list>
#include <ctime>
#ifdef _MSC_VER
#define NOMINMAX
#include <windows.h>
#endif //_MSC_VER
#ifdef __ANDROID__
#include <android/log.h>
#include <cstring>
#define LOG_TAG "TMXlite-Debug"
//#define ALOG(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#endif // __ANDROID__
namespace tmx
{
/*!
\brief Class allowing messages to be logged to a combination
of one or more destinations such as the console, log file or
output window in Visual Studio
*/
class Logger final
{
public:
enum class Output
{
Console,
File,
All
};
enum class Type
{
Info,
Warning,
Error
};
/*!
\brief Logs a message to a given destination.
\param message Message to log
\param type Whether this message gets tagged as information, a warning or an error
\param output Destination for the message. Can be the console via cout, a log file on disk, or both
*/
static void log(const std::string& message, Type type = Type::Info, Output output = Output::Console)
{
std::string outstring;
switch (type)
{
case Type::Info:
default:
outstring = "INFO: " + message;
break;
case Type::Error:
outstring = "ERROR: " + message;
break;
case Type::Warning:
outstring = "WARNING: " + message;
break;
}
if (output == Output::Console || output == Output::All)
{
if (type == Type::Error)
{
#ifdef __ANDROID__
int outstringLength = outstring.length();
char outstring_chararray[outstringLength+1];
std::strcpy(outstring_chararray, outstring.c_str());
LOGE("%s",outstring_chararray);
#endif
std::cerr << outstring << std::endl;
}
else
{
#ifdef __ANDROID__
int outstringLength = outstring.length();
char outstring_chararray[outstringLength+1];
std::strcpy(outstring_chararray, outstring.c_str());
LOGI("%s", outstring_chararray);
#endif
std::cout << outstring << std::endl;
}
const std::size_t maxBuffer = 30;
buffer().push_back(outstring);
if (buffer().size() > maxBuffer)buffer().pop_front(); //no majick here pl0x
updateOutString(maxBuffer);
#ifdef _MSC_VER
outstring += "\n";
OutputDebugStringA(outstring.c_str());
#endif //_MSC_VER
}
if (output == Output::File || output == Output::All)
{
//output to a log file
std::ofstream file("output.log", std::ios::app);
if (file.good())
{
#ifndef __ANDROID__
std::time_t time = std::time(nullptr);
auto tm = *std::localtime(&time);
//put_time isn't implemented by the ndk versions of the stl
file.imbue(std::locale());
file << std::put_time(&tm, "%d/%m/%y-%H:%M:%S: ");
#endif //__ANDROID__
file << outstring << std::endl;
file.close();
}
else
{
log(message, type, Output::Console);
log("Above message was intended for log file. Opening file probably failed.", Type::Warning, Output::Console);
}
}
}
static const std::string& bufferString(){ return stringOutput(); }
private:
static std::list<std::string>& buffer(){ static std::list<std::string> buffer; return buffer; }
static std::string& stringOutput() { static std::string output; return output; }
static void updateOutString(std::size_t maxBuffer)
{
static size_t count = 0;
stringOutput().append(buffer().back());
stringOutput().append("\n");
count++;
if (count > maxBuffer)
{
stringOutput() = stringOutput().substr(stringOutput().find_first_of('\n') + 1, stringOutput().size());
count--;
}
}
};
}
#ifndef _DEBUG_
#define LOG(message, type)
#else
#define LOG(message, type) {\
std::stringstream ss; \
ss << message << " (" << __FILE__ << ", " << __LINE__ << ")"; \
tmx::Logger::log(ss.str(), type);}
#endif //_DEBUG_
#endif //TMXLITE_LOGGER_HPP_