Usable Engine

This commit is contained in:
Isuru Samarathunga
2025-10-07 17:11:20 +05:30
parent 0ef29f4e5f
commit 57c4309cf2
68 changed files with 1007 additions and 127 deletions

View File

@ -0,0 +1,53 @@
// 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/CameraComponent.hpp>
#include <Renderer/Renderer.hpp>
namespace ia::iae
{
CameraComponent::CameraComponent(IN Node2D *node) : IComponent(node)
{
}
VOID CameraComponent::SetViewport(IN INT32 width, IN INT32 height)
{
m_viewport.x = 0;
m_viewport.y = 0;
m_viewport.z = width;
m_viewport.w = height;
m_projectionMatrix =
glm::orthoLH(0.0f, (FLOAT32) width, (FLOAT32) height, 0.0f, Renderer::MIN_DEPTH, Renderer::MAX_DEPTH);
}
VOID CameraComponent::Draw()
{
}
VOID CameraComponent::DebugDraw()
{
}
VOID CameraComponent::Update()
{
}
VOID CameraComponent::FixedUpdate()
{
}
} // namespace ia::iae

View File

@ -0,0 +1,60 @@
// 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/TextureComponent.hpp>
#include <IAEngine/Engine.hpp>
namespace ia::iae
{
TextureComponent::TextureComponent(IN Node2D *node) : IComponent(node)
{
}
VOID TextureComponent::SetTexture(IN Handle image)
{
const auto t = Engine::GetImageExtent(image);
m_texture = image;
m_textureExtent = {t.x, t.y};
}
VOID TextureComponent::Draw()
{
m_drawnSize = m_node->GetScale() * m_textureExtent * m_scaleOffset;
Engine::SetRenderState_Texture(m_texture);
Engine::SetRenderState_FlippedH(m_isFlippedH);
Engine::SetRenderState_FlippedV(m_isFlippedV);
Engine::SetRenderState_ColorOverlay(m_colorOverlay);
Engine::SetRenderState_TextureOffset(m_textureOffset);
Engine::SetRenderState_CameraRelative(m_isCameraRelative);
Engine::SetRenderState_Transform(m_node->GetPosition() + m_positionOffset, m_drawnSize, m_node->GetRotation() + m_rotationOffset, m_node->Layer(), m_node->SortIndex());
Engine::DrawGeometry(Engine::GetGeometry_Quad());
}
VOID TextureComponent::DebugDraw()
{
}
VOID TextureComponent::Update()
{
}
VOID TextureComponent::FixedUpdate()
{
}
} // namespace ia::iae

View File

@ -17,6 +17,8 @@
#include <IAEngine/Engine.hpp>
#include <Renderer/Renderer.hpp>
#include <IACore/File.hpp>
#include <IAEngine/EngineLibraryInterface.hpp>
EXTERN GameFunctionTable g_gameFunctions;
@ -37,4 +39,22 @@ namespace ia::iae
Renderer::OnScreenResize(newWidth, newHeight);
g_gameFunctions.OnResize(newWidth, newHeight);
}
Handle Engine::CreateImageFromFile(IN CONST String &path)
{
const auto data = File::ReadToVector(path.c_str());
return CreateImage(data.data(), data.size());
}
Handle Engine::CreateSoundFromFile(IN CONST String &path)
{
const auto data = File::ReadToVector(path.c_str());
return CreateSound(data.data(), data.size());
}
Handle Engine::CreateSceneFromFile(IN CONST String &path)
{
const auto data = File::ReadToString(path.c_str());
return CreateScene(data);
}
} // namespace ia::iae

View File

