-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at splitting writer logic
- Loading branch information
1 parent
db1de4b
commit 17de8ac
Showing
5 changed files
with
256 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(name + "Width", width); | ||
WriteDefine(name + "Height", height); | ||
} | ||
|
||
void HeaderWriter::WriteCharacterMap(const std::span<uint16_t> charData) | ||
{ | ||
stream << std::endl; | ||
WriteDefine(name + "TilesLen", charData.size() * 2); | ||
WriteSymbol(name + "Tiles", DatType<uint16_t>(), charData.size()); | ||
} | ||
|
||
void HeaderWriter::WriteCollision(const std::span<uint8_t> collisionData) | ||
{ | ||
stream << std::endl; | ||
WriteDefine(name + "CollisionLen", collisionData.size()); | ||
WriteSymbol(name + "Collision", DatType<uint8_t>(), collisionData.size()); | ||
} | ||
|
||
void HeaderWriter::WriteObjects(const std::span<uint32_t> objData) | ||
{ | ||
stream << std::endl; | ||
WriteDefine(name + "ObjCount", objData.size() / 3); | ||
WriteDefine(name + "ObjdatLen", objData.size() * sizeof(int)); | ||
WriteSymbol(name + "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(name); | ||
stream << "#ifndef " << guard << std::endl; | ||
stream << "#define " << guard << std::endl; | ||
} | ||
|
||
void HeaderWriter::WriteGuardEnd() | ||
{ | ||
const std::string guard = GuardName(name); | ||
stream << std::endl << "#endif//" << guard << std::endl; | ||
} | ||
|
||
|
||
HeaderWriter::~HeaderWriter() | ||
{ | ||
if (stream.is_open()) | ||
{ | ||
WriteGuardEnd(); | ||
stream.close(); | ||
} | ||
} | ||
|
||
|
||
bool HeaderWriter::Open(const std::string_view path, const std::string_view name) | ||
{ | ||
this->name = 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* 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> | ||
|
||
template <typename T> | ||
concept NumericType = std::integral<T> || std::floating_point<T>; | ||
|
||
class HeaderWriter | ||
{ | ||
std::ofstream stream; | ||
std::string name; | ||
|
||
void WriteGuardStart(); | ||
void WriteGuardEnd(); | ||
|
||
public: | ||
~HeaderWriter(); | ||
|
||
[[nodiscard]] bool Open(const std::string_view 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* swwriter.hpp - Copyright (C) 2015-2024 a dinosaur (zlib, see COPYING.txt) */ | ||
|
||
#ifndef SWRITER_HPP | ||
#define SWRITER_HPP | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <string> | ||
#include <string_view> | ||
#include <fstream> | ||
#include <vector> | ||
|
||
class SWriter | ||
{ | ||
std::ofstream stream; | ||
int writes = 0; | ||
|
||
template <typename T> static constexpr const char* DatType(); | ||
template <> constexpr const char* DatType<uint8_t>() { return ".byte"; } | ||
template <> constexpr const char* DatType<uint16_t>() { return ".hword"; } | ||
template <> constexpr const char* DatType<uint32_t>() { return ".word"; } | ||
|
||
template <typename T> | ||
static void WriteArray(std::ostream& aOut, const std::vector<T>& aDat, int aPerCol = 16) | ||
{ | ||
int col = 0; | ||
|
||
aOut.setf(std::ios::hex, std::ios::basefield); | ||
aOut.setf(std::ios::showbase); | ||
|
||
size_t i = 0; | ||
for (T element : aDat) | ||
{ | ||
if (col == 0) | ||
aOut << "\t" << DatType<T>() << " "; | ||
|
||
aOut << std::hex << (int)element; | ||
|
||
if (i < aDat.size() - 1) | ||
{ | ||
if (++col < aPerCol) | ||
{ | ||
aOut << ","; | ||
} | ||
else | ||
{ | ||
aOut << "" << std::endl; | ||
col = 0; | ||
} | ||
} | ||
|
||
++i; | ||
} | ||
} | ||
|
||
public: | ||
[[nodiscard]] bool Open(const std::string_view path) | ||
{ | ||
stream.open(path); | ||
return stream.is_open(); | ||
} | ||
|
||
~SWriter() | ||
{ | ||
if (stream.is_open()) | ||
{ | ||
stream.close(); | ||
} | ||
} | ||
|
||
template <typename T> | ||
void WriteArray(const std::string_view name, T data) | ||
{ | ||
if (writes++ != 0) | ||
stream << std::endl; | ||
stream << "\t.section .rodata" << std::endl; | ||
stream << "\t.align 2" << std::endl; | ||
stream << "\t.global " << name << "Tiles" << std::endl; | ||
stream << "\t.hidden " << name << "Tiles" << std::endl; | ||
stream << name << "Tiles" << ":" << std::endl; | ||
WriteArray(stream, data); | ||
stream << std::endl; | ||
} | ||
}; | ||
|
||
#endif//SWRITER_HPP |
Oops, something went wrong.