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

26 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
5c164b239d rewrite argument parsing 2024-03-19 14:39:49 +11:00
a222235605 normalise guards and copyright names in headers 2023-09-17 00:59:19 +10:00
6050224a65 uncringe comments somewhat 2023-09-15 14:14:04 +10:00
c351da76d1 Break out body of licence text 2023-09-15 14:14:04 +10:00
c65a607b61 option for building with asan 2023-09-15 14:14:04 +10:00
04bacae858 Create cmake.yml 2023-01-24 20:24:03 +11:00
46ad0da66c Add install rule and update readme with new instructions 2023-01-09 03:25:19 +11:00
ccde64ab4e Fixed build on GCC 2022-09-07 04:07:52 +10:00
62582723bc Trim leading and tailing whitespace in base64 string, fixes exception in base64 lib 2022-09-07 04:03:49 +10:00
ff162de80a Update README.md 2022-09-07 02:44:51 +10:00
a5ac1d9bfe refactor 2022-09-07 02:34:09 +10:00
6c2cca3fd5 Update libraries 2022-09-07 00:34:17 +10:00
8a5f397f26 MSVC build fix & buildsystem overhaul 2022-09-07 00:11:11 +10:00
46 changed files with 10752 additions and 7414 deletions

47
.github/workflows/cmake.yml vendored Normal file
View File

@@ -0,0 +1,47 @@
name: CMake
on:
push:
pull_request:
branches: [ "master" ]
env:
ARTIFACT_NAME: tmx2gba
BUILD_TYPE: RelWithDebInfo
jobs:
build:
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@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
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
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' || ''}}

26
.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
@@ -27,7 +17,7 @@
*.out
*.app
# VS rubbish.
# VS rubbish
Debug/
Release/
*.opensdf
@@ -35,7 +25,19 @@ Release/
*.v12.suo
*.vcxproj.user
# Some files I used for testing.
# CLion Rubbish
.idea/
# CMake Rubbish
build/
cmake-build-*/
xcode/
# OS Rubbish
.DS_Store
Thumbs.db
# Some files I used for testing
*.tmx
*.raw
plains.png

View File

@@ -1,42 +1,25 @@
# tmx2gba - CMakeLists.txt
# Copyright (C) 2018 Nicholas Curtis (a dinosaur)
#
# 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.
cmake_minimum_required(VERSION "3.15" FATAL_ERROR)
project(tmx2gba VERSION "0.3")
cmake_minimum_required(VERSION 3.10)
project(tmx2gba)
# Options
option(TMX2GBA_DKP_INSTALL "Install into DEVKITPRO prefix" OFF)
option(ASAN "Enable address sanitiser" OFF)
set(CMAKE_CXX_STANDARD 11)
if (ASAN)
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address -shared-libasan)
endif()
include_directories(inc)
set(SOURCES
src/tmx2gba.cpp
src/tmxlayer.cpp src/tmxlayer.h
src/tmxobject.cpp src/tmxobject.h
src/tmxreader.cpp src/tmxreader.h
src/tmxtileset.cpp src/tmxtileset.h
# Libraries
add_subdirectory(ext/base64)
add_subdirectory(ext/miniz)
add_subdirectory(ext/rapidxml)
src/ultragetopt.c inc/ultragetopt.h
src/base64.cpp inc/base64.h
inc/miniz.h
# Main tmx2gba sources
add_subdirectory(src)
inc/rapidxml/rapidxml.hpp
inc/rapidxml/rapidxml_iterators.hpp
inc/rapidxml/rapidxml_print.hpp
inc/rapidxml/rapidxml_utils.hpp)
add_executable(tmx2gba ${SOURCES})
if (MSVC)
# Default to tmx2gba as startup project when generating Solutions
set_property(DIRECTORY ${CMAKE_SOURCE_DIR}
PROPERTY VS_STARTUP_PROJECT tmx2gba)
endif()

17
COPYING.txt Normal file
View File

@@ -0,0 +1,17 @@
Copyright (C) 2015-2024 a dinosaur
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.

View File

@@ -1,6 +1,6 @@
# tmx2gba #
tmx2gba is a simple command line utility that converts [Tiled](http://www.mapeditor.org/) .tmx maps to Game Boy Advance compatible charmaps.
Originally developed for my own personal use, I've thrown it up on glorious Github/Gitlab/whatever in case this is of use to anyone else.
tmx2gba is a simple command line utility that converts [Tiled](http://www.mapeditor.org/) .tmx maps to Game Boy Advance formatted charmaps.
Originally developed for my own personal use, I've thrown it up in case this is of use to anyone else.
If you find a bug, please open an issue.
@@ -17,41 +17,53 @@ Enjoy!
tmx2gba [-h] [-r offset] [-lyc name] [-p 0-15] <-i inpath> <-o outpath>
```
Command | Required | Notes
------------|----------|----------------------------------------------------------------------
-h | N/A | Display help & command info.
-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.
-r (offset) | No | Offset tile indices (default 0).
-p (0-15) | No | Select which palette to use for 4-bit tilesets.
-m (name;id)| No | Map an object name to an ID, will enable object exports.
-i (path) | *Yes* | Path to input TMX file.
-o (path) | *Yes* | Path to output files.
-f <file> | No | Command line instructions list for easy integration with buildscripts
| 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. |
| -r (offset) | No | Offset tile indices (default 0). |
| -p (0-15) | No | Select which palette to use for 4-bit tilesets. |
| -m (name;id) | No | Map an object name to an ID, will enable object exports. |
| -i (path) | *Yes* | Path to input TMX file. |
| -o (path) | *Yes* | Path to output files. |
| -f <file> | No | Command line instructions list for easy integration with buildscripts |
### How do I build it? ###
Dependencies for building are CMake 3.x and a C++11 compliant compiler,
all other dependencies are in-tree so you should be able to build with:
```bash
mkdir build && cd build
cmake ..
make
cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
make -C build -j$(nproc --all)
```
Optionally, to make it convenient for my dkp projects:
Optionally, you may install it to use it system wide:
```bash
sudo cp tmx2gba $DEVKITPRO/tools/bin/tmx2gba
sudo cmake --install build
```
Which will copy the tmx2gba executable to /usr/local/bin/tmx2gba by default,
if you prefer to use /usr for some reason you may specify a prefix like so:
```bash
sudo cmake --install build --prefix /usr
```
If you're a devkitPro user and would prefer to keep all your development tools compartmentalised
you may optionally install to the tools directory with the `TMX2GBA_DKP_INSTALL` option (OFF by default).
The build scripts will respect your `DEVKITPRO` environment variable but if not set will install to
`/opt/devkitpro/tools/bin/tmx2gba` directly, the `--prefix` argument has no effect in this mode.
```bash
cmake -B build -DCMAKE_BUILD_TYPE=Release -DTMX2GBA_DKP_INSTALL:BOOL=ON
cmake --build build
sudo cmake --install build
```
### Todo list ###
* Add support for multi-SBB prepared charmaps.
* Test CMakeLists for Windows compatibility.
* Check if this works for NDS as well.
* Compression support.
* Prehaps use GNU style getopt_long?
* Refactor & Fix bugs. *(duh)*
* Support for less common TMX formats.
### License ###
[tmx2gba](https://github.com/ScrelliCopter/tmx2gba) is licensed under the zlib license.
@@ -61,7 +73,7 @@ sudo cp tmx2gba $DEVKITPRO/tools/bin/tmx2gba
[ultragetopt](https://github.com/kevinoid/ultragetopt) is licensed under the MIT license.
```
Copyright (C) 2015-2019 Nicholas Curtis (a dinosaur)
Copyright (C) 2015-2023 a dinosaur
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View File

@@ -0,0 +1,5 @@
add_library(base64
base64.cpp base64.h)
add_library(External::base64 ALIAS base64)
target_include_directories(base64
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

282
ext/base64/base64.cpp Normal file
View File

@@ -0,0 +1,282 @@
/*
base64.cpp and base64.h
base64 encoding and decoding with C++.
More information at
https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
Version: 2.rc.08 (release candidate)
Copyright (C) 2004-2017, 2020, 2021 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
*/
#include "base64.h"
#include <algorithm>
#include <stdexcept>
//
// Depending on the url parameter in base64_chars, one of
// two sets of base64 characters needs to be chosen.
// They differ in their last two characters.
//
static const char* base64_chars[2] = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"-_"};
static unsigned int pos_of_char(const unsigned char chr) {
//
// Return the position of chr within base64_encode()
//
if (chr >= 'A' && chr <= 'Z') return chr - 'A';
else if (chr >= 'a' && chr <= 'z') return chr - 'a' + ('Z' - 'A') + 1;
else if (chr >= '0' && chr <= '9') return chr - '0' + ('Z' - 'A') + ('z' - 'a') + 2;
else if (chr == '+' || chr == '-') return 62; // Be liberal with input and accept both url ('-') and non-url ('+') base 64 characters (
else if (chr == '/' || chr == '_') return 63; // Ditto for '/' and '_'
else
//
// 2020-10-23: Throw std::exception rather than const char*
//(Pablo Martin-Gomez, https://github.com/Bouska)
//
throw std::runtime_error("Input is not valid base64-encoded data.");
}
static std::string insert_linebreaks(std::string str, size_t distance) {
//
// Provided by https://github.com/JomaCorpFX, adapted by me.
//
if (!str.length()) {
return "";
}
size_t pos = distance;
while (pos < str.size()) {
str.insert(pos, "\n");
pos += distance + 1;
}
return str;
}
template <typename String, unsigned int line_length>
static std::string encode_with_line_breaks(String s) {
return insert_linebreaks(base64_encode(s, false), line_length);
}
template <typename String>
static std::string encode_pem(String s) {
return encode_with_line_breaks<String, 64>(s);
}
template <typename String>
static std::string encode_mime(String s) {
return encode_with_line_breaks<String, 76>(s);
}
template <typename String>
static std::string encode(String s, bool url) {
return base64_encode(reinterpret_cast<const unsigned char*>(s.data()), s.length(), url);
}
std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) {
size_t len_encoded = (in_len +2) / 3 * 4;
unsigned char trailing_char = url ? '.' : '=';
//
// Choose set of base64 characters. They differ
// for the last two positions, depending on the url
// parameter.
// A bool (as is the parameter url) is guaranteed
// to evaluate to either 0 or 1 in C++ therefore,
// the correct character set is chosen by subscripting
// base64_chars with url.
//
const char* base64_chars_ = base64_chars[url];
std::string ret;
ret.reserve(len_encoded);
unsigned int pos = 0;
while (pos < in_len) {
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]);
if (pos+1 < in_len) {
ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) + ((bytes_to_encode[pos + 1] & 0xf0) >> 4)]);
if (pos+2 < in_len) {
ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) + ((bytes_to_encode[pos + 2] & 0xc0) >> 6)]);
ret.push_back(base64_chars_[ bytes_to_encode[pos + 2] & 0x3f]);
}
else {
ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]);
ret.push_back(trailing_char);
}
}
else {
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]);
ret.push_back(trailing_char);
ret.push_back(trailing_char);
}
pos += 3;
}
return ret;
}
template <typename String>
static std::string decode(String encoded_string, bool remove_linebreaks) {
//
// decode(…) is templated so that it can be used with String = const std::string&
// or std::string_view (requires at least C++17)
//
if (encoded_string.empty()) return std::string();
if (remove_linebreaks) {
std::string copy(encoded_string);
copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end());
return base64_decode(copy, false);
}
size_t length_of_string = encoded_string.length();
size_t pos = 0;
//
// The approximate length (bytes) of the decoded string might be one or
// two bytes smaller, depending on the amount of trailing equal signs
// in the encoded string. This approximation is needed to reserve
// enough space in the string to be returned.
//
size_t approx_length_of_decoded_string = length_of_string / 4 * 3;
std::string ret;
ret.reserve(approx_length_of_decoded_string);
while (pos < length_of_string) {
//
// Iterate over encoded input string in chunks. The size of all
// chunks except the last one is 4 bytes.
//
// The last chunk might be padded with equal signs or dots
// in order to make it 4 bytes in size as well, but this
// is not required as per RFC 2045.
//
// All chunks except the last one produce three output bytes.
//
// The last chunk produces at least one and up to three bytes.
//
size_t pos_of_char_1 = pos_of_char(encoded_string[pos+1] );
//
// Emit the first output byte that is produced in each chunk:
//
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char(encoded_string[pos+0]) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4)));
if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045)
encoded_string[pos+2] != '=' &&
encoded_string[pos+2] != '.' // accept URL-safe base 64 strings, too, so check for '.' also.
)
{
//
// Emit a chunk's second byte (which might not be produced in the last chunk).
//
unsigned int pos_of_char_2 = pos_of_char(encoded_string[pos+2] );
ret.push_back(static_cast<std::string::value_type>( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2)));
if ( ( pos + 3 < length_of_string ) &&
encoded_string[pos+3] != '=' &&
encoded_string[pos+3] != '.'
)
{
//
// Emit a chunk's third byte (which might not be produced in the last chunk).
//
ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string[pos+3]) ));
}
}
pos += 4;
}
return ret;
}
std::string base64_decode(std::string const& s, bool remove_linebreaks) {
return decode(s, remove_linebreaks);
}
std::string base64_encode(std::string const& s, bool url) {
return encode(s, url);
}
std::string base64_encode_pem (std::string const& s) {
return encode_pem(s);
}
std::string base64_encode_mime(std::string const& s) {
return encode_mime(s);
}
#if __cplusplus >= 201703L
//
// Interface with std::string_view rather than const std::string&
// Requires C++17
// Provided by Yannic Bonenberger (https://github.com/Yannic)
//
std::string base64_encode(std::string_view s, bool url) {
return encode(s, url);
}
std::string base64_encode_pem(std::string_view s) {
return encode_pem(s);
}
std::string base64_encode_mime(std::string_view s) {
return encode_mime(s);
}
std::string base64_decode(std::string_view s, bool remove_linebreaks) {
return decode(s, remove_linebreaks);
}
#endif // __cplusplus >= 201703L

