Tool refactoring

This commit is contained in:
2023-03-22 18:54:22 -07:00
parent ecee996893
commit 7b9c6576af
38 changed files with 355 additions and 1921 deletions

5
.gitignore vendored
View File

@ -82,6 +82,5 @@ assets/borrowed
.vscode*
.VSCode*
/vita/*
/tools/*
./tools/*
/vita
/tools

View File

@ -6,7 +6,7 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")

View File

@ -5,11 +5,7 @@
# Check for build target, or default. This is pretty much not guaranteed.
if(NOT DEFINED DAWN_BUILD_TARGET)
if(WIN32)
set(DAWN_BUILD_TARGET "target-rose-win32-glfw")
elseif(UNIX AND NOT APPLE)
set(DAWN_BUILD_TARGET "target-rose-linux64-glfw")
endif()
set(DAWN_BUILD_TARGET "target-tools")
endif()
# Now validate we have a build target for real

View File

@ -0,0 +1,9 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
set(DAWN_BUILDING dawnrose CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(DAWN_TARGET_WIN32 true CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(DAWN_TARGET_GLFW true CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(DAWN_TARGET_NAME "Rose" CACHE INTERNAL ${DAWN_CACHE_TARGET})

View File

@ -18,12 +18,12 @@ void SphereMesh::createSphere(
// Create vertices
int32_t c = 0;
for (int32_t i = 0; i <= stacks; i++) {
float_t phi = M_PI * i / stacks;
float_t phi = MATH_PI * i / stacks;
float_t cosPhi = cos(phi);
float_t sinPhi = sin(phi);
for (int32_t j = 0; j <= slices; j++) {
float_t theta = 2 * M_PI * j / slices;
float_t theta = 2 * MATH_PI * j / slices;
float_t cosTheta = cos(theta);
float_t sinTheta = sin(theta);

View File

@ -5,6 +5,7 @@
#pragma once
#include "display/mesh/Mesh.hpp"
#include "util/mathutils.hpp"
namespace Dawn {
class SphereMesh {

View File

@ -19,7 +19,7 @@ bool_t CapsuleCollider::performRaycast(
return raytestCapsule(
ray,
{
(struct PhysicsCapsule){
.height = this->height,
.radius = this->radius,
.origin = this->transform->getWorldPosition()

View File

@ -30,7 +30,7 @@ void GameCamera::onStart() {
if(current != slowTarget) {
float_t m = 6.0f;
float_t s = delta * 3.0f;
current += glm::vec2(.
current += glm::vec2(
mathClamp<float_t>((slowTarget.x - current.x) * s, -m, m),
mathClamp<float_t>((slowTarget.y - current.y) * s, -m, m)
);

View File

@ -19,4 +19,4 @@ set(
include(util/CMakeLists.txt)
# Tools
add_subdirectory(tools)
add_subdirectory(prefabtool)

View File

@ -0,0 +1,47 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(prefabtool VERSION 1.0)
add_executable(prefabtool)
# Sources
target_sources(prefabtool
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
PrefabTool.cpp
)
# Includes
target_include_directories(prefabtool
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(prefabtool
PUBLIC
DAWN_TOOL_INSTANCE=PrefabTool
DAWN_TOOL_HEADER="PrefabTool.hpp"
)
# Libraries
target_link_libraries(prefabtool
PUBLIC
${DAWN_BUILD_HOST_LIBS}
)
# Tool Function
function(tool_prefab target in)
add_custom_target(${target}
COMMAND prefabtool --input="${DAWN_ASSETS_SOURCE_DIR}/${in}"
COMMENT "Generating prefab ${target} from ${in}"
DEPENDS prefabtool
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()

View File

@ -0,0 +1,28 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PrefabTool.hpp"
using namespace Dawn;
std::vector<std::string> PrefabTool::getRequiredFlags() {
return { "input" };
}
std::map<std::string, std::string> PrefabTool::getOptionalFlags() {
return std::map<std::string, std::string>();
}
int32_t PrefabTool::start() {
File input = File(flags["input"]);
if(!input.exists()) {
std::cout << "Input file does not exist!" << std::endl;
return 1;
}
std::cout << "Input: " << input.filename << std::endl;
return 0;
}

View File

@ -6,12 +6,12 @@
#pragma once
#include "util/DawnTool.hpp"
#include "util/File.hpp"
#include "../../util/image.hpp"
namespace Dawn {
class TextureGen : public DawnTool {
class PrefabTool : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
std::map<std::string, std::string> getOptionalFlags() override;
public:
int32_t start();

View File

@ -1,125 +0,0 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
add_subdirectory(texturegen)
add_subdirectory(tilesetgen)
add_subdirectory(truetypegen)
add_subdirectory(languagegen)
add_subdirectory(generatedlanguages)
# Settings
set(DAWN_TOOL_GENERATED_LANG_DIR "${DAWN_TEMP_DIR}/languages" CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(DAWN_SCENE_ITEM_COMPONENT_LIST "" CACHE INTERNAL ${DAWN_CACHE_TARGET})
# Texture Tool
function(tool_texture target in)
add_custom_target(${target}
COMMAND texturegen --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_ASSETS_BUILD_DIR}/${target}"
COMMENT "Generating texture ${target} from ${in}"
DEPENDS texturegen
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()
# Tileset Tool
function(tool_tileset targetTileset targetTexture in cols rows)
tool_texture(${targetTexture} ${in})
add_custom_target(${targetTileset}
COMMAND tilesetgen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${targetTileset}" "${cols}" "${rows}"
COMMENT "Generating tileset ${target} from ${in}"
DEPENDS tilesetgen ${targetTexture}
)
add_dependencies(${DAWN_TARGET_NAME} ${targetTileset})
endfunction()
# Bitmap Font
function(tool_bitmapfont target in columns rows)
tool_tileset(${target}_tileset ${target}_texture ${in} ${columns} ${rows})
endfunction()
# TrueType Tool
function(tool_truetype target in out width height fontSize)
add_custom_target(${target}
COMMAND truetypegen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${out}" "${width}" "${height}" "${fontSize}"
COMMENT "Generating truetype ${target} from ${in}"
DEPENDS truetypegen
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()
# UI Tool
function(tool_ui target in)
add_custom_target(${target}
COMMAND uigen --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_GENERATED_DIR}/prefabs/ui/${target}"
COMMENT "Generating UI ${target} from ${in}"
DEPENDS uigen
)
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${DAWN_GENERATED_DIR}/ui
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()
# Generated Language Rollup Tool
function(tool_generatedlanguages in)
add_custom_target(generatedlanguages
COMMAND generatedlanguagestool --input="${DAWN_TOOL_GENERATED_LANG_DIR}" --output="${DAWN_ASSETS_BUILD_DIR}"
COMMENT "Generating all languages"
DEPENDS generatedlanguagestool
)
if(NOT generatedlanguages IN_LIST DAWN_TARGET_DEPENDENCIES_LAST)
set(
DAWN_TARGET_DEPENDENCIES_LAST
generatedlanguages
CACHE INTERNAL ${DAWN_CACHE_TARGET}
)
endif()
endfunction()
# Language Tool
function(tool_language target in)
add_custom_target(${target}
COMMAND languagegen --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_TOOL_GENERATED_LANG_DIR}/${target}.language"
COMMENT "Generating language set ${target} from ${in}"
DEPENDS languagegen
)
tool_generatedlanguages("${DAWN_TOOL_GENERATED_LANG_DIR}/${target}.language")
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()
# Audio Tool
if(DAWN_TARGET_OPENAL)
add_subdirectory(audiogen)
function(tool_audio target in)
add_custom_target(${target}
COMMAND audiogen --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_ASSETS_BUILD_DIR}/${target}"
COMMENT "Generating audio ${target} from ${in}"
DEPENDS audiogen
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()
endif()
if(DAWN_VISUAL_NOVEL)
add_subdirectory(vnscenegen)
# VN Scene Tool
function(tool_vnscene target in)
tool_generatedlanguages("${DAWN_TOOL_GENERATED_LANG_DIR}/${target}.language")
add_custom_target(${target}
COMMAND vnscenegen --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_GENERATED_DIR}/scenes/${target}" --language-out="${DAWN_TOOL_GENERATED_LANG_DIR}/${target}.language"
COMMENT "Generating VN Scene ${target} from ${in}"
DEPENDS vnscenegen
)
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${DAWN_GENERATED_DIR}
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()
endif()

View File

@ -1,60 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "AudioGen.hpp"
using namespace Dawn;
std::vector<std::string> AudioGen::getRequiredFlags() {
return std::vector<std::string>{ "input", "output" };
}
int32_t AudioGen::start() {
// Finished with XML data, now we can write data out.
File fileOut(flags["output"] + ".audio");
if(fileOut.exists()) return 0;
// Load input file
AudioFile<double> audioFile;
if(!audioFile.load(flags["input"])) {
printf("Failed to load audio file.\n");
return 1;
}
// Open Output File
fileOut.mkdirp();
if(!fileOut.open(FILE_MODE_WRITE)) {
std::cout << "Failed to open " << fileOut.filename << " output for writing" << std::endl;
return 1;
}
// Write Header
char buffer[FILE_BUFFER_SIZE];
sprintf(buffer, "%i|%i|%i|%i|",
audioFile.getNumChannels(),
audioFile.getSampleRate(),
audioFile.getNumSamplesPerChannel(),
audioFile.getNumSamplesPerChannel() * audioFile.getNumChannels()*(
sizeof(int16_t) / sizeof(uint8_t)
)
);
auto bufferLength = strlen(buffer);
fileOut.writeRaw(buffer, bufferLength);
// Convert Data to 16 bit audio
for (int32_t i = 0; i < audioFile.getNumSamplesPerChannel(); i++) {
for(int32_t y = 0; y < audioFile.getNumChannels(); y++) {
double sample = audioFile.samples[y][i];
sample = sample < -1 ? -1 : sample > 1 ? 1 : sample;
auto q = static_cast<int16_t> (sample * 32767.);
buffer[0] = (q >> 8) & 0xFF;
buffer[1] = q & 0xFF;
fileOut.writeRaw(buffer, 2);
}
}
return 0;
}

View File

@ -1,19 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/DawnTool.hpp"
#include "util/File.hpp"
#include "AudioFile.h"
namespace Dawn {
class AudioGen : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
public:
int32_t start();
};
}

View File

@ -1,38 +0,0 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
project(audiogen VERSION 1.0)
add_executable(audiogen)
# Sources
target_sources(audiogen
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
AudioGen.cpp
)
# Includes
target_include_directories(audiogen
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(audiogen
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_TOOL_INSTANCE=AudioGen
DAWN_TOOL_HEADER="AudioGen.hpp"
)
# Libraries
target_link_libraries(audiogen
PUBLIC
${DAWN_BUILD_HOST_LIBS}
AudioFile
)

View File

@ -1,34 +0,0 @@
# Copyright (c) 2023 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Generated Languages Tool
project(generatedlanguagestool VERSION 1.0)
add_executable(generatedlanguagestool)
target_sources(generatedlanguagestool
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
GeneratedLanguages.cpp
)
target_include_directories(generatedlanguagestool
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
target_compile_definitions(generatedlanguagestool
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_TOOL_INSTANCE=GeneratedLanguages
DAWN_TOOL_HEADER="GeneratedLanguages.hpp"
)
target_link_libraries(generatedlanguagestool
PUBLIC
${DAWN_BUILD_HOST_LIBS}
)

View File

@ -1,141 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "GeneratedLanguages.hpp"
using namespace Dawn;
std::vector<std::string> GeneratedLanguages::getRequiredFlags() {
return std::vector<std::string>{ "input", "output" };
}
int32_t GeneratedLanguages::start() {
// Generate list of languages
std::string inNormal = File::normalizeSlashes(flags["input"]);
std::string error;
std::vector<std::string> files;
int32_t ret = this->scanDir(inNormal, &error, &files);
if(ret != 0) {
std::cout << error << std::endl;
return ret;
}
// Now process each language file
std::map<std::string, std::map<std::string, std::string>> strings;
std::vector<std::string> knownKeys;
auto itFiles = files.begin();
while(itFiles != files.end()) {
File file(*itFiles);
file.open(FILE_MODE_READ);
std::string buffer;
size_t n = 0;
while(n < file.length) {
char lang[32];
char key[128];
char string[32178];
// Read lang
if(n != 0) file.setPosition(n);
auto langSize = file.readAhead(lang, 32, '|');
lang[langSize] = '\0';
n += langSize + 1;
if(langSize <= 0) {
std::cout << "Error reading language name: " << langSize << std::endl;
return 1;
}
// Read Key
file.setPosition(n);
auto keySize = file.readAhead(key, 128, '|');
key[keySize] = '\0';
n += keySize + 1;
if(keySize <= 0) {
std::cout << "Error reading language key: " << keySize << std::endl;
return 1;
}
// Read String
file.setPosition(n);
auto stringSize = file.readAhead(string, 32178, '|');
string[stringSize] = '\0';
n += stringSize + 1;
if(stringSize <= 0) {
std::cout << "Error reading language string: " << stringSize << std::endl;
return 1;
}
strings[lang][key] = string;
auto exist = std::find(knownKeys.begin(), knownKeys.end(), key);
if(exist == knownKeys.end()) knownKeys.push_back(key);
}
++itFiles;
}
// Now prepare output file
auto itLang = strings.begin();
std::string bufferOut = "";
while(itLang != strings.end()) {
File langOut(flags["output"] + FILE_PATH_SEP + "language_" + itLang->first + ".language");
bufferOut.clear();
auto itKeys = knownKeys.begin();
while(itKeys != knownKeys.end()) {
auto key = *itKeys;
auto exist = itLang->second.find(key);
if(exist == itLang->second.end()) {
std::cout << "Language " << itLang->first << " is missing key " << key << std::endl;
return 1;
}
bufferOut += exist->first + "|" + exist->second + "|";
++itKeys;
}
// Write out.
langOut.mkdirp();
if(!langOut.writeString(bufferOut)) {
std::cout << "Failed to create output file \"" + langOut.filename + "\"" << std::endl;
return 1;
}
++itLang;
}
return 0;
}
int32_t GeneratedLanguages::scanDir(
std::string dir,
std::string *error,
std::vector<std::string> *files
) {
DIR* handle = opendir(dir.c_str());
if(ENOENT == errno) {
*error = "Input directory \"" + dir + "\" does not exist";
return 0;
}
if(!handle) return 0;
struct dirent *entry;
while((entry=readdir(handle))) {
std::string name(entry->d_name);
if(name.size() == 0 || name[0] == '.') continue;
auto path = dir + FILE_PATH_SEP + entry->d_name;
if(entry->d_type == DT_DIR) {
auto ret = this->scanDir(dir, error, files);
if(ret != 0) return ret;
} else if(entry->d_type == DT_REG) {
files->push_back(path);
}
}
closedir(handle);
return 0;
}

View File

@ -1,25 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/DawnTool.hpp"
#include "util/Xml.hpp"
#include "util/File.hpp"
namespace Dawn {
class GeneratedLanguages : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
int32_t scanDir(
std::string dir,
std::string *error,
std::vector<std::string> *files
);
public:
int32_t start() override;
};
}

View File

@ -1,38 +0,0 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(languagegen VERSION 2.0)
add_executable(languagegen)
# Sources
target_sources(languagegen
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
LanguageGen.cpp
)
# Includes
target_include_directories(languagegen
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(languagegen
PUBLIC
DAWN_TOOL_INSTANCE=LanguageGen
DAWN_TOOL_HEADER="LanguageGen.hpp"
)
# Libraries
target_link_libraries(languagegen
PUBLIC
${DAWN_BUILD_HOST_LIBS}
)

View File

@ -1,152 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "LanguageGen.hpp"
using namespace Dawn;
std::vector<std::string> LanguageParser::getRequiredAttributes() {
return std::vector<std::string>{ "key" };
}
std::map<std::string, std::string> LanguageParser::getOptionalAttributes() {
return std::map<std::string, std::string>();
}
int32_t LanguageParser::onParse(
Xml *node,
std::map<std::string, std::string> values,
struct LanguageString *out,
std::string *error
) {
out->key = values["key"];
out->text = node->value;
return 0;
}
std::vector<std::string> LanguageGroupParser::getRequiredAttributes() {
return std::vector<std::string>{ "key" };
}
std::map<std::string, std::string> LanguageGroupParser::getOptionalAttributes() {
return std::map<std::string, std::string>();
}
int32_t LanguageGroupParser::onParse(
Xml *node,
std::map<std::string, std::string> values,
struct LanguageGroup *out,
std::string *error
) {
std::string key = values["key"];
out->key += key;
auto it = node->children.begin();
int32_t ret;
while(it != node->children.end()) {
auto c = *it;
if(c->node == "string") {
struct LanguageString string;
ret = (LanguageParser()).parse(c, &string, error);
if(ret != 0) return ret;
string.lang = out->lang;
string.key = out->key + "." + string.key;
out->strings.push_back(string);
} else if(c->node == "group") {
struct LanguageGroup group;
group.key += key + ".";
group.lang = out->lang;
ret = (LanguageGroupParser()).parse(c, &group, error);
if(ret != 0) return ret;
vectorAppend(&out->strings, group.strings);
}
++it;
}
return 0;
}
std::vector<std::string> LanguageRootParser::getRequiredAttributes() {
return std::vector<std::string>{ "lang" };
}
std::map<std::string, std::string> LanguageRootParser::getOptionalAttributes() {
return std::map<std::string, std::string>();
}
int32_t LanguageRootParser::onParse(
Xml *node,
std::map<std::string, std::string> values,
struct LanguageRoot *out,
std::string *error
) {
int32_t ret;
out->lang = values["lang"];
auto it = node->children.begin();
while(it != node->children.end()) {
auto c = *it;
if(c->node == "string") {
struct LanguageString string;
ret = (LanguageParser()).parse(c, &string, error);
if(ret != 0) return ret;
string.lang = out->lang;
out->strings.push_back(string);
} else if(c->node == "group") {
struct LanguageGroup group;
group.lang = out->lang;
ret = (LanguageGroupParser()).parse(c, &group, error);
if(ret != 0) return ret;
vectorAppend(&out->strings, group.strings);
}
++it;
}
return 0;
}
std::vector<std::string> LanguageGen::getRequiredFlags() {
return std::vector<std::string>{ "input", "output" };
}
int32_t LanguageGen::start() {
auto fileIn = File(flags["input"]);
std::string buffer;
if(!fileIn.readString(&buffer)) {
std::cout << "Failed to open/read input file " << fileIn.filename << std::endl;
return 1;
}
auto xml = Xml::load(buffer);
std::string error;
struct LanguageRoot root;
auto ret = (LanguageRootParser()).parse(&xml, &root, &error);
if(ret != 0) {
std::cout << error << std::endl;
return ret;
}
// Now dump out the language strings to be picked up later.
ret = languageSaveStrings(flags["output"], root.strings);
if(ret != 0) return ret;
return 0;
}

View File

@ -1,67 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/DawnTool.hpp"
#include "util/File.hpp"
#include "util/XmlParser.hpp"
#include "util/Language.cpp"
namespace Dawn {
struct LanguageGroup {
std::vector<struct LanguageString> strings;
std::string key;
std::string lang;
};
struct LanguageRoot {
std::string lang;
std::vector<struct LanguageString> strings;
};
class LanguageParser : public XmlParser<struct LanguageString> {
protected:
std::vector<std::string> getRequiredAttributes() override;
std::map<std::string, std::string> getOptionalAttributes() override;
int32_t onParse(
Xml *node,
std::map<std::string, std::string> values,
struct LanguageString *out,
std::string *error
) override;
};
class LanguageGroupParser : public XmlParser<struct LanguageGroup> {
protected:
std::vector<std::string> getRequiredAttributes() override;
std::map<std::string, std::string> getOptionalAttributes() override;
int32_t onParse(
Xml *node,
std::map<std::string, std::string> values,
struct LanguageGroup *out,
std::string *error
) override;
};
class LanguageRootParser : public XmlParser<struct LanguageRoot> {
protected:
std::vector<std::string> getRequiredAttributes() override;
std::map<std::string, std::string> getOptionalAttributes() override;
int32_t onParse(
Xml *node,
std::map<std::string, std::string> values,
struct LanguageRoot *out,
std::string *error
) override;
};
class LanguageGen : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
public:
int32_t start() override;
};
}

View File

@ -1,38 +0,0 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(texturegen VERSION 1.0)
add_executable(texturegen)
target_sources(texturegen
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
TextureGen.cpp
../../util/image.cpp
)
target_include_directories(texturegen
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(texturegen
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_TOOL_INSTANCE=TextureGen
DAWN_TOOL_HEADER="TextureGen.hpp"
)
target_link_libraries(texturegen
PUBLIC
${DAWN_BUILD_HOST_LIBS}
stb
)

View File

@ -1,67 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TextureGen.hpp"
using namespace Dawn;
std::vector<std::string> TextureGen::getRequiredFlags() {
return std::vector<std::string>{ "input", "output" };
}
int32_t TextureGen::start() {
// Finished with XML data, now we can write data out.
File fileOut(flags["output"] + ".texture");
if(fileOut.exists()) return 0;
// Load input file
File in(flags["input"]);
if(!in.open(FILE_MODE_READ)) return 1;
int w, h, channels;
auto imageRaw = stbi_load_from_file(in.file, &w, &h, &channels, STBI_rgb_alpha);
if(imageRaw == NULL) {
std::cout << "Failed to load input texture!" << std::endl;
return 1;
}
in.close();
// Convert to floating points
size_t len = STBI_rgb_alpha * w * h;
float_t *dataImage = (float_t*)malloc(sizeof(float) * len);
for(size_t i = 0; i < len; i++) {
auto dataIn = imageRaw[i];
dataImage[i] = ((float_t)dataIn) / 255.0f;
}
stbi_image_free(imageRaw);
// Open and create output
File out(flags["output"] + ".texture");
if(!out.mkdirp()) {
std::cout << "Failed to make output dir " << out.filename << std::endl;
return 1;
}
if(!out.open(FILE_MODE_WRITE)) {
std::cout << "Failed to open texture file for writing " << out.filename << std::endl;
return 1;
}
// Write info
char headerBuffer[64];
size_t headerBufferLength = sprintf((char *)headerBuffer, "%i|%i|", w, h);
if(!out.writeRaw(headerBuffer, headerBufferLength)) {
std::cout << "Failed to write texture header for " << out.filename << std::endl;
return 1;
}
// Write texture
if(!out.writeRaw((char *)dataImage, sizeof(float_t) * len)) {
std::cout << "Failed to write texture data for " << out.filename << std::endl;
return 1;
}
free(dataImage);
return 0;
}

View File

@ -1,25 +0,0 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(tilesetgen VERSION 1.0)
add_executable(tilesetgen)
target_sources(tilesetgen
PRIVATE
main.cpp
../../util/file.cpp
../../util/image.cpp
)
target_include_directories(tilesetgen
PUBLIC
${DAWN_SHARED_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}/../../
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(tilesetgen
PUBLIC
${DAWN_BUILD_HOST_LIBS}
stb
)

View File

@ -1,147 +0,0 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dawnsharedlibs.hpp"
#include "../../util/file.hpp"
#include "../../util/image.hpp"
int main(int argc, char *argv[]) {
char *in;
char *out;
FILE *file;
char path[FILENAME_MAX + 1];
int w, h, channels, cols, rows;
stbi_uc *dataImageRaw;
int gapX, gapY, borderX, borderY;
if(argc < 5) {
printf("Invalid number of arguments");
return 1;
}
in = argv[1];
out = argv[2];
gapX = 0, gapY = 0, borderX = 0, borderY = 0;
cols = atoi(argv[3]);
if(cols <= 0) {
printf("Columns is invalid\n");
return 1;
}
rows = atoi(argv[4]);
if(rows <= 0) {
printf("Rows is invalid");
return 1;
}
if(argc >= 6) {
gapX = atoi(argv[5]);
if(gapX <= 0) {
printf("Gap X is invalid\n");
return 1;
}
}
if(argc >= 7) {
gapY = atoi(argv[6]);
if(gapY <= 0) {
printf("Gap Y is invalid\n");
return 1;
}
}
if(argc >= 8) {
borderX = atoi(argv[7]);
if(borderX <= 0) {
printf("Border X is invalid\n");
return 1;
}
}
if(argc >= 9) {
borderY = atoi(argv[8]);
if(borderY <= 0) {
printf("Border Y is invalid\n");
return 1;
}
}
// Normalize slashes
fileNormalizeSlashes(in);
fileNormalizeSlashes(out);
// Check the output doesn't already exist
sprintf(path, "%s.tileset", out);
file = fopen(path, "rb");
if(file != NULL) {
fclose(file);
return 0;
}
// Read in the original texture
file = fopen(in, "rb");
if(file == NULL) {
printf("Failed to open file!\n");
return 1;
}
// Read image data
dataImageRaw = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha);
if(dataImageRaw == NULL) {
printf("Failed to load input texture!\n");
return 1;
}
fclose(file);
free(dataImageRaw);
if(w <= 0 || h <= 0) {
printf("Reading image failed (corrupted?)\n");
return 1;
}
// Calculate division sizes (pixels)
int divX = (w - (borderX * 2) - (gapX * (cols - 1))) / cols;
int divY = (h - (borderY * 2) - (gapY * (rows - 1))) / rows;
// Calculate the division sizes (units)
float tdivX = (float)divX / (float)w;
float tdivY = (float)divY / (float)h;
// Output buffer prep
char *buffer = (char *)malloc(sizeof(char) * (cols * rows * 48 + 48 + 48));
buffer[0] = '\0';
sprintf(buffer, "%i|%i|%i|%i|", cols, rows, divX, divY);
// Now prep tileset.
for(float y = 0; y < rows; y++) {
for(float x = 0; x < cols; x++) {
float ux0 = ((float)borderX + ((float)divX * x) + ((float)gapX * x)) / (float)w;
float ux1 = ux0 + tdivX;
float uy0 = ((float)borderY + ((float)divY * y) + ((float)gapY * y)) / (float)h;
float uy1 = uy0 + tdivY;
sprintf(buffer, "%s%f,%f,%f,%f|", buffer, ux0, ux1, uy0, uy1);
}
}
// Open output file
fileMkdirp(path);
file = fopen(path, "wb");
if(file == NULL) {
free(buffer);
printf("Invalid tileset file out!\n");
return 1;
}
// Write and close
fwrite(buffer, sizeof(char), strlen(buffer), file);
fclose(file);
free(buffer);
return 0;
}

View File

@ -1,25 +0,0 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(truetypegen VERSION 1.0)
add_executable(truetypegen)
target_sources(truetypegen
PRIVATE
main.cpp
../../util/file.cpp
../../util/image.cpp
)
target_include_directories(truetypegen
PUBLIC
${DAWN_SHARED_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}/../../
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(truetypegen
PUBLIC
${DAWN_BUILD_HOST_LIBS}
stb
)

View File

@ -1,141 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dawnsharedlibs.hpp"
#include "../../util/file.hpp"
#include "../../util/image.hpp"
#ifndef STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#endif
#define TRUETYPE_FIRST_CHAR 32
#define TRUETYPE_NUM_CHARS 96
int main(int argc, char *args[]) {
FILE *file;
char path[FILENAME_MAX + 1];
int32_t width, height, fontSize, textureSize;
/*
args0 - PATH
args1 - Input Filename (TTF file)
args2 - Output Filename
args3 - Width of the output texture (Resolution X)
args4 - Height of the output texture (Resolution Y)
args5 - Font size to draw
*/
if(argc != 6) {
printf("Invalid number of arguments\n");
return 1;
}
char *fileIn = args[1];
char *fileOut = args[2];
char *strWidth = args[3];
char *strHeight = args[4];
char *strFontSize = args[5];
// Normalize slashes
fileNormalizeSlashes(fileIn);
fileNormalizeSlashes(fileOut);
// Check the output doesn't already exist
sprintf(path, "%s.truetype", fileOut);
file = fopen(path, "rb");
if(file != NULL) {
fclose(file);
return 0;
}
width = atoi(strWidth);
if(width <= 0) {
printf("Width is invalid.\n");
return 1;
}
height = atoi(strHeight);
if(height <= 0) {
printf("Height is invalid.\n");
return 1;
}
fontSize = atoi(strFontSize);
if(fontSize <= 0) {
printf("Font size is invalid.\n");
return 1;
}
// Read in the TTF data
file = fopen(fileIn, "rb");
if(file == NULL) {
printf("Failed to open input TTF file.\n");
return 1;
}
// Seek to end, get length, seek back to start.
fseek(file, 0, SEEK_END);
size_t fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
// Read in all data
char *ttfData = (char*)malloc(sizeof(char) * fileSize);
size_t readSize = fread(ttfData, 1, fileSize, file);
fclose(file);
if(readSize < fileSize) {
free(ttfData);
printf("Failed to read all data form TTF\n");
return 1;
}
// Create bitmap data. This is a single channel color (alpha).
textureSize = width * height;
stbi_uc *bitmapData = (stbi_uc*)malloc(sizeof(stbi_uc) * textureSize);
stbtt_bakedchar characterData[TRUETYPE_NUM_CHARS];
// Now parse the TTF itself.
stbtt_BakeFontBitmap(
(uint8_t*)ttfData, 0, (float)fontSize, bitmapData,
width, height,
TRUETYPE_FIRST_CHAR, TRUETYPE_NUM_CHARS,
characterData
);
// Prepare output file for writing.
sprintf(path, "%s.truetype", fileOut);
fileMkdirp(path);
file = fopen(path, "wb");
if(file == NULL) {
printf("Failed to create output TTF file\n");
return 1;
}
// Now prepare output data.
char headerBuffer[64];
int32_t headerBufferLength = sprintf(
headerBuffer, "%i|%i|%i|", width, height, fontSize
);
fwrite(headerBuffer, sizeof(char), headerBufferLength, file);
// Write output pixels.
float outputPixels[4];
for(int32_t i = 0; i < textureSize; i++) {
outputPixels[0] = 1.0f;
outputPixels[1] = 1.0f;
outputPixels[2] = 1.0f;
outputPixels[3] = ((float)bitmapData[i]) / 255.0f;
fwrite(outputPixels, sizeof(float), 4, file);
}
// Now write output quads data.
fwrite(characterData, sizeof(stbtt_bakedchar), TRUETYPE_NUM_CHARS, file);
fclose(file);
free(bitmapData);
return 0;
}

