From a0b5dc2af2f61192c865e3befb763fc200d03fb0 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Fri, 12 Sep 2025 16:03:33 +0530 Subject: [PATCH] Added Renderer --- .vscode/settings.json | 5 + Dependencies/IACore | 2 +- Src/IAESandbox/imp/cpp/Game.cpp | 8 +- Src/IAEngine/CMakeLists.txt | 5 +- Src/IAEngine/imp/cpp/Audio.cpp | 16 ++ .../imp/cpp/Components/AtlasRenderer.cpp | 16 ++ .../imp/cpp/Components/ParticleEmitter.cpp | 16 ++ .../imp/cpp/Components/PhysicsBody.cpp | 16 ++ .../imp/cpp/Components/SoundEmitter.cpp | 16 ++ .../imp/cpp/Components/SpriteRenderer.cpp | 16 ++ .../imp/cpp/Components/TextureRenderer.cpp | 16 ++ Src/IAEngine/imp/cpp/Events/Event.cpp | 16 ++ Src/IAEngine/imp/cpp/IAEngine.cpp | 91 ++++------ Src/IAEngine/imp/cpp/Input.cpp | 16 ++ Src/IAEngine/imp/cpp/Nodes/Node.cpp | 16 ++ Src/IAEngine/imp/cpp/Nodes/Transform.cpp | 16 ++ Src/IAEngine/imp/cpp/Physics/Physics.cpp | 16 ++ Src/IAEngine/imp/cpp/Random.cpp | 16 ++ Src/IAEngine/imp/cpp/Rendering/Camera.cpp | 15 ++ Src/IAEngine/imp/cpp/Rendering/Renderer.cpp | 158 ++++++++++++++++++ Src/IAEngine/imp/cpp/Rendering/Texture.cpp | 47 ++++++ Src/IAEngine/imp/cpp/ResourceManager.cpp | 32 +++- Src/IAEngine/imp/cpp/Scene.cpp | 16 ++ Src/IAEngine/imp/cpp/Texture.cpp | 31 ---- Src/IAEngine/imp/cpp/Time.cpp | 16 ++ .../inc/IAEngine/Components/AtlasRenderer.hpp | 2 +- .../IAEngine/Components/SpriteRenderer.hpp | 2 +- .../IAEngine/Components/TextureRenderer.hpp | 2 +- Src/IAEngine/inc/IAEngine/IAEngine.hpp | 33 ++-- Src/IAEngine/inc/IAEngine/Nodes/Node.hpp | 2 +- .../inc/IAEngine/Rendering/Camera.hpp | 0 .../inc/IAEngine/Rendering/Renderer.hpp | 63 +++++++ .../inc/IAEngine/{ => Rendering}/Texture.hpp | 0 Src/IAEngine/inc/IAEngine/ResourceManager.hpp | 2 +- Vendor/CMakeLists.txt | 2 +- Vendor/SDL | 2 +- Vendor/SDL_Mixer | 2 +- Vendor/imgui | 2 +- 38 files changed, 609 insertions(+), 139 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 Src/IAEngine/imp/cpp/Rendering/Camera.cpp create mode 100644 Src/IAEngine/imp/cpp/Rendering/Renderer.cpp create mode 100644 Src/IAEngine/imp/cpp/Rendering/Texture.cpp delete mode 100644 Src/IAEngine/imp/cpp/Texture.cpp create mode 100644 Src/IAEngine/inc/IAEngine/Rendering/Camera.hpp create mode 100644 Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp rename Src/IAEngine/inc/IAEngine/{ => Rendering}/Texture.hpp (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1507357 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "functional": "cpp" + } +} \ No newline at end of file diff --git a/Dependencies/IACore b/Dependencies/IACore index 0fd33d9..8e2e118 160000 --- a/Dependencies/IACore +++ b/Dependencies/IACore @@ -1 +1 @@ -Subproject commit 0fd33d958e626a605d5d8629329383fda45b380c +Subproject commit 8e2e118dd51b56786a9ae0dda33a7e3ec208ce24 diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index 81f18a3..14a36c7 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -17,13 +17,7 @@ namespace ia::iae::game scene = m_engine->CreateScene(); m_engine->ChangeScene(scene); - const auto player = MakeRefPtr(m_engine); - player->SetLocalPosition({100.0f, 200.0f, 0.0f}); - scene->AddNode(player); - - const auto ground = MakeRefPtr(m_engine); - ground->SetLocalPosition({50.0f, 500.0f, 0.0f}); - scene->AddNode(ground); + iae::Renderer::AddDebugUIWindow("Debug Window", {100, 100}, {100, 200}, [](){}); } VOID Game::Terminate() diff --git a/Src/IAEngine/CMakeLists.txt b/Src/IAEngine/CMakeLists.txt index 3c4b45d..c48572d 100644 --- a/Src/IAEngine/CMakeLists.txt +++ b/Src/IAEngine/CMakeLists.txt @@ -3,13 +3,16 @@ set(IAEngine_Sources imp/cpp/IAEngine.cpp imp/cpp/Time.cpp imp/cpp/Audio.cpp - imp/cpp/Texture.cpp imp/cpp/Input.cpp imp/cpp/Scene.cpp imp/cpp/Random.cpp imp/cpp/ResourceManager.cpp + imp/cpp/Rendering/Texture.cpp + imp/cpp/Rendering/Camera.cpp + imp/cpp/Rendering/Renderer.cpp + imp/cpp/Physics/Physics.cpp imp/cpp/Events/Event.cpp diff --git a/Src/IAEngine/imp/cpp/Audio.cpp b/Src/IAEngine/imp/cpp/Audio.cpp index 542ba31..28eb3ad 100644 --- a/Src/IAEngine/imp/cpp/Audio.cpp +++ b/Src/IAEngine/imp/cpp/Audio.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp b/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp index b67331d..1b483db 100644 --- a/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp +++ b/Src/IAEngine/imp/cpp/Components/AtlasRenderer.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Components/ParticleEmitter.cpp b/Src/IAEngine/imp/cpp/Components/ParticleEmitter.cpp index d91d517..0f3a856 100644 --- a/Src/IAEngine/imp/cpp/Components/ParticleEmitter.cpp +++ b/Src/IAEngine/imp/cpp/Components/ParticleEmitter.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp b/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp index 11b2cfc..86954aa 100644 --- a/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp +++ b/Src/IAEngine/imp/cpp/Components/PhysicsBody.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Components/SoundEmitter.cpp b/Src/IAEngine/imp/cpp/Components/SoundEmitter.cpp index 96f90cd..ba9448a 100644 --- a/Src/IAEngine/imp/cpp/Components/SoundEmitter.cpp +++ b/Src/IAEngine/imp/cpp/Components/SoundEmitter.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp b/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp index f9be886..7bdba0c 100644 --- a/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp +++ b/Src/IAEngine/imp/cpp/Components/SpriteRenderer.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp b/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp index 35c5cf4..16d4521 100644 --- a/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp +++ b/Src/IAEngine/imp/cpp/Components/TextureRenderer.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Events/Event.cpp b/Src/IAEngine/imp/cpp/Events/Event.cpp index 64e9023..b5da295 100644 --- a/Src/IAEngine/imp/cpp/Events/Event.cpp +++ b/Src/IAEngine/imp/cpp/Events/Event.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/IAEngine.cpp b/Src/IAEngine/imp/cpp/IAEngine.cpp index e78528f..e63f2fb 100644 --- a/Src/IAEngine/imp/cpp/IAEngine.cpp +++ b/Src/IAEngine/imp/cpp/IAEngine.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include #include @@ -7,18 +23,15 @@ #include -#include -#include #include +#include namespace ia::iae { struct EngineContext { - SDL_Renderer *Renderer{}; SDL_Window *Window{}; SDL_Event Event{}; - ImGuiIO ImGUIIO{}; BOOL ShouldClose{false}; }; @@ -46,36 +59,20 @@ namespace ia::iae return false; } - if (!SDL_CreateWindowAndRenderer(config.GameName.c_str(), config.WindowWidth, config.WindowHeight, - SDL_WINDOW_RESIZABLE, &m_context->Window, &m_context->Renderer)) + if (!(m_context->Window = SDL_CreateWindow(config.GameName.c_str(), config.WindowWidth, config.WindowHeight, SDL_WINDOW_RESIZABLE))) { - IAE_LOG_ERROR("Couldn't create SDL3 window and renderer: ", SDL_GetError()); + IAE_LOG_ERROR("Couldn't create SDL3 window: ", SDL_GetError()); return false; } - SDL_SetWindowResizable(m_context->Window, false); - SDL_SetRenderVSync(m_context->Renderer, 1); - - const auto mainScale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); - - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - m_context->ImGUIIO = ImGui::GetIO(); - (void) m_context->ImGUIIO; - m_context->ImGUIIO.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - m_context->ImGUIIO.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; - - ImGui::StyleColorsClassic(); - - ImGuiStyle &style = ImGui::GetStyle(); - style.ScaleAllSizes(mainScale); - style.FontScaleDpi = mainScale; - - ImGui_ImplSDL3_InitForSDLRenderer(m_context->Window, m_context->Renderer); - ImGui_ImplSDLRenderer3_Init(m_context->Renderer); Time::Initialize(); + + if (!Renderer::Initialize(this)) + return false; + Random::Initialize(); + Input::Initialize(); Audio::Initialize(); Physics::Initialize(); @@ -89,12 +86,8 @@ namespace ia::iae Physics::Terminate(); Audio::Terminate(); + Renderer::Terminate(); - ImGui_ImplSDLRenderer3_Shutdown(); - ImGui_ImplSDL3_Shutdown(); - ImGui::DestroyContext(); - - SDL_DestroyRenderer(m_context->Renderer); SDL_DestroyWindow(m_context->Window); SDL_Quit(); @@ -113,47 +106,21 @@ namespace ia::iae while (m_updateTimer >= GAME_UPDATE_INTERVAL) m_updateTimer -= GAME_UPDATE_INTERVAL; } - ImGui_ImplSDLRenderer3_NewFrame(); - ImGui_ImplSDL3_NewFrame(); - ImGui::NewFrame(); - RenderDebugUI(); - ImGui::Render(); - SDL_SetRenderScale(m_context->Renderer, m_context->ImGUIIO.DisplayFramebufferScale.x, - m_context->ImGUIIO.DisplayFramebufferScale.y); - SDL_SetRenderDrawColorFloat(m_context->Renderer, 0.33f, 0.33f, 0.33f, 1.0f); - SDL_RenderClear(m_context->Renderer); + Renderer::BeginFrame(); RenderGame(); } VOID Engine::EndFrame() { - ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), m_context->Renderer); - SDL_RenderPresent(m_context->Renderer); + Renderer::EndFrame(); Time::NextFrame(); } - VOID Engine::AddDebugUIWindow(IN PCCHAR title, IN CONST iam::Vec2f &position, IN CONST iam::Vec2f &size, - IN std::function contentDrawCallback) - { - m_debugUIWindows.pushBack(DebugUIWindow{title, position, size, contentDrawCallback}); - } - BOOL Engine::ShouldClose() { return m_context->ShouldClose; } - VOID Engine::RenderDebugUI() - { - for (const auto &w : m_debugUIWindows) { - ImGui::Begin(w.Title); - ImGui::SetWindowPos({w.Position.X, w.Position.Y}); - ImGui::SetWindowSize({w.Size.X, w.Size.Y}); - w.ContentDrawCallback(); - ImGui::End(); - } - } - VOID Engine::ProcessEvents() { ImGui_ImplSDL3_ProcessEvent(&m_context->Event); @@ -184,8 +151,8 @@ namespace ia::iae return MakeRefPtr(this); } - PVOID Engine::GetRendererHandle() CONST + PVOID Engine::GetWindowHandle() CONST { - return m_context->Renderer; + return m_context->Window; } } // namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Input.cpp b/Src/IAEngine/imp/cpp/Input.cpp index e83cede..faf6411 100644 --- a/Src/IAEngine/imp/cpp/Input.cpp +++ b/Src/IAEngine/imp/cpp/Input.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Nodes/Node.cpp b/Src/IAEngine/imp/cpp/Nodes/Node.cpp index 844eca4..2749752 100644 --- a/Src/IAEngine/imp/cpp/Nodes/Node.cpp +++ b/Src/IAEngine/imp/cpp/Nodes/Node.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Nodes/Transform.cpp b/Src/IAEngine/imp/cpp/Nodes/Transform.cpp index c6ce246..1dcec9b 100644 --- a/Src/IAEngine/imp/cpp/Nodes/Transform.cpp +++ b/Src/IAEngine/imp/cpp/Nodes/Transform.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Physics/Physics.cpp b/Src/IAEngine/imp/cpp/Physics/Physics.cpp index 04813d7..e33243e 100644 --- a/Src/IAEngine/imp/cpp/Physics/Physics.cpp +++ b/Src/IAEngine/imp/cpp/Physics/Physics.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Random.cpp b/Src/IAEngine/imp/cpp/Random.cpp index 733856d..6a9fa0b 100644 --- a/Src/IAEngine/imp/cpp/Random.cpp +++ b/Src/IAEngine/imp/cpp/Random.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/imp/cpp/Rendering/Camera.cpp b/Src/IAEngine/imp/cpp/Rendering/Camera.cpp new file mode 100644 index 0000000..4bf94d3 --- /dev/null +++ b/Src/IAEngine/imp/cpp/Rendering/Camera.cpp @@ -0,0 +1,15 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . diff --git a/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp b/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp new file mode 100644 index 0000000..dd8710e --- /dev/null +++ b/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp @@ -0,0 +1,158 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + +#include +#include + +#include + +#include +#include + +namespace ia::iae +{ + INT32 Renderer::s_width{}; + INT32 Renderer::s_height{}; + Vector Renderer::s_debugUIWindows; + + SDL_Window *g_windowHandle{}; + SDL_GPUDevice *g_gpuDevice{}; + + // Render State + SDL_GPUCommandBuffer *g_cmdBuffer{}; + SDL_GPURenderPass *g_renderPass{}; + SDL_GPUTexture *g_swpChainTexture{}; + + // ImGUI State + ImGuiIO g_imGUIIO{}; + ImDrawData* g_imDrawData{}; + + BOOL Renderer::Initialize(IN Engine *engine) + { + g_windowHandle = (SDL_Window *) engine->GetWindowHandle(); + + if (!(g_gpuDevice = + SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_MSL, + engine->IsDebugMode, nullptr))) + { + IAE_LOG_ERROR("Couldn't create SDL3 GPU Device: ", SDL_GetError()); + return false; + } + + if (!SDL_ClaimWindowForGPUDevice(g_gpuDevice, g_windowHandle)) + { + IAE_LOG_ERROR("Couldn't initialize SDL3 GPU for the window: ", SDL_GetError()); + return false; + } + SDL_SetGPUSwapchainParameters(g_gpuDevice, g_windowHandle, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC); + + const auto mainScale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + g_imGUIIO = ImGui::GetIO(); + g_imGUIIO.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + g_imGUIIO.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + + ImGui::StyleColorsClassic(); + + ImGuiStyle &style = ImGui::GetStyle(); + style.ScaleAllSizes(mainScale); + style.FontScaleDpi = mainScale; + + ImGui_ImplSDLGPU3_InitInfo initInfo{ + .Device = g_gpuDevice, + .ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(g_gpuDevice, g_windowHandle), + .PresentMode = SDL_GPU_PRESENTMODE_VSYNC + }; + ImGui_ImplSDL3_InitForSDLGPU(g_windowHandle); + ImGui_ImplSDLGPU3_Init(&initInfo); + + return true; + } + + VOID Renderer::Terminate() + { + SDL_WaitForGPUIdle(g_gpuDevice); + + ImGui_ImplSDL3_Shutdown(); + ImGui_ImplSDLGPU3_Shutdown(); + ImGui::DestroyContext(); + + SDL_ReleaseWindowFromGPUDevice(g_gpuDevice, g_windowHandle); + SDL_DestroyGPUDevice(g_gpuDevice); + } + + VOID Renderer::AddDebugUIWindow(IN PCCHAR title, IN CONST iam::Vec2f &position, IN CONST iam::Vec2f &size, + IN std::function contentDrawCallback) + { + s_debugUIWindows.pushBack(DebugUIWindow{title, position, size, contentDrawCallback}); + } + + VOID Renderer::BeginFrame() + { + ImGui_ImplSDLGPU3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + ImGui::NewFrame(); + for (const auto &w : s_debugUIWindows) + { + ImGui::Begin(w.Title); + ImGui::SetWindowPos({w.Position.X, w.Position.Y}); + ImGui::SetWindowSize({w.Size.X, w.Size.Y}); + w.ContentDrawCallback(); + ImGui::End(); + } + ImGui::Render(); + + if (!(g_cmdBuffer = SDL_AcquireGPUCommandBuffer(g_gpuDevice))) + { + IAE_LOG_ERROR("Couldn't acquire SDL3 GPU command buffer: ", SDL_GetError()); + return; + } + + if (!SDL_WaitAndAcquireGPUSwapchainTexture(g_cmdBuffer, g_windowHandle, &g_swpChainTexture, (PUINT32) &s_width, + (PUINT32) &s_height)) + { + IAE_LOG_ERROR("Couldn't acquire SDL3 GPU Swapchain texture: ", SDL_GetError()); + return; + } + + if (!g_swpChainTexture) + return; + + g_imDrawData = ImGui::GetDrawData(); + + ImGui_ImplSDLGPU3_PrepareDrawData(g_imDrawData, g_cmdBuffer); + + SDL_GPUColorTargetInfo colorTargetInfo = {0}; + colorTargetInfo.texture = g_swpChainTexture; + colorTargetInfo.clear_color = SDL_FColor{0.33f, 0.33f, 0.33f, 1.0f}; + colorTargetInfo.load_op = SDL_GPU_LOADOP_CLEAR; + colorTargetInfo.store_op = SDL_GPU_STOREOP_STORE; + + g_renderPass = SDL_BeginGPURenderPass(g_cmdBuffer, &colorTargetInfo, 1, NULL); + } + + VOID Renderer::EndFrame() + { + if (!g_swpChainTexture) + return; + + ImGui_ImplSDLGPU3_RenderDrawData(g_imDrawData, g_cmdBuffer, g_renderPass); + SDL_EndGPURenderPass(g_renderPass); + SDL_SubmitGPUCommandBuffer(g_cmdBuffer); + } +} // namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Rendering/Texture.cpp b/Src/IAEngine/imp/cpp/Rendering/Texture.cpp new file mode 100644 index 0000000..c2dd54a --- /dev/null +++ b/Src/IAEngine/imp/cpp/Rendering/Texture.cpp @@ -0,0 +1,47 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + +#include +#include + +#include + +#define TEXTURE_HANDLE (SDL_Texture *) m_handle + +namespace ia::iae +{ + Texture::Texture(IN Engine *engine) : m_engine(engine) + { + } + + Texture::~Texture() + { + SDL_DestroyTexture(TEXTURE_HANDLE); + } + + VOID Texture::Draw(IN CONST iam::Vec3f &position, IN CONST iam::Vec3f &scale, IN FLOAT32 rotation, IN BOOL flipH, + IN BOOL flipV, IN CONST iam::Vec4f &colorOverlay) CONST + { + SDL_FRect rect{.x = position.X, .y = position.Y, .w = m_width * scale.X, .h = m_height * scale.Y}; + + //SDL_SetTextureColorModFloat(TEXTURE_HANDLE, colorOverlay.X, colorOverlay.Y, colorOverlay.Z); + //SDL_SetTextureAlphaModFloat(TEXTURE_HANDLE, colorOverlay.W); + //SDL_RenderTextureRotated((SDL_Renderer *) m_engine->GetRendererHandle(), TEXTURE_HANDLE, nullptr, &rect, + // rotation, nullptr, + // (SDL_FlipMode) (SDL_FLIP_NONE | (flipV ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE) | + // (flipH ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE))); + } +} // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/ResourceManager.cpp b/Src/IAEngine/imp/cpp/ResourceManager.cpp index 4323288..1635fb0 100644 --- a/Src/IAEngine/imp/cpp/ResourceManager.cpp +++ b/Src/IAEngine/imp/cpp/ResourceManager.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include @@ -35,14 +51,14 @@ namespace ia::iae result->m_width = width; result->m_height = height; - SDL_Surface *surface = - SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_RGBA32, (void *) rgbaData, width << 2); - if (!surface) - THROW_UNKNOWN("Failed to create SDL surface: ", SDL_GetError()); - result->m_handle = SDL_CreateTextureFromSurface((SDL_Renderer *) m_engine->GetRendererHandle(), surface); - if (!result->m_handle) - THROW_UNKNOWN("Failed to create SDL Texture: ", SDL_GetError()); - SDL_DestroySurface(surface); + //SDL_Surface *surface = + // SDL_CreateSurfaceFrom(width, height, SDL_PIXELFORMAT_RGBA32, (void *) rgbaData, width << 2); + //if (!surface) + // THROW_UNKNOWN("Failed to create SDL surface: ", SDL_GetError()); + //result->m_handle = SDL_CreateTextureFromSurface((SDL_Renderer *) m_engine->GetRendererHandle(), surface); + //if (!result->m_handle) + // THROW_UNKNOWN("Failed to create SDL Texture: ", SDL_GetError()); + //SDL_DestroySurface(surface); return result; } diff --git a/Src/IAEngine/imp/cpp/Scene.cpp b/Src/IAEngine/imp/cpp/Scene.cpp index 8fbfb29..6719fa1 100644 --- a/Src/IAEngine/imp/cpp/Scene.cpp +++ b/Src/IAEngine/imp/cpp/Scene.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include namespace ia::iae diff --git a/Src/IAEngine/imp/cpp/Texture.cpp b/Src/IAEngine/imp/cpp/Texture.cpp deleted file mode 100644 index 26349ef..0000000 --- a/Src/IAEngine/imp/cpp/Texture.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include - -#include - -#define TEXTURE_HANDLE (SDL_Texture *) m_handle - -namespace ia::iae -{ - Texture::Texture(IN Engine *engine) : m_engine(engine) - { - } - - Texture::~Texture() - { - SDL_DestroyTexture(TEXTURE_HANDLE); - } - - VOID Texture::Draw(IN CONST iam::Vec3f &position, IN CONST iam::Vec3f &scale, IN FLOAT32 rotation, IN BOOL flipH, - IN BOOL flipV, IN CONST iam::Vec4f &colorOverlay) CONST - { - SDL_FRect rect{.x = position.X, .y = position.Y, .w = m_width * scale.X, .h = m_height * scale.Y}; - - SDL_SetTextureColorModFloat(TEXTURE_HANDLE, colorOverlay.X, colorOverlay.Y, colorOverlay.Z); - SDL_SetTextureAlphaModFloat(TEXTURE_HANDLE, colorOverlay.W); - SDL_RenderTextureRotated((SDL_Renderer *) m_engine->GetRendererHandle(), TEXTURE_HANDLE, nullptr, &rect, - rotation, nullptr, - (SDL_FlipMode) (SDL_FLIP_NONE | (flipV ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE) | - (flipH ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE))); - } -} // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/Time.cpp b/Src/IAEngine/imp/cpp/Time.cpp index 91b8f46..a7834e9 100644 --- a/Src/IAEngine/imp/cpp/Time.cpp +++ b/Src/IAEngine/imp/cpp/Time.cpp @@ -1,3 +1,19 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + #include #include diff --git a/Src/IAEngine/inc/IAEngine/Components/AtlasRenderer.hpp b/Src/IAEngine/inc/IAEngine/Components/AtlasRenderer.hpp index 56507d6..63e3743 100644 --- a/Src/IAEngine/inc/IAEngine/Components/AtlasRenderer.hpp +++ b/Src/IAEngine/inc/IAEngine/Components/AtlasRenderer.hpp @@ -17,7 +17,7 @@ #pragma once #include -#include +#include namespace ia::iae { diff --git a/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp b/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp index 075a0da..eaf55a6 100644 --- a/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp +++ b/Src/IAEngine/inc/IAEngine/Components/SpriteRenderer.hpp @@ -17,7 +17,7 @@ #pragma once #include -#include +#include namespace ia::iae { diff --git a/Src/IAEngine/inc/IAEngine/Components/TextureRenderer.hpp b/Src/IAEngine/inc/IAEngine/Components/TextureRenderer.hpp index 320087a..5630ca4 100644 --- a/Src/IAEngine/inc/IAEngine/Components/TextureRenderer.hpp +++ b/Src/IAEngine/inc/IAEngine/Components/TextureRenderer.hpp @@ -17,7 +17,7 @@ #pragma once #include -#include +#include namespace ia::iae { diff --git a/Src/IAEngine/inc/IAEngine/IAEngine.hpp b/Src/IAEngine/inc/IAEngine/IAEngine.hpp index 225e865..a94ac07 100644 --- a/Src/IAEngine/inc/IAEngine/IAEngine.hpp +++ b/Src/IAEngine/inc/IAEngine/IAEngine.hpp @@ -17,9 +17,10 @@ #pragma once #include -#include -#include +#include +#include #include +#include namespace ia::iae { @@ -27,14 +28,6 @@ namespace ia::iae class Engine { - struct DebugUIWindow - { - PCCHAR Title{""}; - iam::Vec2f Position{}; - iam::Vec2f Size{}; - std::function ContentDrawCallback{}; - }; - public: struct InitConfig { @@ -43,6 +36,12 @@ namespace ia::iae INT32 WindowHeight{600}; }; +#if defined(__DEBUG_MODE__) + STATIC CONSTEXPR BOOL IsDebugMode = TRUE; +#else + STATIC CONSTEXPR BOOL IsDebugMode = FALSE; +#endif + public: Engine(); ~Engine(); @@ -54,10 +53,7 @@ namespace ia::iae VOID EndFrame(); BOOL ShouldClose(); - VOID AddDebugUIWindow(IN PCCHAR title, IN CONST iam::Vec2f &position, IN CONST iam::Vec2f &size, IN std::function contentDrawCallback); - - template - _class_type* RegisterResourceManager(); + template _class_type *RegisterResourceManager(); public: RefPtr CreateScene(); @@ -65,10 +61,9 @@ namespace ia::iae VOID ChangeScene(IN RefPtr scene); public: - PVOID GetRendererHandle() CONST; + PVOID GetWindowHandle() CONST; private: - VOID RenderDebugUI(); VOID ProcessEvents(); VOID UpdateGame(); VOID RenderGame(); @@ -78,13 +73,11 @@ namespace ia::iae RefPtr m_activeScene{}; CONST RefPtr m_context; RefPtr m_resourceManager; - Vector m_debugUIWindows; }; - template - _class_type* Engine::RegisterResourceManager() + template _class_type *Engine::RegisterResourceManager() { m_resourceManager = MakeRefPtr<_class_type>(this); - return (_class_type*)m_resourceManager.get(); + return (_class_type *) m_resourceManager.get(); } } // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp b/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp index 785d54c..2a17e59 100644 --- a/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp +++ b/Src/IAEngine/inc/IAEngine/Nodes/Node.hpp @@ -18,7 +18,7 @@ #include #include -#include +#include namespace ia::iae { diff --git a/Src/IAEngine/inc/IAEngine/Rendering/Camera.hpp b/Src/IAEngine/inc/IAEngine/Rendering/Camera.hpp new file mode 100644 index 0000000..e69de29 diff --git a/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp b/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp new file mode 100644 index 0000000..3b521b2 --- /dev/null +++ b/Src/IAEngine/inc/IAEngine/Rendering/Renderer.hpp @@ -0,0 +1,63 @@ +// IAEngine: 2D Game Engine by IA +// Copyright (C) 2025 IAS (ias@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 . + +#pragma once + +#include + +namespace ia::iae +{ + class Engine; + + class Renderer + { + struct DebugUIWindow + { + PCCHAR Title{""}; + iam::Vec2f Position{}; + iam::Vec2f Size{}; + std::function ContentDrawCallback{}; + }; + + public: + STATIC BOOL Initialize(IN Engine *engine); + STATIC VOID Terminate(); + + public: + STATIC VOID AddDebugUIWindow(IN PCCHAR title, IN CONST iam::Vec2f &position, IN CONST iam::Vec2f &size, + IN std::function contentDrawCallback); + + public: + STATIC VOID BeginFrame(); + STATIC VOID EndFrame(); + + public: + STATIC INT32 Width() + { + return s_width; + } + + STATIC INT32 Height() + { + return s_height; + } + + private: + STATIC INT32 s_width; + STATIC INT32 s_height; + STATIC Vector s_debugUIWindows; + }; +} // namespace ia::iae \ No newline at end of file diff --git a/Src/IAEngine/inc/IAEngine/Texture.hpp b/Src/IAEngine/inc/IAEngine/Rendering/Texture.hpp similarity index 100% rename from Src/IAEngine/inc/IAEngine/Texture.hpp rename to Src/IAEngine/inc/IAEngine/Rendering/Texture.hpp diff --git a/Src/IAEngine/inc/IAEngine/ResourceManager.hpp b/Src/IAEngine/inc/IAEngine/ResourceManager.hpp index 1cc223d..50fa392 100644 --- a/Src/IAEngine/inc/IAEngine/ResourceManager.hpp +++ b/Src/IAEngine/inc/IAEngine/ResourceManager.hpp @@ -17,7 +17,7 @@ #pragma once #include -#include +#include namespace ia::iae { diff --git a/Vendor/CMakeLists.txt b/Vendor/CMakeLists.txt index 9e69248..3539e36 100644 --- a/Vendor/CMakeLists.txt +++ b/Vendor/CMakeLists.txt @@ -23,7 +23,7 @@ add_library( "imgui/imgui_tables.cpp" "imgui/imgui_widgets.cpp" "imgui/backends/imgui_impl_sdl3.cpp" - "imgui/backends/imgui_impl_sdlrenderer3.cpp" + "imgui/backends/imgui_impl_sdlgpu3.cpp" ) target_include_directories( ImGui PRIVATE diff --git a/Vendor/SDL b/Vendor/SDL index 4efdfd9..cfa31df 160000 --- a/Vendor/SDL +++ b/Vendor/SDL @@ -1 +1 @@ -Subproject commit 4efdfd92a24ff3bbe6780666189000bf5d84ed30 +Subproject commit cfa31df2d5e4b76648896d3a50b49bd254054143 diff --git a/Vendor/SDL_Mixer b/Vendor/SDL_Mixer index 1729977..40a580f 160000 --- a/Vendor/SDL_Mixer +++ b/Vendor/SDL_Mixer @@ -1 +1 @@ -Subproject commit 172997758bb69c9217a2caec57bd9450d86dc558 +Subproject commit 40a580fe85c1092c9719b6588739a5e00517d022 diff --git a/Vendor/imgui b/Vendor/imgui index 8eb22ea..02af06e 160000 --- a/Vendor/imgui +++ b/Vendor/imgui @@ -1 +1 @@ -Subproject commit 8eb22ea6203f2653c317670277d4b9604cbae068 +Subproject commit 02af06ea5f57696b93f0dfe77a9e2525522ba76e