35
ext/base64/base64.h Normal file
View File

@@ -0,0 +1,35 @@
//
// base64 encoding and decoding with C++.
// Version: 2.rc.08 (release candidate)
//
#ifndef BASE64_H_C0CE2A47_D10E_42C9_A27C_C883944E704A
#define BASE64_H_C0CE2A47_D10E_42C9_A27C_C883944E704A
#include <string>
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
std::string base64_encode (std::string const& s, bool url = false);
std::string base64_encode_pem (std::string const& s);
std::string base64_encode_mime(std::string const& s);
std::string base64_decode(std::string const& s, bool remove_linebreaks = false);
std::string base64_encode(unsigned char const*, size_t len, bool url = false);
#if __cplusplus >= 201703L
//
// Interface with std::string_view rather than const std::string&
// Requires C++17
// Provided by Yannic Bonenberger (https://github.com/Yannic)
//
std::string base64_encode (std::string_view s, bool url = false);
std::string base64_encode_pem (std::string_view s);
std::string base64_encode_mime(std::string_view s);
std::string base64_decode(std::string_view s, bool remove_linebreaks = false);
#endif // __cplusplus >= 201703L
#endif /* BASE64_H_C0CE2A47_D10E_42C9_A27C_C883944E704A */

5
ext/miniz/CMakeLists.txt Normal file
View File

