491 lines
22 KiB
C++
491 lines
22 KiB
C++
// 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 <Renderer/DebugDraw.hpp>
|
|
#include <Renderer/EmbeddedShader.hpp>
|
|
#include <Renderer/Renderer.hpp>
|
|
|
|
#include <FontManager.hpp>
|
|
#include <ResourceManager.hpp>
|
|
#include <WorldManager.hpp>
|
|
|
|
#include <IAEngine/imgui/backends/imgui_impl_sdl3.h>
|
|
#include <IAEngine/imgui/backends/imgui_impl_sdlgpu3.h>
|
|
|
|
#include <algorithm>
|
|
|
|
namespace ia::iae
|
|
{
|
|
STATIC Mat4 IdentityMatrix(1.0f);
|
|
|
|
EXTERN SDL_Window *g_windowHandle;
|
|
|
|
INT32 Renderer::s_screenWidth{};
|
|
INT32 Renderer::s_screenHeight{};
|
|
Renderer::State Renderer::s_state{};
|
|
SDL_GPUDevice *Renderer::s_gpuDevice{};
|
|
|
|
Pipeline *Renderer::s_geometryPipeline{};
|
|
|
|
Renderer::Geometry *Renderer::s_quadGeometry{};
|
|
Renderer::Geometry *Renderer::s_circleGeometry{};
|
|
|
|
Vector<Renderer::DrawEntry> Renderer::s_drawEntries;
|
|
|
|
SDL_GPURenderPass *Renderer::s_activeRenderPass{};
|
|
SDL_GPUCommandBuffer *Renderer::s_activeCommandBuffer{};
|
|
SDL_GPUColorTargetInfo Renderer::s_colorTargetInfo{};
|
|
class CameraComponent *Renderer::s_activeCamera{};
|
|
BOOL Renderer::s_ySortingEnabled{false};
|
|
SDL_Rect Renderer::s_defaultScissor{};
|
|
SDL_GPUViewport Renderer::s_defaultViewport{};
|
|
Vec2 Renderer::s_activeSceneDesignViewport{};
|
|
|
|
EXTERN Vec2 g_sceneScalingFactor;
|
|
EXTERN Vec2 g_sceneDesignViewport;
|
|
|
|
VOID Renderer::Initialize()
|
|
{
|
|
SDL_PropertiesID deviceCreateProps = SDL_CreateProperties();
|
|
SDL_SetStringProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, nullptr);
|
|
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, true);
|
|
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, Engine::IsDebugMode());
|
|
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN, false);
|
|
if (!(s_gpuDevice = SDL_CreateGPUDeviceWithProperties(deviceCreateProps)))
|
|
THROW_UNKNOWN("Failed to create the SDL GPU Device: ", SDL_GetError());
|
|
SDL_DestroyProperties(deviceCreateProps);
|
|
|
|
if (!SDL_ClaimWindowForGPUDevice(s_gpuDevice, g_windowHandle))
|
|
THROW_UNKNOWN("Failed to initialize SDL GPU for the window: ", SDL_GetError());
|
|
|
|
SDL_SetGPUSwapchainParameters(s_gpuDevice, g_windowHandle, SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
|
|
SDL_GPU_PRESENTMODE_VSYNC);
|
|
|
|
GPUResourceManager::Initialize();
|
|
|
|
SDL_GetWindowSizeInPixels(g_windowHandle, &s_screenWidth, &s_screenHeight);
|
|
OnScreenResize(s_screenWidth, s_screenHeight);
|
|
|
|
// Initialize Pipeline
|
|
s_geometryPipeline = new Pipeline(
|
|
Pipeline::StageDesc{
|
|
.SourceData = SHADER_SOURCE_GEOMETRY_VERT,
|
|
.SourceLength = sizeof(SHADER_SOURCE_GEOMETRY_VERT),
|
|
.SamplerCount = 0,
|
|
.UniformBufferCount = 3,
|
|
},
|
|
Pipeline::StageDesc{
|
|
.SourceData = SHADER_SOURCE_GEOMETRY_FRAG,
|
|
.SourceLength = sizeof(SHADER_SOURCE_GEOMETRY_FRAG),
|
|
.SamplerCount = 1,
|
|
.UniformBufferCount = 1,
|
|
},
|
|
true, true);
|
|
|
|
DebugDraw::Initialize();
|
|
|
|
s_colorTargetInfo.clear_color = SDL_FColor{0.0f, 0.0f, 0.0f, 1.0f};
|
|
s_colorTargetInfo.load_op = SDL_GPU_LOADOP_CLEAR;
|
|
s_colorTargetInfo.store_op = SDL_GPU_STOREOP_STORE;
|
|
|
|
s_quadGeometry = CreateGeometry(
|
|
{
|
|
{glm::vec3{0, 1, 0}, glm::vec2{0, 1}, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}},
|
|
{glm::vec3{1, 1, 0}, glm::vec2{1, 1}, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}},
|
|
{glm::vec3{1, 0, 0}, glm::vec2{1, 0}, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}},
|
|
{glm::vec3{0, 0, 0}, glm::vec2{0, 0}, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}},
|
|
},
|
|
{0, 1, 2, 2, 3, 0});
|
|
|
|
s_circleGeometry = CreateGeometry(
|
|
{
|
|
{{0.0000000f, 1 * 0.0000000f}, {0.5000000f, 0.5000000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 0 Center
|
|
{{1.0000000f, 1 * 0.0000000f}, {1.0000000f, 0.5000000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 1
|
|
{{0.9848078f, 1 * 0.1736482f}, {0.9924039f, 0.5868241f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 2
|
|
{{0.9396926f, 1 * 0.3420201f}, {0.9698463f, 0.6710101f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 3
|
|
{{0.8660254f, 1 * 0.5000000f}, {0.9330127f, 0.7500000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 4
|
|
{{0.7660444f, 1 * 0.6427876f}, {0.8830222f, 0.8213938f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 5
|
|
{{0.6427876f, 1 * 0.7660444f}, {0.8213938f, 0.8830222f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 6
|
|
{{0.5000000f, 1 * 0.8660254f}, {0.7500000f, 0.9330127f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 7
|
|
{{0.3420201f, 1 * 0.9396926f}, {0.6710101f, 0.9698463f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 8
|
|
{{0.1736482f, 1 * 0.9848078f}, {0.5868241f, 0.9924039f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 9
|
|
{{0.0000000f, 1 * 1.0000000f}, {0.5000000f, 1.0000000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 10
|
|
{{-0.1736482f, 1 * 0.9848078f}, {0.4131759f, 0.9924039f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 11
|
|
{{-0.3420201f, 1 * 0.9396926f}, {0.3289899f, 0.9698463f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 12
|
|
{{-0.5000000f, 1 * 0.8660254f}, {0.2500000f, 0.9330127f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 13
|
|
{{-0.6427876f, 1 * 0.7660444f}, {0.1786062f, 0.8830222f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 14
|
|
{{-0.7660444f, 1 * 0.6427876f}, {0.1169778f, 0.8213938f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 15
|
|
{{-0.8660254f, 1 * 0.5000000f}, {0.0669873f, 0.7500000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 16
|
|
{{-0.9396926f, 1 * 0.3420201f}, {0.0301537f, 0.6710101f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 17
|
|
{{-0.9848078f, 1 * 0.1736482f}, {0.0075961f, 0.5868241f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 18
|
|
{{-1.0000000f, 1 * 0.0000000f}, {0.0000000f, 0.5000000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 19
|
|
{{-0.9848078f, 1 * -0.1736482f}, {0.0075961f, 0.4131759f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 20
|
|
{{-0.9396926f, 1 * -0.3420201f}, {0.0301537f, 0.3289899f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 21
|
|
{{-0.8660254f, 1 * -0.5000000f}, {0.0669873f, 0.2500000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 22
|
|
{{-0.7660444f, 1 * -0.6427876f}, {0.1169778f, 0.1786062f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 23
|
|
{{-0.6427876f, 1 * -0.7660444f}, {0.1786062f, 0.1169778f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 24
|
|
{{-0.5000000f, 1 * -0.8660254f}, {0.2500000f, 0.0669873f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 25
|
|
{{-0.3420201f, 1 * -0.9396926f}, {0.3289899f, 0.0301537f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 26
|
|
{{-0.1736482f, 1 * -0.9848078f}, {0.4131759f, 0.0075961f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 27
|
|
{{-0.0000000f, 1 * -1.0000000f}, {0.5000000f, 0.0000000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 28
|
|
{{0.1736482f, 1 * -0.9848078f}, {0.5868241f, 0.0075961f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 29
|
|
{{0.3420201f, 1 * -0.9396926f}, {0.6710101f, 0.0301537f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 30
|
|
{{0.5000000f, 1 * -0.8660254f}, {0.7500000f, 0.0669873f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 31
|
|
{{0.6427876f, 1 * -0.7660444f}, {0.8213938f, 0.1169778f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 32
|
|
{{0.7660444f, 1 * -0.6427876f}, {0.8830222f, 0.1786062f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 33
|
|
{{0.8660254f, 1 * -0.5000000f}, {0.9330127f, 0.2500000f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 34
|
|
{{0.9396926f, 1 * -0.3420201f}, {0.9698463f, 0.3289899f}, {1.0f, 1.0f, 1.0f, 1.0f}}, // 35
|
|
{{0.9848078f, 1 * -0.1736482f}, {0.9924039f, 0.4131759f}, {1.0f, 1.0f, 1.0f, 1.0f}} // 36
|
|
},
|
|
{0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9, 0, 9, 10,
|
|
0, 10, 11, 0, 11, 12, 0, 12, 13, 0, 13, 14, 0, 14, 15, 0, 15, 16, 0, 16, 17, 0, 17, 18, 0, 18, 19,
|
|
0, 19, 20, 0, 20, 21, 0, 21, 22, 0, 22, 23, 0, 23, 24, 0, 24, 25, 0, 25, 26, 0, 26, 27, 0, 27, 28,
|
|
0, 28, 29, 0, 29, 30, 0, 30, 31, 0, 31, 32, 0, 32, 33, 0, 33, 34, 0, 34, 35, 0, 35, 36, 0, 36, 1});
|
|
}
|
|
|
|
VOID Renderer::Terminate()
|
|
{
|
|
SDL_WaitForGPUIdle(s_gpuDevice);
|
|
|
|
DestroyGeometry(s_quadGeometry);
|
|
DestroyGeometry(s_circleGeometry);
|
|
|
|
DebugDraw::Terminate();
|
|
|
|
delete s_geometryPipeline;
|
|
|
|
GPUResourceManager::Terminate();
|
|
|
|
SDL_ReleaseWindowFromGPUDevice(s_gpuDevice, g_windowHandle);
|
|
SDL_DestroyGPUDevice(s_gpuDevice);
|
|
}
|
|
|
|
ImDrawData *g_imDrawData{};
|
|
|
|
VOID Renderer::BeginFrame()
|
|
{
|
|
s_drawEntries.clear();
|
|
|
|
if (!(s_activeCommandBuffer = SDL_AcquireGPUCommandBuffer(s_gpuDevice)))
|
|
THROW_UNKNOWN("Failed to acquire SDL GPU command buffer: ", SDL_GetError());
|
|
|
|
SDL_GPUTexture *swapChainTexture{};
|
|
if (!SDL_WaitAndAcquireGPUSwapchainTexture(s_activeCommandBuffer, g_windowHandle, &swapChainTexture,
|
|
(PUINT32) &s_screenWidth, (PUINT32) &s_screenHeight))
|
|
THROW_UNKNOWN("Failed to acquire SDL GPU Swapchain texture: ", SDL_GetError());
|
|
|
|
if (!swapChainTexture)
|
|
return;
|
|
|
|
DebugDraw::Render();
|
|
g_imDrawData = ImGui::GetDrawData();
|
|
ImGui_ImplSDLGPU3_PrepareDrawData(g_imDrawData, s_activeCommandBuffer);
|
|
|
|
const auto clearColor = WorldManager::GetActiveScene()->BackgroundColor().GetAsFloatVec();
|
|
s_colorTargetInfo.clear_color = SDL_FColor{clearColor.x, clearColor.y, clearColor.z, 1.0f};
|
|
s_colorTargetInfo.texture = swapChainTexture;
|
|
s_activeRenderPass = SDL_BeginGPURenderPass(s_activeCommandBuffer, &s_colorTargetInfo, 1, nullptr);
|
|
|
|
SDL_BindGPUGraphicsPipeline(s_activeRenderPass, s_geometryPipeline->GetHandle());
|
|
SDL_PushGPUVertexUniformData(s_activeCommandBuffer, 0,
|
|
s_activeCamera ? s_activeCamera->GetProjectionMatrix() : &IdentityMatrix,
|
|
sizeof(Mat4));
|
|
|
|
s_state.Scissor = s_defaultScissor;
|
|
}
|
|
|
|
VOID Renderer::EndFrame()
|
|
{
|
|
std::sort(s_drawEntries.begin(), s_drawEntries.end(), [](IN CONST DrawEntry &a, IN CONST DrawEntry &b) {
|
|
if (a.Layer != b.Layer)
|
|
return a.Layer < b.Layer;
|
|
return a.SortIndex < b.SortIndex;
|
|
});
|
|
|
|
for (const auto &t : s_drawEntries)
|
|
Draw(t);
|
|
|
|
ImGui_ImplSDLGPU3_RenderDrawData(g_imDrawData, s_activeCommandBuffer, s_activeRenderPass);
|
|
|
|
SDL_EndGPURenderPass(s_activeRenderPass);
|
|
|
|
SDL_SubmitGPUCommandBuffer(s_activeCommandBuffer);
|
|
}
|
|
|
|
VOID Renderer::Draw(IN CONST DrawEntry &t)
|
|
{
|
|
#pragma pack(push, 1)
|
|
|
|
STATIC struct
|
|
{
|
|
INT32 FlippedH{false};
|
|
INT32 FlippedV{false};
|
|
Vec2 TextureOffset{0.0f, 0.0f};
|
|
Vec4 ColorOverlay{1.0f, 1.0f, 1.0f, 1.0f};
|
|
} s_fragmentUniform{};
|
|
|
|
#pragma pack(pop)
|
|
|
|
Vec2 position = t.DrawState.Position * g_sceneScalingFactor;
|
|
Vec2 scale = t.DrawState.Scale * g_sceneScalingFactor;
|
|
|
|
Mat4 modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3{position.x, position.y, 0});
|
|
modelMatrix = glm::rotate(modelMatrix, t.DrawState.Rotation, glm::vec3(0.0f, 0.0f, 1.0f));
|
|
modelMatrix = glm::scale(modelMatrix, glm::vec3{scale.x, scale.y, 1.0f});
|
|
|
|
SDL_PushGPUVertexUniformData(s_activeCommandBuffer, 1,
|
|
(s_activeCamera && t.DrawState.CameraRelative) ? s_activeCamera->GetViewMatrix()
|
|
: &IdentityMatrix,
|
|
sizeof(Mat4));
|
|
|
|
SDL_PushGPUVertexUniformData(Renderer::s_activeCommandBuffer, 2, &modelMatrix, sizeof(Mat4));
|
|
|
|
s_fragmentUniform.ColorOverlay = t.DrawState.ColorOverlay.GetAsFloatVec();
|
|
s_fragmentUniform.FlippedH = t.DrawState.FlippedH;
|
|
s_fragmentUniform.FlippedV = t.DrawState.FlippedV;
|
|
s_fragmentUniform.TextureOffset = t.DrawState.TextureOffset;
|
|
SDL_GPUTextureSamplerBinding textureBinding{.texture = t.DrawState.ActiveTexture,
|
|
.sampler = ((t.DrawState.TextureOffset.x <= FLOAT32_EPSILON) &&
|
|
(t.DrawState.TextureOffset.y <= FLOAT32_EPSILON))
|
|
? GPUResourceManager::GetSampler_LinearClamp()
|
|
: GPUResourceManager::GetSampler_LinearRepeat()};
|
|
SDL_BindGPUFragmentSamplers(s_activeRenderPass, 0, &textureBinding, 1);
|
|
SDL_PushGPUFragmentUniformData(s_activeCommandBuffer, 0, &s_fragmentUniform, sizeof(s_fragmentUniform));
|
|
|
|
SDL_SetGPUScissor(s_activeRenderPass, &t.DrawState.Scissor);
|
|
|
|
SDL_GPUBufferBinding bufferBindings[] = {{.buffer = t.GeometryHandle->VertexBuffer, .offset = 0},
|
|
{.buffer = t.GeometryHandle->IndexBuffer, .offset = 0}};
|
|
SDL_BindGPUVertexBuffers(s_activeRenderPass, 0, bufferBindings, 1);
|
|
SDL_BindGPUIndexBuffer(s_activeRenderPass, &bufferBindings[1], SDL_GPU_INDEXELEMENTSIZE_32BIT);
|
|
SDL_DrawGPUIndexedPrimitives(s_activeRenderPass, t.GeometryHandle->IndexCount, 1, 0, 0, 0);
|
|
}
|
|
|
|
VOID Renderer::OnScreenResize(IN INT32 newWidth, IN INT32 newHeight)
|
|
{
|
|
s_screenWidth = newWidth;
|
|
s_screenHeight = newHeight;
|
|
|
|
s_defaultScissor = {0, 0, newWidth, newHeight};
|
|
|
|
s_defaultViewport.x = 0;
|
|
s_defaultViewport.y = 0;
|
|
s_defaultViewport.w = newWidth;
|
|
s_defaultViewport.h = newHeight;
|
|
s_defaultViewport.min_depth = 0.0f;
|
|
s_defaultViewport.max_depth = 1.0f;
|
|
|
|
if (s_activeCamera)
|
|
s_activeCamera->SetViewport(newWidth, newHeight);
|
|
|
|
UpdateSceneScalingFactor();
|
|
}
|
|
|
|
VOID Renderer::UpdateSceneScalingFactor()
|
|
{
|
|
g_sceneScalingFactor = {(FLOAT32) s_screenWidth / s_activeSceneDesignViewport.x,
|
|
(FLOAT32) s_screenHeight / s_activeSceneDesignViewport.y};
|
|
IAE_LOG_INFO("Updated Scene Scale Factor: (", g_sceneScalingFactor.x, ", ", g_sceneScalingFactor.y, ")");
|
|
}
|
|
|
|
SDL_GPUTextureFormat Renderer::GetRenderTargetFormat()
|
|
{
|
|
return SDL_GetGPUSwapchainTextureFormat(s_gpuDevice, g_windowHandle);
|
|
}
|
|
|
|
Renderer::Geometry *Renderer::CreateGeometry(IN CONST Vector<GeometryVertex> &vertices,
|
|
IN CONST Vector<INT32> &indices)
|
|
{
|
|
const auto mesh = new Geometry();
|
|
mesh->VertexBuffer = GPUResourceManager::CreateDeviceLocalBuffer(
|
|
SDL_GPU_BUFFERUSAGE_VERTEX, vertices.data(), static_cast<UINT32>(vertices.size() * sizeof(vertices[0])));
|
|
mesh->IndexBuffer = GPUResourceManager::CreateDeviceLocalBuffer(
|
|
SDL_GPU_BUFFERUSAGE_INDEX, indices.data(), static_cast<UINT32>(indices.size() * sizeof(indices[0])));
|
|
mesh->IndexCount = static_cast<UINT32>(indices.size());
|
|
return mesh;
|
|
}
|
|
|
|
VOID Renderer::DestroyGeometry(IN Geometry *handle)
|
|
{
|
|
GPUResourceManager::DestroyBuffer(handle->VertexBuffer);
|
|
GPUResourceManager::DestroyBuffer(handle->IndexBuffer);
|
|
delete handle;
|
|
}
|
|
|
|
VOID Renderer::DrawGeometry(IN Geometry *handle, IN SDL_GPUTexture *texture, IN Vec2 position, IN Vec2 scale,
|
|
IN FLOAT32 rotation, IN UINT8 layer, IN UINT16 sortIndex)
|
|
{
|
|
s_state.ActiveTexture = texture;
|
|
s_state.Position = position;
|
|
s_state.Rotation = rotation;
|
|
s_state.Scale = scale;
|
|
s_drawEntries.pushBack(
|
|
DrawEntry{.Layer = layer,
|
|
.SortIndex = (UINT16) (sortIndex + ((UINT16) s_state.Position.y * s_ySortingEnabled)),
|
|
.DrawState = s_state,
|
|
.GeometryHandle = handle});
|
|
}
|
|
|
|
VOID Renderer::DrawText(IN CONST String &text, IN Vec2 position, IN FLOAT32 scale, IN FLOAT32 rotation,
|
|
IN UINT8 layer, IN UINT16 sortIndex)
|
|
{
|
|
const auto t = (UINT16) (sortIndex + ((UINT16) s_state.Position.y * s_ySortingEnabled));
|
|
const auto &font = FontManager::GetFont("Roboto");
|
|
for (const auto &c : text)
|
|
{
|
|
if (!c)
|
|
break;
|
|
|
|
const auto glyph = font.Chars[(UINT8) c];
|
|
|
|
s_state.ActiveTexture = glyph.Texture;
|
|
s_state.Position = position + Vec2(glyph.Bearing.x, glyph.Size.y - glyph.Bearing.y) * scale;
|
|
s_state.Position.y += (16 - glyph.Size.y) * scale;
|
|
s_state.Rotation = rotation;
|
|
s_state.Scale = Vec2(scale * glyph.Size.x, scale * glyph.Size.y);
|
|
|
|
s_drawEntries.pushBack(
|
|
DrawEntry{.Layer = layer, .SortIndex = t, .DrawState = s_state, .GeometryHandle = s_quadGeometry});
|
|
|
|
position.x += glyph.Advance * scale;
|
|
}
|
|
}
|
|
|
|
VOID Renderer::WaitForGPUIdle()
|
|
{
|
|
SDL_WaitForGPUIdle(s_gpuDevice);
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
IVec2 Engine::GetDisplayExtent()
|
|
{
|
|
return IVec2(Renderer::s_screenWidth, Renderer::s_screenHeight);
|
|
}
|
|
|
|
FLOAT32 Engine::GetDisplayAspectRatio()
|
|
{
|
|
return (FLOAT32) Renderer::s_screenHeight / (FLOAT32) Renderer::s_screenWidth;
|
|
}
|
|
|
|
VOID Engine::SetActiveCamera(IN CameraComponent *cameraComponent)
|
|
{
|
|
Renderer::s_activeCamera = cameraComponent;
|
|
Renderer::OnScreenResize(Renderer::s_screenWidth, Renderer::s_screenHeight);
|
|
}
|
|
|
|
CameraComponent *Engine::GetActiveCamera()
|
|
{
|
|
return Renderer::s_activeCamera;
|
|
}
|
|
|
|
Handle Engine::CreateGeometry(IN CONST Vector<GeometryVertex> &vertices, IN CONST Vector<INT32> &indices)
|
|
{
|
|
return (Handle) Renderer::CreateGeometry(vertices, indices);
|
|
}
|
|
|
|
VOID Engine::DestroyGeometry(IN Handle geometry)
|
|
{
|
|
Renderer::DestroyGeometry((Renderer::Geometry *) geometry);
|
|
}
|
|
|
|
VOID Engine::DrawGeometry(IN Handle handle, IN Handle texture, IN Vec2 position, IN Vec2 scale, IN FLOAT32 rotation,
|
|
IN UINT8 layer, IN UINT16 sortIndex)
|
|
{
|
|
Renderer::DrawGeometry((Renderer::Geometry *) handle, ResourceManager::GetTextureFromImage(texture), position,
|
|
scale, rotation, layer, sortIndex);
|
|
}
|
|
|
|
VOID Engine::DrawText(IN CONST String &text, IN Vec2 position, IN FLOAT32 scale, IN FLOAT32 rotation,
|
|
IN UINT8 layer, IN UINT16 sortIndex)
|
|
{
|
|
Renderer::DrawText(text, position, scale, rotation, layer, sortIndex);
|
|
}
|
|
|
|
VOID Engine::DrawQuad(IN Vec2 position, IN Handle texture, IN Vec2 scale, IN FLOAT32 rotation, IN UINT8 layer,
|
|
IN UINT16 sortIndex)
|
|
{
|
|
Renderer::DrawGeometry(Renderer::s_quadGeometry, ResourceManager::GetTextureFromImage(texture), position, scale,
|
|
rotation, layer, sortIndex);
|
|
}
|
|
|
|
VOID Engine::DrawCircle(IN Vec2 position, IN Handle texture, IN FLOAT32 radius, IN FLOAT32 rotation, IN UINT8 layer,
|
|
IN UINT16 sortIndex)
|
|
{
|
|
Renderer::DrawGeometry(Renderer::s_circleGeometry, ResourceManager::GetTextureFromImage(texture), position,
|
|
{radius, radius}, rotation, layer, sortIndex);
|
|
}
|
|
|
|
VOID Engine::SetRenderState_Scissor(IN IVec4 rect)
|
|
{
|
|
Renderer::s_state.Scissor = rect.z ? SDL_Rect{0, 0, Renderer::s_screenWidth, Renderer::s_screenHeight}
|
|
: SDL_Rect{rect.x, rect.y, rect.z, rect.w};
|
|
}
|
|
|
|
VOID Engine::SetRenderState_FlippedH(IN BOOL value)
|
|
{
|
|
Renderer::s_state.FlippedH = value;
|
|
}
|
|
|
|
VOID Engine::SetRenderState_FlippedV(IN BOOL value)
|
|
{
|
|
Renderer::s_state.FlippedV = value;
|
|
}
|
|
|
|
VOID Engine::SetRenderState_TextureOffset(IN Vec2 off)
|
|
{
|
|
Renderer::s_state.TextureOffset = off;
|
|
}
|
|
|
|
VOID Engine::SetRenderState_ColorOverlay(IN Color color)
|
|
{
|
|
Renderer::s_state.ColorOverlay = color;
|
|
}
|
|
|
|
VOID Engine::SetRenderState_CameraRelative(IN BOOL value)
|
|
{
|
|
Renderer::s_state.CameraRelative = value;
|
|
}
|
|
|
|
VOID Engine::SetRenderState_YSortingEnabled(IN BOOL value)
|
|
{
|
|
Renderer::s_ySortingEnabled = value;
|
|
}
|
|
|
|
Handle Engine::GetGeometry_Quad()
|
|
{
|
|
return (Handle) Renderer::s_quadGeometry;
|
|
}
|
|
|
|
Handle Engine::GetGeometry_Circle()
|
|
{
|
|
return (Handle) Renderer::s_circleGeometry;
|
|
}
|
|
|
|
VOID Engine::SetSceneDesignViewport(IN Vec2 value)
|
|
{
|
|
if (!value.x || !value.y)
|
|
{
|
|
Renderer::s_activeSceneDesignViewport = g_sceneDesignViewport;
|
|
Renderer::UpdateSceneScalingFactor();
|
|
return;
|
|
}
|
|
Renderer::s_activeSceneDesignViewport = value;
|
|
Renderer::UpdateSceneScalingFactor();
|
|
}
|
|
} // namespace ia::iae
|