diff --git a/src/game/Gen.cpp b/src/game/Gen.cpp new file mode 100644 index 0000000..0e3de66 --- /dev/null +++ b/src/game/Gen.cpp @@ -0,0 +1,48 @@ +#include "Gen.h" + +namespace Generated +{ + bool IsValid(const ModelHandle& h) + { + return h.ModelIdx != UINT16_MAX && IsValid(h.Asset); + } + bool IsValid(const AssetHandle& h) + { + return h.Idx != UINT32_MAX; + } + bool operator==(const AssetHandle& lhs, const AssetHandle& rhs) + { + return lhs.Idx == rhs.Idx; + } + bool operator==(const ModelHandle& lhs, const ModelHandle& rhs) + { + if (!IsValid(lhs) || !IsValid(rhs)) return IsValid(lhs) == IsValid(rhs); + return lhs.ModelIdx == rhs.ModelIdx && lhs.Asset == rhs.Asset; + } + PuzPos operator+=(PuzPos lhs, const PuzPos& rhs) + { + lhs.X += rhs.X; + lhs.Y += rhs.Y; + return lhs; + } + PuzPos operator+(PuzPos lhs, const PuzPos& rhs) + { + PuzPos res = lhs; + res.X += rhs.X; + res.Y += rhs.Y; + return res; + } + PuzPos operator-=(PuzPos lhs, const PuzPos& rhs) + { + lhs.X -= rhs.X; + lhs.Y -= rhs.Y; + return lhs; + } + PuzPos operator-(PuzPos lhs, const PuzPos& rhs) + { + PuzPos res = lhs; + res.X += rhs.X; + res.Y += rhs.Y; + return res; + } +} // namespace Generated diff --git a/src/game/Gen.h b/src/game/Gen.h new file mode 100644 index 0000000..923f467 --- /dev/null +++ b/src/game/Gen.h @@ -0,0 +1,14 @@ +#pragma once +#include "../gen/Generated.h" + +namespace Generated +{ + bool IsValid(const AssetHandle& h); + bool IsValid(const ModelHandle& h); + + bool operator==(const AssetHandle& lhs, const AssetHandle& rhs); + bool operator==(const ModelHandle& lhs, const ModelHandle& rhs); + + PuzPos operator+=(PuzPos lhs, const PuzPos& rhs); + PuzPos operator+(PuzPos lhs, const PuzPos& rhs); +} // namespace Generated diff --git a/src/game/Instance.h b/src/game/Instance.h index 1390636..c69cc22 100644 --- a/src/game/Instance.h +++ b/src/game/Instance.h @@ -45,7 +45,7 @@ namespace Game char ImguiIni[4096]{0}; static constexpr uint32_t MaxAssets = 128; uint32_t AssetCount = 0; - uint32_t AssetHandles[MaxAssets]{0}; + Generated::AssetHandle AssetHandles[MaxAssets]{0}; char AssetHandlePaths[MaxAssets][128]; bool ShowImguiDemo = false; uint8_t DebugCardRotation = 0; diff --git a/src/game/Level.cpp b/src/game/Level.cpp index bf76178..108bc9b 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -1,4 +1,5 @@ #include "../gen/Def.h" +#include "Gen.h" #include "Global.h" #include "Input.h" #include "Instance.h" @@ -23,14 +24,14 @@ namespace Game { void EntityRenderData::Render(const Model* models, const Material* materials) { - if (ModelHandle == UINT16_MAX || MaterialHandle == UINT16_MAX) return; + if (!Generated::IsValid(ModelH) || MaterialHandle == UINT16_MAX) return; if (!Visible) return; auto& rendering = GameRendering::Get(); Transform.UpdateMatrix(); bgfx::setTransform(Transform.M.M); - const Model& currentModel = models[ModelHandle]; + const Model& currentModel = models[ModelH.ModelIdx]; const Material& currentMaterial = materials[MaterialHandle]; bgfx::setVertexBuffer(0, currentModel.VertexBuffer); bgfx::setIndexBuffer(currentModel.IndexBuffer); @@ -277,7 +278,7 @@ namespace Game void Cube::Setup() { EData.MaterialHandle = 0; - EData.ModelHandle = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf"); + EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf"); } void Cube::Update() @@ -300,7 +301,7 @@ namespace Game void TestEntity::Setup() { EData.MaterialHandle = 0; - EData.ModelHandle = GameRendering::Get().GetModelHandleFromPath("models/zurg.gltf"); + EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/zurg.gltf"); EData.Transform.Position = {0.0f, 0.0f, 0.0f}; } @@ -326,7 +327,7 @@ namespace Game Level& level = GetInstance().GameLevel; auto& staticCards = Puzzle::GetStaticPuzzleData().Cards; - for (int8_t y = 0; y < Data.WidthTiles / Puzzle::Config::CardSize; ++y) + for (int8_t y = 0; y < Data.HeightTiles / Puzzle::Config::CardSize; ++y) { for (int8_t x = 0; x < Data.WidthTiles / Puzzle::Config::CardSize; ++x) { @@ -336,21 +337,24 @@ namespace Game bool IsValid = Puzzle::IsValid(card.RefCard); tile.EData.Visible = true; - quad.EData.Visible = IsValid; + quad.EData.Visible = true; - // const Generated::StaticPuzzleCard& cData = Puzzle::GetCard(card.RefCard); - tile.EData.ModelHandle = - IsValid ? staticCards[card.RefCard.Idx].ModelHandle : staticCards[0].ModelHandle; - tile.EData.Transform.SetPosition({ + tile.EData.ModelH = IsValid ? staticCards[card.RefCard.Idx].ModelHandle : staticCards[0].ModelHandle; + Vec3 cardPos = { (float)card.Position.X * Puzzle::Config::CardScaleWorld, -5.0f, (float)card.Position.Y * Puzzle::Config::CardScaleWorld, - }); + }; + if (!IsValid) + { + cardPos = {x * Puzzle::Config::CardScaleWorld, -5.0f, y * Puzzle::Config::CardScaleWorld}; + } + tile.EData.Transform.SetPosition(cardPos); bx::mtxRotateY(tile.EData.Transform.Rotation.M, card.Rotation * bx::kPi * 0.5f); Vec3 fw = {0, -1, 0}; - quad.EData.Transform.SetPosition({}); - quad.EData.Transform.Rotation = camTransform.Rotation; + quad.EData.Transform.SetPosition(fw); + quad.EData.Transform.Rotation = {}; } } } diff --git a/src/game/Level.h b/src/game/Level.h index fd822a1..fa1bb27 100644 --- a/src/game/Level.h +++ b/src/game/Level.h @@ -21,7 +21,7 @@ namespace Game Vec4 BaseColor{0.0f, 0.0f, 0.0f, 1.0f}; Transform Transform; uint16_t MaterialHandle = UINT16_MAX; - uint16_t ModelHandle = UINT16_MAX; + Generated::ModelHandle ModelH; bool Visible = true; void Render(const Model* models, const Material* materials); diff --git a/src/game/Mesh.cpp b/src/game/Mesh.cpp index d93998b..23fc3a0 100644 --- a/src/game/Mesh.cpp +++ b/src/game/Mesh.cpp @@ -133,6 +133,7 @@ namespace Game } LOG("Found %u models!", modelFilePathCount); + auto& inst = GetInstance(); int32_t writeI = 0; for (int32_t i = 0; i < modelFilePathCount; ++i) { @@ -141,11 +142,11 @@ namespace Game Model& mod = models[writeI]; if (LoadMesh(mod, fullPath.getCPtr(), modelFileIsBinary[i])) { - mod.AssetHandle = CrcPath(fullPath.getCPtr()); - auto& inst = GetInstance(); + mod.Handle.Asset.Idx = CrcPath(fullPath.getCPtr()); + mod.Handle.ModelIdx = inst.DebugData.AssetCount; if (inst.DebugData.AssetCount < inst.DebugData.MaxAssets) { - inst.DebugData.AssetHandles[inst.DebugData.AssetCount] = mod.AssetHandle; + inst.DebugData.AssetHandles[inst.DebugData.AssetCount] = mod.Handle.Asset; bx::strCopy(inst.DebugData.AssetHandlePaths[inst.DebugData.AssetCount], sizeof(inst.DebugData.AssetHandlePaths[inst.DebugData.AssetCount]), fullPath.getCPtr()); diff --git a/src/game/Puzzle.cpp b/src/game/Puzzle.cpp index 8367d3d..9bae4a7 100644 --- a/src/game/Puzzle.cpp +++ b/src/game/Puzzle.cpp @@ -588,33 +588,3 @@ namespace Puzzle return isVisible; } } // namespace Puzzle - -namespace Generated -{ - PuzPos operator+=(PuzPos lhs, const PuzPos& rhs) - { - lhs.X += rhs.X; - lhs.Y += rhs.Y; - return lhs; - } - PuzPos operator+(PuzPos lhs, const PuzPos& rhs) - { - PuzPos res = lhs; - res.X += rhs.X; - res.Y += rhs.Y; - return res; - } - PuzPos operator-=(PuzPos lhs, const PuzPos& rhs) - { - lhs.X -= rhs.X; - lhs.Y -= rhs.Y; - return lhs; - } - PuzPos operator-(PuzPos lhs, const PuzPos& rhs) - { - PuzPos res = lhs; - res.X += rhs.X; - res.Y += rhs.Y; - return res; - } -} // namespace Generated diff --git a/src/game/Puzzle.h b/src/game/Puzzle.h index ce4daf0..42fdb5f 100644 --- a/src/game/Puzzle.h +++ b/src/game/Puzzle.h @@ -2,7 +2,7 @@ #include #include -#include "../../gen/Generated.h" +#include "Gen.h" namespace Puzzle { @@ -50,9 +50,3 @@ namespace Puzzle }; bool RenderDebugUI(PuzzleData& obj); } // namespace Puzzle - -namespace Generated -{ - PuzPos operator+=(PuzPos lhs, const PuzPos& rhs); - PuzPos operator+(PuzPos lhs, const PuzPos& rhs); -} // namespace Generated diff --git a/src/game/Tools.cpp b/src/game/Tools.cpp index 35fbf30..cf00a77 100644 --- a/src/game/Tools.cpp +++ b/src/game/Tools.cpp @@ -1,14 +1,13 @@ #include "Global.h" #include "Instance.h" #include "Tools.h" -#include "bx/string.h" #include "rendering/Rendering.h" #include namespace Tools { - const char* GetAssetPath(uint32_t assetHandle) + const char* GetAssetPath(Generated::AssetHandle assetHandle) { const auto& inst = Game::GetInstance(); for (int32_t j = 0; j < inst.DebugData.AssetCount; ++j) @@ -21,26 +20,17 @@ namespace Tools return "---"; } - void ModelDropdown(uint32_t& modelHandle) + void ModelDropdown(Generated::ModelHandle& modelHandle) { auto& R = Game::GameRendering::Get(); - const char* name = GetAssetPath(modelHandle); - int32_t idx = -1; - for (int32_t i = 0; i < R.ModelCount; ++i) - { - if (R.Models[i].AssetHandle == modelHandle) - { - idx = i; - break; - } - } + const char* name = GetAssetPath(modelHandle.Asset); if (ImGui::BeginCombo("Models", name)) { for (int32_t i = 0; i < R.ModelCount; ++i) { - if (ImGui::Selectable(GetAssetPath(R.Models[i].AssetHandle), i == idx)) + if (ImGui::Selectable(GetAssetPath(R.Models[i].Handle.Asset), i == modelHandle.ModelIdx)) { - modelHandle = i; + modelHandle = R.Models[i].Handle; } } ImGui::EndCombo(); diff --git a/src/game/Tools.h b/src/game/Tools.h index c2020ce..d17a012 100644 --- a/src/game/Tools.h +++ b/src/game/Tools.h @@ -1,7 +1,7 @@ #pragma once -#include +#include "Gen.h" namespace Tools { - void ModelDropdown(uint32_t& modelHandle); + void ModelDropdown(Generated::ModelHandle& modelHandle); } // namespace Tools diff --git a/src/game/data/puzzles/0.pzl b/src/game/data/puzzles/0.pzl index ab1e767..2bb71ee 100644 --- a/src/game/data/puzzles/0.pzl +++ b/src/game/data/puzzles/0.pzl @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b6c70072903838f61c24ad449706759d1e1b1820fc79b5499bb9b9d3e46a3cc3 -size 5816 +oid sha256:4947c3539ee4bef2783ae0632897b501b4eb3d5d248f23d8a7fe175d807cae89 +size 5820 diff --git a/src/game/data/static/puzzle.dat b/src/game/data/static/puzzle.dat index 1af5799..c6e81e4 100644 Binary files a/src/game/data/static/puzzle.dat and b/src/game/data/static/puzzle.dat differ diff --git a/src/game/mini.def b/src/game/mini.def index 8358672..45f60c6 100644 --- a/src/game/mini.def +++ b/src/game/mini.def @@ -38,6 +38,17 @@ type Mat4 }") } +type AssetHandle +{ + u32 Idx Default("UINT32_MAX") +} + +type ModelHandle +{ + u16 ModelIdx Default("UINT16_MAX") + AssetHandle Asset +} + type PuzPos { i8 X @@ -59,7 +70,8 @@ enum PuzzleElementType(u8) type StaticPuzzleCard { PuzzleElementType Elements Arr(4) - u32 ModelHandle + ModelHandle ModelHandle + } type StaticPuzzleCardHandle diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp index e7553cf..c1d6ce3 100644 --- a/src/game/rendering/Rendering.cpp +++ b/src/game/rendering/Rendering.cpp @@ -731,17 +731,17 @@ namespace Game return mat; } - uint16_t GameRendering::GetModelHandleFromPath(const char* path) + Generated::ModelHandle GameRendering::GetModelHandleFromPath(const char* path) { uint32_t AssetHandle = CrcPath(path); for (int32_t i = 0; i < ModelCount; ++i) { - if (Models[i].AssetHandle == AssetHandle) + if (Models[i].Handle.Asset.Idx == AssetHandle) { - return i; + return Models[i].Handle; } } - return 0; + return {}; } } // namespace Game diff --git a/src/game/rendering/Rendering.h b/src/game/rendering/Rendering.h index 199167e..39129bf 100644 --- a/src/game/rendering/Rendering.h +++ b/src/game/rendering/Rendering.h @@ -1,11 +1,12 @@ #pragma once #include "bgfx/defines.h" +#include "imgui.h" #include #include #include +#include "../Gen.h" #include "../Global.h" -#include "imgui.h" union SDL_Event; @@ -36,7 +37,7 @@ namespace Game bgfx::VertexBufferHandle VertexBuffer; bgfx::IndexBufferHandle IndexBuffer; bgfx::VertexLayout VertLayout; - uint32_t AssetHandle = UINT16_MAX; + Generated::ModelHandle Handle; }; struct Material @@ -118,6 +119,6 @@ namespace Game void HandleEvents(); void RenderDebugUI(); void Shutdown(); - uint16_t GetModelHandleFromPath(const char* path); + Generated::ModelHandle GetModelHandleFromPath(const char* path); }; } // namespace Game diff --git a/src/gen/Generated.cpp b/src/gen/Generated.cpp index f0cbbe4..c4d535f 100644 --- a/src/gen/Generated.cpp +++ b/src/gen/Generated.cpp @@ -1,4 +1,3 @@ -#include "Def.h" #include "Generated.h" namespace Generated @@ -125,6 +124,44 @@ namespace Generated } return isOk; } + bool Save(const AssetHandle* obj, uint32_t count, Serializer& serializer) + { + bool isOk = true; + for (uint32_t i = 0; i < count; ++i) + { + isOk = Save(&obj[i].Idx, 1, serializer) && isOk; + } + return isOk; + } + bool Load(AssetHandle* obj, uint32_t count, Deserializer& serializer) + { + bool isOk = true; + for (uint32_t i = 0; i < count; ++i) + { + isOk = Load(&obj[i].Idx, 1, serializer) && isOk; + } + return isOk; + } + bool Save(const ModelHandle* obj, uint32_t count, Serializer& serializer) + { + bool isOk = true; + for (uint32_t i = 0; i < count; ++i) + { + isOk = Save(&obj[i].ModelIdx, 1, serializer) && isOk; + isOk = Save(&obj[i].Asset, 1, serializer) && isOk; + } + return isOk; + } + bool Load(ModelHandle* obj, uint32_t count, Deserializer& serializer) + { + bool isOk = true; + for (uint32_t i = 0; i < count; ++i) + { + isOk = Load(&obj[i].ModelIdx, 1, serializer) && isOk; + isOk = Load(&obj[i].Asset, 1, serializer) && isOk; + } + return isOk; + } bool Save(const PuzPos* obj, uint32_t count, Serializer& serializer) { bool isOk = true; diff --git a/src/gen/Generated.h b/src/gen/Generated.h index ecd9b79..211707a 100644 --- a/src/gen/Generated.h +++ b/src/gen/Generated.h @@ -1,14 +1,12 @@ #pragma once -#include +#include "Def.h" namespace Generated { - struct Serializer; - struct Deserializer; struct PuzzleElementType { - static constexpr int32_t EntryCount = 8; static constexpr uint32_t Hash = 2024002654; + static constexpr int32_t EntryCount = 8; enum Enum : int32_t { None, @@ -94,6 +92,17 @@ namespace Generated 0.0f, 0.0f, 0.0f, 1.0f }; }; + struct AssetHandle + { + static constexpr uint32_t Hash = 2609735487; + uint32_t Idx = UINT32_MAX; + }; + struct ModelHandle + { + static constexpr uint32_t Hash = 298089627; + uint16_t ModelIdx = UINT16_MAX; + AssetHandle Asset = {}; + }; struct PuzPos { static constexpr uint32_t Hash = 1834398141; @@ -102,9 +111,9 @@ namespace Generated }; struct StaticPuzzleCard { - static constexpr uint32_t Hash = 1414289929; + static constexpr uint32_t Hash = 2851442461; PuzzleElementType::Enum Elements[4] = {}; - uint32_t ModelHandle = {}; + ModelHandle ModelHandle = {}; }; struct StaticPuzzleCardHandle { @@ -113,7 +122,7 @@ namespace Generated }; struct StaticPuzzleData { - static constexpr uint32_t Hash = 84985581; + static constexpr uint32_t Hash = 4204694691; StaticPuzzleCard Cards[64] = {}; }; struct PuzzleCardStack @@ -157,6 +166,10 @@ namespace Generated bool Load(Mat3* obj, uint32_t count, Deserializer& serializer); bool Save(const Mat4* obj, uint32_t count, Serializer& serializer); bool Load(Mat4* obj, uint32_t count, Deserializer& serializer); + bool Save(const AssetHandle* obj, uint32_t count, Serializer& serializer); + bool Load(AssetHandle* obj, uint32_t count, Deserializer& serializer); + bool Save(const ModelHandle* obj, uint32_t count, Serializer& serializer); + bool Load(ModelHandle* obj, uint32_t count, Deserializer& serializer); bool Save(const PuzPos* obj, uint32_t count, Serializer& serializer); bool Load(PuzPos* obj, uint32_t count, Deserializer& serializer); bool Save(const StaticPuzzleCard* obj, uint32_t count, Serializer& serializer);