@@ -0,0 +1,5 @@
add_library(miniz
miniz.c miniz.h)
add_library(External::miniz ALIAS miniz)
target_include_directories(miniz
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

7733
ext/miniz/miniz.c Normal file

File diff suppressed because it is too large Load Diff

1350
ext/miniz/miniz.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
add_library(rapidxml INTERFACE)
add_library(External::rapidxml ALIAS rapidxml)
target_include_directories(rapidxml
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -1,9 +0,0 @@
#ifndef __BASE64_H__
#define __BASE64_H__
#include <string>
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,105 +0,0 @@
/* define ULTRAGETOPT_REPLACE_GETOPT for ultragetopt*() to replace getopt*() */
/* define ULTRAGETOPT_ONLY_DOS for ultragetopt*_dos() to replace ultragetopt*() */
#ifndef INCLUDED_GETOPT_H
#define INCLUDED_GETOPT_H 1
#ifdef __cplusplus
extern "C" {
#endif
#define ULTRAGETOPT_REPLACE_GETOPT
#define ULTRAGETOPT_LIKE_GNU
#define ULTRAGETOPT_LINKAGE extern
/* Flag values to pass to getopt_tunable() */
#define UGO_CASEINSENSITIVE 0x1
#define UGO_SINGLELEADERLONG 0x2
#define UGO_OPTIONPERMUTE 0x4
#define UGO_NONOPTARG 0x8
#define UGO_NOPRINTERR 0x10
#define UGO_OPTIONALARG 0x20
#define UGO_MISSINGCOLON 0x40
#define UGO_SEPARATEDOPTIONAL 0x80
#define UGO_SHORTOPTASSIGN 0x100
#define UGO_NOEATDASHDASH 0x200
#define UGO_LONGOPTADJACENT 0x400
#define UGO_HYPHENARG 0x800
#define UGO_SINGLELEADERONLY 0x1000
#ifndef required_argument
# define no_argument 0
# define required_argument 1
# define optional_argument 2
struct option {
const char *name; /* Name of the option */
int has_arg; /* Does the option take an argument? */
int *flag; /* Location to store val when option encountered */
int val; /* Value to return (or store in flag) for option */
};
#endif /* required_argument */
ULTRAGETOPT_LINKAGE char *ultraoptarg;
ULTRAGETOPT_LINKAGE int ultraoptind, ultraopterr, ultraoptopt, ultraoptreset;
ULTRAGETOPT_LINKAGE int ultragetopt(int argc, char *const argv[],
const char *optstring);
ULTRAGETOPT_LINKAGE int ultragetopt_long(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr);
ULTRAGETOPT_LINKAGE int ultragetopt_long_only(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr);
ULTRAGETOPT_LINKAGE int ultragetopt_dos(int argc, char * const argv[],
const char *optstring);
ULTRAGETOPT_LINKAGE int ultragetopt_long_dos(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr);
/* Getopt with modifiable (tunable) behavior - also the backend for all other
* getopt functions.
* assigners - string of characters accepted to assign to an option
* (e.g. --outfile=file.txt where '=' is the assigner)
* optleaders - string of characters that indicate an option
* (usually "-" on UNIX, "/" on DOS)
* flags - see README for list of accepted flags
*/
ULTRAGETOPT_LINKAGE int ultragetopt_tunable(int argc, char *const argv[],
const char *shortopts, const struct option *longopts, int *indexptr,
const char *assigners, const char *optleaders, int flags);
#ifdef ULTRAGETOPT_REPLACE_GETOPT
# define optarg ultraoptarg
# define optind ultraoptind
# define opterr ultraopterr
# define optopt ultraoptopt
# define optreset ultraoptreset
# define getopt(argc, argv, optstring) \
ultragetopt(argc, argv, optstring)
# define getopt_long(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long(argc, argv, shortopts, longopts, indexptr)
# define getopt_long_only(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_only(argc, argv, shortopts, longopts, indexptr)
# define getopt_dos(argc, argv, optstring) \
ultragetopt_dos(argc, argv, optstring)
# define getopt_long_dos(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_dos(argc, argv, shortopts, longopts, indexptr)
#endif /* GETOPT_NO_EXTENSIONS */
#ifdef ULTRAGETOPT_DOS_ONLY
# define ultragetopt(argc, argv, optstring) \
ultragetopt_dos(argc, argv, optstring)
# define ultragetopt_long(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_dos(argc, argv, shortopts, longopts, indexptr)
# define ultragetopt_long_only(argc, argv, shortopts, longopts, indexptr) \
ultragetopt_long_dos(argc, argv, shortopts, longopts, indexptr)
#endif /* ULTRAGETOPT_DOS_ONLY */
#ifdef __cplusplus
}
#endif
#endif /* INCLUDED_GETOPT_H */

36
src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,36 @@
add_executable(tmx2gba
argparse.hpp argparse.cpp
tmxlayer.hpp
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
External::rapidxml)
if (TMX2GBA_DKP_INSTALL)
if (DEFINED ENV{DEVKITPRO})
set(TMX2GBA_INSTALL_DESTINATION "$ENV{DEVKITPRO}/tools/bin")
else()
set(TMX2GBA_INSTALL_DESTINATION /opt/devkitpro/tools/bin)
endif()
else()
set(TMX2GBA_INSTALL_DESTINATION bin)
endif()
install(TARGETS tmx2gba RUNTIME DESTINATION "${TMX2GBA_INSTALL_DESTINATION}")

View File

@@ -1,212 +0,0 @@
// XGetopt.cpp Version 1.2
//
// Author: Hans Dietrich
// hdietrich2@hotmail.com
//
// Description:
// XGetopt.cpp implements getopt(), a function to parse command lines.
//
// History
// Version 1.2 - 2003 May 17
// - Added Unicode support
//
// Version 1.1 - 2002 March 10
// - Added example to XGetopt.cpp module header
//
// This software is released into the public domain.
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed
// or implied warranty. I accept no liability for any
// damage or loss of business that this software may cause.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// if you are not using precompiled headers then include these lines:
#include <stdio.h>
#include <string.h>
///////////////////////////////////////////////////////////////////////////////
#include "XGetopt.h"
///////////////////////////////////////////////////////////////////////////////
//
// X G e t o p t . c p p
//
//
// NAME
// getopt -- parse command line options
//
// SYNOPSIS
// int getopt(int argc, char *argv[], char *optstring)
//
// extern char *optarg;
// extern int optind;
//
// DESCRIPTION
// The getopt() function parses the command line arguments. Its
// arguments argc and argv are the argument count and array as
// passed into the application on program invocation. In the case
// of Visual C++ programs, argc and argv are available via the
// variables __argc and __argv (double underscores), respectively.
// getopt returns the next option letter in argv that matches a
// letter in optstring. (Note: Unicode programs should use
// __targv instead of __argv. Also, all character and string
// literals should be enclosed in _T( ) ).
//
// optstring is a string of recognized option letters; if a letter
// is followed by a colon, the option is expected to have an argument
// that may or may not be separated from it by white space. optarg
// is set to point to the start of the option argument on return from
// getopt.
//
// Option letters may be combined, e.g., "-ab" is equivalent to
// "-a -b". Option letters are case sensitive.
//
// getopt places in the external variable optind the argv index
// of the next argument to be processed. optind is initialized
// to 0 before the first call to getopt.
//
// When all options have been processed (i.e., up to the first
// non-option argument), getopt returns EOF, optarg will point
// to the argument, and optind will be set to the argv index of
// the argument. If there are no non-option arguments, optarg
// will be set to nullptr.
//
// The special option "--" may be used to delimit the end of the
// options; EOF will be returned, and "--" (and everything after it)
// will be skipped.
//
// RETURN VALUE
// For option letters contained in the string optstring, getopt
// will return the option letter. getopt returns a question mark (?)
// when it encounters an option letter not included in optstring.
// EOF is returned when processing is finished.
//
// BUGS
// 1) Long options are not supported.
// 2) The GNU double-colon extension is not supported.
// 3) The environment variable POSIXLY_CORRECT is not supported.
// 4) The + syntax is not supported.
// 5) The automatic permutation of arguments is not supported.
// 6) This implementation of getopt() returns EOF if an error is
// encountered, instead of -1 as the latest standard requires.
//
// EXAMPLE
// BOOL CMyApp::ProcessCommandLine(int argc, char *argv[])
// {
// int c;
//
// while ((c = getopt(argc, argv, _T("aBn:"))) != EOF)
// {
// switch (c)
// {
// case _T('a'):
// TRACE(_T("option a\n"));
// //
// // set some flag here
// //
// break;
//
// case _T('B'):
// TRACE( _T("option B\n"));
// //
// // set some other flag here
// //
// break;
//
// case _T('n'):
// TRACE(_T("option n: value=%d\n"), atoi(optarg));
// //
// // do something with value here
// //
// break;
//
// case _T('?'):
// TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]);
// return FALSE;
// break;
//
// default:
// TRACE(_T("WARNING: no handler for option %c\n"), c);
// return FALSE;
// break;
// }
// }
// //
// // check for non-option args here
// //
// return TRUE;
// }
//
///////////////////////////////////////////////////////////////////////////////
char *optarg; // global argument pointer
char *next;
int optind = 0; // global argv index
int getopt(int argc, char *argv[], char *optstring)
{
if (optind == 0)
next = nullptr;
optarg = nullptr;
if (next == nullptr || *next == '\0')
{
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
{
optarg = nullptr;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
if (strcmp(argv[optind], "--") == 0)
{
optind++;
optarg = nullptr;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
next = argv[optind];
next++; // skip past -
optind++;
}
char c = *next++;
char *cp = strchr(optstring, c);
if (cp == nullptr || c == ':')
return '?';
cp++;
if (*cp == ':')
{
if (*next != '\0')
{
optarg = next;
next = nullptr;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
return '?';
}
}
return c;
}

216
src/argparse.cpp Normal file
View File

@@ -0,0 +1,216 @@
/* argparse.cpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#include "argparse.hpp"
#include <optional>
#include <cstring>
#include <filesystem>
#include <iomanip>
#include <iostream>
ArgParse::ArgParser::ArgParser(
const std::string_view argv0,
Options options,
HandleOption&& handler
) noexcept :
name(std::filesystem::path(argv0).filename().string()),
options(options),
handler(std::forward<HandleOption>(handler)) {}
void ArgParse::Options::ShowShortUsage(const std::string_view name, std::ostream& out) const
{
out << "Usage: " << name;
for (const auto& it : options)
{
if (it.argumentName)
{
// Option with argument
it.required
? out << " <-" << it.flag << ' ' << it.argumentName << '>'
: out << " [-" << it.flag << ' ' << it.argumentName << ']';
}
else
{
// Argument-less flag
it.required
? out << " <-" << it.flag << '>'
: out << " [-" << it.flag << ']';
}
}
out << std::endl;
}
void ArgParse::Options::ShowHelpUsage(const std::string_view name, std::ostream& out) const
{
// Base usage
out << "Usage: " << name << " [-";
for (const auto& it : options)
if (!it.required)
out << it.flag;
out << "] <-";
for (const auto& it : options)
if (it.required)
out << it.flag;
out << ">" << std::endl;
// Determine the alignment width from the longest argument
auto paramLength = [](const Option& p) -> int { return p.argumentName
? static_cast<int>(std::strlen(p.argumentName) + 3)
: 1; };
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)
{
auto decorateArgument = [=] { return " <" + std::string(it.argumentName) + "> "; };
out << " -" << it.flag
<< std::left << std::setw(alignWidth) << std::setfill('-') << (it.argumentName ? decorateArgument() : " ")
<< " " << it.helpString << std::endl;
}
out << std::flush;
}
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.options)
if (opt.flag == flag)
return std::optional(std::cref(opt));
return {};
};
if (expectArg)
{
expectArg = false;
return handler(flagChar, token);
}
else
{
auto flag = getFlag(token);
if (flag.has_value())
{
flagChar = flag.value();
const auto opt = getOption(flagChar);
if (opt.has_value())
{
bool expect = opt.value().get().argumentName != nullptr;
if (token.length() <= 2)
{
expectArg = expect;
if (!expectArg)
return handler(flagChar, "");
}
else
{
return handler(flagChar, expect ? token.substr(2) : "");
}
}
}
else if (!token.empty())
{
return ParseCtrl::QUIT_ERR_UNEXPECTED;
}
}
return ParseCtrl::CONTINUE;
}
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;
const auto store = [&](const std::string_view token, bool quote)
{
if (quote)
quoteStr = token;
else
tokens.emplace_back(token);
};
while (!file.eof())
{
if (!inQuote)
{
std::string token;
file >> token;
if (!token.empty())
{
std::string::size_type beg = 0, end;
while ((end = token.find_first_of('"', beg)) != std::string::npos)
{
auto size = end - beg;
if (size > 0)
store(token.substr(beg, size), !inQuote);
inQuote = !inQuote;
beg = end + 1;
}
if (beg > 0)
{
auto size = token.length() - beg;
if (size > 0)
token = token.substr(beg);
else
token.clear();
}
if (!token.empty())
store(token, inQuote);
}
}
else
{
const int c = file.get();
if (c == '"')
{
tokens.emplace_back(quoteStr);
quoteStr.clear();
inQuote = false;
}
else
{
quoteStr.push_back(static_cast<char>(c));
}
}
}
return !inQuote;
}

132
src/argparse.hpp Normal file
View File

@@ -0,0 +1,132 @@
/* argparse.hpp - Copyright (C) 2024 a dinosaur (zlib, see COPYING.txt) */
#ifndef ARGPARSE_HPP
#define ARGPARSE_HPP
#include <initializer_list>
#include <functional>
#include <string>
#include <ostream>
#include <span>
#include <ranges>
#include <string_view>
#include <vector>
namespace ArgParse
{
struct Option
{
const char* argumentName;
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
{
CONTINUE,
QUIT_EARLY,
QUIT_ERR_UNKNOWN,
QUIT_ERR_UNEXPECTED,
QUIT_ERR_EXPECTARG,
QUIT_ERR_INVALID,
QUIT_ERR_RANGE
};
enum class ParseErr
{
OK,
OPT_UNKNOWN,
UNEXPECTED,
ARG_EXPECTED,
ARG_INVALID,
ARG_RANGE
};
using HandleOption = std::function<ParseCtrl(int, const std::string_view)>;
class ParserState
{
HandleOption handler;
const Options& options;
int flagChar;
bool expectArg = false;
public:
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);
};
class ArgParser
{
const std::string name;
Options options;
HandleOption handler;
[[nodiscard]] bool CheckParse(ArgParse::ParseErr err) const;
public:
explicit ArgParser(const std::string_view argv0, Options options, HandleOption&& handler) noexcept;
[[nodiscard]] const std::string_view GetName() const noexcept { return name; }
void DisplayError(const std::string_view message, bool helpPrompt = true) const;
template <typename V>
[[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: 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 CheckParse(state.ExpectingArg() ? ParseErr::ARG_EXPECTED : ParseErr::OK);
}
[[nodiscard]] inline bool Parse(std::initializer_list<std::string_view> args)
{
return Parse<std::initializer_list<std::string_view>>(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);
}
#endif//ARGPARSE_HPP

View File

@@ -1,123 +0,0 @@
/*
base64.cpp and base64.h
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
*/
#include "base64.h"
#include <iostream>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string base64_decode(std::string const& encoded_string) {
int 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] = 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] = 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;
}

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

@@ -1,152 +1,116 @@
/* tmx2gba.cpp
/* tmx2gba.cpp - Copyright (C) 2015-2024 a dinosaur (zlib, see COPYING.txt) */
Copyright (C) 2015-2019 Nicholas Curtis (a dinosaur)
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.
*/
#include "tmxreader.h"
#include "tmxlayer.h"
#include "tmxobject.h"
#include "argparse.hpp"
#include "tmxreader.hpp"
#include "tmxlayer.hpp"
#include "tmxobject.hpp"
#include "headerwriter.hpp"
#include "swriter.hpp"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <cstdint>
#include <algorithm>
#include <ultragetopt.h>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::map;
using std::ifstream;
using std::ofstream;
using std::stoi;
using std::min;
using std::max;
using std::ios;
static const char* versionStr = "tmx2gba version 0.3, (c) 2015-2022 a dinosaur";
static const string g_strUsage = "Usage: tmx2gba [-h] [-f file] [-r offset] [-lyc name] [-p 0-15] [-m name;id] <-i inpath> <-o outpath>";
static const string g_strHelpShort = "Run 'tmx2gba -h' to view all available options.";
static const string g_strHelpFull = R"(
-h ------------ Display this help & command info.
-l <name> ----- Name of layer to use (default first layer in TMX).
-y <name> ----- Layer for palette mappings.
-c <name> ----- Output a separate 8bit collision map of the specified layer.
-r <offset> --- Offset tile indices (default 0).
-p <0-15> ----- Select which palette to use for 4-bit tilesets.
-m <name;id> -- Map an object name to an ID, will enable object exports.
-i <path> ----- Path to input TMX file.
-o <path> ----- Path to output files.
-f <file> ----- Specify a file to use for flags, will override any options specified on the command line.)";
struct SParams
struct Arguments
{
bool help = false;
string inPath, outPath;
string layer, collisionlay, paletteLay;
string flagFile;
bool help = false, showVersion = false;
std::string inPath, outPath;
std::string layer, collisionlay, paletteLay;
std::string flagFile;
int offset = 0;
int palette = 0;
vector<string> objMappings;
bool objExport = false;
std::vector<std::string> objMappings;
};
void ParseArgs ( int argc, char** argv, SParams* params )
using ArgParse::Option;
static const ArgParse::Options options =
{
char cOption;
optreset = 1;
while ( ( cOption = (char)getopt ( argc, argv, "hr:l:c:p:y:m:i:o:f:" ) ) > 0 )
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)
{
switch ( cOption )
auto parser = ArgParse::ArgParser(argv[0], options, [&](int opt, const std::string_view arg)
-> ArgParse::ParseCtrl
{
case ( 'h' ):
params->help = true;
return;
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;
case 'r': params.offset = std::stoi(std::string(arg)); return ParseCtrl::CONTINUE;
case 'p': params.palette = std::stoi(std::string(arg)); return ParseCtrl::CONTINUE;
case 'm': params.objMappings.emplace_back(arg); return ParseCtrl::CONTINUE;
case 'i': params.inPath = arg; return ParseCtrl::CONTINUE;
case 'o': params.outPath = arg; return ParseCtrl::CONTINUE;
case 'f': params.flagFile = arg; return ParseCtrl::CONTINUE;
case ( 'l' ):
params->layer = optarg;
break;
case ( 'c' ):
params->collisionlay = optarg;
break;
case ( 'y' ):
params->paletteLay = optarg;
break;
case ( 'r' ):
params->offset = stoi ( optarg );
break;
case ( 'p' ):
params->palette = stoi ( optarg );
break;
case ( 'm' ):
params->objExport = true;
params->objMappings.push_back ( optarg );
break;
case ( 'i' ):
params->inPath = optarg;
break;
case ( 'o' ):
params->outPath = optarg;
break;
case ( 'f' ):
params->flagFile = optarg;
break;
default:
break;
}
default: return ParseCtrl::QUIT_ERR_UNKNOWN;
}
}
catch (std::invalid_argument const&) { return ParseCtrl::QUIT_ERR_INVALID; }
catch (std::out_of_range const&) { return ParseCtrl::QUIT_ERR_RANGE; }
});
bool CheckArgs ( const SParams* params )
if (!parser.Parse(std::span(argv + 1, argc - 1)))
return false;
if (params.help || params.showVersion)
return true;
if (!params.flagFile.empty())
{
// Check my paranoia.
if ( params->inPath.empty () )
std::ifstream paramFile(params.flagFile);
if (!paramFile.is_open())
{
cerr << "No input file specified." << endl;
cout << g_strUsage << endl << g_strHelpShort << endl;
std::cerr << "Failed to open param file." << std::endl;
return false;
}
if ( params->outPath.empty () )
std::vector<std::string> tokens;
if (!ArgParse::ReadParamFile(tokens, paramFile))
{
cerr << "No output file specified." << endl;
cout << g_strUsage << endl << g_strHelpShort << endl;
std::cerr << "Failed to read param file: Unterminated quote string." << std::endl;
return false;
}
if ( params->palette < 0 || params->palette > 15 )
if (!parser.Parse(tokens))
return false;
}
// Check my paranoia
if (params.inPath.empty())
{
cerr << "Invalid palette index." << endl;
cout << g_strUsage << endl << g_strHelpShort << endl;
parser.DisplayError("No input file specified.");
return false;
}
if (params.outPath.empty())
{
parser.DisplayError("No output file specified.");
return false;
}
if (params.palette < 0 || params.palette > 15)
{
parser.DisplayError("Invalid palette index.");
return false;
}
@@ -154,344 +118,183 @@ bool CheckArgs ( const SParams* params )
}
template <typename T>
void WriteArray ( ofstream& a_fout, const vector<T>& a_dat, int a_perCol = 16 )
{
const int w = sizeof(T) * 2;
int col = 0;
string datType = "ERR";
if ( sizeof(T) == 1 )
{
datType = ".byte";
}
else
if ( sizeof(T) == 2 )
{
datType = ".hword";
}
else
if ( sizeof(T) == 4 )
{
datType = ".word";
}
a_fout.setf ( ios::hex, ios::basefield );
a_fout.setf ( ios::showbase );
size_t i = 0;
for ( T element : a_dat )
{
if ( col == 0 )
{
a_fout << "\t" << datType << " ";
}
a_fout << std::hex << (int)element;
if ( i < a_dat.size () - 1 )
{
if ( ++col < a_perCol )
{
a_fout << ",";
}
else
{
a_fout << "" << endl;
col = 0;
}
}
++i;
}
}
int main(int argc, char** argv)
{
SParams params;
ParseArgs ( argc, argv, &params );
if ( params.help )
Arguments p;
if (!ParseArgs(argc, argv, p))
return 1;
if (p.help)
{
cout << g_strUsage << endl << g_strHelpFull << endl;
options.ShowHelpUsage(argv[0], std::cout);
return 0;
}
if (p.showVersion)
{
std::cout << versionStr << std::endl;
return 0;
}
if ( params.flagFile.length () != 0 )
// Object mappings
std::map<std::string, uint32_t> objMapping;
if (!p.objMappings.empty())
{
ifstream paramFile ( params.flagFile );
if ( !paramFile.is_open () )
for (const auto& objToken : p.objMappings)
{
cerr << "Failed to open param file." << endl;
return -1;
}
vector<string> fileArgTokens;
fileArgTokens.push_back ( "auu~~" );
bool carry = false;
string token;
while ( !paramFile.eof () )
auto splitter = objToken.find_last_of(';');
if (splitter == std::string::npos)
{
if ( carry )
{
string tmp;
paramFile >> tmp;
token += " ";
token += tmp;
}
else
{
token.clear ();
paramFile >> token;
}
if ( token == "" )
{
continue;
}
bool qFr = token[0] == '"';
bool qBk = token[token.length () - 1] == '"';
if ( qFr && qBk )
{
fileArgTokens.push_back ( token.substr ( 1, token.length () - 2 ) );
}
else
if ( qFr )
{
fileArgTokens.push_back ( token.substr ( 1, token.length () - 1 ) );
carry = true;
}
else
if ( qBk )
{
fileArgTokens.push_back ( token.substr ( 0, token.length () - 1 ) );
carry = false;
}
else
{
fileArgTokens.push_back ( token );
}
}
vector<const char*> fileArgs;
fileArgs.reserve ( fileArgTokens.size () );
for ( auto& token : fileArgTokens )
{
fileArgs.push_back ( token.c_str () );
}
fileArgs.push_back(nullptr);
ParseArgs ( fileArgs.size () - 1, (char**)fileArgs.data (), &params );
}
if ( !CheckArgs ( &params ) )
{
return -1;
}
// Object mappings.
map<string, uint32_t> objMapping;
if ( params.objExport )
{
for ( auto token : params.objMappings )
{
int splitter = token.find_last_of ( ';' );
if ( splitter == -1 )
{
cerr << "Malformed mapping (missing a splitter)." << endl;
return -1;
std::cerr << "Malformed mapping (missing a splitter)." << std::endl;
return 1;
}
try
{
string name = token.substr ( 0, splitter );
int id = stoi ( token.substr ( splitter + 1 ) );
std::string name = objToken.substr(0, splitter);
int id = std::stoi(objToken.substr(splitter + 1));
objMapping[name] = id;
}
catch ( std::exception )
catch (std::exception&)
{
cerr << "Malformed mapping, make sure id is numeric." << endl;
std::cerr << "Malformed mapping, make sure id is numeric." << std::endl;
}
}
}
// Open & read input file.
CTmxReader tmx;
ifstream fin ( params.inPath );
// Open & read input file
TmxReader tmx;
std::ifstream fin(p.inPath);
if (!fin.is_open())
{
cerr << "Failed to open input file." << endl;
return -1;
std::cerr << "Failed to open input file." << std::endl;
return 1;
}
tmx.Open(fin);
// Get layers.
// Get layers
if (tmx.GetLayerCount() == 0)
{
cerr << "No layers found." << endl;
return -1;
std::cerr << "No layers found." << std::endl;
return 1;
}
const CTmxLayer* pLayerGfx = params.layer.empty () ? tmx.GetLayer ( 0 ) : tmx.GetLayer ( params.layer );
const CTmxLayer* pLayerCls = params.collisionlay.empty () ? nullptr : tmx.GetLayer ( params.collisionlay );
const CTmxLayer* pLayerPal = params.paletteLay.empty () ? nullptr : tmx.GetLayer ( params.paletteLay );
const TmxLayer* layerGfx = p.layer.empty()
? tmx.GetLayer(0)
: tmx.GetLayer(p.layer);
const TmxLayer* layerCls = p.collisionlay.empty()
? nullptr
: tmx.GetLayer(p.collisionlay);
const TmxLayer* layerPal = p.paletteLay.empty()
? nullptr
: tmx.GetLayer(p.paletteLay);
if ( pLayerGfx == nullptr )
if (layerGfx == nullptr)
{
cerr << "Input layer not found." << endl;
return -1;
std::cerr << "Input layer not found." << std::endl;
return 1;
}
// Open output files.
ofstream foutS ( params.outPath + ".s" );
ofstream foutH ( params.outPath + ".h" );
if ( !foutS.is_open () || !foutH.is_open () )
{
cerr << "Failed to create output file(s).";
return -1;
}
int slashPos = max ( (int)params.outPath.find_last_of ( '/' ), (int)params.outPath.find_last_of ( '\\' ) );
string name = params.outPath;
// 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);
// 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;
}
// Write header guards.
string guard = "__TMX2GBA_" + name + "__";
for ( auto& c: guard ) c = (char)toupper ( (int)c );
foutH << "#ifndef " << guard << endl;
foutH << "#define " << guard << endl;
foutH << endl;
foutH << "#define " << name << "Width " << tmx.GetWidth () << endl;
foutH << "#define " << name << "Height " << tmx.GetHeight () << endl;
foutH << endl;
// Convert to GBA-friendly charmap data.
const uint32_t* pRead = pLayerGfx->GetData ();
const uint32_t* pPalRead = pLayerPal == nullptr ? nullptr : pLayerPal->GetData ();
vector<uint16_t> vucCharDat;
vucCharDat.reserve ( pLayerGfx->GetWidth () * pLayerGfx->GetHeight () );
for ( size_t i = 0; i < size_t(pLayerGfx->GetWidth () * pLayerGfx->GetHeight ()); ++i )
// 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;
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 uiRead = (*pRead++);
uint32_t read = (*gfxTiles++);
uint16_t usTile = (uint16_t)max<int32_t> ( 0, tmx.LidFromGid ( uiRead & ~FLIP_MASK ) + params.offset );
uint8_t ucFlags = 0x0;
uint16_t tile = std::max(0, static_cast<int>(tmx.LidFromGid(read & ~TmxLayer::FLIP_MASK)) + p.offset);
uint8_t flags = 0x0;
// Get flipped!
ucFlags |= ( uiRead & FLIP_HORZ ) ? 0x4 : 0x0;
ucFlags |= ( uiRead & FLIP_VERT ) ? 0x8 : 0x0;
flags |= (read & TmxLayer::FLIP_HORZ) ? 0x4 : 0x0;
flags |= (read & TmxLayer::FLIP_VERT) ? 0x8 : 0x0;
// Determine palette ID.
uint32_t uiIndex = 0;
if ( pPalRead != nullptr )
{
uiIndex = tmx.LidFromGid ( (*pPalRead++) & ~FLIP_MASK );
}
if ( uiIndex == 0 )
{
uiIndex = params.palette + 1;
}
ucFlags |= (uint8_t)(uiIndex - 1) << 4;
// Determine palette ID
uint32_t idx = 0;
if (palTiles != nullptr)
idx = tmx.LidFromGid((*palTiles++) & ~TmxLayer::FLIP_MASK);
if (idx == 0)
idx = p.palette + 1;
flags |= static_cast<uint8_t>(idx - 1) << 4;
vucCharDat.push_back ( usTile | ( uint16_t(ucFlags) << 8 ) );
charDat.push_back(tile | (static_cast<uint16_t>(flags) << 8));
}
// Save out charmap.
foutH << "#define " << name << "TilesLen " << vucCharDat.size () * 2 << endl;
foutH << "extern const unsigned short " << name << "Tiles[" << vucCharDat.size () << "];" << endl;
foutH << endl;
// Write out charmap
outH.WriteSize(tmx.GetWidth(), tmx.GetHeight());
outH.WriteCharacterMap(charDat);
outS.WriteArray("Tiles", charDat);
foutS << "\t.section .rodata" << endl;
foutS << "\t.align 2" << endl;
foutS << "\t.global " << name << "Tiles" << endl;
foutS << "\t.hidden " << name << "Tiles" << endl;
foutS << name << "Tiles" << ":" << endl;
WriteArray<uint16_t> ( foutS, vucCharDat );
foutS << endl;
// Convert collision map & save it out.
if ( pLayerCls != nullptr )
// Convert collision map & write it out
if (layerCls != nullptr)
{
vector<uint8_t> vucCollisionDat;
vucCollisionDat.reserve ( pLayerCls->GetWidth () * pLayerCls->GetHeight () );
std::vector<uint8_t> collisionDat;
collisionDat.reserve(layerCls->GetWidth() * layerCls->GetHeight());
pRead = pLayerCls->GetData ();
for ( int i = 0; i < pLayerCls->GetWidth () * pLayerCls->GetHeight (); ++i )
gfxTiles = layerCls->GetData();
for (int i = 0; i < layerCls->GetWidth() * layerCls->GetHeight(); ++i)
{
uint8_t ucTile = (uint8_t)tmx.LidFromGid ( (*pRead++) & ~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.
string strPath;
size_t extPos = params.outPath.find_last_of ( '.' );
if ( extPos != string::npos )
{
strPath = params.outPath.insert ( extPos, "_collision" );
}
// Try to nicely append "_collision" to the output name
std::string path;
size_t extPos = p.outPath.find_last_of('.');
if (extPos != std::string::npos)
path = p.outPath.insert(extPos, "_collision");
else
{
strPath = params.outPath + "_collision";
path = p.outPath + "_collision";
// Write collision
outH.WriteCollision(collisionDat);
outS.WriteArray("Collision", collisionDat, 32);
}
// Save it out.
foutH << "#define " << name << "CollisionLen " << vucCollisionDat.size () << endl;
foutH << "extern const unsigned char " << name << "Collision[" << vucCollisionDat.size () << "];" << endl;
foutH << endl;
foutS << endl;
foutS << "\t.section .rodata" << endl;
foutS << "\t.align 2" << endl;
foutS << "\t.global " << name << "Collision" << endl;
foutS << "\t.hidden " << name << "Collision" << endl;
foutS << name << "Collision" << ":" << endl;
WriteArray<uint8_t> ( foutS, vucCollisionDat );
foutS << endl;
}
if ( params.objExport )
if (!p.objMappings.empty())
{
vector<uint32_t> objDat;
for ( int i = 0; i < tmx.GetObjectCount (); ++i )
std::vector<uint32_t> objDat;
for (size_t i = 0; i < tmx.GetObjectCount(); ++i)
{
auto obj = tmx.GetObject(i);
auto it = objMapping.find(obj->GetName());
if (it == objMapping.end())
{
continue;
}
float x, y;
obj->GetPos ( &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));
}
// Save it out.
foutH << "#define " << name << "ObjCount " << objDat.size () / 3 << endl;
foutH << "#define " << name << "ObjdatLen " << objDat.size () * sizeof(int) << endl;
foutH << "extern const unsigned int " << name << "Objdat[" << objDat.size () << "];" << endl;
foutH << endl;
foutS << endl;
foutS << "\t.section .rodata" << endl;
foutS << "\t.align 2" << endl;
foutS << "\t.global " << name << "Objdat" << endl;
foutS << "\t.hidden " << name << "Objdat" << endl;
foutS << name << "Objdat" << ":" << endl;
WriteArray<uint32_t> ( foutS, objDat );
foutS << endl;
// Write objects
outH.WriteObjects(objDat);
outS.WriteArray("Objdat", objDat);
}
foutH << "#endif//" << guard << endl;
foutH.close ();
foutS.close ();
return 0;
}

View File

@@ -1,68 +0,0 @@
/* tmxlayer.cpp
Copyright (C) 2015 Nicholas Curtis
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.
*/
#include "tmxlayer.h"
CTmxLayer::CTmxLayer () :
m_iWidth ( 0 ),
m_iHeight ( 0 ),
m_strName ( "" ),
m_pTileDat ( nullptr )
{
}
CTmxLayer::CTmxLayer ( int a_iWidth, int a_iHeight, const char* a_szName, uint32_t* a_pTileDat ) :
m_iWidth ( a_iWidth ),
m_iHeight ( a_iHeight ),
m_strName ( a_szName ),
m_pTileDat ( a_pTileDat )
{
}
CTmxLayer::~CTmxLayer ()
{
delete[] m_pTileDat;
}
const std::string& CTmxLayer::GetName () const
{
return m_strName;
}
int CTmxLayer::GetWidth () const
{
return m_iWidth;
}
int CTmxLayer::GetHeight () const
{
return m_iHeight;
}
const uint32_t* CTmxLayer::GetData () const
{
return m_pTileDat;
}

View File

@@ -1,31 +0,0 @@
#ifndef __TMXLAYER_H__
#define __TMXLAYER_H__
#include <string>
#include <cstdint>
const uint32_t FLIP_HORZ = 0x80000000;
const uint32_t FLIP_VERT = 0x40000000;
const uint32_t FLIP_DIAG = 0x20000000;
const uint32_t FLIP_MASK = 0xE0000000;
class CTmxLayer
{
public:
CTmxLayer ();
CTmxLayer ( int a_iWidth, int a_iHeight, const char* a_szName, uint32_t* a_pTileDat );
~CTmxLayer ();
const std::string& GetName () const;
int GetWidth () const;
int GetHeight () const;
const uint32_t* GetData () const;
private:
std::string m_strName;
int m_iWidth, m_iHeight;
uint32_t* m_pTileDat;
};
#endif//__TMXLAYER_H__

