diff --git a/Engine/src/Engine/Math/TempExtensions.cpp b/Engine/src/Engine/Math/Random.cpp similarity index 75% rename from Engine/src/Engine/Math/TempExtensions.cpp rename to Engine/src/Engine/Math/Random.cpp index 22f9f86..aa68abd 100644 --- a/Engine/src/Engine/Math/TempExtensions.cpp +++ b/Engine/src/Engine/Math/Random.cpp @@ -1,9 +1,9 @@ #include "ltpch.h" -#include "TempExtensions.h" +#include "Random.h" #include -namespace Light { +namespace Light { namespace Math { float Math::Rand(int min, int max, int decimals /* = 0 */) { @@ -32,10 +32,10 @@ namespace Light { min *= dec; max *= dec; - float r1 = (min + (rand() % (max)-min)) / (float)dec; - float r2 = (min + (rand() % (max)-min)) / (float)dec; - float r3 = (min + (rand() % (max)-min)) / (float)dec; + float r1 = (min + (rand() % (max - min))) / (float)dec; + float r2 = (min + (rand() % (max - min))) / (float)dec; + float r3 = (min + (rand() % (max - min))) / (float)dec; return glm::vec3(r1, r2, r3); } -} \ No newline at end of file +}} \ No newline at end of file diff --git a/Engine/src/Engine/Math/Random.h b/Engine/src/Engine/Math/Random.h new file mode 100644 index 0000000..52264b4 --- /dev/null +++ b/Engine/src/Engine/Math/Random.h @@ -0,0 +1,16 @@ +#pragma once + +// #todo: make proper math stuff + +// this is a temporary header file to extend the glm math +// light engine will either have it's own math library or extend upon the glm + +#include + +namespace Light { namespace Math { + + float Rand(int min, int max, int decimals = 0); + glm::vec2 RandVec2(int min, int max, int decimals = 0); + glm::vec3 RandVec3(int min, int max, int decimals = 0); + +}} \ No newline at end of file diff --git a/Engine/src/Engine/Math/TempExtensions.h b/Engine/src/Engine/Math/TempExtensions.h deleted file mode 100644 index 9b8c717..0000000 --- a/Engine/src/Engine/Math/TempExtensions.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -// #todo: make proper math stuff - -// this is a temporary header file to extend the glm math -// light engine will either have it's own math library or extend upon the glm - -#include - -namespace Light { - - class Math - { - public: - static float Rand(int min, int max, int decimals = 0); - static glm::vec2 RandVec2(int min, int max, int decimals = 0); - static glm::vec3 RandVec3(int min, int max, int decimals = 0); - }; - -} diff --git a/Engine/src/Engine/Utility/ResourceManager.cpp b/Engine/src/Engine/Utility/ResourceManager.cpp index 1fb3d9b..c9c1327 100644 --- a/Engine/src/Engine/Utility/ResourceManager.cpp +++ b/Engine/src/Engine/Utility/ResourceManager.cpp @@ -93,6 +93,17 @@ namespace Light { m_Textures[name] = Ref(Texture::Create(width, height, components, pixels, m_SharedGraphicsContext)); } + void ResourceManager::ReleaseTextureImpl(const std::string& name) + { + if (!m_Textures[name]) + { + LT_ENGINE_WARN("ResourceManager::ReleaseTextureImpl: failed to find texture named: {}", name); + return; + } + + m_Textures[name] = nullptr; + } + void ResourceManager::ExtractShaderSource(std::string& src, const std::string& delim) { size_t begDelimPos, endDelimPos; diff --git a/Engine/src/Engine/Utility/ResourceManager.h b/Engine/src/Engine/Utility/ResourceManager.h index 0409ce9..90d791c 100644 --- a/Engine/src/Engine/Utility/ResourceManager.h +++ b/Engine/src/Engine/Utility/ResourceManager.h @@ -29,6 +29,8 @@ namespace Light { static inline void LoadTexture(const std::string& name, const std::string& path, unsigned int desiredComponents = 4u) { s_Context->LoadTextureImpl(name, path, desiredComponents); } + static inline void ReleaseTexture(const std::string& name) { s_Context->ReleaseTextureImpl(name); } + static inline Ref GetShader(const std::string& name) { return s_Context->m_Shaders[name]; } static inline Ref GetTexture(const std::string& name) { return s_Context->m_Textures[name]; } @@ -40,6 +42,8 @@ namespace Light { void LoadTextureImpl(const std::string& name, const std::string& path, unsigned int desiredComponents = 4u); + void ReleaseTextureImpl(const std::string& name); + private: void ExtractShaderSource(std::string& src, const std::string& delim); }; diff --git a/Engine/src/LightEngine.h b/Engine/src/LightEngine.h index a1ad896..98478ef 100644 --- a/Engine/src/LightEngine.h +++ b/Engine/src/LightEngine.h @@ -46,7 +46,7 @@ #include // math -#include "Math/TempExtensions.h" +#include "Math/Random.h" // scene #include "Scene/Scene.h" diff --git a/Mirror/src/EditorLayer.cpp b/Mirror/src/EditorLayer.cpp index 4c8d6a1..9d2e1f5 100644 --- a/Mirror/src/EditorLayer.cpp +++ b/Mirror/src/EditorLayer.cpp @@ -3,73 +3,68 @@ namespace Light { EditorLayer::EditorLayer(const std::string& name) - : Layer(name), m_Direction(glm::vec2(0.0f, 0.0f)) + : Layer(name) { m_Scene = CreateRef(); + m_PropertiesPanel = CreateRef(); m_SceneHierarchyPanel = CreateRef(m_Scene, m_PropertiesPanel); SummonAwesomeness(); - m_CameraEntity = m_Scene->CreateEntity("Camera", TransformComponent(glm::vec3(0.0f, 0.0f, 1000.0f))); + m_Framebuffer = Framebuffer::Create({ 1, 1, 1 }, GraphicsContext::GetSharedContext()); + + m_CameraEntity = m_Scene->CreateEntity("Game Camera", TransformComponent({0.0f, 0.0f, 1000.0f})); m_CameraEntity.AddComponent(SceneCamera(), true); - m_Framebuffer = std::shared_ptr(Framebuffer::Create({ 1u, 1u, 1u }, GraphicsContext::GetSharedContext())); - - - // for native scripts + // for native script components m_Scene->OnCreate(); - } + // #temp + srand(time(NULL)); + } + void EditorLayer::OnUpdate(float deltaTime) { - if (Input::GetKeyboardKey(Key::A)) - m_Direction.x = -1.0f; - else if (Input::GetKeyboardKey(Key::D)) - m_Direction.x = 1.0f; - else - m_Direction.x = 0.0f; - - if (Input::GetKeyboardKey(Key::W)) - m_Direction.y = 1.0f; - else if (Input::GetKeyboardKey(Key::S)) - m_Direction.y = -1.0f; - else - m_Direction.y = 0.0f; - - auto& transform = m_CameraEntity.GetComponent(); - transform.translation += glm::vec3(m_Direction * m_Speed * deltaTime, 0.0); - m_Scene->OnUpdate(deltaTime); - } + m_Direction.x = Input::GetKeyboardKey(Key::A) ? -1.0f : + Input::GetKeyboardKey(Key::D) ? 1.0f : 0.0f; + + + m_Direction.y = Input::GetKeyboardKey(Key::S) ? -1.0f : + Input::GetKeyboardKey(Key::W) ? 1.0f : 0.0f; + + auto& cameraTranslation = m_CameraEntity.GetComponent().translation; + cameraTranslation += glm::vec3(m_Direction * m_Speed * deltaTime, 0.0f); + } + void EditorLayer::OnRender() { m_Scene->OnRender(m_Framebuffer); } - + void EditorLayer::OnUserInterfaceUpdate() { UserInterface::DockspaceBegin(); ImGui::ShowDemoWindow(); - if (ImGui::Begin("GameView")) + if (ImGui::Begin("Game")) { Input::ReceiveGameEvents(ImGui::IsWindowFocused()); - ImVec2 regionAvail = ImGui::GetContentRegionAvail(); - if (m_AvailableContentRegionPrev != regionAvail) + if(m_AvailableContentRegionPrev != regionAvail) { - m_AvailableContentRegionPrev = regionAvail; m_Framebuffer->Resize({ regionAvail.x, regionAvail.y }); + auto& camera = m_CameraEntity.GetComponent().camera; + camera.SetViewportSize(regionAvail.x, regionAvail.y);; - auto& cameraComp = m_CameraEntity.GetComponent(); - cameraComp.camera.SetViewportSize(regionAvail.x, regionAvail.y); + m_AvailableContentRegionPrev = regionAvail; } - if (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX) + if(GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX) ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail); - else // opengl + else ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail, ImVec2(0, 1), ImVec2(1, 0)); } ImGui::End(); @@ -78,21 +73,25 @@ namespace Light { UserInterface::DockspaceEnd(); } - + void EditorLayer::SummonAwesomeness() { ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png"); auto texture = ResourceManager::GetTexture("awesomeface"); - for (int i = 0; i < 420; i++) + for(unsigned int i = 0; i < 250; i++) { const glm::vec3 translation = Math::RandVec3(-500, 500); - const glm::vec3 scale = glm::vec3(Math::Rand(250, 350)); - const glm::vec3 rotation = glm::radians(Math::RandVec3(0, 360, 2));; + const glm::vec3 scale = glm::vec3(Math::Rand(125, 200)); + const glm::vec3 rotation = glm::radians(glm::vec3(0.0f, 0.0f, Math::Rand(0, 360))); - Entity quad = m_Scene->CreateEntity("quad" + std::to_string(i), { translation, scale, rotation }); - quad.AddComponent(texture); + const glm::vec4 tint = glm::vec4(Math::RandVec3(0, 1, 2), 1.0f); + + auto& entity = m_Scene->CreateEntity("quad" + std::to_string(i), { translation, scale, rotation }); + entity.AddComponent(texture, tint); } + + ResourceManager::ReleaseTexture("awesomeface"); } } \ No newline at end of file