TileMap Component

This commit is contained in:
Isuru Samarathunga
2025-10-11 23:54:31 +05:30
parent e0411333fb
commit 09131d7fab
11 changed files with 218 additions and 15 deletions

View File

@ -22,6 +22,7 @@ set(SRC_FILES
"Src/Imp/CPP/Nodes/CameraNode.cpp" "Src/Imp/CPP/Nodes/CameraNode.cpp"
"Src/Imp/CPP/Nodes/SpriteNode.cpp" "Src/Imp/CPP/Nodes/SpriteNode.cpp"
"Src/Imp/CPP/Nodes/TextureNode.cpp" "Src/Imp/CPP/Nodes/TextureNode.cpp"
"Src/Imp/CPP/Nodes/TileMapNode.cpp"
"Src/Imp/CPP/Components/CameraComponent.cpp" "Src/Imp/CPP/Components/CameraComponent.cpp"
"Src/Imp/CPP/Components/PhysicsComponent.cpp" "Src/Imp/CPP/Components/PhysicsComponent.cpp"

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/Components/CameraComponent.hpp> #include <IAEngine/Components/CameraComponent.hpp>
#include <IAEngine/Nodes/Node2D.hpp>
#include <Renderer/Renderer.hpp> #include <Renderer/Renderer.hpp>
@ -45,6 +46,8 @@ namespace ia::iae
VOID CameraComponent::Update() VOID CameraComponent::Update()
{ {
const auto pos = m_node->GetPosition();
m_viewMatrix = glm::lookAtLH(glm::vec3{pos, -2.0f}, {pos, 0.0f}, {0.0f, 1.0f, 0.0f});
} }
VOID CameraComponent::FixedUpdate() VOID CameraComponent::FixedUpdate()

View File

@ -0,0 +1,74 @@
// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/Components/TileMapComponent.hpp>
#include <IAEngine/Engine.hpp>
namespace ia::iae
{
STATIC Vector<Handle> TileTextures;
TileMapComponent::TileMapComponent(IN Node2D *node) : TextureComponent(node)
{
}
VOID TileMapComponent::Draw()
{
TextureComponent::Draw();
}
VOID TileMapComponent::DebugDraw()
{
TextureComponent::DebugDraw();
}
VOID TileMapComponent::Update()
{
TextureComponent::Update();
}
VOID TileMapComponent::FixedUpdate()
{
TextureComponent::FixedUpdate();
}
VOID TileMapComponent::BeginGridSetup(IN INT32 tileWidth, IN INT32 tileHeight, IN INT32 tileCountX,
IN INT32 tileCountY)
{
m_tileWidth = tileWidth;
m_tileHeight = tileHeight;
m_tileCountX = tileCountX;
m_tileCountY = tileCountY;
TileTextures.resize(m_tileCountX * m_tileCountY);
}
VOID TileMapComponent::SetupGridTile(IN INT32 index, IN Handle texture)
{
TileTextures[index] = texture;
}
VOID TileMapComponent::SetupGridTile(IN INT32 x, IN INT32 y, IN Handle texture)
{
TileTextures[x + y * m_tileCountX] = texture;
}
VOID TileMapComponent::EndGridSetup()
{
m_mapTexture = Engine::CombineImages(TileTextures, m_tileWidth, m_tileHeight, m_tileCountX, m_tileCountY);
SetTexture(m_mapTexture);
TileTextures.reset();
}
} // namespace ia::iae

View File

@ -63,8 +63,8 @@ namespace ia::iae
Vec2 InputManager::GetAxis() Vec2 InputManager::GetAxis()
{ {
return Vec2{ return Vec2{
IsKeyDown(s_axisInputs[1]) + IsKeyDown(s_axisInputs[0]) * -1, IsKeyDown(s_axisInputs[3]) + IsKeyDown(s_axisInputs[2]) * -1,
IsKeyDown(s_axisInputs[3]) + IsKeyDown(s_axisInputs[2]) * -1 IsKeyDown(s_axisInputs[1]) + IsKeyDown(s_axisInputs[0]) * -1
}; };
} }

View File

@ -0,0 +1,25 @@
// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/Engine.hpp>
#include <IAEngine/Nodes/TileMapNode.hpp>
namespace ia::iae
{
TileMapNode::TileMapNode(IN CONST String &name) : Node2D(name), m_tilemapComponent(AddComponent<TileMapComponent>())
{
}
} // namespace ia::iae

View File

@ -154,7 +154,7 @@ namespace ia::iae
s_state.ActiveCamera ? s_state.ActiveCamera->GetProjectionMatrix() : &IdentityMatrix, sizeof(Mat4)); s_state.ActiveCamera ? s_state.ActiveCamera->GetProjectionMatrix() : &IdentityMatrix, sizeof(Mat4));
SDL_PushGPUVertexUniformData( SDL_PushGPUVertexUniformData(
s_state.ActiveCommandBuffer, 1, s_state.ActiveCommandBuffer, 1,
(s_state.ActiveCamera && !s_state.CameraRelative) ? s_state.ActiveCamera->GetViewMatrix() : &IdentityMatrix, (s_state.ActiveCamera && s_state.CameraRelative) ? s_state.ActiveCamera->GetViewMatrix() : &IdentityMatrix,
sizeof(Mat4)); sizeof(Mat4));
} }