34
src/tmxlayer.hpp Normal file
View File

@@ -0,0 +1,34 @@
/* tmxlayer.hpp - Copyright (C) 2015-2022 a dinosaur (zlib, see COPYING.txt) */
#ifndef TMXLAYER_HPP
#define TMXLAYER_HPP
#include <string>
#include <cstdint>
#include <utility>
class TmxLayer
{
public:
static constexpr uint32_t FLIP_HORZ = 0x80000000;
static constexpr uint32_t FLIP_VERT = 0x40000000;
static constexpr uint32_t FLIP_DIAG = 0x20000000;
static constexpr uint32_t FLIP_MASK = 0xE0000000;
TmxLayer() : mWidth(0), mHeight(0), mTileDat(nullptr) {}
TmxLayer(int aWidth, int aHeight, std::string aName, uint32_t* aTileDat)
: mName(std::move(aName)), mWidth(aWidth), mHeight(aHeight), mTileDat(aTileDat) {}
inline ~TmxLayer() { delete[] mTileDat; }
constexpr const std::string& GetName() const { return mName; }
constexpr int GetWidth() const { return mWidth; }
constexpr int GetHeight() const { return mHeight; }
constexpr const uint32_t* GetData() const { return mTileDat; }
private:
std::string mName;
int mWidth, mHeight;
uint32_t* mTileDat;
};
#endif//TMXLAYER_HPP

View File

@@ -1,61 +0,0 @@
/* tmxobject.cpp
Copyright (C) 2015 Nicholas Curtis
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.
*/
#include "tmxobject.h"
CTmxObject::CTmxObject () :
m_name ( "" ),
m_x ( 0.0f ), m_y ( 0.0f )
{
}
CTmxObject::CTmxObject ( const std::string& a_name, float a_x, float a_y ) :
m_name ( a_name ),
m_x ( a_x ), m_y ( a_y )
{
}
CTmxObject::~CTmxObject ()
{
}
const std::string& CTmxObject::GetName () const
{
return m_name;
}
void CTmxObject::GetPos ( float* a_outX, float* a_outY ) const
{
if ( a_outX != nullptr )
{
*a_outX = m_x;
}
if ( a_outY != nullptr )
{
*a_outY = m_y;
}
}

View File

@@ -1,21 +0,0 @@
#ifndef __TMXOBJECT_H__
#define __TMXOBJECT_H__
#include <string>
class CTmxObject
{
public:
CTmxObject ();
CTmxObject ( const std::string& a_name, float a_x, float a_y );
~CTmxObject ();
const std::string& GetName () const;
void GetPos ( float* a_outX, float* a_outY ) const;
private:
std::string m_name;
float m_x, m_y;
};
#endif//__TMXOBJECT_H__

25
src/tmxobject.hpp Normal file
View File

@@ -0,0 +1,25 @@
/* tmxobject.hpp - Copyright (C) 2015-2022 a dinosaur (zlib, see COPYING.txt) */
#ifndef TMXOBJECT_HPP
#define TMXOBJECT_HPP
#include <string>
#include <utility>
class TmxObject
{
public:
TmxObject() : mX(0.0f), mY(0.0f) {}
TmxObject(std::string aName, float aX, float aY)
: mName(std::move(aName)), mX(aX), mY(aY) {}
~TmxObject() = default;
constexpr const std::string& GetName() const { return mName; }
inline void GetPos(float& aOutX, float& aOutY) const { aOutX = mX; aOutY = mY; }
private:
std::string mName;
float mX, mY;
};
#endif//TMXOBJECT_HPP

View File

@@ -1,29 +1,9 @@
/* tmxreader.cpp
/* tmxreader.cpp - Copyright (C) 2015-2022 a dinosaur (zlib, see COPYING.txt) */
Copyright (C) 2015 Nicholas Curtis
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.
*/
#include "tmxreader.h"
#include "tmxtileset.h"
#include "tmxobject.h"
#include "tmxlayer.h"
#include "tmxreader.hpp"
#include "tmxtileset.hpp"
#include "tmxobject.hpp"
#include "tmxlayer.hpp"
#include <cstdint>
#include <sstream>
#include <algorithm>
@@ -32,296 +12,216 @@
#include <miniz.h>
CTmxReader::CTmxReader ()
{
}
CTmxReader::~CTmxReader ()
{
// Delete old tilesets.
for ( auto pTileset : m_tileset )
TmxReader::~TmxReader()
{
// Delete old tilesets
for (auto pTileset : mTilesets)
delete pTileset;
}
m_tileset.clear ();
mTilesets.clear();
// Delete old layers.
for ( auto pLay : m_layers )
{
// Delete old layers
for (auto pLay : mLayers)
delete pLay;
}
m_layers.clear ();
mLayers.clear();
}
bool CTmxReader::DecodeMap ( uint32_t* a_pOut, size_t a_outSize, const std::string& a_strIn )
{
// Decode base64 string.
size_t cutTheCrap = a_strIn.find_first_not_of ( " \t\n\r" );
std::string strDec = base64_decode ( a_strIn.substr ( cutTheCrap ) );
// Decompress compressed data.
mz_ulong uiDstSize = a_outSize;
int iRes = uncompress (
(unsigned char*)a_pOut, &uiDstSize,
(const unsigned char*)strDec.data (), strDec.size ()
);
strDec.clear ();
if ( iRes < 0 )
bool TmxReader::DecodeMap(uint32_t* aOut, size_t aOutSize, const std::string& aBase64Dat)
{
// Cut leading & trailing whitespace (including newlines)
auto beg = std::find_if_not(aBase64Dat.begin(), aBase64Dat.end(), ::isspace);
if (beg == std::end(aBase64Dat))
return false;
auto end = std::find_if_not(aBase64Dat.rbegin(), aBase64Dat.rend(), ::isspace);
std::size_t begOff = std::distance(aBase64Dat.begin(), beg);
std::size_t endOff = std::distance(end, aBase64Dat.rend()) - begOff;
auto trimmed = aBase64Dat.substr(begOff, endOff);
// Decode base64 string
std::string decoded = base64_decode(trimmed);
// Decompress compressed data
auto dstSize = static_cast<mz_ulong>(aOutSize);
int res = uncompress(
reinterpret_cast<unsigned char*>(aOut),
&dstSize,
reinterpret_cast<const unsigned char*>(decoded.data()),
static_cast<mz_ulong>(decoded.size()));
decoded.clear();
if (res < 0)
return false;
}
return true;
}
void CTmxReader::ReadTileset ( rapidxml::xml_node<>* a_xNode )
void TmxReader::ReadTileset(rapidxml::xml_node<>* aXNode)
{
rapidxml::xml_attribute<>* xAttrib;
const char* szName = "";
const char* szSource = "";
uint32_t uiFirstGid = 0;
const char* name = "";
const char* source = "";
uint32_t firstGid = 0;
// Read name.
xAttrib = a_xNode->first_attribute ( "name" );
// Read name
xAttrib = aXNode->first_attribute("name");
if (xAttrib != nullptr)
{
szName = xAttrib->value ();
}
name = xAttrib->value();
// Read source.
xAttrib = a_xNode->first_attribute ( "source" );
// Read source
xAttrib = aXNode->first_attribute("source");
if (xAttrib != nullptr)
{
szSource = xAttrib->value ();
}
source = xAttrib->value();
// Read first global ID.
xAttrib = a_xNode->first_attribute ( "firstgid" );
// Read first global ID
xAttrib = aXNode->first_attribute("firstgid");
if (xAttrib != nullptr)
{
uiFirstGid = std::stoul ( xAttrib->value () );
firstGid = static_cast<uint32_t>(std::stoul(xAttrib->value()));
mTilesets.push_back(new TmxTileset(name, source, firstGid));
}
m_tileset.push_back ( new CTmxTileset ( szName, szSource, uiFirstGid ) );
}
void CTmxReader::ReadLayer ( rapidxml::xml_node<>* a_xNode )
void TmxReader::ReadLayer(rapidxml::xml_node<>* aXNode)
{
rapidxml::xml_attribute<>* xAttrib;
const char* szName = "";
int iWidth = 0;
int iHeight = 0;
uint32_t* pTileDat = nullptr;
const char* name = "";
int width = 0;
int height = 0;
uint32_t* tileDat = nullptr;
// Read name.
xAttrib = a_xNode->first_attribute ( "name" );
// Read name
xAttrib = aXNode->first_attribute("name");
if (xAttrib != nullptr)
{
szName = xAttrib->value ();
}
name = xAttrib->value();
// Read width.
xAttrib = a_xNode->first_attribute ( "width" );
// Read width
xAttrib = aXNode->first_attribute("width");
if (xAttrib != nullptr)
{
iWidth = std::stoi ( xAttrib->value () );
}
width = std::stoi(xAttrib->value());
// Read height.
xAttrib = a_xNode->first_attribute ( "height" );
// Read height
xAttrib = aXNode->first_attribute("height");
if (xAttrib != nullptr)
{
iHeight = std::stoi ( xAttrib->value () );
}
height = std::stoi(xAttrib->value());
// Read tile data.
auto xData = a_xNode->first_node ( "data" );
// Read tile data
auto xData = aXNode->first_node("data");
if (xData != nullptr)
{
// TODO: don't assume base64 & zlib.
pTileDat = new uint32_t[iWidth * iHeight];
if ( !DecodeMap ( pTileDat, iWidth * iHeight * sizeof(uint32_t), std::string ( xData->value () ) ) )
// TODO: don't assume base64 & zlib
tileDat = new uint32_t[width * height];
if (!DecodeMap(tileDat, width * height * sizeof(uint32_t), std::string(xData->value())))
{
delete[] pTileDat;
pTileDat = nullptr;
delete[] tileDat;
tileDat = nullptr;
}
}
m_layers.push_back ( new CTmxLayer ( iWidth, iHeight, szName, pTileDat ) );
mLayers.push_back(new TmxLayer(width, height, name, tileDat));
}
void CTmxReader::ReadObjects ( rapidxml::xml_node<>* a_xNode )
void TmxReader::ReadObjects(rapidxml::xml_node<>* aXNode)
{
for ( auto xNode = a_xNode->first_node (); xNode != nullptr; xNode = xNode->next_sibling () )
for (auto xNode = aXNode->first_node(); xNode != nullptr; xNode = xNode->next_sibling())
{
if (strcmp(xNode->name(), "object") != 0)
{
continue;
}
rapidxml::xml_attribute<>* xAttrib;
const char* name = "";
float x = 0.0f;
float y = 0.0f;
// Read name.
// Read name
xAttrib = xNode->first_attribute("name");
if (xAttrib != nullptr)
{
name = xAttrib->value();
}
// Read X pos.
// Read X pos
xAttrib = xNode->first_attribute("x");
if (xAttrib != nullptr)
{
x = std::stof(xAttrib->value());
}
// Read Y pos.
// Read Y pos
xAttrib = xNode->first_attribute("y");
if (xAttrib != nullptr)
{
y = std::stof(xAttrib->value());
}
m_objects.push_back ( new CTmxObject ( name, x, y ) );
mObjects.push_back(new TmxObject(name, x, y));
}
}
void CTmxReader::Open ( std::istream& a_in )
void TmxReader::Open(std::istream& aIn)
{
// Delete old tilesets.
for ( auto pTileset : m_tileset )
{
delete pTileset;
}
m_tileset.clear ();
// Delete old tilesets
for (auto tileset : mTilesets)
delete tileset;
mTilesets.clear();
// Delete old layers.
for ( auto pLay : m_layers )
{
delete pLay;
}
m_layers.clear ();
// Delete old layers
for (auto layer : mLayers)
delete layer;
mLayers.clear();
m_gidTable.clear ();
mGidTable.clear();
// Read string into a buffer.
// Read string into a buffer
std::stringstream buf;
buf << a_in.rdbuf ();
buf << aIn.rdbuf();
std::string strXml = buf.str();
buf.clear();
// Parse document.
// 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.
// Get map node
auto xMap = xDoc.first_node("map");
if (xMap == nullptr)
{
return;
}
// Read map attribs.
// Read map attribs
rapidxml::xml_attribute<>* xAttrib = nullptr;
if ((xAttrib = xMap->first_attribute("width")) != nullptr)
{
m_width = std::stoi ( xAttrib->value () );
}
mWidth = std::stoi(xAttrib->value());
if ((xAttrib = xMap->first_attribute("height")) != nullptr)
{
m_height = std::stoi ( xAttrib->value () );
}
mHeight = std::stoi(xAttrib->value());
// Read nodes.
// Read nodes
for (auto xNode = xMap->first_node(); xNode != nullptr; xNode = xNode->next_sibling())
{
// Read layer nodes.
// Read layer nodes
if (strcmp(xNode->name(), "layer") == 0)
{
ReadLayer(xNode);
}
else
if (strcmp(xNode->name(), "tileset") == 0)
{
ReadTileset(xNode);
}
else
if (strcmp(xNode->name(), "objectgroup") == 0)
{
ReadObjects(xNode);
}
// Generate global id table
for (auto tileset : mTilesets)
mGidTable.push_back(tileset->GetFirstGid());
std::sort(mGidTable.rbegin(), mGidTable.rend());
}
// Generate global id table.
for ( auto pTileset : m_tileset )
const TmxLayer* TmxReader::GetLayer(const std::string& aName) const
{
m_gidTable.push_back ( pTileset->GetFirstGid () );
}
std::sort ( m_gidTable.rbegin (), m_gidTable.rend () );
}
int CTmxReader::GetWidth () const
for (auto layer : mLayers)
{
return m_width;
if (layer->GetName() == aName)
return layer;
}
int CTmxReader::GetHeight () const
{
return m_height;
}
const CTmxLayer* CTmxReader::GetLayer ( int a_id ) const
{
return m_layers[a_id];
}
const CTmxLayer* CTmxReader::GetLayer ( std::string a_strName ) const
{
for ( auto pLay : m_layers )
{
if ( pLay->GetName ().compare ( a_strName ) == 0 )
{
return pLay;
}
}
return nullptr;
}
int CTmxReader::GetLayerCount () const
uint32_t TmxReader::LidFromGid(uint32_t aGid)
{
return m_layers.size ();
}
const CTmxObject* CTmxReader::GetObject ( int a_id ) const
for (uint32_t first : mGidTable)
{
return m_objects[a_id];
if (first <= aGid)
return aGid - (first - 1);
}
int CTmxReader::GetObjectCount () const
{
return m_objects.size ();
}
uint32_t CTmxReader::LidFromGid ( uint32_t a_uiGid )
{
for ( uint32_t uiFirst : m_gidTable )
{
if ( uiFirst <= a_uiGid )
{
return a_uiGid - ( uiFirst - 1 );
}
}
return a_uiGid;
return aGid;
}

