From 4f4f25e1650a1c820de1412ce91904cd132104f7 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Sun, 14 Sep 2025 07:40:30 +0530 Subject: [PATCH] Y Sorting --- .vscode/settings.json | 11 +++- Src/IAESandbox/imp/cpp/Game.cpp | 50 +++++++++++++++---- .../imp/cpp/Components/AtlasRenderer.cpp | 2 +- .../imp/cpp/Components/SpriteRenderer.cpp | 4 +- .../imp/cpp/Components/TextureRenderer.cpp | 1 + Src/IAEngine/imp/cpp/IAEngine.cpp | 5 ++ Src/IAEngine/imp/cpp/Rendering/Mesh/Quad.cpp | 4 +- Src/IAEngine/imp/cpp/Rendering/Renderer.cpp | 17 ++++--- Src/IAEngine/imp/cpp/Texture.cpp | 4 +- .../IAEngine/Components/SpriteRenderer.hpp | 2 +- Src/IAEngine/inc/IAEngine/IAEngine.hpp | 1 + Src/IAEngine/inc/IAEngine/Nodes/Node.hpp | 33 ++++++------ .../inc/IAEngine/Rendering/Mesh/Quad.hpp | 2 +- .../inc/IAEngine/Rendering/Renderer.hpp | 4 +- Src/IAEngine/inc/IAEngine/Scene.hpp | 6 +++ Src/IAEngine/inc/IAEngine/Texture.hpp | 2 +- 16 files changed, 103 insertions(+), 45 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6d3153f..6e995d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,15 @@ "type_traits": "cpp", "xmemory": "cpp", "xtr1common": "cpp", - "chrono": "cpp" + "chrono": "cpp", + "iterator": "cpp", + "list": "cpp", + "vector": "cpp", + "xhash": "cpp", + "xtree": "cpp", + "initializer_list": "cpp", + "random": "cpp", + "span": "cpp", + "xstring": "cpp" } } \ No newline at end of file diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index 1e8c0c2..f7c7391 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -1,24 +1,58 @@ #include +#include #include +#include #include namespace ia::iae::game { - RefPtr scene; + RefPtr scene; - Texture g_tex; - Texture g_tex2; + RefPtr g_player; VOID Game::Initialize() { scene = Engine::CreateScene(); + scene->YSortingEnabled() = true; Engine::ChangeScene(scene); - const auto d = File::ReadToVector("Graphics/1.jpg"); - g_tex = Engine::CreateTexture(d.data(), d.size()); - g_tex2 = Engine::CreateTexture(d.data(), d.size()); + g_player = MakeRefPtr(); + { + const auto t = g_player->AddComponent(); + t->AddAnimation({ + .ShouldLoop = true, + .Keys = { + SpriteRendererComponent::AnimationKeyFrame { + .Scale = {0.1f, 0.1f, 0.1f}, + .Texture = Engine::CreateTexture(File::ReadToVector("Graphics/green.png")), + } + }, + }); + t->BakeAnimations(); + } + g_player->SetLocalPosition({200, 200, 0}); + + const auto obstacle = MakeRefPtr(); + { + const auto t = obstacle->AddComponent(); + t->AddAnimation({ + .ShouldLoop = true, + .Keys = { + SpriteRendererComponent::AnimationKeyFrame { + .Scale = {0.25f, 0.25f, 0.1f}, + .Texture = Engine::CreateTexture(File::ReadToVector("Graphics/red.png")), + } + }, + }); + t->BakeAnimations(); + } + obstacle->SortOffset() = 20; + obstacle->SetLocalPosition({150, 100, 0}); + + scene->AddNode(g_player); + scene->AddNode(obstacle); } VOID Game::Terminate() @@ -27,8 +61,6 @@ namespace ia::iae::game VOID Game::Update() { - g_tex.Draw({200.0f, 150.0f, 10.0f}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f}); - g_tex2.Draw({300.0f, 150.0f, 15.0f}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 0.0f, 1.0f, 1.0f}); - //iae::Renderer::GetCamera()->Position().x += 0.1f; + g_player->SetLocalPosition(g_player->GetLocalPosition() + glm::vec3{Input::GetDirectionalInput(), 0.0f}); } } \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp b/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp index ac98409..85d2909 100644 --- a/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp +++ b/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp @@ -56,7 +56,7 @@ namespace ia::iae { const auto t = m_tileGrid.m_tileTextures[x + (y * m_tileGrid.TileCountX)]; if (t != INVALID_HANDLE) - m_textures[t]->Draw(p + m_tileGrid.Position, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f}); + m_textures[t]->Draw(m_node->SortOffset(), p + m_tileGrid.Position, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f}); p.x += m_tileGrid.TileWidth; } p.x = m_node->GetPosition().x; diff --git a/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp b/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp index 364068e..09c1882 100644 --- a/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp +++ b/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp @@ -66,8 +66,8 @@ namespace ia::iae VOID SpriteRendererComponent::Draw() { const auto &animFrame = m_currentAnimationState; - if(!animFrame.Texture) return; - animFrame.Texture->Draw( + animFrame.Texture.Draw( + m_node->SortOffset(), m_node->GetPosition() + animFrame.Position, m_node->GetScale() * animFrame.Scale, m_node->GetRotation().z + animFrame.Rotation.z, m_isFlippedH, m_isFlippedV, animFrame.ColorOverlay); } diff --git a/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp b/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp index 6de0026..ecdad3c 100644 --- a/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp +++ b/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp @@ -32,6 +32,7 @@ namespace ia::iae VOID TextureRendererComponent::Draw() { m_texture->Draw( + m_node->SortOffset(), m_node->GetPosition() + m_position, m_node->GetScale(), m_node->GetRotation().z, false, false, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); } diff --git a/Src/IAEngine/imp/cpp/IAEngine.cpp b/Src/IAEngine/imp/cpp/IAEngine.cpp index f940bd9..990898a 100644 --- a/Src/IAEngine/imp/cpp/IAEngine.cpp +++ b/Src/IAEngine/imp/cpp/IAEngine.cpp @@ -144,6 +144,11 @@ namespace ia::iae { g_activeScene = scene; } + + Scene* Engine::GetActiveScene() + { + return g_activeScene.get(); + } } // namespace ia::iae namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Rendering/Mesh/Quad.cpp b/Src/IAEngine/imp/cpp/Rendering/Mesh/Quad.cpp index 5005d29..e4186e0 100644 --- a/Src/IAEngine/imp/cpp/Rendering/Mesh/Quad.cpp +++ b/Src/IAEngine/imp/cpp/Rendering/Mesh/Quad.cpp @@ -37,8 +37,8 @@ namespace ia::iae g_quadMeshVertexBuffer.reset(); } - VOID QuadMesh::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation) + VOID QuadMesh::Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation) { - Renderer::Draw(position, scale, rotation, g_quadMeshVertexBuffer->GetHandle(), 6); + Renderer::Draw(sortOffset, position, scale, rotation, g_quadMeshVertexBuffer->GetHandle(), 6); } } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp b/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp index 904d23a..5588f62 100644 --- a/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp +++ b/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp @@ -102,8 +102,8 @@ namespace ia::iae .format = SDL_GPU_TEXTUREFORMAT_D16_UNORM, .usage = SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, - .width = (UINT32)s_width, - .height = (UINT32)s_height, + .width = (UINT32) s_width, + .height = (UINT32) s_height, .layer_count_or_depth = 1, .num_levels = 1, .sample_count = SDL_GPU_SAMPLECOUNT_1}; @@ -234,27 +234,28 @@ namespace ia::iae SDL_PushGPUFragmentUniformData(g_cmdBuffer, 0, &textureState, sizeof(textureState)); } - VOID SetModelTransformMatrix(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation) + VOID SetModelTransformMatrix(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation) { - matModel = glm::translate(glm::mat4(1.0f), position); + const auto depthTestOffset = sortOffset + (Engine::GetActiveScene()->YSortingEnabled() ? position.y : 0); + matModel = glm::translate(glm::mat4(1.0f), {position.x, position.y, position.z + depthTestOffset}); matModel = glm::rotate(matModel, rotation, glm::vec3(0.0f, 0.0f, 1.0f)); matModel = glm::scale(matModel, scale); SDL_PushGPUVertexUniformData(g_cmdBuffer, 2, &matModel, sizeof(matModel)); } - VOID Renderer::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, + VOID Renderer::Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount) { - SetModelTransformMatrix(position, scale, rotation); + SetModelTransformMatrix(sortOffset, position, scale, rotation); SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0}}; SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1); SDL_DrawGPUPrimitives(g_renderPass, vertexCount, 1, 0, 0); } - VOID Renderer::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, + VOID Renderer::Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount) { - SetModelTransformMatrix(position, scale, rotation); + SetModelTransformMatrix(sortOffset, position, scale, rotation); SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0}, {.buffer = (SDL_GPUBuffer *) indexBufferHandle, .offset = 0}}; SDL_BindGPUVertexBuffers(g_renderPass, 0, bindings, 1); diff --git a/Src/IAEngine/imp/cpp/Texture.cpp b/Src/IAEngine/imp/cpp/Texture.cpp index c4ea8a9..27a38ae 100644 --- a/Src/IAEngine/imp/cpp/Texture.cpp +++ b/Src/IAEngine/imp/cpp/Texture.cpp @@ -35,10 +35,10 @@ namespace ia::iae { } - VOID Texture::Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN BOOL flipH, + VOID Texture::Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN BOOL flipH, IN BOOL flipV, IN CONST glm::vec4 &colorOverlay) CONST { Renderer::BindTexture(m_handle, flipV, flipH, colorOverlay); - QuadMesh::Draw(position, m_size * scale, rotation); + QuadMesh::Draw(sortOffset, position, m_size * scale, rotation); } } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp b/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp index c74c592..5bfd04d 100644 --- a/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp +++ b/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp @@ -32,7 +32,7 @@ namespace ia::iae glm::vec3 Scale{1.0f, 1.0f, 1.0f}; glm::vec4 ColorOverlay{1.0f, 1.0f, 1.0f, 1.0f}; BOOL ShouldInterpolate{}; - RefPtr Texture; + Texture Texture; }; struct Animation diff --git a/Src/IAEngine/inc/IAEngine/IAEngine.hpp b/Src/IAEngine/inc/IAEngine/IAEngine.hpp index c311f4b..7eac6b3 100644 --- a/Src/IAEngine/inc/IAEngine/IAEngine.hpp +++ b/Src/IAEngine/inc/IAEngine/IAEngine.hpp @@ -48,6 +48,7 @@ namespace ia::iae STATIC BOOL ShouldClose(); STATIC VOID ChangeScene(IN RefPtr scene); + STATIC Scene* GetActiveScene(); public: STATIC RefPtr CreateScene(); diff --git a/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp b/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp index 785d54c..b4446db 100644 --- a/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp +++ b/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp @@ -27,7 +27,7 @@ namespace ia::iae class Node : public Transform { public: - VIRTUAL VOID OnAdded(IN Scene* scene); + VIRTUAL VOID OnAdded(IN Scene *scene); VIRTUAL VOID OnRemoved(); VIRTUAL VOID Draw(); @@ -38,19 +38,24 @@ namespace ia::iae public: VOID AddChild(IN RefPtr node); - template - _component_type* AddComponent(); + template _component_type *AddComponent(); - template - _component_type* GetComponent(); + template _component_type *GetComponent(); - CONST Vector>& GetComponents() CONST + CONST Vector> &GetComponents() CONST { return m_components; } + public: + FLOAT32 &SortOffset() + { + return m_sortOffset; + } + protected: - Scene* m_scene{}; + Scene *m_scene{}; + FLOAT32 m_sortOffset{}; BOOL m_isEnabled{true}; protected: @@ -58,25 +63,23 @@ namespace ia::iae friend class IComponent; - private: + private: VOID AddComponent(IN RefPtr component); }; - template - _component_type* Node::AddComponent() + template _component_type *Node::AddComponent() { const auto c = MakeRefPtr<_component_type>(this); AddComponent(c); return c.get(); } - template - _component_type* Node::GetComponent() + template _component_type *Node::GetComponent() { - for(auto& c: m_components) + for (auto &c : m_components) { - _component_type* comp = dynamic_cast<_component_type*>(c.get()); - if(comp) + _component_type *comp = dynamic_cast<_component_type *>(c.get()); + if (comp) return comp; } return nullptr; diff --git a/Src/IAEngine/inc/IAEngine/Rendering/Mesh/Quad.hpp b/Src/IAEngine/inc/IAEngine/Rendering/Mesh/Quad.hpp index 35240c1..f225eb4 100644 --- a/Src/IAEngine/inc/IAEngine/Rendering/Mesh/Quad.hpp +++ b/Src/IAEngine/inc/IAEngine/Rendering/Mesh/Quad.hpp @@ -26,6 +26,6 @@ namespace ia::iae STATIC VOID Initialize(); STATIC VOID Terminate(); - STATIC VOID Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation); + STATIC VOID Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation); }; } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp b/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp index f397488..dd58709 100644 --- a/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp +++ b/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp @@ -46,8 +46,8 @@ namespace ia::iae STATIC VOID BindTexture(IN Handle handle, IN BOOL flipV, IN BOOL flipH, IN CONST glm::vec4& colorOverlay); - STATIC VOID Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount); - STATIC VOID Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount); + STATIC VOID Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount); + STATIC VOID Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount); STATIC Camera2D* GetCamera(); diff --git a/Src/IAEngine/inc/IAEngine/Scene.hpp b/Src/IAEngine/inc/IAEngine/Scene.hpp index 31574d5..2f3f3db 100644 --- a/Src/IAEngine/inc/IAEngine/Scene.hpp +++ b/Src/IAEngine/inc/IAEngine/Scene.hpp @@ -36,7 +36,13 @@ namespace ia::iae return m_nodes; } + BOOL &YSortingEnabled() + { + return m_ySortingEnabled; + } + private: + BOOL m_ySortingEnabled{false}; Vector> m_nodes; }; } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Texture.hpp b/Src/IAEngine/inc/IAEngine/Texture.hpp index 56f7a05..89abd4c 100644 --- a/Src/IAEngine/inc/IAEngine/Texture.hpp +++ b/Src/IAEngine/inc/IAEngine/Texture.hpp @@ -28,7 +28,7 @@ namespace ia::iae ~Texture(); public: - VOID Draw(IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN BOOL flipH, + VOID Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN BOOL flipH, IN BOOL flipV, IN CONST glm::vec4 &colorOverlay) CONST; public: