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

13 Commits

Author SHA1 Message Date
b9c56ce5a7 actions: fix windows & universal artifacts 2024-03-22 15:17:47 +11:00
0b635ebe87 actions: convert to matrix 2024-03-22 14:41:44 +11:00
5d5dda81c9 rewrite assembly writer 2024-03-21 10:14:17 +11:00
417bc11fae clang warning pass 2024-03-21 08:21:02 +11:00
696057b5e6 correctness 2024-03-21 07:53:01 +11:00
17de8ac3ec first pass at splitting writer logic 2024-03-21 06:54:24 +11:00
db1de4ba8e update gitignore 2024-03-21 05:07:54 +11:00
f4930668ee fix accidental constexpr 2024-03-21 05:02:07 +11:00
5e466598ea update & fix ci (hopefully) 2024-03-21 05:00:02 +11:00
e2a69bf433 Merge branch 'master' into argparse
# Conflicts:
#	CMakeLists.txt
#	src/tmx2gba.cpp
2024-03-21 04:52:03 +11:00
d59fb39857 cut a versioned release before making potentially breaking changes 2024-03-21 04:38:23 +11:00
57455e0b73 msvc fix 2024-03-20 07:50:40 +11:00
c3bbe8135d argparse refactor 2024-03-20 07:29:29 +11:00
14 changed files with 554 additions and 251 deletions

View File

@@ -2,36 +2,46 @@ name: CMake
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
ARTIFACT_NAME: tmx2gba
BUILD_TYPE: RelWithDebInfo
jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest
strategy:
matrix:
config:
- { name: "MacOS 13.0 Universal", os: macos-13, artifact: macos-universal, arch: arm64;x86_64 }
- { name: "Windows MSVC x86", os: windows-latest, artifact: windows-x86, arch: x86 }
- { name: "Windows MSVC x64", os: windows-latest, artifact: windows-x64 }
- { name: "Windows MSVC ARM64", os: windows-latest, artifact: windows-arm64, arch: amd64_arm64 }
- { name: "Ubuntu", artifact: "linux", os: ubuntu-latest }
runs-on: ${{matrix.config.os}}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
# Since ninja isn't used it will take less time if this step is skipped
- uses: lukka/get-cmake@latest
if: ${{!startsWith(matrix.config.os, 'windows')}}
- uses: TheMrMilchmann/setup-msvc-dev@v3
if: ${{startsWith(matrix.config.os, 'windows')}}
with:
arch: ${{matrix.config.arch && matrix.config.arch || 'x64'}}
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
run: >-
cmake -B build
-G "${{startsWith(matrix.config.os, 'windows') && 'NMake Makefiles' || 'Ninja'}}"
${{(startsWith(matrix.config.os, 'macos') && matrix.config.arch) && format('-DCMAKE_OSX_ARCHITECTURES="{0}"', matrix.config.arch) || ''}}
${{matrix.config.extra}} -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}
run: cmake --build build --config ${{env.BUILD_TYPE}}
- uses: actions/upload-artifact@v4
with:
name: ${{env.ARTIFACT_NAME}}-${{matrix.config.artifact}}
path: build/src/${{env.ARTIFACT_NAME}}${{startsWith(matrix.config.os, 'windows') && '.exe' || ''}}

15
.gitignore vendored
View File

@@ -1,23 +1,13 @@
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
@@ -41,6 +31,11 @@ Release/
# CMake Rubbish
build/
cmake-build-*/
xcode/
# OS Rubbish
.DS_Store
Thumbs.db
# Some files I used for testing
*.tmx

View File

@@ -1,22 +1,10 @@
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(tmx2gba)
cmake_minimum_required(VERSION "3.15" FATAL_ERROR)
project(tmx2gba VERSION "0.3")
# Options
option(TMX2GBA_DKP_INSTALL "Install into DEVKITPRO prefix" OFF)
option(ASAN "Enable address sanitiser" OFF)
# C++20 & C99
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 99)
# Enable strong warnings
if (MSVC)
string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "/W3" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
else()
add_compile_options(-Wall)
endif()
if (ASAN)
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address -shared-libasan)

View File

@@ -20,6 +20,7 @@ tmx2gba [-h] [-r offset] [-lyc name] [-p 0-15] <-i inpath> <-o outpath>
| Command | Required | Notes |
|--------------|----------|-----------------------------------------------------------------------|
| -h | N/A | Display help & command info. |
| -v | No | Display version & quit. |
| -l (name) | No | Name of layer to use (default first layer in TMX). |
| -y (name) | No | Layer for palette mappings. |
| -c (name) | No | Output a separate 8bit collision map of the specified layer. |

View File

