diff --git a/CMakeLists.txt b/CMakeLists.txt index d54f377..8aa338d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ find_package(stb REQUIRED) find_package(yaml-cpp REQUIRED) find_package(EnTT REQUIRED) find_package(opengl_system REQUIRED) +find_package(nlohmann_json REQUIRED) +find_package(lz4 REQUIRED) add_subdirectory(./modules) add_subdirectory(./external) diff --git a/conanfile.py b/conanfile.py index 5619cb4..c9d4b3f 100644 --- a/conanfile.py +++ b/conanfile.py @@ -26,6 +26,8 @@ class LightRecipe(ConanFile): self.requires("stb/cci.20240531") self.requires("volk/1.3.296.0") self.requires("yaml-cpp/0.8.0") + self.requires("nlohmann_json/3.12.0") + self.requires("lz4/1.10.0") def layout(self): cmake_layout(self) diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index c3169ec..e04a6b5 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -1,2 +1,5 @@ +add_subdirectory(./asset_baker) +add_subdirectory(./asset_parser) + add_subdirectory(./engine) add_subdirectory(./mirror) diff --git a/modules/asset_baker/.clang-format b/modules/asset_baker/.clang-format deleted file mode 100644 index a98b593..0000000 --- a/modules/asset_baker/.clang-format +++ /dev/null @@ -1,151 +0,0 @@ ---- -# Core -Language: Cpp -Standard: Cpp11 -ColumnLimit: '80' # No limit - -# Bin pack -BinPackArguments: 'false' -BinPackParameters: 'false' - -# Includes -SortIncludes: 'true' -IncludeBlocks: Regroup -IncludeCategories: - # Current Project - - Regex: '"' - Priority: 001 - - # Custom Project Categories... - - # Dependecies - - Regex: '' - Priority: 400 - - # Dependecies - - Regex: '<' - Priority: 500 - - - # Custom Deependencies Categories... - - # C++ includes - - Regex: '[^.h .hpp]>' - Priority: 998 - - # C includes - - Regex: '<[^/\n]+[.]h>' - Priority: 999 - -# Braces -BreakBeforeBraces: Custom -BraceWrapping: - AfterCaseLabel: true - AfterClass: true - AfterControlStatement: true - AfterEnum: true - AfterFunction: true - AfterNamespace: false - AfterObjCDeclaration: true - AfterStruct: true - AfterUnion: true - AfterExternBlock: true - BeforeCatch: true - BeforeElse: true - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true - -# Indentation -UseTab: ForIndentation -TabWidth: '2' -IndentWidth: '2' -ContinuationIndentWidth: '2' -ConstructorInitializerIndentWidth: '2' -IndentCaseLabels: 'false' -IndentWrappedFunctionNames: 'true' -IndentPPDirectives: BeforeHash -NamespaceIndentation: None -AccessModifierOffset: '-2' - -# Space -SpaceAfterCStyleCast: 'false' -SpaceAfterLogicalNot: 'false' -SpaceAfterTemplateKeyword: 'false' -SpaceBeforeAssignmentOperators: 'true' -SpaceBeforeCpp11BracedList: 'true' -SpaceBeforeCtorInitializerColon: 'false' -SpaceBeforeInheritanceColon: 'false' -SpaceBeforeParens: ControlStatements -SpaceBeforeRangeBasedForLoopColon: 'true' -SpaceInEmptyParentheses: 'false' -SpacesBeforeTrailingComments: '1' -SpacesInAngles: 'false' -SpacesInCStyleCastParentheses: 'false' -SpacesInContainerLiterals: 'false' -SpacesInParentheses: 'false' -SpacesInSquareBrackets: 'false' - -# Alignment -PointerAlignment: Left -DerivePointerAlignment: 'false' -AlignEscapedNewlines: Left -AlignAfterOpenBracket: BlockIndent -AlignConsecutiveDeclarations: 'false' -AlignConsecutiveAssignments: 'true' -AlignConsecutiveMacros: 'true' -AlignOperands: 'true' -AlignTrailingComments: 'true' - -# Single Line -AllowShortCaseLabelsOnASingleLine: 'true' -AllowShortFunctionsOnASingleLine: 'false' -AllowShortLambdasOnASingleLine: Inline -AllowAllArgumentsOnNextLine: 'false' -AllowShortLoopsOnASingleLine: 'false' -AllowShortBlocksOnASingleLine: 'false' -AllowAllParametersOfDeclarationOnNextLine: 'false' -AllowShortIfStatementsOnASingleLine: Never - -# Break -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: 'false' -AlwaysBreakTemplateDeclarations: 'Yes' -BreakBeforeBinaryOperators: All -BreakBeforeTernaryOperators: 'false' -BreakInheritanceList: BeforeComma -BreakStringLiterals: 'false' - -# Penalties -PenaltyBreakAssignment: '99999' -PenaltyBreakBeforeFirstCallParameter: '0' -PenaltyBreakComment: '0' -PenaltyBreakFirstLessLess: '0' -PenaltyBreakString: '0' -PenaltyBreakTemplateDeclaration: '0' -PenaltyExcessCharacter: '999999999' -PenaltyReturnTypeOnItsOwnLine: '999999999' # Nope - -# Constructor Initializers -ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' -AllowAllConstructorInitializersOnNextLine: 'false' -BreakConstructorInitializers: BeforeComma - -# Comments -ReflowComments: 'true' -CommentPragmas: '^ TODO@:' -FixNamespaceComments: 'true' - -# Misc -Cpp11BracedListStyle: 'false' -SortUsingDeclarations: 'true' -KeepEmptyLinesAtTheStartOfBlocks: 'false' -MaxEmptyLinesToKeep: '2' - - -ExperimentalAutoDetectBinPacking: false -AllowAllParametersOfDeclarationOnNextLine: false - - - diff --git a/modules/asset_baker/CMakeLists.txt b/modules/asset_baker/CMakeLists.txt index 20a64bf..99b95d8 100644 --- a/modules/asset_baker/CMakeLists.txt +++ b/modules/asset_baker/CMakeLists.txt @@ -1,22 +1,9 @@ -add_executable( - AssetBaker - - ${CMAKE_CURRENT_SOURCE_DIR}/AssetBaker.cpp -) - -set_target_properties( - AssetBaker PROPERTIES - CXX_STANDARD 20 -) - -target_include_directories( - AssetBaker - PRIVATE ${CMAKE_SOURCE_DIR}/AssetParser/ - PRIVATE ${CMAKE_SOURCE_DIR}/Vendor/stb/ +add_executable_module( + asset_baker baker.cpp ) target_link_libraries( - AssetBaker - PRIVATE ${CONAN_LIBS} - PRIVATE AssetParser + asset_baker + PRIVATE asset_parser + PRIVATE stb::stb ) diff --git a/modules/asset_baker/AssetBaker.cpp b/modules/asset_baker/src/baker.cpp similarity index 64% rename from modules/asset_baker/AssetBaker.cpp rename to modules/asset_baker/src/baker.cpp index d850d7c..eba6fed 100644 --- a/modules/asset_baker/AssetBaker.cpp +++ b/modules/asset_baker/src/baker.cpp @@ -1,40 +1,31 @@ -#include -#include +#include +#include #include #include #define STB_IMAGE_IMPLEMENTATION #include -#define ASSERT(x, ...) \ - if (!(x)) \ - { \ - log(__VA_ARGS__); \ - return -1; \ +#define ASSERT(x, ...) \ + if (!(x)) \ + { \ + log(__VA_ARGS__); \ + return -1; \ } template -void log(Args&&... args) +void log(Args &&...args) { (std::cout << ... << args); std::cout << '\n'; } -bool convert_image( - const std::filesystem::path& input, - const std::filesystem::path& output -) +bool convert_image(const std::filesystem::path &input, const std::filesystem::path &output) { int width, height, channels; - stbi_uc* pixels = stbi_load( - input.string().c_str(), - &width, - &height, - &channels, - 4 - ); + stbi_uc *pixels = stbi_load(input.string().c_str(), &width, &height, &channels, 4); if (!pixels) return false; @@ -59,16 +50,16 @@ bool convert_image( return true; } -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { std::ios_base::sync_with_stdio(false); ASSERT( - argc == 3, - "Argc MUST be 3, 1: execution-path(implicit), 2: input-directory, 3: output-directory" + argc == 3, + "Argc MUST be 3, 1: execution-path(implicit), 2: input-directory, 3: output-directory" ); - for (auto& p : std::filesystem::directory_iterator(argv[1])) + for (auto &p : std::filesystem::directory_iterator(argv[1])) { if (p.path().extension() == ".png") { diff --git a/modules/asset_parser/.clang-format b/modules/asset_parser/.clang-format deleted file mode 100644 index 090f268..0000000 --- a/modules/asset_parser/.clang-format +++ /dev/null @@ -1,150 +0,0 @@ ---- -# Core -Language: Cpp -Standard: Cpp11 -ColumnLimit: '80' # No limit - -# Bin pack -BinPackArguments: 'false' -BinPackParameters: 'false' - -# Includes -SortIncludes: 'true' -IncludeBlocks: Regroup -IncludeCategories: - # Current Project - - Regex: '"' - Priority: 001 - - # Custom Project Categories... - - # Dependecies - - Regex: '' - Priority: 400 - - # Dependecies - - Regex: '<' - Priority: 500 - - - # Custom Deependencies Categories... - - # C++ includes - - Regex: '[^.h .hpp]>' - Priority: 998 - - # C includes - - Regex: '<[^/\n]+[.]h>' - Priority: 999 - -# Braces -BreakBeforeBraces: Custom -BraceWrapping: - AfterCaseLabel: true - AfterClass: true - AfterControlStatement: true - AfterEnum: true - AfterFunction: true - AfterNamespace: false - AfterObjCDeclaration: true - AfterStruct: true - AfterUnion: true - AfterExternBlock: true - BeforeCatch: true - BeforeElse: true - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true - -# Indentation -UseTab: ForIndentation -TabWidth: '2' -IndentWidth: '2' -ContinuationIndentWidth: '2' -ConstructorInitializerIndentWidth: '2' -IndentCaseLabels: 'false' -IndentWrappedFunctionNames: 'true' -IndentPPDirectives: BeforeHash -NamespaceIndentation: None -AccessModifierOffset: '-2' - -# Space -SpaceAfterCStyleCast: 'false' -SpaceAfterLogicalNot: 'false' -SpaceAfterTemplateKeyword: 'false' -SpaceBeforeAssignmentOperators: 'true' -SpaceBeforeCpp11BracedList: 'true' -SpaceBeforeCtorInitializerColon: 'false' -SpaceBeforeInheritanceColon: 'false' -SpaceBeforeParens: ControlStatements -SpaceBeforeRangeBasedForLoopColon: 'true' -SpaceInEmptyParentheses: 'false' -SpacesBeforeTrailingComments: '1' -SpacesInAngles: 'false' -SpacesInCStyleCastParentheses: 'false' -SpacesInContainerLiterals: 'false' -SpacesInParentheses: 'false' -SpacesInSquareBrackets: 'false' - -# Alignment -PointerAlignment: Left -DerivePointerAlignment: 'false' -AlignEscapedNewlines: Left -AlignAfterOpenBracket: BlockIndent -AlignConsecutiveDeclarations: 'false' -AlignConsecutiveAssignments: 'true' -AlignConsecutiveMacros: 'true' -AlignOperands: 'true' -AlignTrailingComments: 'true' - -# Single Line -AllowShortCaseLabelsOnASingleLine: 'true' -AllowShortFunctionsOnASingleLine: 'false' -AllowShortLambdasOnASingleLine: Inline -AllowAllArgumentsOnNextLine: 'false' -AllowShortLoopsOnASingleLine: 'false' -AllowShortBlocksOnASingleLine: 'false' -AllowAllParametersOfDeclarationOnNextLine: 'false' -AllowShortIfStatementsOnASingleLine: Never - -# Break -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: 'false' -AlwaysBreakTemplateDeclarations: 'Yes' -BreakBeforeBinaryOperators: All -BreakBeforeTernaryOperators: 'false' -BreakInheritanceList: BeforeComma -BreakStringLiterals: 'false' - -# Penalties -PenaltyBreakAssignment: '99999' -PenaltyBreakBeforeFirstCallParameter: '0' -PenaltyBreakComment: '0' -PenaltyBreakFirstLessLess: '0' -PenaltyBreakString: '0' -PenaltyBreakTemplateDeclaration: '0' -PenaltyExcessCharacter: '999999999' -PenaltyReturnTypeOnItsOwnLine: '999999999' # Nope - -# Constructor Initializers -ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' -AllowAllConstructorInitializersOnNextLine: 'false' -BreakConstructorInitializers: BeforeComma - -# Comments -ReflowComments: 'true' -CommentPragmas: '^ TODO@:' -FixNamespaceComments: 'true' - -# Misc -Cpp11BracedListStyle: 'false' -SortUsingDeclarations: 'true' -KeepEmptyLinesAtTheStartOfBlocks: 'false' -MaxEmptyLinesToKeep: '2' - - -ExperimentalAutoDetectBinPacking: false -AllowAllParametersOfDeclarationOnNextLine: false - - diff --git a/modules/asset_parser/AssetParser.cpp b/modules/asset_parser/AssetParser.cpp deleted file mode 100644 index 2e6236e..0000000 --- a/modules/asset_parser/AssetParser.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "AssetParser.hpp" - -#include -#include -#include - -namespace Assets { - -bool save_binary_file(const char* path, const AssetFile& file) -{ - std::ofstream outstream(path, std::ios::binary | std::ios::out); - - outstream.write((const char*)&file.version, sizeof(uint32_t)); - outstream.write((const char*)&file.type, sizeof(AssetFile::Type)); - - uint32_t json_size = file.json.size(); - uint32_t blob_size = file.blob.size(); - outstream.write((const char*)&json_size, sizeof(uint32_t)); - outstream.write((const char*)&blob_size, sizeof(uint32_t)); - - outstream.write(file.json.c_str(), json_size); - outstream.write((const char*)file.blob.data(), blob_size); - - outstream.close(); - - outstream.close(); - - return true; -} - -bool load_binary_file(const char* path, AssetFile& out_file) -{ - std::ifstream instream(path, std::ios::binary); - instream.seekg(0ull); - - if (!instream.is_open()) - return false; - - instream.read((char*)&out_file.version, sizeof(uint32_t)); - instream.read((char*)&out_file.type, sizeof(AssetFile::Type)); - - uint32_t json_size; - uint32_t blob_size; - instream.read((char*)&json_size, sizeof(uint32_t)); - instream.read((char*)&blob_size, sizeof(uint32_t)); - - out_file.json.resize(json_size); - out_file.blob.resize(blob_size); - instream.read((char*)out_file.json.data(), json_size); - instream.read((char*)out_file.blob.data(), blob_size); - - instream.close(); - - return true; -} - -} // namespace Assets diff --git a/modules/asset_parser/CMakeLists.txt b/modules/asset_parser/CMakeLists.txt index c51d1f9..a53a21a 100644 --- a/modules/asset_parser/CMakeLists.txt +++ b/modules/asset_parser/CMakeLists.txt @@ -1,16 +1,10 @@ -add_library(AssetParser - ${CMAKE_CURRENT_SOURCE_DIR}/AssetParser.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/TextureAsset.cpp -) - -target_include_directories( - AssetParser - PRIVATE ${CMAKE_SOURCE_DIR}/Vendor/nlohmann/single_include - PRIVATE ${CMAKE_SOURCE_DIR}/Vendor/lz4/lib/ +add_library_module(asset_parser + parser.cpp + assets/texture.cpp ) target_link_libraries( - AssetParser - PRIVATE lz4_static - PRIVATE nlohmann_json::nlohmann_json + asset_parser + PRIVATE LZ4::lz4_static + PRIVATE nlohmann_json::nlohmann_json ) diff --git a/modules/asset_parser/TextureAsset.hpp b/modules/asset_parser/include/asset_parser/assets/texture.hpp similarity index 53% rename from modules/asset_parser/TextureAsset.hpp rename to modules/asset_parser/include/asset_parser/assets/texture.hpp index df48476..ad17646 100644 --- a/modules/asset_parser/TextureAsset.hpp +++ b/modules/asset_parser/include/asset_parser/assets/texture.hpp @@ -1,6 +1,6 @@ #pragma once -#include "AssetParser.hpp" +#include namespace Assets { @@ -19,15 +19,15 @@ struct TextureInfo std::string original_file; }; -TextureInfo read_texture_info(AssetFile* file); +TextureInfo read_texture_info(AssetFile *file); void unpack_texture( - TextureInfo* info, - const void* source_buffer, - size_t source_size, - void* destination + TextureInfo *info, + const void *source_buffer, + size_t source_size, + void *destination ); -AssetFile pack_texture(TextureInfo* info, void* pixel_data); +AssetFile pack_texture(TextureInfo *info, void *pixel_data); } // namespace Assets diff --git a/modules/asset_parser/AssetParser.hpp b/modules/asset_parser/include/asset_parser/parser.hpp similarity index 100% rename from modules/asset_parser/AssetParser.hpp rename to modules/asset_parser/include/asset_parser/parser.hpp diff --git a/modules/asset_parser/TextureAsset.cpp b/modules/asset_parser/src/assets/texture.cpp similarity index 61% rename from modules/asset_parser/TextureAsset.cpp rename to modules/asset_parser/src/assets/texture.cpp index ddc4821..fea4e16 100644 --- a/modules/asset_parser/TextureAsset.cpp +++ b/modules/asset_parser/src/assets/texture.cpp @@ -1,5 +1,4 @@ -#include "TextureAsset.hpp" - +#include #include #include @@ -7,7 +6,7 @@ namespace Assets { using namespace nlohmann; -TextureInfo read_texture_info(AssetFile* file) +TextureInfo read_texture_info(AssetFile *file) { json texture_meta_data = json::parse(file->json); @@ -25,19 +24,19 @@ TextureInfo read_texture_info(AssetFile* file) } void unpack_texture( - TextureInfo* info, - const void* source_buffer, - size_t source_size, - void* destination + TextureInfo *info, + const void *source_buffer, + size_t source_size, + void *destination ) { if (info->compression_mode == CompressionMode::LZ4) { LZ4_decompress_safe( - (const char*)source_buffer, - (char*)destination, - source_size, - info->size + (const char *)source_buffer, + (char *)destination, + source_size, + info->size ); } else @@ -46,27 +45,27 @@ void unpack_texture( } } -AssetFile pack_texture(TextureInfo* info, void* pixel_data) +AssetFile pack_texture(TextureInfo *info, void *pixel_data) { json metadata; - metadata["format"] = info->format; - metadata["width"] = info->pixel_size[0]; - metadata["height"] = info->pixel_size[1]; - metadata["bufferSize"] = info->size; + metadata["format"] = info->format; + metadata["width"] = info->pixel_size[0]; + metadata["height"] = info->pixel_size[1]; + metadata["bufferSize"] = info->size; metadata["originalFile"] = info->original_file; - metadata["compression"] = CompressionMode::LZ4; + metadata["compression"] = CompressionMode::LZ4; AssetFile file; - file.type = AssetFile::Type::Texture; + file.type = AssetFile::Type::Texture; file.version = 1u; const int compress_staging = LZ4_compressBound(info->size); file.blob.resize(compress_staging); const int compression_size = LZ4_compress_default( - (const char*)pixel_data, - (char*)file.blob.data(), - info->size, - compress_staging + (const char *)pixel_data, + (char *)file.blob.data(), + info->size, + compress_staging ); file.blob.resize(compression_size); diff --git a/modules/asset_parser/src/parser.cpp b/modules/asset_parser/src/parser.cpp new file mode 100644 index 0000000..840ea7e --- /dev/null +++ b/modules/asset_parser/src/parser.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +namespace Assets { + +bool save_binary_file(const char *path, const AssetFile &file) +{ + std::ofstream outstream(path, std::ios::binary | std::ios::out); + + outstream.write((const char *)&file.version, sizeof(uint32_t)); + outstream.write((const char *)&file.type, sizeof(AssetFile::Type)); + + uint32_t json_size = file.json.size(); + uint32_t blob_size = file.blob.size(); + outstream.write((const char *)&json_size, sizeof(uint32_t)); + outstream.write((const char *)&blob_size, sizeof(uint32_t)); + + outstream.write(file.json.c_str(), json_size); + outstream.write((const char *)file.blob.data(), blob_size); + + outstream.close(); + + outstream.close(); + + return true; +} + +bool load_binary_file(const char *path, AssetFile &out_file) +{ + std::ifstream instream(path, std::ios::binary); + instream.seekg(0ull); + + if (!instream.is_open()) + return false; + + instream.read((char *)&out_file.version, sizeof(uint32_t)); + instream.read((char *)&out_file.type, sizeof(AssetFile::Type)); + + uint32_t json_size; + uint32_t blob_size; + instream.read((char *)&json_size, sizeof(uint32_t)); + instream.read((char *)&blob_size, sizeof(uint32_t)); + + out_file.json.resize(json_size); + out_file.blob.resize(blob_size); + instream.read((char *)out_file.json.data(), json_size); + instream.read((char *)out_file.blob.data(), blob_size); + + instream.close(); + + return true; +} + +} // namespace Assets