View File

@ -18,8 +18,8 @@
#include <ResourceManager.hpp> #include <ResourceManager.hpp>
#include <AudioManager.hpp> #include <AudioManager.hpp>
#include <Renderer/Renderer.hpp>
#include <Renderer/GPUResourceManager.hpp> #include <Renderer/GPUResourceManager.hpp>
#include <Renderer/Renderer.hpp>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION #define STB_IMAGE_RESIZE_IMPLEMENTATION
@ -63,8 +63,8 @@ namespace ia::iae
Handle ResourceManager::CreateImage(IN CONST String &name, IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height) Handle ResourceManager::CreateImage(IN CONST String &name, IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height)
{ {
const auto texture = GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height, const auto texture = GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height, rgbaData,
rgbaData, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM); SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
s_imageHandles.pushBack(texture); s_imageHandles.pushBack(texture);
Handle handle = s_imageHandles.size() - 1; Handle handle = s_imageHandles.size() - 1;
s_images[name] = handle; s_images[name] = handle;
@ -149,11 +149,19 @@ namespace ia::iae
Handle ResourceManager::CombineImages(IN CONST Vector<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight, Handle ResourceManager::CombineImages(IN CONST Vector<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight,
IN INT32 unitCountX, IN INT32 unitCountY) IN INT32 unitCountX, IN INT32 unitCountY)
{ {
const auto w = unitWidth * unitCountX;
const auto h = unitHeight * unitCountY;
Vector<SDL_GPUTexture *> textures; Vector<SDL_GPUTexture *> textures;
for (const auto &t : images) for (const auto &t : images)
textures.pushBack(s_imageHandles[t]); textures.pushBack(s_imageHandles[t]);
return (Handle) GPUResourceManager::CombineTextures(textures.data(), unitWidth, unitHeight, const auto texture =
unitCountX, unitCountY); GPUResourceManager::CombineTextures(textures.data(), unitWidth, unitHeight, unitCountX, unitCountY);
s_imageHandles.pushBack(texture);
Handle handle = s_imageHandles.size() - 1;
s_images[Engine::GetUniqueResourceName()] = handle;
s_imageExtents[handle] = (((UINT64) w) << 32) | h;
s_nonScaledImageExtents[handle] = s_imageExtents[handle];
return handle;
} }
VOID ResourceManager::RescaleAllImages(IN FLOAT32 factorX, IN FLOAT32 factorY) VOID ResourceManager::RescaleAllImages(IN FLOAT32 factorX, IN FLOAT32 factorY)

View File

@ -0,0 +1,48 @@
// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <IAEngine/Components/TextureComponent.hpp>
namespace ia::iae
{
class TileMapComponent : public TextureComponent
{
public:
TileMapComponent(IN Node2D *node);
public:
VIRTUAL VOID Draw();
VIRTUAL VOID DebugDraw();
VIRTUAL VOID Update();
VIRTUAL VOID FixedUpdate();
public:
VOID BeginGridSetup(IN INT32 tileWidth, IN INT32 tileHeight, IN INT32 tileCountX, IN INT32 tileCountY);
VOID SetupGridTile(IN INT32 index, IN Handle texture);
VOID SetupGridTile(IN INT32 x, IN INT32 y, IN Handle texture);
VOID EndGridSetup();
private:
INT32 m_tileWidth{};
INT32 m_tileHeight{};
INT32 m_tileCountX{};
INT32 m_tileCountY{};
Handle m_mapTexture{INVALID_HANDLE};
};
} // namespace ia::iae

View File

@ -26,6 +26,7 @@
#include <IAEngine/UI.hpp> #include <IAEngine/UI.hpp>
#include <IAEngine/Scene.hpp> #include <IAEngine/Scene.hpp>
#include <IAEngine/Utils.hpp>
namespace ia::iae namespace ia::iae
{ {

View File

@ -58,6 +58,11 @@ namespace ia::iae
return result; return result;
} }
VOID AddChild(IN RefPtr<Node2D> node)
{
m_children.pushBack(node);
}
public: public:
VOID Translate(IN Vec2 v) VOID Translate(IN Vec2 v)
{ {

View File

@ -0,0 +1,38 @@
// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <IAEngine/Components/TileMapComponent.hpp>
#include <IAEngine/Nodes/Node2D.hpp>
namespace ia::iae
{
class TileMapNode : public Node2D
{
public:
TileMapNode(IN CONST String &name);
public:
TileMapComponent *GetTileMapComponent()
{
return m_tilemapComponent;
}
protected:
TileMapComponent *CONST m_tilemapComponent{};
};
} // namespace ia::iae