@@ -4,8 +4,21 @@ add_executable(tmx2gba
tmxobject.hpp
tmxreader.hpp tmxreader.cpp
tmxtileset.hpp
swriter.hpp swriter.cpp
headerwriter.hpp headerwriter.cpp
tmx2gba.cpp)
set_target_properties(tmx2gba PROPERTIES
# C++20 & C99
CXX_STANDARD 20
C_STANDARD 99)
# Enable strong warnings
target_compile_options(tmx2gba PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/Wall>
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra -pedantic>
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-Weverything -Wno-c++98-compat>)
target_link_libraries(tmx2gba
External::base64
External::miniz

View File

@@ -1,14 +1,16 @@
/* argparse.cpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#include "argparse.hpp"
#include <iomanip>
#include <filesystem>
#include <optional>
#include <cstring>
#include <filesystem>
#include <iomanip>
#include <iostream>
ArgParse::ArgParser::ArgParser(
const std::string_view argv0,
std::initializer_list<Option> options,
Options options,
HandleOption&& handler
) noexcept :
name(std::filesystem::path(argv0).filename().string()),
@@ -16,7 +18,7 @@ ArgParse::ArgParser::ArgParser(
handler(std::forward<HandleOption>(handler)) {}
void ArgParse::ArgParser::ShowShortUsage(std::ostream& out) const
void ArgParse::Options::ShowShortUsage(const std::string_view name, std::ostream& out) const
{
out << "Usage: " << name;
for (const auto& it : options)
@@ -39,7 +41,7 @@ void ArgParse::ArgParser::ShowShortUsage(std::ostream& out) const
out << std::endl;
}
void ArgParse::ArgParser::ShowHelpUsage(std::ostream& out) const
void ArgParse::Options::ShowHelpUsage(const std::string_view name, std::ostream& out) const
{
// Base usage
out << "Usage: " << name << " [-";
@@ -56,8 +58,9 @@ void ArgParse::ArgParser::ShowHelpUsage(std::ostream& out) const
auto paramLength = [](const Option& p) -> int { return p.argumentName
? static_cast<int>(std::strlen(p.argumentName) + 3)
: 1; };
auto longestParam = std::max(options, [=](auto a, auto b) -> bool { return paramLength(a) < paramLength(b); });
auto alignWidth = paramLength(longestParam) + 3;
auto longestParam = std::max_element(options.begin(), options.end(),
[=](auto a, auto b) -> bool { return paramLength(a) < paramLength(b); });
auto alignWidth = paramLength(*longestParam) + 3;
// print argument descriptions
for (const auto& it : options)
@@ -76,7 +79,7 @@ ArgParse::ParseCtrl ArgParse::ParserState::Next(const std::string_view token)
auto getFlag = [](const std::string_view s) { return s[0] == '-' && s[1] ? std::optional<int>(s[1]) : std::nullopt; };
auto getOption = [&](int flag) -> std::optional<std::reference_wrapper<const Option>>
{
for (auto& opt : options)
for (auto& opt : options.options)
if (opt.flag == flag)
return std::optional(std::cref(opt));
return {};
@@ -119,7 +122,40 @@ ArgParse::ParseCtrl ArgParse::ParserState::Next(const std::string_view token)
}
bool ReadParamFile(std::vector<std::string>& tokens, std::istream& file)
void ArgParse::ArgParser::DisplayError(const std::string_view message, bool helpPrompt) const
{
std::cerr << GetName() << ": " << message << std::endl;
options.ShowShortUsage(GetName(), std::cerr);
if (helpPrompt)
std::cerr << "Run '" << GetName() << " -h' to view all available options." << std::endl;
}
bool ArgParse::ArgParser::CheckParse(ArgParse::ParseErr err) const
{
switch (err)
{
case ParseErr::OK:
return true;
case ParseErr::OPT_UNKNOWN:
DisplayError("Unrecognised option.");
return false;
case ParseErr::UNEXPECTED:
DisplayError("Unexpected token.");
return false;
case ParseErr::ARG_EXPECTED:
DisplayError("Requires an argument.");
return false;
case ParseErr::ARG_INVALID:
DisplayError("Invalid argument.", false);
return false;
case ParseErr::ARG_RANGE:
DisplayError("Argument out of range.", false);
return false;
}
}
bool ArgParse::ReadParamFile(std::vector<std::string>& tokens, std::istream& file)
{
bool inQuote = false;
std::string quoteStr;
@@ -171,7 +207,7 @@ bool ReadParamFile(std::vector<std::string>& tokens, std::istream& file)
}
else
{
quoteStr.push_back(c);
quoteStr.push_back(static_cast<char>(c));
}
}
}

View File

@@ -16,10 +16,29 @@ namespace ArgParse
{
struct Option
{
char flag;
const char* argumentName;
bool required;
const char* helpString;
char flag;
bool required;
static constexpr Option Optional(char flag, const char* name, const char* help)
{
return { name, help, flag, false };
}
static constexpr Option Required(char flag, const char* name, const char* help)
{
return { name, help, flag, false };
}
};
struct Options
{
const std::vector<Option> options;
inline Options(const std::initializer_list<Option>&& rhs) : options(rhs) {}
void ShowShortUsage(const std::string_view name, std::ostream& out) const;
void ShowHelpUsage(const std::string_view name, std::ostream& out) const;
};
enum class ParseCtrl
@@ -47,13 +66,13 @@ namespace ArgParse
class ParserState
{
bool expectArg = false;
int flagChar;
HandleOption handler;
const std::initializer_list<Option>& options;
const Options& options;
int flagChar;
bool expectArg = false;
public:
ParserState(HandleOption handler, const std::initializer_list<Option>& options) noexcept
ParserState(HandleOption handler, const Options& options) noexcept
: handler(handler), options(options) {}
[[nodiscard]] bool ExpectingArg() const { return expectArg; }
[[nodiscard]] ParseCtrl Next(const std::string_view token);
@@ -62,49 +81,52 @@ namespace ArgParse
class ArgParser
{
const std::string name;
std::initializer_list<Option> options;
Options options;
HandleOption handler;
[[nodiscard]] bool CheckParse(ArgParse::ParseErr err) const;
public:
explicit ArgParser(const std::string_view argv0, std::initializer_list<Option> options, HandleOption&& handler) noexcept;
explicit ArgParser(const std::string_view argv0, Options options, HandleOption&& handler) noexcept;
[[nodiscard]] const std::string_view GetName() const { return name; }
void ShowShortUsage(std::ostream& out) const;
void ShowHelpUsage(std::ostream& out) const;
[[nodiscard]] const std::string_view GetName() const noexcept { return name; }
void DisplayError(const std::string_view message, bool helpPrompt = true) const;
template <typename V>
ParseErr Parse(V args)
[[nodiscard]] bool Parse(V args)
{
ParserState state(handler, options);
for (auto arg : args)
{
ParseErr err = ParseErr::UNEXPECTED;
switch (state.Next(arg))
{
case ParseCtrl::CONTINUE: continue;
case ParseCtrl::QUIT_EARLY: return ParseErr::OK;
case ParseCtrl::QUIT_ERR_UNKNOWN: return ParseErr::OPT_UNKNOWN;
case ParseCtrl::QUIT_ERR_UNEXPECTED: return ParseErr::UNEXPECTED;
case ParseCtrl::QUIT_ERR_EXPECTARG: return ParseErr::ARG_EXPECTED;
case ParseCtrl::QUIT_ERR_INVALID: return ParseErr::ARG_INVALID;
case ParseCtrl::QUIT_ERR_RANGE: return ParseErr::ARG_RANGE;
case ParseCtrl::QUIT_EARLY: err = ParseErr::OK; break;
case ParseCtrl::QUIT_ERR_UNKNOWN: err = ParseErr::OPT_UNKNOWN; break;
case ParseCtrl::QUIT_ERR_UNEXPECTED: err = ParseErr::UNEXPECTED; break;
case ParseCtrl::QUIT_ERR_EXPECTARG: err = ParseErr::ARG_EXPECTED; break;
case ParseCtrl::QUIT_ERR_INVALID: err = ParseErr::ARG_INVALID; break;
case ParseCtrl::QUIT_ERR_RANGE: err = ParseErr::ARG_RANGE; break;
}
if (!CheckParse(err))
return false;
}
return state.ExpectingArg() ? ParseErr::ARG_EXPECTED : ParseErr::OK;
return CheckParse(state.ExpectingArg() ? ParseErr::ARG_EXPECTED : ParseErr::OK);
}
inline ParseErr Parse(std::initializer_list<std::string_view> args)
[[nodiscard]] inline bool Parse(std::initializer_list<std::string_view> args)
{
return Parse<std::initializer_list<std::string_view>>(args);
}
inline ParseErr Parse(std::span<char*> args)
[[nodiscard]] inline bool Parse(std::span<char*> args)
{
return Parse(args | std::views::transform([](char const* v){ return std::string_view(v); }));
}
};
[[nodiscard]] extern bool ReadParamFile(std::vector<std::string>& tokens, std::istream& file);
}
extern bool ReadParamFile(std::vector<std::string>& tokens, std::istream& file);
#endif//ARGPARSE_HPP

93
src/headerwriter.cpp Normal file
View File

@@ -0,0 +1,93 @@
/* headerwriter.cpp - Copyright (C) 2015-2024 a dinosaur (zlib, see COPYING.txt) */
#include "headerwriter.hpp"
template <typename T> static constexpr std::string_view DatType();
template <> constexpr std::string_view DatType<uint8_t>() { return "unsigned char"; }
template <> constexpr std::string_view DatType<uint16_t>() { return "unsigned short"; }
template <> constexpr std::string_view DatType<uint32_t>() { return "unsigned int"; }
void HeaderWriter::WriteSize(int width, int height)
{
stream << std::endl;
WriteDefine(mName + "Width", width);
WriteDefine(mName + "Height", height);
}
void HeaderWriter::WriteCharacterMap(const std::span<uint16_t> charData)
{
stream << std::endl;
WriteDefine(mName + "TilesLen", charData.size() * 2);
WriteSymbol(mName + "Tiles", DatType<uint16_t>(), charData.size());
}
void HeaderWriter::WriteCollision(const std::span<uint8_t> collisionData)
{
stream << std::endl;
WriteDefine(mName + "CollisionLen", collisionData.size());
WriteSymbol(mName + "Collision", DatType<uint8_t>(), collisionData.size());
}
void HeaderWriter::WriteObjects(const std::span<uint32_t> objData)
{
stream << std::endl;
WriteDefine(mName + "ObjCount", objData.size() / 3);
WriteDefine(mName + "ObjdatLen", objData.size() * sizeof(int));
WriteSymbol(mName + "Objdat", DatType<uint32_t>(), objData.size());
}
static std::string GuardName(const std::string_view name)
{
auto upper = std::string(name);
for (auto& c: upper)
c = static_cast<char>(toupper(c));
return "TMX2GBA_" + upper;
}
void HeaderWriter::WriteGuardStart()
{
const std::string guard = GuardName(mName);
stream << "#ifndef " << guard << std::endl;
stream << "#define " << guard << std::endl;
}
void HeaderWriter::WriteGuardEnd()
{
const std::string guard = GuardName(mName);
stream << std::endl << "#endif//" << guard << std::endl;
}
HeaderWriter::~HeaderWriter()
{
if (stream.is_open())
{
WriteGuardEnd();
stream.close();
}
}
bool HeaderWriter::Open(const std::filesystem::path& path, const std::string_view name)
{
mName = name;
stream.open(path);
if (!stream.is_open())
return false;
WriteGuardStart();
return true;
}
void HeaderWriter::WriteDefine(const std::string_view name, const std::string_view value)
{
stream << "#define " << name << " " << value << std::endl;
}
void HeaderWriter::WriteSymbol(const std::string_view name, const std::string_view type, std::size_t count)
{
stream << "extern const " << type << " " << name << "[" << count << "];" << std::endl;
}

46
src/headerwriter.hpp Normal file
View File

@@ -0,0 +1,46 @@
/* headerwriter.hpp - Copyright (C) 2015-2024 a dinosaur (zlib, see COPYING.txt) */
#ifndef HEADERWRITER_HPP
#define HEADERWRITER_HPP
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <span>
#include <concepts>
#include <fstream>
#include <filesystem>
template <typename T>
concept NumericType = std::integral<T> || std::floating_point<T>;
class HeaderWriter
{
std::ofstream stream;
std::string mName;
void WriteGuardStart();
void WriteGuardEnd();
public:
~HeaderWriter();
[[nodiscard]] bool Open(const std::filesystem::path& path, const std::string_view name);
void WriteDefine(const std::string_view name, const std::string_view value);
void WriteSymbol(const std::string_view name, const std::string_view type, std::size_t count);
template <NumericType T>
void WriteDefine(const std::string_view name, T value)
{
WriteDefine(name, std::to_string(value));
}
void WriteSize(int width, int height);
void WriteCharacterMap(const std::span<uint16_t> charData);
void WriteCollision(const std::span<uint8_t> collisionData);
void WriteObjects(const std::span<uint32_t> objData);
};
#endif//HEADERWRITER_HPP

163
src/swriter.cpp Normal file
View File

@@ -0,0 +1,163 @@
/* swwriter.cpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#include "swriter.hpp"
#include <type_traits>
#include <limits>
#define GNU_STYLE 0
#define MASM_STYLE 1
#define HEX_STYLE GNU_STYLE
static inline constexpr char HexU(uint8_t h) { return "0123456789ABCDEF"[h >> 4]; }
static inline constexpr char HexL(uint8_t l) { return "0123456789ABCDEF"[l & 15]; }
#if HEX_STYLE == GNU_STYLE
template <typename T> static void CHex(std::ostream& s, T x);
template <> void CHex(std::ostream& s, uint8_t x)
{
if (x > 9) s << "0x";
if (x > 15) s << HexU(x);
s << HexL(x);
}
template <> void CHex(std::ostream& s, uint16_t x)
{
if (x > 9) s << "0x";
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8));
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8));
if (x > 15) s << HexU(static_cast<uint8_t>(x));
s << HexL(static_cast<uint8_t>(x));
}
template <> void CHex(std::ostream& s, uint32_t x)
{
if (x > 9) s << "0x";
if (x > 0xFFFFFFF) s << HexU(static_cast<uint8_t>(x >> 24));
if (x > 0xFFFFFF) s << HexL(static_cast<uint8_t>(x >> 24));
if (x > 0xFFFFF) s << HexU(static_cast<uint8_t>(x >> 16));
if (x > 65535) s << HexL(static_cast<uint8_t>(x >> 16));
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8));
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8));
if (x > 15) s << HexU(static_cast<uint8_t>(x));
s << HexL(static_cast<uint8_t>(x));
}
#elif HEX_STYLE == MASM_STYLE
template <typename T> static void MHex(std::ostream& s, T x);
template <> void MHex(std::ostream& s, uint8_t x)
{
if (x > 159) s << "0";
if (x > 15) s << HexU(x); else if (x > 9) s << "0";
s << HexL(x);
if (x > 9) s << "h";
}
template <> void MHex(std::ostream& s, uint16_t x)
{
if (x > 40959) s << "0";
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8)); else if (x > 2559) s << "0";
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8)); else if (x > 159) s << "0";
if (x > 15) s << HexU(static_cast<uint8_t>(x)); else if (x > 9) s << "0";
s << HexL(static_cast<uint8_t>(x));
if (x > 9) s << "h";
}
template <> void MHex(std::ostream& s, uint32_t x)
{
if (x > 0x9FFFFFFF) s << "0";
if (x > 0xFFFFFFF) s << HexU(static_cast<uint8_t>(x >> 24)); else if (x > 0x9FFFFFF) s << "0";
if (x > 0xFFFFFF) s << HexL(static_cast<uint8_t>(x >> 24)); else if (x > 0x9FFFFF) s << "0";
if (x > 0xFFFFF) s << HexU(static_cast<uint8_t>(x >> 16)); else if (x > 655359) s << "0";
if (x > 65535) s << HexL(static_cast<uint8_t>(x >> 16)); else if (x > 40959) s << "0";
if (x > 4095) s << HexU(static_cast<uint8_t>(x >> 8)); else if (x > 2559) s << "0";
if (x > 255) s << HexL(static_cast<uint8_t>(x >> 8)); else if (x > 159) s << "0";
if (x > 15) s << HexU(static_cast<uint8_t>(x)); else if (x > 9) s << "0";
s << HexL(static_cast<uint8_t>(x));
if (x > 9) s << "h";
}
#else
# error "Unknown hex style"
#endif
template <typename T> static constexpr const std::string_view DataType();
template <> constexpr const std::string_view DataType<uint8_t>() { return ".byte"; }
template <> constexpr const std::string_view DataType<uint16_t>() { return ".hword"; }
template <> constexpr const std::string_view DataType<uint32_t>() { return ".word"; }
template <typename I>
static void WriteArrayDetail(std::ostream& s, const I beg, const I end, int perCol)
{
typedef typename std::iterator_traits<I>::value_type Element;
int col = 0;
for (auto it = beg;;)
{
if (col == 0)
s << "\t" << DataType<Element>() << " ";
const Element e = *it;
#if HEX_STYLE == MASM_STYLE
MHex(s, e);
#elif HEX_STYLE == GNU_STYLE
CHex(s, e);
#endif
if (++it == end)
break;
if (++col < perCol)
{
s << ",";
}
else
{
s << std::endl;
col = 0;
}
}
s << std::endl;
}
void SWriter::WriteSymbol(const std::string_view suffix)
{
if (writes++ != 0)
stream << std::endl;
stream << "\t.section .rodata" << std::endl;
stream << "\t.align 2" << std::endl;
stream << "\t.global " << mName << suffix << std::endl;
stream << "\t.hidden " << mName << suffix << std::endl;
stream << mName << suffix << ":" << std::endl;
}
void SWriter::WriteArray(const std::string_view suffix, std::span<uint8_t> data, int numCols)
{
WriteSymbol(suffix);
WriteArrayDetail(stream, data.begin(), data.end(), numCols);
}
void SWriter::WriteArray(const std::string_view suffix, std::span<uint16_t> data, int numCols)
{
WriteSymbol(suffix);
WriteArrayDetail(stream, data.begin(), data.end(), numCols);
}
void SWriter::WriteArray(const std::string_view suffix, std::span<uint32_t> data, int numCols)
{
WriteSymbol(suffix);
WriteArrayDetail(stream, data.begin(), data.end(), numCols);
}
SWriter::~SWriter()
{
if (stream.is_open())
{
stream.close();
}
}
bool SWriter::Open(const std::filesystem::path& path, const std::string_view name)
{
mName = name;
stream.open(path);
return stream.is_open();
}

32
src/swriter.hpp Normal file
View File

@@ -0,0 +1,32 @@
/* swwriter.hpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#ifndef SWRITER_HPP
#define SWRITER_HPP
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <span>
#include <fstream>
#include <filesystem>
class SWriter
{
std::ofstream stream;
std::string mName;
int writes = 0;
void WriteSymbol(const std::string_view suffix);
public:
~SWriter();
bool Open(const std::filesystem::path& path, const std::string_view name);
void WriteArray(const std::string_view suffix, std::span<uint8_t> data, int numCols = 16);
void WriteArray(const std::string_view suffix, std::span<uint16_t> data, int numCols = 16);
void WriteArray(const std::string_view suffix, std::span<uint32_t> data, int numCols = 16);
};
#endif//SWRITER_HPP

View File

@@ -4,21 +4,18 @@
#include "tmxreader.hpp"
#include "tmxlayer.hpp"
#include "tmxobject.hpp"
#include "headerwriter.hpp"
#include "swriter.hpp"
#include <iostream>
#include <fstream>
#include <map>
#include <cstdint>
#include <algorithm>
using ArgParse::ArgParser;
using ArgParse::ParseCtrl;
using ArgParse::ParseErr;
static const char* versionStr = "tmx2gba version 0.3, (c) 2015-2022 a dinosaur";
struct Arguments
{
bool help = false;
bool help = false, showVersion = false;
std::string inPath, outPath;
std::string layer, collisionlay, paletteLay;
std::string flagFile;
@@ -27,59 +24,36 @@ struct Arguments
std::vector<std::string> objMappings;
};
static void DisplayError(const ArgParser& parser, const std::string& message, bool helpPrompt = true)
{
std::cerr << parser.GetName() << ": " << message << std::endl;
parser.ShowShortUsage(std::cerr);
if (helpPrompt)
std::cerr << "Run '" << parser.GetName() << " -h' to view all available options." << std::endl;
}
using ArgParse::Option;
bool CheckParse(const ArgParser& parser, ParseErr err)
static const ArgParse::Options options =
{
switch (err)
{
case ParseErr::OK:
return true;
case ParseErr::OPT_UNKNOWN:
DisplayError(parser, "Unrecognised option.");
return false;
case ParseErr::UNEXPECTED:
DisplayError(parser, "Unexpected token.");
return false;
case ParseErr::ARG_EXPECTED:
DisplayError(parser, "Requires an argument.");
return false;
case ParseErr::ARG_INVALID:
DisplayError(parser, "Invalid argument.", false);
return false;
case ParseErr::ARG_RANGE:
DisplayError(parser, "Argument out of range.", false);
return false;
}
}
Option::Optional('h', nullptr, "Display this help & command info"),
Option::Optional('v', nullptr, "Display version & quit"),
Option::Optional('l', "name", "Name of layer to use (default first layer in TMX)"),
Option::Optional('y', "name", "Layer for palette mappings"),
Option::Optional('c', "name", "Output a separate 8bit collision map of the specified layer"),
Option::Optional('r', "offset", "Offset tile indices (default 0)"),
Option::Optional('p', "0-15", "Select which palette to use for 4-bit tilesets"),
Option::Optional('m', "name;id", "Map an object name to an ID, will enable object exports"),
Option::Required('i', "inpath", "Path to input TMX file"),
Option::Required('o', "outpath", "Path to output files"),
Option::Optional('f', "file", "Specify a file to use for flags, will override any options"
" specified on the command line")
};
bool ParseArgs(int argc, char** argv, Arguments& params)
{
auto parser = ArgParser(argv[0], {
{ 'h', nullptr, false, "Display this help & command info" },
{ 'l', "name", false, "Name of layer to use (default first layer in TMX)" },
{ 'y', "name", false, "Layer for palette mappings" },
{ 'c', "name", false, "Output a separate 8bit collision map of the specified layer" },
{ 'r', "offset", false, "Offset tile indices (default 0)" },
{ 'p', "0-15", false, "Select which palette to use for 4-bit tilesets" },
{ 'm', "name;id", false, "Map an object name to an ID, will enable object exports" },
{ 'i', "inpath", true, "Path to input TMX file" },
{ 'o', "outpath", true, "Path to output files" },
{ 'f', "file", false, "Specify a file to use for flags, will override any options"
" specified on the command line" }
}, [&](int opt, const std::string_view arg) -> ParseCtrl
auto parser = ArgParse::ArgParser(argv[0], options, [&](int opt, const std::string_view arg)
-> ArgParse::ParseCtrl
{
using ArgParse::ParseCtrl;
try
{
switch (opt)
{
case 'h': params.help = true; return ParseCtrl::QUIT_EARLY;
case 'v': params.showVersion = true; return ParseCtrl::QUIT_EARLY;
case 'l': params.layer = arg; return ParseCtrl::CONTINUE;
case 'c': params.collisionlay = arg; return ParseCtrl::CONTINUE;
case 'y': params.paletteLay = arg; return ParseCtrl::CONTINUE;
@@ -93,18 +67,15 @@ bool ParseArgs(int argc, char** argv, Arguments& params)
default: return ParseCtrl::QUIT_ERR_UNKNOWN;
}
}
catch (std::invalid_argument const& e) { return ParseCtrl::QUIT_ERR_INVALID; }
catch (std::out_of_range const& e) { return ParseCtrl::QUIT_ERR_RANGE; }
catch (std::invalid_argument const&) { return ParseCtrl::QUIT_ERR_INVALID; }
catch (std::out_of_range const&) { return ParseCtrl::QUIT_ERR_RANGE; }
});
if (!CheckParse(parser, parser.Parse(std::span(argv + 1, argc - 1))))
if (!parser.Parse(std::span(argv + 1, argc - 1)))
return false;
if (params.help)
{
parser.ShowHelpUsage(std::cout);
if (params.help || params.showVersion)
return true;
}
if (!params.flagFile.empty())
{
@@ -116,73 +87,36 @@ bool ParseArgs(int argc, char** argv, Arguments& params)
}
std::vector<std::string> tokens;
if (!ReadParamFile(tokens, paramFile))
if (!ArgParse::ReadParamFile(tokens, paramFile))
{
std::cerr << "Failed to read param file: Unterminated quote string." << std::endl;
return false;
}
if (!CheckParse(parser, parser.Parse(tokens)))
if (!parser.Parse(tokens))
return false;
}
// Check my paranoia
if (params.inPath.empty())
{
DisplayError(parser, "No input file specified.");
parser.DisplayError("No input file specified.");
return false;
}
if (params.outPath.empty())
{
DisplayError(parser, "No output file specified.");
parser.DisplayError("No output file specified.");
return false;
}
if (params.palette < 0 || params.palette > 15)
{
DisplayError(parser, "Invalid palette index.");
parser.DisplayError("Invalid palette index.");
return false;
}
return true;
}
template <typename T> constexpr const char* DatType();
template <> constexpr const char* DatType<uint8_t>() { return ".byte"; }
template <> constexpr const char* DatType<uint16_t>() { return ".hword"; }
template <> constexpr const char* DatType<uint32_t>() { return ".word"; }
template <typename T>
void WriteArray(std::ofstream& aOut, const std::vector<T>& aDat, int aPerCol = 16)
{
int col = 0;
aOut.setf(std::ios::hex, std::ios::basefield);
aOut.setf(std::ios::showbase);
size_t i = 0;
for (T element : aDat)
{
if (col == 0)
aOut << "\t" << DatType<T>() << " ";
aOut << std::hex << (int)element;
if (i < aDat.size() - 1)
{
if (++col < aPerCol)
{
aOut << ",";
}
else
{
aOut << "" << std::endl;
col = 0;
}
}
++i;
}
}
int main(int argc, char** argv)
{
@@ -190,7 +124,15 @@ int main(int argc, char** argv)
if (!ParseArgs(argc, argv, p))
return 1;
if (p.help)
{
options.ShowHelpUsage(argv[0], std::cout);
return 0;
}
if (p.showVersion)
{
std::cout << versionStr << std::endl;
return 0;
}
// Object mappings
std::map<std::string, uint32_t> objMapping;
@@ -202,7 +144,7 @@ int main(int argc, char** argv)
if (splitter == std::string::npos)
{
std::cerr << "Malformed mapping (missing a splitter)." << std::endl;
return -1;
return 1;
}
try
@@ -225,7 +167,7 @@ int main(int argc, char** argv)
if (!fin.is_open())
{
std::cerr << "Failed to open input file." << std::endl;
return -1;
return 1;
}
tmx.Open(fin);
@@ -233,7 +175,7 @@ int main(int argc, char** argv)
if (tmx.GetLayerCount() == 0)
{
std::cerr << "No layers found." << std::endl;
return -1;
return 1;
}
const TmxLayer* layerGfx = p.layer.empty()
? tmx.GetLayer(0)
@@ -248,45 +190,42 @@ int main(int argc, char** argv)
if (layerGfx == nullptr)
{
std::cerr << "Input layer not found." << std::endl;
return -1;
return 1;
}
// Open output files
std::ofstream foutS(p.outPath + ".s");
std::ofstream foutH(p.outPath + ".h");
if (!foutS.is_open() || !foutH.is_open())
{
std::cerr << "Failed to create output file(s).";
return -1;
}
int slashPos = std::max((int)p.outPath.find_last_of('/'), (int)p.outPath.find_last_of('\\'));
// Get name from file
//TODO: properly sanitise
int slashPos = std::max(
static_cast<int>(p.outPath.find_last_of('/')),
static_cast<int>(p.outPath.find_last_of('\\')));
std::string name = p.outPath;
if (slashPos != -1)
name = name.substr(slashPos + 1);
// Write header guards
std::string guard = "TMX2GBA_" + name;
for (auto& c: guard)
c = static_cast<char>(toupper(c));
foutH << "#ifndef " << guard << std::endl;
foutH << "#define " << guard << std::endl;
foutH << std::endl;
foutH << "#define " << name << "Width " << tmx.GetWidth() << std::endl;
foutH << "#define " << name << "Height " << tmx.GetHeight() << std::endl;
foutH << std::endl;
// Open output files
SWriter outS; HeaderWriter outH;
if (!outS.Open(p.outPath + ".s", name))
{
std::cerr << "Failed to create output file \"" << p.outPath << ".s\".";
return 1;
}
if (!outH.Open(p.outPath + ".h", name))
{
std::cerr << "Failed to create output file \"" << p.outPath << ".h\".";
return 1;
}
// Convert to GBA-friendly charmap data
const uint32_t* gfxTiles = layerGfx->GetData();
const uint32_t* palTiles = (layerPal == nullptr) ? nullptr : layerPal->GetData();
std::vector<uint16_t> charDat;
size_t numTiles = static_cast<size_t>(layerGfx->GetWidth()) * static_cast<size_t>(layerGfx->GetHeight());
const size_t numTiles = static_cast<size_t>(layerGfx->GetWidth()) * static_cast<size_t>(layerGfx->GetHeight());
charDat.reserve(numTiles);
for (size_t i = 0; i < numTiles; ++i)
{
uint32_t read = (*gfxTiles++);
uint16_t tile = (uint16_t)std::max<int32_t>(0, tmx.LidFromGid(read & ~TmxLayer::FLIP_MASK) + p.offset);
uint16_t tile = std::max(0, static_cast<int>(tmx.LidFromGid(read & ~TmxLayer::FLIP_MASK)) + p.offset);
uint8_t flags = 0x0;
// Get flipped!
@@ -305,29 +244,21 @@ int main(int argc, char** argv)
}
// Write out charmap
foutH << "#define " << name << "TilesLen " << charDat.size() * 2 << std::endl;
foutH << "extern const unsigned short " << name << "Tiles[" << charDat.size() << "];" << std::endl;
foutH << std::endl;
foutS << "\t.section .rodata" << std::endl;
foutS << "\t.align 2" << std::endl;
foutS << "\t.global " << name << "Tiles" << std::endl;
foutS << "\t.hidden " << name << "Tiles" << std::endl;
foutS << name << "Tiles" << ":" << std::endl;
WriteArray<uint16_t>(foutS, charDat);
foutS << std::endl;
outH.WriteSize(tmx.GetWidth(), tmx.GetHeight());
outH.WriteCharacterMap(charDat);
outS.WriteArray("Tiles", charDat);
// Convert collision map & write it out
if (layerCls != nullptr)
{
std::vector<uint8_t> vucCollisionDat;
vucCollisionDat.reserve(layerCls->GetWidth() * layerCls->GetHeight());
std::vector<uint8_t> collisionDat;
collisionDat.reserve(layerCls->GetWidth() * layerCls->GetHeight());
gfxTiles = layerCls->GetData();
for (int i = 0; i < layerCls->GetWidth() * layerCls->GetHeight(); ++i)
{
uint8_t ucTile = (uint8_t)tmx.LidFromGid((*gfxTiles++) & ~TmxLayer::FLIP_MASK);
vucCollisionDat.push_back(ucTile);
uint8_t ucTile = static_cast<uint8_t>(tmx.LidFromGid((*gfxTiles++) & ~TmxLayer::FLIP_MASK));
collisionDat.push_back(ucTile);
}
// Try to nicely append "_collision" to the output name
@@ -339,18 +270,8 @@ int main(int argc, char** argv)
path = p.outPath + "_collision";
// Write collision
foutH << "#define " << name << "CollisionLen " << vucCollisionDat.size() << std::endl;
foutH << "extern const unsigned char " << name << "Collision[" << vucCollisionDat.size() << "];" << std::endl;
foutH << std::endl;
foutS << std::endl;
foutS << "\t.section .rodata" << std::endl;
foutS << "\t.align 2" << std::endl;
foutS << "\t.global " << name << "Collision" << std::endl;
foutS << "\t.hidden " << name << "Collision" << std::endl;
foutS << name << "Collision" << ":" << std::endl;
WriteArray<uint8_t>(foutS, vucCollisionDat);
foutS << std::endl;
outH.WriteCollision(collisionDat);
outS.WriteArray("Collision", collisionDat, 32);
}
if (!p.objMappings.empty())
@@ -366,30 +287,14 @@ int main(int argc, char** argv)
float x, y;
obj->GetPos(x, y);
objDat.push_back(it->second);
objDat.push_back((int)(x * 256.0f));
objDat.push_back((int)(y * 256.0f));
objDat.push_back(static_cast<int>(x * 256.0f));
objDat.push_back(static_cast<int>(y * 256.0f));
}
// Write objects
foutH << "#define " << name << "ObjCount " << objDat.size() / 3 << std::endl;
foutH << "#define " << name << "ObjdatLen " << objDat.size() * sizeof(int) << std::endl;
foutH << "extern const unsigned int " << name << "Objdat[" << objDat.size() << "];" << std::endl;
foutH << std::endl;
foutS << std::endl;
foutS << "\t.section .rodata" << std::endl;
foutS << "\t.align 2" << std::endl;
foutS << "\t.global " << name << "Objdat" << std::endl;
foutS << "\t.hidden " << name << "Objdat" << std::endl;
foutS << name << "Objdat" << ":" << std::endl;
WriteArray<uint32_t>(foutS, objDat);
foutS << std::endl;
outH.WriteObjects(objDat);
outS.WriteArray("Objdat", objDat);
}
foutH << "#endif//" << guard << std::endl;
foutH.close();
foutS.close();
return 0;
}

View File

@@ -43,7 +43,7 @@ bool TmxReader::DecodeMap(uint32_t* aOut, size_t aOutSize, const std::string& aB
// Decompress compressed data
auto dstSize = static_cast<mz_ulong>(aOutSize);
int res = uncompress(
(unsigned char*)aOut,
reinterpret_cast<unsigned char*>(aOut),
&dstSize,
reinterpret_cast<const unsigned char*>(decoded.data()),
static_cast<mz_ulong>(decoded.size()));
@@ -172,7 +172,7 @@ void TmxReader::Open(std::istream& aIn)
// Parse document
rapidxml::xml_document<> xDoc;
xDoc.parse<0>((char*)strXml.c_str());
xDoc.parse<0>(const_cast<char*>(strXml.c_str()));
// Get map node
auto xMap = xDoc.first_node("map");

View File

@@ -23,7 +23,6 @@ private:
std::string mName;
std::string mSource;
uint32_t mFirstGid;
};
#endif//TMXTILESET_HPP