@ -85,6 +85,9 @@ namespace ia::iae
VOID __Internal_Engine::Iterate()
{
WorldManager::Update();
WorldManager::FixedUpdate();
g_gameFunctions.OnUpdate(Time::GetFrameDeltaTime());
g_gameFunctions.OnFixedUpdate();
Renderer::BeginFrame();
WorldManager::Draw();

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/CameraNode.hpp>
namespace ia::iae
{
CameraNode::CameraNode(IN CONST String &name) : Node2D(name), m_cameraComponent(AddComponent<CameraComponent>())
{
}
} // namespace ia::iae

View File

@ -0,0 +1,63 @@
// 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/Node2D.hpp>
namespace ia::iae
{
Node2D::Node2D(IN CONST String &name) : INode(name)
{
}
Node2D::~Node2D()
{
for(auto& t: m_components)
delete t;
}
VOID Node2D::Draw()
{
for(auto& t: m_components)
t->Draw();
for(auto& t: m_children)
t->Draw();
}
VOID Node2D::DebugDraw()
{
for(auto& t: m_components)
t->DebugDraw();
for(auto& t: m_children)
t->DebugDraw();
}
VOID Node2D::Update()
{
for(auto& t: m_components)
t->Update();
for(auto& t: m_children)
t->Update();
}
VOID Node2D::FixedUpdate()
{
for(auto& t: m_components)
t->FixedUpdate();
for(auto& t: m_children)
t->FixedUpdate();
}
} // namespace ia::iae

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/TextureNode.hpp>
namespace ia::iae
{
TextureNode::TextureNode(IN CONST String &name) : Node2D(name), m_textureComponent(AddComponent<TextureComponent>())
{
}
} // namespace ia::iae

View File

@ -23,6 +23,9 @@
#include <backends/imgui_impl_sdl3.h>
#include <backends/imgui_impl_sdlgpu3.h>
#include <IAEngine/GameLibraryInterface.hpp>
EXTERN GameFunctionTable g_gameFunctions;
namespace ia::iae
{
EXTERN SDL_Window *g_windowHandle;
@ -67,6 +70,7 @@ namespace ia::iae
ImGui::NewFrame();
WorldManager::DebugDraw();
g_gameFunctions.OnDebugDraw();
ImGui::Render();
}

View File

@ -144,10 +144,12 @@ namespace ia::iae
&s_state.DepthStencilTargetInfo);
SDL_BindGPUGraphicsPipeline(s_state.ActiveRenderPass, s_geometryPipeline->GetHandle());
SDL_PushGPUVertexUniformData(s_state.ActiveCommandBuffer, 0, &s_state.ProjectionMatrix, sizeof(Mat4));
SDL_PushGPUVertexUniformData(
s_state.ActiveCommandBuffer, 0,
s_state.ActiveCamera ? s_state.ActiveCamera->GetProjectionMatrix() : &IdentityMatrix, sizeof(Mat4));
SDL_PushGPUVertexUniformData(
s_state.ActiveCommandBuffer, 1,
(s_state.ActiveCamera && !s_state.CameraRelative) ? s_state.ActiveCamera->GetMatrix() : &IdentityMatrix,
(s_state.ActiveCamera && !s_state.CameraRelative) ? s_state.ActiveCamera->GetViewMatrix() : &IdentityMatrix,
sizeof(Mat4));
}
@ -210,8 +212,17 @@ namespace ia::iae
SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER, s_screenWidth, s_screenHeight, nullptr,
SDL_GetGPUSwapchainTextureFormat(s_gpuDevice, g_windowHandle));
s_state.ProjectionMatrix =
glm::orthoLH(0.0f, (FLOAT32) s_screenWidth, (FLOAT32) s_screenHeight, 0.0f, -2097152.0f, 2097152.0f);
s_state.Scissor = {0, 0, newWidth, newHeight};
s_state.ActiveViewport.x = 0;
s_state.ActiveViewport.y = 0;
s_state.ActiveViewport.w = newWidth;
s_state.ActiveViewport.h = newHeight;
s_state.ActiveViewport.min_depth = 0.0f;
s_state.ActiveViewport.max_depth = 1.0f;
if (s_state.ActiveCamera)
s_state.ActiveCamera->SetViewport(newWidth, newHeight);
}
SDL_GPUTextureFormat Renderer::GetRenderTargetFormat()
@ -233,6 +244,8 @@ namespace ia::iae
VOID Renderer::DestroyGeometry(IN Geometry *handle)
{
GPUResourceManager::DestroyBuffer(handle->VertexBuffer);
GPUResourceManager::DestroyBuffer(handle->IndexBuffer);
delete handle;
}
@ -250,8 +263,7 @@ namespace ia::iae
#pragma pack(pop)
s_fragmentUniform.ColorOverlay = Vec4(s_state.ColorOverlay.R / 255.0f, s_state.ColorOverlay.G / 255.0f,
s_state.ColorOverlay.B / 255.0f, s_state.ColorOverlay.A / 255.0f);
s_fragmentUniform.ColorOverlay = s_state.ColorOverlay.GetAsFloatVec();
s_fragmentUniform.FlippedH = s_state.FlippedH;
s_fragmentUniform.FlippedV = s_state.FlippedV;
s_fragmentUniform.TextureOffset = s_state.TextureOffset;
@ -261,6 +273,7 @@ namespace ia::iae
SDL_PushGPUFragmentUniformData(s_state.ActiveCommandBuffer, 0, &s_fragmentUniform, sizeof(s_fragmentUniform));
SDL_SetGPUScissor(s_state.ActiveRenderPass, &s_state.Scissor);
SDL_SetGPUViewport(s_state.ActiveRenderPass, &s_state.ActiveViewport);
SDL_GPUBufferBinding bufferBindings[] = {{.buffer = handle->VertexBuffer, .offset = 0},
{.buffer = handle->IndexBuffer, .offset = 0}};
@ -272,9 +285,10 @@ namespace ia::iae
namespace ia::iae
{
VOID Engine::SetActiveCamera(IN ICameraComponent *cameraComponent)
VOID Engine::SetActiveCamera(IN CameraComponent *cameraComponent)
{
Renderer::s_state.ActiveCamera = cameraComponent;
Renderer::OnScreenResize(Renderer::s_screenWidth, Renderer::s_screenHeight);
}
Handle Engine::CreateGeometry(IN CONST Vector<GeometryVertex> &vertices, IN CONST Vector<INT32> &indices)
@ -354,6 +368,6 @@ namespace ia::iae
Handle Engine::GetGeometry_Quad()
{
return (Handle)Renderer::s_quadGeometry;
return (Handle) Renderer::s_quadGeometry;
}
} // namespace ia::iae