View File

@ -5,9 +5,6 @@
#pragma once
#include "dawnsharedlibs.hpp"
#include "../../util/file.hpp"
#include "../../util/csv.hpp"
#include "../../util/xml.hpp"
#include <iostream>
#include <map>
#include <vector>

View File

@ -1,120 +0,0 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "csv.hpp"
char * csvGetCell(csv_t *csv, int32_t row, int32_t cell) {
return csv->rows[(row * CSV_COLUMN_COUNT_MAX) + cell];
}
void csvParse(char *string, csv_t *csv) {
char c;
size_t i, j, length;
csvparsestate_t state;
int32_t rowCellCount;
length = strlen(string);
csv->buffer = (char *)malloc(sizeof(char) * (length+1) * CSV_COLUMN_COUNT_MAX * CSV_ROW_COUNT_MAX);
csv->cellCounts = (int32_t *)malloc(sizeof(int32_t) * CSV_ROW_COUNT_MAX);
csv->rows = (char**)malloc(sizeof(char*) * CSV_ROW_COUNT_MAX * CSV_COLUMN_COUNT_MAX);
i = 0;
j = 0;
rowCellCount = 0;
csv->rowCount = 0;
state = CSV_PARSE_STATE_FIND_CELL;
while(i < length) {
c = string[i++];
// What are we doing
switch(state) {
case CSV_PARSE_STATE_FIND_CELL:
if(c == '"') {
state = CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES;
csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j;
rowCellCount++;
continue;
} else if(c == '\r' || c == '\n') {
// Newline (todo: is this a blank line?)
state = CSV_PARSE_STATE_LINE_END;
continue;
} else if(c == ',') {
csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->buffer[j++] = '\0';
rowCellCount++;
continue;
} else {
state = CSV_PARSE_STATE_PARSE_CELL;
csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->buffer[j++] = c;
rowCellCount++;
continue;
}
case CSV_PARSE_STATE_PARSE_CELL:
if(c == '\r' || c == '\n') {
state = CSV_PARSE_STATE_LINE_END;
csv->buffer[j++] = '\0';
continue;
} else if(c == ',') {
state = CSV_PARSE_STATE_FIND_CELL;
csv->buffer[j++] = '\0';
continue;
}
csv->buffer[j++] = c;
continue;
case CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES:
if((c == '\\' && string[i] == '"') || (c == '"' && string[i] == '"')) {
// Handle escaped quotes. I normally see [\"] but excel does [""] in
// most cases
csv->buffer[j++] = '"';
i++;
continue;
} else if(c == '"') {
// Handle end of quoted string
state = CSV_PARSE_STATE_FIND_CELL;
csv->buffer[j++] = '\0';
// Because we tend to do [",] at the end of a quoted cell, we do this
// to prevent [,,] cases being treated the same
if(string[i] == ',') i++;
continue;
}
// Normal character.
csv->buffer[j++] = c;
continue;
case CSV_PARSE_STATE_LINE_END:
// Skip blanks
if(c == '\r' || c == '\n') continue;
csv->cellCounts[csv->rowCount] = rowCellCount;
csv->rowCount++;
rowCellCount = 0;
state = CSV_PARSE_STATE_FIND_CELL;
i--;
continue;
default:
printf("Error occured during parse operation.");
free(NULL);
}
}
csv->buffer[j++] = '\0';
if(rowCellCount != 0) {
csv->cellCounts[csv->rowCount] = rowCellCount;
csv->rowCount++;
}
}
void csvDispose(csv_t *csv) {
free(csv->buffer);
free(csv->cellCounts);
free(csv->rows);
}