View File

@@ -1,48 +0,0 @@
#ifndef __TMXREADER_H__
#define __TMXREADER_H__
#include <istream>
#include <vector>
#include <string>
#include <cstdint>
#include <rapidxml/rapidxml.hpp>
class CTmxTileset;
class CTmxLayer;
class CTmxObject;
class CTmxReader
{
public:
CTmxReader ();
~CTmxReader ();
void Open ( std::istream& a_in );
int GetWidth () const;
int GetHeight () const;
const CTmxLayer* GetLayer ( int a_id ) const;
const CTmxLayer* GetLayer ( std::string a_strName ) const;
int GetLayerCount () const;
const CTmxObject* GetObject ( int a_id ) const;
int GetObjectCount () const;
uint32_t LidFromGid ( uint32_t a_gid );
private:
bool DecodeMap ( uint32_t* a_out, size_t a_outSize, const std::string& a_strIn );
void ReadTileset ( rapidxml::xml_node<>* a_xNode );
void ReadLayer ( rapidxml::xml_node<>* a_xNode );
void ReadObjects ( rapidxml::xml_node<>* a_xNode );
int m_width, m_height;
std::vector<CTmxTileset*> m_tileset;
std::vector<CTmxLayer*> m_layers;
std::vector<CTmxObject*> m_objects;
std::vector<uint32_t> m_gidTable;
};
#endif//__TMXREADER_H__

50
src/tmxreader.hpp Normal file
View File

@@ -0,0 +1,50 @@
/* tmxreader.hpp - Copyright (C) 2015-2022 a dinosaur (zlib, see COPYING.txt) */
#ifndef TMXREADER_HPP
#define TMXREADER_HPP
#include <istream>
#include <vector>
#include <string>
#include <cstdint>
#include <rapidxml/rapidxml.hpp>
class TmxTileset;
class TmxLayer;
class TmxObject;
class TmxReader
{
public:
TmxReader() = default;
~TmxReader();
void Open(std::istream& aIn);
constexpr int GetWidth() const { return mWidth; }
constexpr int GetHeight() const { return mHeight; }
inline const TmxLayer* GetLayer(size_t aId) const { return mLayers.at(aId); }
const TmxLayer* GetLayer(const std::string& aName) const;
inline std::size_t GetLayerCount() const { return mLayers.size(); }
inline const TmxObject* GetObject(size_t aId) const { return mObjects.at(aId); }
inline size_t GetObjectCount() const { return mObjects.size(); }
uint32_t LidFromGid(uint32_t aGid);
private:
static bool DecodeMap(uint32_t* aOut, size_t aOutSize, const std::string& aBase64Dat);
void ReadTileset(rapidxml::xml_node<>* aXNode);
void ReadLayer(rapidxml::xml_node<>* aXNode);
void ReadObjects(rapidxml::xml_node<>* aXNode);
int mWidth, mHeight;
std::vector<TmxTileset*> mTilesets;
std::vector<TmxLayer*> mLayers;
std::vector<TmxObject*> mObjects;
std::vector<uint32_t> mGidTable;
};
#endif//TMXREADER_HPP

View File

@@ -1,60 +0,0 @@
/* tmxtileset.cpp
Copyright (C) 2015 Nicholas Curtis
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.
*/
#include "tmxtileset.h"
CTmxTileset::CTmxTileset () :
m_strName ( "" ),
m_strSource ( "" ),
m_uiFirstGid ( 0 )
{
}
CTmxTileset::CTmxTileset ( const char* a_szName, const char* a_szSource, uint32_t a_uiFirstGid ) :
m_strName ( a_szName ),
m_strSource ( a_szSource ),
m_uiFirstGid ( a_uiFirstGid )
{
}
CTmxTileset::~CTmxTileset ()
{
}
const std::string& CTmxTileset::GetName () const
{
return m_strName;
}
const std::string& CTmxTileset::GetSource () const
{
return m_strSource;
}
uint32_t CTmxTileset::GetFirstGid () const
{
return m_uiFirstGid;
}

View File

@@ -1,25 +0,0 @@
#ifndef __TMXTILESET_H__
#define __TMXTILESET_H__
#include <string>
#include <cstdint>
class CTmxTileset
{
public:
CTmxTileset ();
CTmxTileset ( const char* a_szName, const char* a_szSource, uint32_t a_uiFirstGid );
~CTmxTileset ();
const std::string& GetName () const;
const std::string& GetSource () const;
uint32_t GetFirstGid () const;
private:
std::string m_strName;
std::string m_strSource;
uint32_t m_uiFirstGid;
};
#endif//__TMXTILESET_H__

28
src/tmxtileset.hpp Normal file
View File

@@ -0,0 +1,28 @@
/* tmxtileset.hpp - Copyright (C) 2015-2022 a dinosaur (zlib, see COPYING.txt) */
#ifndef TMXTILESET_HPP
#define TMXTILESET_HPP
#include <string>
#include <cstdint>
#include <utility>
class TmxTileset
{
public:
TmxTileset() : mFirstGid(0) {}
TmxTileset(std::string aName, std::string aSource, uint32_t aFirstGid)
: mName(std::move(aName)), mSource(std::move(aSource)), mFirstGid(aFirstGid) {}
~TmxTileset() = default;
constexpr const std::string& GetName() const { return mName; }
constexpr const std::string& GetSource() const { return mSource; }
constexpr uint32_t GetFirstGid() const { return mFirstGid; }
private:
std::string mName;
std::string mSource;
uint32_t mFirstGid;
};
#endif//TMXTILESET_HPP

View File