View File

@ -27,6 +27,8 @@
namespace ia::iae
{
Vector<Handle> ResourceManager::s_images;
Vector<Handle> ResourceManager::s_sounds;
Map<Handle, UINT64> ResourceManager::s_imageExtents;
VOID ResourceManager::Initialize()
@ -35,6 +37,10 @@ namespace ia::iae
VOID ResourceManager::Terminate()
{
for (const auto &t : s_images)
GPUResourceManager::DestroyTexture((SDL_GPUTexture *) t);
for (const auto &t : s_sounds)
AudioManager::DestoryAudio(t);
}
Handle ResourceManager::CreateImage(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize)
@ -51,12 +57,15 @@ namespace ia::iae
const auto handle = (Handle) GPUResourceManager::CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height,
rgbaData, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
s_imageExtents[handle] = (((UINT64) width) << 32) | height;
s_images.pushBack(handle);
return handle;
}
Handle ResourceManager::CreateSound(IN PCUINT8 encodedData, IN SIZE_T encodedDataSize)
{
return AudioManager::CreateAudio(encodedData, encodedDataSize);
const auto handle = AudioManager::CreateAudio(encodedData, encodedDataSize);
s_sounds.pushBack(handle);
return handle;
}
VOID ResourceManager::DestroyImage(IN Handle image)
@ -80,7 +89,6 @@ namespace ia::iae
const auto currentExtent = GetImageExtent(image);
const auto pixelData =
GPUResourceManager::GetTexturePixelData((SDL_GPUTexture *) image, currentExtent.x, currentExtent.y);
GPUResourceManager::DestroyTexture((SDL_GPUTexture *) image);
const auto newPixelData =
stbir_resize_uint8_linear(pixelData.data(), currentExtent.x, currentExtent.y, currentExtent.x * 4, nullptr,
newWidth, newHeight, newWidth * 4, stbir_pixel_layout::STBIR_RGBA);
@ -92,7 +100,8 @@ namespace ia::iae
Handle ResourceManager::CombineImages(IN CONST Vector<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight,
IN INT32 unitCountX, IN INT32 unitCountY)
{
return (Handle)GPUResourceManager::CombineTextures((SDL_GPUTexture**)images.data(), unitWidth, unitHeight, unitCountX, unitCountY);
return (Handle) GPUResourceManager::CombineTextures((SDL_GPUTexture **) images.data(), unitWidth, unitHeight,
unitCountX, unitCountY);
}
} // namespace ia::iae