View File

@ -1,33 +0,0 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawnsharedlibs.hpp"
#include "string.h"
#define CSV_ROW_COUNT_MAX 128
#define CSV_COLUMN_COUNT_MAX 16
typedef enum {
CSV_PARSE_STATE_FIND_CELL,//0
CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES,
CSV_PARSE_STATE_PARSE_CELL,//2
CSV_PARSE_STATE_LINE_END
} csvparsestate_t;
typedef struct {
char *buffer;
char **rows;
int32_t rowCount;
int32_t *cellCounts;
} csv_t;
char * csvGetCell(csv_t *csv, int32_t row, int32_t cell);
void csvParse(char *string, csv_t *csv);
void csvDispose(csv_t *csv);

View File

@ -1,26 +1,26 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "file.hpp"
#include "File.hpp"
void fileNormalizeSlashes(char *string) {
char c;
int i = 0;
using namespace Dawn;
while(c = string[i++]) {
if(c != '\\' && c != '/') continue;
string[i-1] = FILE_PATH_SEP;
std::string File::normalizeSlashes(std::string str) {
size_t i = 0;
while(i < str.size()) {
auto c = str[i];
if(c == '\\' || c == '/') str[i] = FILE_PATH_SEP;
++i;
}
return str;
}
void fileMkdirp(char *path) {
char buffer[FILENAME_MAX];
void File::mkdirp(std::string path) {
std::string buffer;
char c;
int i = 0;
size_t i = 0;
bool inFile;
bool hasMore;
@ -28,172 +28,166 @@ void fileMkdirp(char *path) {
hasMore = false;
while(c = path[i]) {
if((c == '\\' || c == '/') && i > 0) {
buffer[i] = '\0';
fileMkdir(buffer, 0755);
fileMkdir(buffer.c_str(), 0755);
inFile = false;
hasMore = false;
buffer[i] = FILE_PATH_SEP;
buffer += FILE_PATH_SEP;
i++;
continue;
}
if(c == '.') inFile = true;
hasMore = true;
buffer[i] = c;
buffer += c;
i++;
}
if(!inFile && hasMore) {
buffer[i] = '\0';
fileMkdir(buffer, 0755);
fileMkdir(buffer.c_str(), 0755);
}
}
size_t assetReadString(FILE *file, char *buffer) {
size_t length;
fseek(file, 0, SEEK_END);// Seek to the end
length = ftell(file);// Get our current position (the end)
fseek(file, 0, SEEK_SET);// Reset the seek
if(buffer == NULL) return length;
return fread(buffer, 1, length, file);// Read all the bytes
//
File::File(std::string filename) {
this->filename = File::normalizeSlashes(filename);
}
int32_t readAhead(
char *bufferIn, int32_t start,
char *bufferOut,
char *needles, int32_t needleCount
) {
int32_t i = start, k = 0, j;
char c;
bool needleFound = false;
bool_t File::open(enum FileMode mode) {
assertNull(this->file);
if(bufferIn[i] == '\0') return 0;
this->mode = mode;
this->file = fopen(
this->filename.c_str(),
mode == FILE_MODE_READ ? "rb" : "wb"
);
while((c = bufferIn[i++]) != '\0') {
for(j = 0; j < needleCount; j++) {
if(c != needles[j]) continue;
needleFound = true;
}
if(needleFound) break;
if(bufferOut != NULL) bufferOut[k] = c;
k++;
}
if(this->file == NULL) return false;
if(bufferOut != NULL) bufferOut[k] = '\0';
return k;
}
if(mode == FILE_MODE_READ) {
fseek(this->file, 0, SEEK_END);
this->length = ftell(this->file);
fseek(this->file, 0, SEEK_SET);
int32_t skipAhead(
char *bufferIn, int32_t start,
char *needles, int32_t needleCount
) {
char c;
int32_t j, k = 0, i = start;
bool needleFound;
while((c = bufferIn[i++]) != '\0') {
needleFound = false;
for(j = 0; j < needleCount; j++) {
if(c != needles[j]) continue;
needleFound = true;
break;
}
if(!needleFound) break;
k++;
}
return k;
}
void fileGetDirectory(char *file, char* buffer) {
char *c, *p;
int32_t i;
p = strrchr(file, FILE_PATH_SEP);
c = file;
i = 0;
do {
buffer[i++] = *c;
} while(++c < p);
buffer[i] = '\0';
}
bool fileListChildren(
char *directory,
char *buffer,
int32_t *count,
uint8_t *types,
char **children
) {
#if defined(_MSC_VER)
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
char sPath[2048];
int32_t i;
// Append wildcard
sprintf(sPath, "%s\\*.*", directory);
// Scan first
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) {
printf("Path not found: [%s]\n", directory);
if(this->length <= 0) {
this->close();
return false;
}
} else {
this->length = 0;
}
// Iterate
i = 0;
do {
if(
strcmp(fdFile.cFileName, ".") == 0 ||
strcmp(fdFile.cFileName, "..") == 0
) continue;
return true;
}
// Get Full path.
sprintf(sPath, "%s\\%s", directory, fdFile.cFileName);
bool_t File::isOpen() {
return this->file != nullptr;
}
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
types[i] = FILE_CHILD_TYPE_DIR;
} else {
types[i] = FILE_CHILD_TYPE_FILE;
}
bool_t File::exists() {
if(this->file != nullptr) return true;
FILE *f = fopen(this->filename.c_str(), "rb");
if(f == NULL || f == nullptr) return false;
fclose(f);
return true;
}
children[i] = buffer + (i * FILE_CHILD_NAME_MAX);
strcpy(children[i], fdFile.cFileName);
i++;
} while(FindNextFile(hFind, &fdFile));
void File::close() {
assertNotNull(this->file);
fclose(this->file);
this->file = nullptr;
}
*count = i;
return true;
#else
struct dirent *de;
DIR *dr;
int32_t i;
bool_t File::mkdirp() {
File::mkdirp(this->filename);
return true;
}
// Open Dir
dr = opendir(directory);
if(dr == NULL) {
printf("Could not open directory");
return false;
}
bool_t File::readString(std::string *out) {
assertNotNull(out);
if(!this->isOpen()) {
if(!this->open(FILE_MODE_READ)) return false;
}
assertTrue(this->mode == FILE_MODE_READ);
out->clear();
// Iterate
i = 0;
while ((de = readdir(dr)) != NULL) {
// File or folder?
if(de->d_type != DT_REG) continue;
size_t i = 0;
char buffer[FILE_BUFFER_SIZE + 1];// +1 for null term
while(i != this->length) {
size_t amt = mathMin<size_t>(FILE_BUFFER_SIZE, (this->length - i));
auto amtRead = fread(buffer, sizeof(char), amt, this->file);
if(amtRead != amt) return false;
i += amtRead;
buffer[amtRead] = '\0';
out->append(buffer);
}
// Copy into child buffer
children[i] = buffer + (i * FILE_CHILD_NAME_MAX);
strcpy(children[i], de->d_name);
i++;
}
if(closedir(dr)) return false;
return true;
}
size_t File::readAhead(char *buffer, size_t max, char needle) {
assertNotNull(buffer);
assertTrue(max > 0);
if(!this->isOpen()) {
if(!this->open(FILE_MODE_READ)) return 0;
}
assertTrue(this->mode == FILE_MODE_READ);
// Buffer
size_t pos = ftell(this->file);
size_t amountLeftToRead = mathMin<size_t>(max, this->length - pos);
char temporary[FILE_BUFFER_SIZE];
size_t n = 0;
*count = i;
return true;
#endif
while(amountLeftToRead > 0) {
size_t toRead = mathMin<size_t>(amountLeftToRead, FILE_BUFFER_SIZE);
amountLeftToRead -= toRead;
// Read bytes
size_t read = fread(temporary, sizeof(char), toRead, this->file);
// Read error?
if(toRead != read) return 0;
// Did we read the needle?
size_t i = 0;
while(i < read) {
char c = temporary[i++];
if(c == needle) {
return n;
} else {
buffer[n++] = c;
}
}
}
// Needle was not found.
return -1;
}
std::string fileNormalizeSlashesNew(std::string str) {
return str;
bool_t File::writeString(std::string in) {
if(!this->isOpen()) {
if(!this->open(FILE_MODE_WRITE)) return false;
}
assertTrue(this->mode == FILE_MODE_WRITE);
return this->writeRaw((char *)in.c_str(), in.size()) && this->length == in.size();
}
bool_t File::writeRaw(char *data, size_t len) {
if(!this->isOpen()) {
if(!this->open(FILE_MODE_WRITE)) return false;
}
assertTrue(this->mode == FILE_MODE_WRITE);
this->length = fwrite(data, sizeof(char_t), len, this->file);
return true;
}
void File::setPosition(size_t n) {
fseek(this->file, 0, SEEK_SET);
fseek(this->file, n, SEEK_CUR);
}
File::~File() {
if(this->file != nullptr) this->close();
}

View File

@ -1,17 +1,11 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnsharedlibs.hpp"
#define FILE_CHILD_TYPE_DIR 0x00
#define FILE_CHILD_TYPE_FILE 0x01
#define FILE_CHILD_NAME_MAX 512
#define FILE_CHILD_COUNT_MAX 64
#include "assert/assert.hpp"
#include "util/mathutils.hpp"
#if defined(_MSC_VER)
#include <direct.h>
@ -26,51 +20,121 @@
#define FILE_PATH_SEP '/'
#define fileMkdir(path, perms) mkdir(path, perms)
#endif
#include <errno.h>
void fileNormalizeSlashes(char *string);
#define FILE_BUFFER_SIZE 512
void fileMkdirp(char *path);
namespace Dawn {
enum FileMode {
FILE_MODE_READ,
FILE_MODE_WRITE
};
size_t assetReadString(FILE *file, char *buffer);
class File {
private:
enum FileMode mode;
void fileGetDirectory(char *file, char* buffer);
public:
static std::string normalizeSlashes(std::string str);
static void mkdirp(std::string path);
bool fileListChildren(
char *directory,
char *buffer,
int32_t *count,
uint8_t *types,
char **children
);
std::string filename;
size_t length;
FILE *file = nullptr;
/**
* Reads ahead to the first instance of the given character you provide.
*
* @param bufferIn Buffer to scan.
* @param start Start position within the buffer to scan from (inclusive).
* @param bufferOut Where to write the temporary data that was read ahead.
* @param needles Array of characters to scan for.
* @param needleCount How many elements are within the needles array.
* @return The count of characters skipped.
*/
int32_t readAhead(
char *bufferIn, int32_t start,
char *bufferOut,
char *needles, int32_t needleCount
);
/**
* Constructs a new File interface class.
*
* @param filename Filename that you want to interface with.
*/
File(std::string filename);
/**
* Skips any characters found in the needles.
*
* @param bufferIn Buffer of chars to read.
* @param start Start of the buffer.
* @param needles Needles you are trying to skip.
* @param needleCount Count of needles in the needles array.
* @return The count of chars to skip ahead.
*/
int32_t skipAhead(
char *bufferIn, int32_t start,
char *needles, int32_t needleCount
);
/**
* Opens a connection to the file.
*
* @param mode File mode to use for this interface.
* @return True if success, otherwise for failure.
*/
bool_t open(enum FileMode mode);
static std::string fileNormalizeSlashesNew(std::string str);
/**
* Returns whether or not the file connection is currently opened.
*
* @return True if the connection is open, otherwise if it's not.
*/
bool_t isOpen();
/**
* Returns whether or not the file exists. Will open the connection if it
* does exist.
*
* @return True if exists, otherwsie if it doesn't.
*/
bool_t exists();
/**
* Closes the currently open interface to the file. Done automatically
* when this object is disposed.
*/
void close();
/**
* Makes all directories above this file's filename.
*
* @return True if successful, otherwise false.
*/
bool_t mkdirp();
/**
* Reads the entire contents of a file to the given output string buffer.
* This is a bit dangerous since the length of the file isn't checked
* against the memory to be consumed.
*
* @param out Pointer to a string where the output data will be stored.
* @return True if the read was successful, otherwise false.
*/
bool_t readString(std::string *out);
/**
* Reads ahead from the current position to a specific needle (character).
*
* @param buffer Buffer to output read chars to.
* @param max Max length of the buffer / amount of chars to read ahead.
* @param needle The character (needle) to look for.
* @return Amount of chars read, or <= 0 on error.
*/
size_t readAhead(
char *buffer,
size_t max,
char needle
);
/**
* Writes the entire contents of a string to a file.
*
* @param in String to write to this file.
* @return True if written successfully, otherwise false.
*/
bool_t writeString(std::string in);
/**
* Write raw bytes to the file.
*
* @param data Data to write.
* @return True if written successfully, otherwise false.
*/
bool_t writeRaw(char *data, size_t );
/**
* Set the position of the cursor of the file reader.
*
* @param pos Position to set.
*/
void setPosition(size_t pos);
/**
* Destruct the File manager.
*/
~File();
};
}

View File

@ -1,55 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "image.hpp"
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#endif
#ifndef STB_IMAGE_RESIZE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize.h>
#endif
#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#endif
void imageCopy(
uint8_t *source, int32_t sourceWidth, int32_t sourceHeight,
uint8_t *dest, int32_t destWidth, int32_t destHeight,
int32_t cropX, int32_t cropY, int32_t cropWidth, int32_t cropHeight,
int32_t pasteX, int32_t pasteY,
int32_t channels
) {
int32_t x, y, c;
int32_t absX, absY;
int32_t sourceIndex, targetIndex;
if(cropX == -1) cropX = 0;
if(cropY == -1) cropY = 0;
if(cropWidth == -1) cropWidth = sourceWidth;
if(cropHeight == -1) cropHeight = sourceHeight;
if(pasteX == -1) pasteX = 0;
if(pasteY == -1) pasteY = 0;
for(x = cropX; x < cropX + cropWidth; x++) {
for(y = cropY; y < cropY + cropHeight; y++) {
absX = x - cropX + pasteX;
absY = y - cropY + pasteY;
if(absX >= destWidth || absY >= destHeight || absX < 0 || absY < 0)continue;
targetIndex = absY * destWidth + absX;
sourceIndex = y * sourceWidth + x;
for(c = 0; c < channels; c++) {
dest[(targetIndex*channels) + c] = source[(sourceIndex*channels) + c];
}
}
}
}

View File

@ -1,39 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawnsharedlibs.hpp"
#include "file.hpp"
#include <stb_image.h>
#include <stb_image_resize.h>
#include <stb_image_write.h>
/**
* Unused currently.
*
* @param source
* @param sourceWidth
* @param sourceHeight
* @param dest
* @param destWidth
* @param destHeight
* @param cropX
* @param cropY
* @param cropWidth
* @param cropHeight
* @param pasteX
* @param pasteY
* @param channels
*/
void imageCopy(
uint8_t *source, int32_t sourceWidth, int32_t sourceHeight,
uint8_t *dest, int32_t destWidth, int32_t destHeight,
int32_t cropX, int32_t cropY, int32_t cropWidth, int32_t cropHeight,
int32_t pasteX, int32_t pasteY,
int32_t channels
);

View File

@ -1,231 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "xml.hpp"
int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i) {
char c;
int32_t level = 0;
uint8_t doing = XML_DOING_NOTHING;
uint8_t doingBeforeComment;
bool insideTag = false;
char* buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX);
int32_t bufferLength = 0;
xml->value = NULL;
xml->attributeCount = 0;
xml->children = (xml_t *)malloc(sizeof(xml_t) * XML_CHILD_COUNT_MAX);
xml->childrenCount = 0;
while(c = data[i++]) {
switch(doing) {
case XML_DOING_NOTHING:
// Look for either an opening tag (<) or a word for a value.
if(c == '>') continue;
if(c == '<') {
if(data[i] == '!' && data[i+1] == '-' && data[i+2] == '-') {
doingBeforeComment = doing;
doing = XML_PARSING_COMMENT;
i += 3;
} else if(insideTag) {
i = xmlLoadChild(xml->children + xml->childrenCount++, data, i-1);
doing = XML_PARSING_CHILD;
} else {
doing = XML_PARSING_TAG_NAME;
level++;
insideTag = true;
}
continue;
}
if(xmlIsWhitespace(c)) continue;
doing = XML_PARSING_VALUE;
buffer[bufferLength++] = c;
break;
case XML_PARSING_TAG_NAME:
// Just keep reading until we either hit a space (end of the tag name)
// or a closing tag value, either / or >
if(xmlIsWhitespace(c) || c == '>' || c == '/') {
buffer[bufferLength] = '\0';
xml->node = buffer;
buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX);
bufferLength = 0;
if(c == '/') {
level--;
insideTag = false;
doing = XML_PARSING_CLOSE;
} else {
doing = c == '>' ? XML_DOING_NOTHING : XML_LOOKING_FOR_ATTRIBUTE;
}
continue;
}
buffer[bufferLength++] = c;
break;
case XML_LOOKING_FOR_ATTRIBUTE:
// Look until we hit either the end of a tag, or the attribute itself
if(xmlIsWhitespace(c) || c == '>' || c == '/' || c == '=') {
if(c == '>' || c == '/') {
doing = XML_DOING_NOTHING;
if(c == '/') {
level--;
insideTag = false;
doing = XML_PARSING_CLOSE;
}
} else if(c == '=') {
doing = XML_LOOKING_FOR_ATTRIBUTE_VALUE;
} else {
doing = XML_LOOKING_FOR_ATTRIBUTE;
}
if(bufferLength > 0) {
buffer[bufferLength] = '\0';
xml->attributeNames[xml->attributeCount++] = buffer;
xml->attributeDatas[xml->attributeCount] = NULL;
buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX);
bufferLength = 0;
}
continue;
}
buffer[bufferLength++] = c;
break;
case XML_LOOKING_FOR_ATTRIBUTE_VALUE:
// Keep looking until we find a quote mark
if(xmlIsWhitespace(c)) continue;
if(c == '>' || c == '/') {
doing = XML_DOING_NOTHING;
insideTag = false;
continue;
}
if(c != '"') continue;
doing = XML_PARSING_ATTRIBUTE_VALUE;
break;
case XML_PARSING_ATTRIBUTE_VALUE:
// Parse the attribute value until we find a quote mark.
if(c == '"') {
doing = XML_LOOKING_FOR_ATTRIBUTE;
buffer[bufferLength] = '\0';
xml->attributeDatas[xml->attributeCount - 1] = buffer;
buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX);
bufferLength = 0;
continue;
}
buffer[bufferLength++] = c;
break;
case XML_PARSING_VALUE:
// Keep parsing child until we find a < for an opening/closing tag.
if(c == '<') {
// In HTML Spec there could be a child here but not in XML spec.
doing = XML_PARSING_CLOSE;
buffer[bufferLength] = '\0';
bufferLength = 0;
xml->value = buffer;
buffer = (char *)malloc(sizeof(char) * XML_TEXT_BUFFER_MAX);
continue;
}
buffer[bufferLength++] = c;
break;
case XML_PARSING_CHILD:
if(c == '<') {
// Read ahead and confirm this is a close or not
if(data[i] == '/') {
doing = XML_PARSING_CLOSE;
continue;
}
if(data[i] == '!' && data[i+1] == '-' && data[i+2] == '-') {
doingBeforeComment = doing;
doing = XML_PARSING_COMMENT;
i += 3;
continue;
}
// Likely another child.
i = xmlLoadChild(xml->children + xml->childrenCount++, data, i-1);
}
if(xmlIsWhitespace(c)) continue;
// In HTML Spec there's a chance for there to be a value here, but not
// in the XML spec.
break;
case XML_PARSING_CLOSE:
// Just keep parsing until the tag closer finishes.
if(c != '>') continue;
doing = XML_DOING_NOTHING;
//TODO: Return index or something?
free(buffer);
return i;
case XML_PARSING_COMMENT:
if(c != '-') continue;
if(data[i] != '-') continue;
if(data[i+1] != '>') continue;
i += 2;
doing = doingBeforeComment;
break;
default:
break;
}
}
free(buffer);
return i;
}
void xmlLoad(xml_t *xml, char *data) {
xmlLoadChild(xml, data, 0);
}
void xmlDispose(xml_t *xml) {
uint8_t i;
// Dispose children recursively
for(i = 0; i < xml->childrenCount; i++) {
xmlDispose(xml->children + i);
}
// Free children array.
free(xml->children);
// Dispose attributes
for(i = 0; i < xml->attributeCount; i++) {
free(xml->attributeNames[i]);
if((xml->attributeDatas + i) != NULL) {
free(xml->attributeDatas[i]);
}
}
free(xml->node);
if(xml-> value != NULL) free(xml->value);
}
int16_t xmlGetAttributeByName(xml_t *xml, char *name) {
int16_t i;
for(i = 0; i < xml->attributeCount; i++) {
if(strcmp(xml->attributeNames[i], name) == 0) return i;
}
return -1;
}
bool xmlIsWhitespace(char c) {
return c == ' ' || c == '\r' || c == '\n' || c == '\t';
}

View File

@ -1,81 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawnsharedlibs.hpp"
#include "file.hpp"
#define XML_DOING_NOTHING 0x00
#define XML_PARSING_TAG_NAME 0x01
#define XML_LOOKING_FOR_ATTRIBUTE 0x02
#define XML_PARSING_ATTRIBUTE_NAME 0x03
#define XML_LOOKING_FOR_ATTRIBUTE_VALUE 0x04
#define XML_PARSING_ATTRIBUTE_VALUE 0x05
#define XML_PARSING_VALUE 0x06
#define XML_PARSING_CHILD 0x07
#define XML_PARSING_CLOSE 0x08
#define XML_PARSING_COMMENT 0x09
#define XML_TEXT_BUFFER_MAX 2048
#define XML_CHILD_COUNT_MAX 128
#define XML_ATTRIBUTE_MAX 128
typedef struct _xml_t xml_t;
typedef struct _xml_t {
char *node;
char *value;
char *attributeNames[XML_ATTRIBUTE_MAX];
char *attributeDatas[XML_ATTRIBUTE_MAX];
uint8_t attributeCount;
xml_t *children;
uint8_t childrenCount;
} xml_t;
/**
* Load an XML child from a string buffer.
*
* @param xml XML to load.
* @param data Data to parse
* @param i Character index within the data
* @return The index in the data string this XML node ends.
*/
int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i);
/**
* Load an XML String into an XML memory.
*
* @param xml XML to load into.
* @param data XML string.
*/
void xmlLoad(xml_t *xml, char *data);
/**
* Dispose a previously loaded XML.
*
* @param xml XML to dispose.
*/
void xmlDispose(xml_t *xml);
/**
* Find an attribute index by its name.
*
* @param xml Xml node to get the attribute from.
* @param name The name of the attribute you're trying to find.
* @return -1 if not found, otherwise the index of the attribute within array.
*/
int16_t xmlGetAttributeByName(xml_t *xml, char *name);
/**
* Checks if a given character is a whitespace character or not.
*
* @param c Character to check if a whitespace.
* @return True if whitespace, otherwise false.
*/
bool xmlIsWhitespace(char c);