@@ -1,826 +0,0 @@
/* Ultra-Getopt - A replacement for getopt() with support for many common
* extensions, MS-DOS formatted option strings, and much more.
*
* To use this library as a replacement for vendor-provided getopt() functions,
* define ULTRAGETOPT_REPLACE_GETOPT and include "ultragetopt.h" after the
* vendor-provided headers for getopt() functions.
*
* Copyright (C) 2007-2011, Kevin Locke <kevin@kevinlocke.name>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <ctype.h> /* islower() isupper() tolower() toupper() */
#include <stdarg.h>
#include <stdio.h> /* fprintf() */
#include <stdlib.h> /* getenv() */
#include <string.h> /* strcmp(), strncmp(), strchr() */
#if HAVE_STRINGS_H
# include <strings.h> /* strcasecmp(), strncasecmp() */
#endif
#undef ULTRAGETOPT_REPLACE_GETOPT /* Protect against project-wide defines */
#include "ultragetopt.h"
/* Define replacements for missing functions */
#if !HAVE_STRCASECMP && HAVE__STRICMP
# define strcasecmp _stricmp
#endif
#if !HAVE_STRNCASECMP && HAVE__STRNICMP
# define strncasecmp _strnicmp
#endif
#if !HAVE_STRCHR && HAVE_INDEX
# define strchr index
#endif
/* Supported defines:
* ULTRAGETOPT_LIKE_BSD Behave like BSD getopt()
* ULTRAGETOPT_LIKE_DARWIN Behave like Darwin (Mac OS) getopt()
* ULTRAGETOPT_LIKE_GNU Behave like GNU getopt()
* ULTRAGETOPT_LIKE_POSIX Behave like POSIX definition of getopt()
*
* ULTRAGETOPT_ASSIGNSPACE Parse "-o value" as "value" rather than " value"
* Note: Only applicable when argv[x] == "-o value"
* Not for argv[x] == "-o" [x+1] == "value"
* ULTRAGETOPT_DEFAULTOPTOPT Set optopt to this value by default on each
* call to getopt()
* ULTRAGETOPT_HYPHENARG Accept -option -arg as -option with argument
* "-arg" rather than -option missing argument
* Note: A single "-" is always accepted as an
* argument
* ULTRAGETOPT_LONGOPTADJACENT Accept adjacent arguments to long options
* (e.g. --optionarg) based on first longest-match
* ULTRAGETOPT_OPTIONPERMUTE Permute options, do not stop at first non-option
* Behaves like GNU getopt where leading '+' or
* $POSIXLY_CORRECT both stop this @ runtime
* ULTRAGETOPT_SHORTOPTASSIGN Support -o=file syntax for short options
* ULTRAGETOPT_SEPARATEDOPTIONAL Accept separated optional arguments
* Parse -o file as -o with argument file
* ULTRAGETOPT_DOS_DASH Support - and -- options in ultragetopt*_dos()
* ULTRAGETOPT_BSD_ERRORS Print error messages matching BSD getopt
* ULTRAGETOPT_DARWIN_ERRORS Print error messages matching Darwin getopt
* ULTRAGETOPT_GNU_ERRORS Print error messages matching GNU getopt
* ULTRAGETOPT_NO_EATDASHDASH Do not increment optind when argv[optind] is --
* ULTRAGETOPT_NO_OPTIONALARG Do not support GNU "::" optional argument
* Always supported in *_long*()
* ULTRAGETOPT_NO_OPTIONASSIGN Do not support --option=value syntax
*/
#ifdef ULTRAGETOPT_LIKE_POSIX
# define ULTRAGETOPT_NO_OPTIONALARG
# define ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_BSD_ERRORS
# undef ULTRAGETOPT_DARWIN_ERRORS
# undef ULTRAGETOPT_GNU_ERRORS
# undef ULTRAGETOPT_OPTIONPERMUTE
# undef ULTRAGETOPT_SHORTOPTASSIGN
#elif defined(ULTRAGETOPT_LIKE_GNU)
# define ULTRAGETOPT_GNU_ERRORS
# define ULTRAGETOPT_HYPHENARG
# define ULTRAGETOPT_OPTIONPERMUTE
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_NO_OPTIONALARG
# undef ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_SHORTOPTASSIGN
# undef ULTRAGETOPT_SEPARATEOPTIONAL
# undef ULTRAGETOPT_LONGOPTADJACENT
#elif defined(ULTRAGETOPT_LIKE_BSD)
# define ULTRAGETOPT_BSD_ERRORS
# define ULTRAGETOPT_OPTIONPERMUTE
# define ULTRAGETOPT_DEFAULTOPTOPT '?'
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_NO_OPTIONALARG
# undef ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_SHORTOPTASSIGN
# undef ULTRAGETOPT_SEPARATEOPTIONAL
# undef ULTRAGETOPT_LONGOPTADJACENT
#elif defined(ULTRAGETOPT_LIKE_DARWIN)
# define ULTRAGETOPT_DARWIN_ERRORS
# define ULTRAGETOPT_OPTIONPERMUTE
# undef ULTRAGETOPT_ASSIGNSPACE
# undef ULTRAGETOPT_NO_OPTIONALARG
# undef ULTRAGETOPT_NO_OPTIONASSIGN
# undef ULTRAGETOPT_SHORTOPTASSIGN
# undef ULTRAGETOPT_NO_EATDASHDASH
# undef ULTRAGETOPT_SEPARATEOPTIONAL
# undef ULTRAGETOPT_LONGOPTADJACENT
#endif
#ifdef ULTRAGETOPT_NO_OPTIONASSIGN
static const char *const unixassigners = "";
static const char *const dosassigners = ":";
#elif defined(ULTRAGETOPT_OPTIONSPACE)
static const char *const unixassigners = "= ";
static const char *const dosassigners = ":= ";
#else
static const char *const unixassigners = "=";
static const char *const dosassigners = ":=";
#endif
#ifdef ULTRAGETOPT_DOS_DASH
static const char *const unixleaders = "-";
static const char *const dosleaders = "/-";
#else
static const char *const unixleaders = "-";
static const char *const dosleaders = "/";
#endif
/* Flags for all variants of ultragetopt*() */
static const int getoptflags = 0
#ifdef ULTRAGETOPT_SEPARATEDOPTIONAL
| UGO_SEPARATEDOPTIONAL
#endif
#ifdef ULTRAGETOPT_SHORTOPTASSIGN
| UGO_SHORTOPTASSIGN
#endif
#ifdef ULTRAGETOPT_NO_EATDASHDASH
| UGO_NOEATDASHDASH
#endif
#ifdef ULTRAGETOPT_HYPHENARG
| UGO_HYPHENARG
#endif
;
#ifdef ULTRAGETOPT_GNU_ERRORS
static const char *const errorarg =
"%s: option `%.*s' doesn't allow an argument\n";
static const char *const errornoarg =
"%s: option `%.*s' requires an argument \n";
static const char *const erroropt =
"%s: unrecognized option `%.*s'\n";
static const char *const errorargc =
"%s: option `-%c' does not take an argument\n";
static const char *const errornoargc =
"%s: option requires an argument -- `-%c'\n";
static const char *const erroroptc =
"%s: invalid option -- %c\n";
#elif defined(ULTRAGETOPT_BSD_ERRORS)
static const char *const errorarg =
"%s: option doesn't take an argument -- %.*s\n";
static const char *const errornoarg =
"%s: option requires an argument -- %.*s\n";
static const char *const erroropt =
"%s: unknown option -- %.*s\n";
static const char *const errorargc =
"%s: option doesn't take an argument -- %c\n";
static const char *const errornoargc =
"%s: option requires an argument -- %c\n";
static const char *const erroroptc =
"%s: unknown option -- %c\n";
#elif defined(ULTRAGETOPT_DARWIN_ERRORS)
static const char *const errorarg =
"%s: option `%.*s' doesn't allow an argument\n"; /* with -- */
static const char *const errornoarg =
"%s: option `%.*s' requires an argument\n";
static const char *const erroropt =
"%s: unrecognized option `%.*s'\n"; /* with -- */
static const char *const errorargc =
"%s: option doesn't take an argument -- %c\n";
static const char *const errornoargc =
"%s: option requires an argument -- %c\n";
static const char *const erroroptc =
"%s: invalid option -- %c\n";
#else /* POSIX-like */
static const char *const errorarg =
"%s: option does not take an argument -- %.*s\n";
static const char *const errornoarg =
"%s: option requires an argument -- %.*s\n";
static const char *const erroropt =
"%s: illegal option -- %.*s\n";
static const char *const errorargc =
"%s: option does not take an argument -- %c\n";
static const char *const errornoargc =
"%s: option requires an argument -- %c\n";
static const char *const erroroptc =
"%s: illegal option -- %c\n";
#endif
/* Globals to match optarg, optind, opterr, optopt, optreset */
char *ultraoptarg = NULL;
int ultraoptind = 1;
int ultraopterr = 1;
int ultraoptreset = 0;
#ifdef ULTRAGETOPT_DEFAULTOPTOPT
int ultraoptopt = ULTRAGETOPT_DEFAULTOPTOPT -0;
#else
int ultraoptopt = 0;
#endif
static int ultraoptnum = 0; /* How many options of the current multi-option
argument have been processed? (e.g. -vvv) */
/* Add format error checking for gcc versions that support it */
#if defined(__GNUC__) && __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR > 6)
static void print_error(int flags, const char *template, ...)
__attribute__ ((format (printf, 2, 3)));
#endif
/* Print errors only if not suppressed */
static void print_error(int flags, const char *template, ...)
{
va_list ap;
va_start(ap, template);
if (ultraopterr != 0 && !(flags & UGO_NOPRINTERR))
vfprintf(stderr, template, ap);
va_end(ap);
}
/* Check if an argument string looks like an option string */
static inline int like_option(const char *arg, const char *optleaders)
{
return arg != NULL &&
arg[0] != '\0' && /* >= 2 characters long */
arg[1] != '\0' &&
strchr(optleaders, arg[0]) && /* Starts with optleader */
(arg[2] != '\0' || arg[0] != arg[1]); /* Not -- */
}
/* Check if an argument string looks like the option terminator string */
static inline int like_optterm(const char *arg, const char *optleaders)
{
return arg != NULL &&
arg[0] != '\0' &&
arg[1] != '\0' &&
arg[2] == '\0' &&
arg[0] == arg[1] &&
strchr(optleaders, arg[0]);
}
/* Check if an argument string looks like an option argument string */
static inline int like_optarg(const char *arg, const char *optleaders,
int allow_option)
{
return arg != NULL &&
(allow_option ||
(!like_option(arg, optleaders) && !like_optterm(arg, optleaders)));
}
/* If argv[curopt] matches a long option, return the index of that option
* Otherwise, return -1
* If it has an adjacent argument, return pointer to it in longarg, else NULL
*/
static int match_longopt(int curopt, char *const argv[],
const struct option *longopts, const char *assigners,
const char *optleaders, int flags, char **longarg)
{
size_t alen, optnamelen = 0;
char *optname;
char *temp;
int i;
int (*optncmp)(const char *s1, const char *s2, size_t n);
if (longarg == NULL)
longarg = &temp;
*longarg = NULL;
if (flags & UGO_CASEINSENSITIVE)
optncmp = strncasecmp;
else
optncmp = strncmp;
if (longopts == NULL)
return -1;
if (!like_option(argv[curopt], optleaders))
return -1;
if (flags & UGO_SINGLELEADERONLY) {
optname = argv[curopt]+1;
} else if (!strchr(optleaders, argv[curopt][1])) {
/* Possible short option */
if (flags & UGO_SINGLELEADERLONG)
optname = argv[curopt]+1;
else
return -1;
} else {
optname = argv[curopt]+2;
}
/* Do first longest-match search if requested */
if (flags & UGO_LONGOPTADJACENT) {
size_t matchlen = 0;
int matchind = -1;
for (i=0; longopts[i].name != NULL; i++) {
size_t longnamelen = strlen(longopts[i].name);
if (longnamelen > matchlen
&& optncmp(optname, longopts[i].name, longnamelen) == 0) {
matchind = i;
matchlen = longnamelen;
}
}
if (matchlen > 0) {
/* See if our match has an adjacent argument */
if (optname[matchlen] != '\0') {
/* Strip assigner character if present */
if (strchr(assigners, optname[matchlen]))
*longarg = optname+matchlen+1;
else
*longarg = optname+matchlen;
}
return matchind;
}
return -1;
}
/* Check for assigner in the option */
alen = strlen(assigners);
for (i=0; (unsigned)i < alen; i++) {
char *asn = strchr(optname, assigners[i]);
if (asn != NULL) {
optnamelen = asn - optname;
*longarg = asn+1;
break;
}
}
if (optnamelen == 0)
optnamelen = strlen(optname);
for (i=0; longopts[i].name != NULL; i++)
if (optncmp(optname, longopts[i].name, optnamelen) == 0
&& strlen(longopts[i].name) == optnamelen)
return i;
return -1;
}
/* Check if an option has a separate argument (in the following argv[] index) */
static int has_separate_argument(int curopt, int argc, char *const argv[],
const char *shortopts,
const struct option *longopts,
const char *assigners, const char *optleaders,
int flags)
{
int longind;
char *longarg;
assert(curopt < argc && like_option(argv[curopt], optleaders));
/* Check if we have a long option */
longind = match_longopt(ultraoptind, argv, longopts, assigners, optleaders,
flags, &longarg);
if (longind >= 0) {
if (longopts[longind].has_arg == no_argument
|| longarg != NULL
|| (longopts[longind].has_arg == optional_argument
&& !(flags & UGO_SEPARATEDOPTIONAL)))
return 0;
return like_optarg(argv[curopt+1], optleaders,
(flags & UGO_HYPHENARG) &&
longopts[longind].has_arg == required_argument);
} else if (!strchr(optleaders, argv[curopt][1])) {
/* Short option */
char *optpos;
optpos = strchr(shortopts, argv[curopt][1]);
if ((flags & UGO_CASEINSENSITIVE) && optpos == NULL) {
if (islower(argv[curopt][1]))
optpos = strchr(shortopts, toupper(argv[curopt][1]));
else
optpos = strchr(shortopts, tolower(argv[curopt][1]));
}
return optpos != NULL /* Option found */
&& optpos[1] == ':' /* Option takes argument */
&& (optpos[2] != ':' || (flags & UGO_SEPARATEDOPTIONAL))
&& argv[curopt][2] == '\0' /* Argument is not adjacent */
&& like_optarg(argv[curopt+1], /* Is an argument */
optleaders,
(flags & UGO_HYPHENARG) && optpos[2] != ':');
}
/* No match */
return 0;
}
/* Bring the next option, or terminator, up to ultraoptind if there is one
* Returns number of words shifted forward
*/
static int permute_options(int argc, char *argv[], const char *shortopts,
const struct option *longopts,
const char *assigners, const char *optleaders,
int flags)
{
int curopt = ultraoptind;
/* If we already have an option or no more possible, give up */
if (curopt >= argc || like_option(argv[curopt], optleaders))
return 0;
for ( ; curopt < argc && argv[curopt]; curopt++) {
int shiftarg = 0;
int i;
/* Permute options and the option terminator */
if (like_option(argv[curopt], optleaders)) {
/* Check if we need to shift argument too */
shiftarg = has_separate_argument(curopt, argc, argv, shortopts,
longopts, assigners, optleaders,
flags);
} else if (!like_optterm(argv[curopt], optleaders)) {
continue;
}
/* Shift option */
for (i=curopt; i>ultraoptind; i--) {
char *temp = argv[i];
argv[i] = argv[i-1];
argv[i-1] = temp;
if (shiftarg) {
temp = argv[i+1];
argv[i+1] = argv[i];
argv[i] = temp;
}
}
/* All done */
if (shiftarg)
return 2;
else
return 1;
}
/* Couldn't find an option, bummer */
return 0;
}
/* Handle a longopts[longind] matches argv[ultraoptind] actions */
static int handle_longopt(int longind, char *longarg, int noseparg,
char *const argv[],
const struct option *longopts, int *indexptr,
const char *optleaders, int flags)
{
/* Handle assignment arguments */
if (longarg && longopts[longind].has_arg == no_argument) {
print_error(flags, errorarg, argv[0],
longarg-argv[ultraoptind]-1, argv[ultraoptind]);
/* TODO: What is a good value to put in ultraoptopt? */
/* Looks like GNU getopt() uses val */
ultraoptopt = longopts[longind].val;
ultraoptind++;
return '?';
} else if (longarg) {
ultraoptind++;
ultraoptarg = longarg;
if (indexptr)
*indexptr = longind;
if (longopts[longind].flag) {
*(longopts[longind].flag) = longopts[longind].val;
return 0;
} else
return longopts[longind].val;
}
/* Handle missing required argument */
if (longopts[longind].has_arg == required_argument
&& (noseparg
|| !like_optarg(argv[ultraoptind+1],
optleaders,
flags & UGO_HYPHENARG))) {
print_error(flags, errornoarg, argv[0],
strlen(argv[ultraoptind]), argv[ultraoptind]);
ultraoptind++;
if (flags & UGO_MISSINGCOLON)
return ':';
else
return '?';
}
/* Handle available argument */
if ((longopts[longind].has_arg == required_argument
|| (longopts[longind].has_arg == optional_argument
&& (flags & UGO_SEPARATEDOPTIONAL)))
&& !noseparg
&& like_optarg(argv[ultraoptind+1],
optleaders,
(flags & UGO_HYPHENARG) &&
longopts[longind].has_arg == required_argument)) {
ultraoptarg = argv[ultraoptind+1];
ultraoptind += 2;
} else
ultraoptind++;
if (indexptr)
*indexptr = longind;
if (longopts[longind].flag) {
*(longopts[longind].flag) = longopts[longind].val;
return 0;
} else
return longopts[longind].val;
}
int ultragetopt_tunable(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr,
const char *assigners, const char *optleaders,
int flags)
{
char *opt; /* Option we are processing */
char *optpos; /* Pointer to opt in shortopts */
int noseparg = 0; /* Force option not to have a separate argument */
if (ultraoptreset) {
ultraoptind = 1;
ultraopterr = 1;
ultraoptnum = 0;
ultraoptreset = 0;
}
ultraoptarg = NULL;
#ifdef ULTRAGETOPT_DEFAULTOPTOPT
ultraoptopt = ULTRAGETOPT_DEFAULTOPTOPT -0;
#endif
/* Sanity check (These are specified verbatim in SUS) */
if (ultraoptind > argc
|| argv[ultraoptind] == NULL)
return -1;
/* No permuting when $POSIXLY_CORRECT is set (to match GNU getopt) */
if (getenv("POSIXLY_CORRECT"))
flags &= ~UGO_OPTIONPERMUTE;
/* Get flags from shortopts */
for ( ; shortopts && *shortopts; shortopts++) {
if (*shortopts == '+')
flags &= ~UGO_OPTIONPERMUTE;
else if (*shortopts == '-')
flags |= UGO_NONOPTARG;
else if (*shortopts == ':') {
flags |= UGO_NOPRINTERR;
flags |= UGO_MISSINGCOLON;
} else
break;
}
/* Found non-option */
if (!like_option(argv[ultraoptind], optleaders)) {
int shifted;
if (like_optterm(argv[ultraoptind], optleaders)) {
if (!(flags & UGO_NOEATDASHDASH))
ultraoptind++;
return -1;
}
if (flags & UGO_NONOPTARG) {
ultraoptarg = argv[ultraoptind];
ultraoptind++;
return 1;
}
if (!(flags & UGO_OPTIONPERMUTE))
return -1;
shifted = permute_options(argc, (char **)argv, shortopts, longopts,
assigners, optleaders, flags);
if (shifted == 0)
return -1;
else if (shifted == 1)
noseparg = 1;
if (like_optterm(argv[ultraoptind], optleaders)) {
if (!(flags & UGO_NOEATDASHDASH))
ultraoptind++;
return -1;
}
}
/* At this point we must have an option of some sort */
assert(like_option(argv[ultraoptind], optleaders));
/* Handle --* */
if (argv[ultraoptind][0] == argv[ultraoptind][1]) {
int longind;
char *longarg;
/* Handle long option */
longind = match_longopt(ultraoptind, argv, longopts, assigners,
optleaders, flags, &longarg);
if (longind < 0) {
if (longarg == NULL)
print_error(flags, erroropt, argv[0],
strlen(argv[ultraoptind]), argv[ultraoptind]);
else
print_error(flags, erroropt, argv[0],
longarg - argv[ultraoptind] - 1, argv[ultraoptind]);
/* TODO: What is a good value for optopt in this case? */
/* Looks like BSD uses 0 */
ultraoptopt = 0;
ultraoptind++;
return '?';
}
return handle_longopt(longind, longarg, noseparg, argv,
longopts, indexptr, optleaders, flags);
}
/* See if it matches a long-only option */
if (longopts != NULL &&
ultraoptnum == 0 &&
((flags & UGO_SINGLELEADERLONG) ||
(flags & UGO_SINGLELEADERONLY))) {
int longind;
char *longarg;
longind = match_longopt(ultraoptind, argv, longopts, assigners,
optleaders, flags, &longarg);
if (longind >= 0)
return handle_longopt(longind, longarg, noseparg, argv,
longopts, indexptr, optleaders, flags);
}
/* No long matches, process short option */
opt = argv[ultraoptind] + ultraoptnum + 1;
optpos = strchr(shortopts, opt[0]);
if (optpos == NULL && (flags & UGO_CASEINSENSITIVE)) {
if (islower(opt[0]))
optpos = strchr(shortopts, toupper(opt[0]));
else
optpos = strchr(shortopts, tolower(opt[0]));
}
/* This could indicate ultraoptnum not being reset properly */
assert(opt[0] != '\0');
/* Check for invalid or unrecognized option */
if (optpos == NULL || opt[0] == ':') {
print_error(flags, erroroptc, argv[0], opt[0]);
ultraoptopt = opt[0];
if (opt[1] != '\0')
ultraoptnum++;
else {
ultraoptnum = 0;
ultraoptind++;
}
return '?';
}
/* Handle arguments */
if (optpos[1] == ':') {
ultraoptnum = 0;
/* Handle adjacent arguments -ofile.txt */
if (opt[1] != '\0') {
/* Skip over assignment character */
if ((flags & UGO_SHORTOPTASSIGN) && strchr(assigners, opt[1]))
ultraoptarg = opt + 2;
else
ultraoptarg = opt + 1;
ultraoptind++;
return optpos[0];
}
/* Handle optional argument not present */
if ((flags & UGO_OPTIONALARG) /* accept optionals */
&& optpos[2] == ':' /* opt takes optional */
&& (argv[ultraoptind+1] == NULL /* optional doesn't exist */
|| !(flags & UGO_SEPARATEDOPTIONAL) /* separated not accepted */
|| like_option(argv[ultraoptind+1], optleaders))) {
ultraoptind++;
return optpos[0];
}
/* Handle separated argument missing */
if (ultraoptind+2 > argc
|| noseparg
|| !like_optarg(argv[ultraoptind+1],
optleaders,
(flags & UGO_HYPHENARG))) {
ultraoptind++;
print_error(flags, errornoargc, argv[0], opt[0]);
ultraoptopt = opt[0];
if (flags & UGO_MISSINGCOLON)
return ':';
else
return '?';
}
ultraoptind += 2;
ultraoptarg = argv[ultraoptind-1];
return optpos[0];
}
/* Handle argumentless option with assigned option */
if ((flags & UGO_SHORTOPTASSIGN)
&& opt[1] != '\0' && strchr(assigners, opt[1])) {
print_error(flags, errorargc, argv[0], opt[0]);
ultraoptnum = 0;
ultraoptopt = opt[0];
ultraoptind++;
return '?';
}
if (opt[1] != '\0') {
ultraoptnum++;
} else {
ultraoptnum = 0;
ultraoptind++;
}
return optpos[0];
}
/* POSIX-compliant getopt
*
* Handles optional argument '::' specifier as an extension for compatibility
* with glibc
*/
int ultragetopt(int argc, char * const argv[], const char *optstring)
{
int flags = getoptflags;
#ifdef ULTRAGETOPT_OPTIONPERMUTE
flags |= UGO_OPTIONPERMUTE;
#endif
#ifndef ULTRAGETOPT_NO_OPTIONALARG
flags |= UGO_OPTIONALARG;
#endif
return ultragetopt_tunable(argc, argv, optstring, NULL, NULL,
unixassigners, unixleaders, flags);
}
/* GNU getopt_long workalike
*
* Argument reordering not yet implemented
* Leading + and - under consideration (behavior violates POSIX...)
*/
int ultragetopt_long(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr)
{
return ultragetopt_tunable(argc, argv, shortopts, longopts, indexptr,
unixassigners, unixleaders,
getoptflags | UGO_OPTIONPERMUTE | UGO_OPTIONALARG);
}
/* GNU getopt_long_only workalike */
int ultragetopt_long_only(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr)
{
return ultragetopt_tunable(argc, argv, shortopts, longopts, indexptr,
unixassigners, unixleaders,
getoptflags | UGO_SINGLELEADERLONG
| UGO_OPTIONPERMUTE | UGO_OPTIONALARG);
}
int ultragetopt_dos(int argc, char * const argv[], const char *optstring)
{
return ultragetopt_tunable(argc, argv, optstring, NULL, NULL,
dosassigners, dosleaders,
getoptflags | UGO_CASEINSENSITIVE);
}
int ultragetopt_long_dos(int argc, char *const argv[], const char *shortopts,
const struct option *longopts, int *indexptr)
{
return ultragetopt_tunable(argc, argv, shortopts, longopts, indexptr,
dosassigners, dosleaders,
getoptflags | UGO_CASEINSENSITIVE
| UGO_SINGLELEADERLONG | UGO_SINGLELEADERONLY
| UGO_OPTIONPERMUTE | UGO_OPTIONALARG);
}
/* vim:set sts=4 sw=4: */