View File

@ -0,0 +1,77 @@
// 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 <Scene.hpp>
namespace ia::iae
{
Scene *Scene::Create()
{
return new Scene();
}
Scene *Scene::Create(IN CONST String &sceneXML)
{
const auto scene = new Scene();
return scene;
}
VOID Scene::Destroy(IN Scene *scene)
{
delete scene;
}
VOID Scene::Draw()
{
for (auto &t : m_nodes)
t->Value->Draw();
}
VOID Scene::DebugDraw()
{
for (auto &t : m_nodes)
t->Value->DebugDraw();
}
VOID Scene::FixedUpdate()
{
for (auto &t : m_nodes)
t->Value->FixedUpdate();
}
VOID Scene::Update()
{
for (auto &t : m_nodes)
t->Value->Update();
}
VOID Scene::AddNode(IN RefPtr<INode> node)
{
m_nodes[node->GetName()] = node;
}
INode *Scene::GetNode(IN CONST String &name)
{
return m_nodes[name].get();
}
VOID Scene::RemoveNode(IN CONST String &name)
{
m_nodes[name] = nullptr;
}
} // namespace ia::iae

View File

@ -19,74 +19,104 @@
namespace ia::iae
{
Scene *WorldManager::m_activeScene{};
VOID WorldManager::Initialize()
{
m_activeScene = Scene::Create();
}
VOID WorldManager::Terminate()
{
if (m_activeScene)
Scene::Destroy(m_activeScene);
}
VOID WorldManager::Draw()
{
m_activeScene->Draw();
}
VOID WorldManager::DebugDraw()
{
m_activeScene->DebugDraw();
}
VOID WorldManager::Update()
{
m_activeScene->Update();
}
VOID WorldManager::FixedUpdate()
{
m_activeScene->FixedUpdate();
}
VOID WorldManager::ChangeActiveScene(IN Scene *scene)
{
m_activeScene = scene;
}
VOID WorldManager::AddNodeToActiveScene(IN RefPtr<INode> node)
{
m_activeScene->AddNode(node);
}
INode *WorldManager::GetNodeFromActiveScene(IN CONST String &name)
{
return m_activeScene->GetNode(name);
}
VOID WorldManager::RemoveNodeFromActiveScene(IN CONST String &name)
{
m_activeScene->RemoveNode(name);
}
} // namespace ia::iae
namespace ia::iae
{
Handle Engine::CreatePhysicsBody()
{
return INVALID_HANDLE;
}
VOID Engine::DestroyPhysicsBody(IN Handle body)
Handle Engine::CreateScene(IN CONST String &sceneXML)
{
return (Handle) Scene::Create(sceneXML);
}
Handle Engine::CreateEmptyScene()
{
return INVALID_HANDLE;
return (Handle) Scene::Create();
}
VOID Engine::DestroyScene(IN Handle handle)
{
Scene::Destroy((Scene *) handle);
}
VOID Engine::ChangeActiveScene(IN Handle scene)
{
WorldManager::ChangeActiveScene((Scene *) scene);
}
VOID Engine::AddNodeToActiveScene(IN RefPtr<INode> node)
{
WorldManager::AddNodeToActiveScene(node);
}
INode *Engine::GetNodeFromActiveScene(IN CONST String &name)
{
return nullptr;
return WorldManager::GetNodeFromActiveScene(name);
}
VOID Engine::RemoveNodeFromActiveScene(IN CONST String &name)
{
WorldManager::RemoveNodeFromActiveScene(name);
}
VOID Engine::AddNodeToScene(IN Handle scene, IN RefPtr<INode> node)
{
((Scene *) scene)->AddNode(node);
}
VOID Engine::RemoveNodeFromScene(IN Handle scene, IN CONST String &name)
{
}
Handle Engine::CreateScene(IN CONST String& sceneXML)
{
return INVALID_HANDLE;
((Scene *) scene)->RemoveNode(name);
}
} // namespace ia::iae