From 09062d6836586138e88f120a27476f684bcbcfc0 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Sat, 25 Oct 2025 17:37:04 +0200 Subject: [PATCH] Version 0.2.0 Fixed Size/UV Issues Atlas builder Calculate UVS during Packing Fix File loader loading height into width Set Atlas Size on File load Push Entries on File load Only build palladium, if it is not in build space Add functionality to d7rc-make to check files and create files Fix Palladium Submodule Integration --- .gitmodules | 2 +- CMakeLists.txt | 8 +++-- include/d7rc/atlas.hpp | 1 + source/lib/atlas.cpp | 13 +++++-- source/lib/file.cpp | 4 ++- source/tool/main.cpp | 81 ++++++++++++++++++++++++++++++++++-------- vendor/palladium | 1 + 7 files changed, 88 insertions(+), 22 deletions(-) create mode 160000 vendor/palladium diff --git a/.gitmodules b/.gitmodules index c986e29..46409c5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "palladium"] +[submodule "vendor/palladium"] path = vendor/palladium url = https://dev.npid7.de/tobid7/palladium diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c35d6..4df8555 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,13 @@ cmake_minimum_required(VERSION 3.22) -project(d7rc VERSION 0.1.0) +project(d7rc VERSION 0.2.0) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED true) +if(NOT TARGET palladium) add_subdirectory(vendor/palladium) +endif() add_library(${PROJECT_NAME} source/lib/file.cpp @@ -26,4 +28,6 @@ add_executable(${PROJECT_NAME}-make target_include_directories(${PROJECT_NAME}-make PRIVATE palladium include) target_link_libraries(${PROJECT_NAME}-make PRIVATE d7rc palladium) -target_compile_definitions(${PROJECT_NAME}-make PRIVATE -DVERSION="${PROJECT_VERSION}") \ No newline at end of file +target_compile_definitions(${PROJECT_NAME}-make PRIVATE -DVERSION="${PROJECT_VERSION}") + +install(TARGETS d7rc-make DESTINATION bin) \ No newline at end of file diff --git a/include/d7rc/atlas.hpp b/include/d7rc/atlas.hpp index a6f872e..6c8d183 100644 --- a/include/d7rc/atlas.hpp +++ b/include/d7rc/atlas.hpp @@ -48,6 +48,7 @@ class Atlas { PD::Li::Texture::Ref Get(const std::string& name); void pSplit(int i, PD::ivec4 pos); + void pCreateUVs(); struct Entry { PD::Li::Rect UV; diff --git a/source/lib/atlas.cpp b/source/lib/atlas.cpp index 2c369ac..6230e97 100644 --- a/source/lib/atlas.cpp +++ b/source/lib/atlas.cpp @@ -40,9 +40,6 @@ bool Atlas::AppendImage(PD::Image::Ref img, const std::string& name) { e.iImg = img; e.iPos = PD::ivec2(it.x, it.y); e.Size = PD::ivec2(img->Width(), img->Height()); - e.UV = PD::fvec4((float)it.x / (float)Size.x, (float)it.y / (float)Size.y, - (float)(it.x + img->Width()) / (float)Size.x, - (float)(it.x + img->Height()) / (float)Size.y); Entries.push_back(e); #ifdef DEBUG std::cout << std::format("Created Image {} ({})\n", name, @@ -86,6 +83,7 @@ void Atlas::Pack() { if (!PD::BitUtil::IsSingleBit(Max.y)) { Size.y = PD::BitUtil::GetPow2(Max.y); } + pCreateUVs(); Img.resize(Size.x * Size.y * 4); for (auto& it : Entries) { for (int i = 0; i < it.Size.x; i++) { @@ -128,4 +126,13 @@ PD::Li::Texture::Ref Atlas::Get(const std::string& name) { } return nullptr; } + +void Atlas::pCreateUVs() { + for (auto& e : Entries) { + e.UV = PD::fvec4((float)e.iPos.x / (float)Size.x, + 1.f - (float)e.iPos.y / (float)Size.y, + (float)(e.iPos.x + e.Size.x) / (float)Size.x, + 1.f - (float)(e.iPos.y + e.Size.y) / (float)Size.y); + } +} } // namespace D7RC \ No newline at end of file diff --git a/source/lib/file.cpp b/source/lib/file.cpp index b67149c..8b5455d 100644 --- a/source/lib/file.cpp +++ b/source/lib/file.cpp @@ -107,6 +107,7 @@ void File::Read(Atlas& atlas, const std::string& path) { hdr.Read(iff); pReadTab(iff, atlas); atlas.Img.resize(hdr.DataSize); + atlas.Size = PD::ivec2(hdr.Width, hdr.Height); iff.read(reinterpret_cast(atlas.Img.data()), hdr.DataSize); iff.close(); } @@ -118,11 +119,12 @@ void File::pReadTab(std::ifstream& iff, Atlas& atlas) { PD::u32 namelen = 0; iff.read(reinterpret_cast(&e.UV), sizeof(e.UV)); iff.read(reinterpret_cast(&w), sizeof(w)); - iff.read(reinterpret_cast(&w), sizeof(h)); + iff.read(reinterpret_cast(&h), sizeof(h)); iff.read(reinterpret_cast(&namelen), sizeof(PD::u32)); e.Name.resize(namelen); iff.read(e.Name.data(), namelen); e.Size = PD::ivec2(w, h); + atlas.Entries.push_back(e); } } diff --git a/source/tool/main.cpp b/source/tool/main.cpp index 31c3445..9c142ab 100644 --- a/source/tool/main.cpp +++ b/source/tool/main.cpp @@ -28,21 +28,72 @@ SOFTWARE. #define VERSION "" #endif -int main() { - std::cout << "d7rc-make v" VERSION << std::endl; - int szs = 1024; - D7RC::Atlas a(szs); - std::map imgs; - for (auto& it : std::filesystem::directory_iterator("images")) { - auto img = PD::Image::New(it.path().string()); - PD::Image::Convert(img, PD::Image::RGBA); - imgs[img] = it.path().filename().string(); +int main(int argc, char** argv) { + if (argc > 1) { + if (std::string(argv[1]) == "version") { + std::cout << "d7rc-make v" << VERSION << std::endl; + return 0; + } else if (std::string(argv[1]) == "help") { + std::cout << "d7rc-make v" << VERSION << std::endl; + std::cout << "Usage: d7rc-make [cmd]" << std::endl; + std::cout << "Commands:" << std::endl; + std::cout << " version Show version" << std::endl; + std::cout << " help Show this help message" + << std::endl; + std::cout << " tex paths out Create Texture Atlas" << std::endl; + std::cout << " check path Show D7RC file info" << std::endl; + return 0; + } else if (std::string(argv[1]) == "tex") { + if (argc < 4) { + std::cout << "Not paths given for 'tex'." << std::endl; + return 0; + } + int szs = 1024; + D7RC::Atlas a(szs); + std::map imgs; + for (int i = 2; i < argc - 1; i++) { + for (auto& it : std::filesystem::directory_iterator(argv[i])) { + auto img = PD::Image::New(it.path().string()); + PD::Image::Convert(img, PD::Image::RGBA); + imgs[img] = it.path().filename().string(); + } + } + for (auto& it : imgs) { + a.AppendImage(it.first, it.second); + } + a.Pack(); + D7RC::File f; + f.Write(a, std::string(argv[argc - 1])); + return 0; + } else if (std::string(argv[1]) == "check") { + if (argc < 3) { + std::cout << "No path to .d7rc file was specified!" << std::endl; + return 0; + } + D7RC::Atlas a; + D7RC::File f; + f.Read(a, argv[2]); + // Probably not the way how you should output it... but my favourite + std::cout << std::format("Magic: {}", std::string((char*)&f.hdr.Magic, 4)) + << std::endl; + std::cout << std::format("Size: {}", PD::ivec2(f.hdr.Width, f.hdr.Height)) + << std::endl; + std::cout << "Data Size: " << PD::Strings::FormatBytes(f.hdr.DataSize) + << std::endl; + std::cout << "Images: " << f.hdr.NumEntries << std::endl; + std::cout << std::format( + "Atlas Info:\n {} Entries\n {} bytes\nEntries:\n", a.Entries.size(), + a.Img.size()); + for (auto& it : a.Entries) { + std::cout << std::format(" {} {}\n", it.Name, it.Size); + } + } else { + std::cout << "Unknown command: " << argv[1] << std::endl; + return 0; + } + } else { + std::cout << "d7rc-make v" << VERSION << std::endl; + std::cout << "Use 'd7rc-make help' for more information." << std::endl; } - for (auto& it : imgs) { - a.AppendImage(it.first, it.second); - } - a.Pack(); - D7RC::File f; - f.Write(a, "test.d7rc"); return 0; } \ No newline at end of file diff --git a/vendor/palladium b/vendor/palladium new file mode 160000 index 0000000..8328181 --- /dev/null +++ b/vendor/palladium @@ -0,0 +1 @@ +Subproject commit 8328181a2addb8f1ff6b21624ec66cb75d0b1543