View File

@@ -1,22 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tmx2gba", "tmx2gba.vcxproj", "{F8167A3B-4B77-4449-B730-F19ECB133A08}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F8167A3B-4B77-4449-B730-F19ECB133A08}.Debug|Win32.ActiveCfg = Debug|Win32
{F8167A3B-4B77-4449-B730-F19ECB133A08}.Debug|Win32.Build.0 = Debug|Win32
{F8167A3B-4B77-4449-B730-F19ECB133A08}.Release|Win32.ActiveCfg = Release|Win32
{F8167A3B-4B77-4449-B730-F19ECB133A08}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -1,93 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{F8167A3B-4B77-4449-B730-F19ECB133A08}</ProjectGuid>
<RootNamespace>tmx2gba</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>inc\</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>inc\</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\base64.cpp" />
<ClCompile Include="src\tmx2gba.cpp" />
<ClCompile Include="src\tmxlayer.cpp" />
<ClCompile Include="src\tmxobject.cpp" />
<ClCompile Include="src\tmxreader.cpp" />
<ClCompile Include="src\XGetopt.cpp" />
<ClCompile Include="src\tmxtileset.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="inc\base64.h" />
<ClInclude Include="inc\miniz.h" />
<ClInclude Include="inc\rapidxml\rapidxml.hpp" />
<ClInclude Include="inc\rapidxml\rapidxml_iterators.hpp" />
<ClInclude Include="inc\rapidxml\rapidxml_print.hpp" />
<ClInclude Include="inc\rapidxml\rapidxml_utils.hpp" />
<ClInclude Include="inc\XGetopt.h" />
<ClInclude Include="src\tmxlayer.h" />
<ClInclude Include="src\tmxobject.h" />
<ClInclude Include="src\tmxreader.h" />
<ClInclude Include="src\tmxtileset.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,75 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\tmx2gba.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\base64.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\XGetopt.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tmxreader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tmxlayer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tmxtileset.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tmxobject.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="inc\base64.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="inc\rapidxml\rapidxml.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="inc\rapidxml\rapidxml_iterators.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="inc\rapidxml\rapidxml_print.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="inc\rapidxml\rapidxml_utils.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="inc\miniz.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="inc\XGetopt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\tmxreader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\tmxlayer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\tmxtileset.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\tmxobject.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>