- Added Math namespace
- Added Rand functions
- Maintenance
This commit is contained in:
Light 2021-08-16 16:21:03 +04:30
parent b3997880fe
commit 32df2e01c6
7 changed files with 77 additions and 67 deletions

View file

@ -1,9 +1,9 @@
#include "ltpch.h" #include "ltpch.h"
#include "TempExtensions.h" #include "Random.h"
#include <cmath> #include <cmath>
namespace Light { namespace Light { namespace Math {
float Math::Rand(int min, int max, int decimals /* = 0 */) float Math::Rand(int min, int max, int decimals /* = 0 */)
{ {
@ -32,10 +32,10 @@ namespace Light {
min *= dec; min *= dec;
max *= dec; max *= dec;
float r1 = (min + (rand() % (max)-min)) / (float)dec; float r1 = (min + (rand() % (max - min))) / (float)dec;
float r2 = (min + (rand() % (max)-min)) / (float)dec; float r2 = (min + (rand() % (max - min))) / (float)dec;
float r3 = (min + (rand() % (max)-min)) / (float)dec; float r3 = (min + (rand() % (max - min))) / (float)dec;
return glm::vec3(r1, r2, r3); return glm::vec3(r1, r2, r3);
} }
} }}

View file

@ -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 <glm/glm.hpp>
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);
}}

View file

@ -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 <glm/glm.hpp>
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);
};
}

View file

@ -93,6 +93,17 @@ namespace Light {
m_Textures[name] = Ref<Texture>(Texture::Create(width, height, components, pixels, m_SharedGraphicsContext)); m_Textures[name] = Ref<Texture>(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) void ResourceManager::ExtractShaderSource(std::string& src, const std::string& delim)
{ {
size_t begDelimPos, endDelimPos; size_t begDelimPos, endDelimPos;

View file

@ -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 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<Shader> GetShader(const std::string& name) { return s_Context->m_Shaders[name]; } static inline Ref<Shader> GetShader(const std::string& name) { return s_Context->m_Shaders[name]; }
static inline Ref<Texture> GetTexture(const std::string& name) { return s_Context->m_Textures[name]; } static inline Ref<Texture> 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 LoadTextureImpl(const std::string& name, const std::string& path, unsigned int desiredComponents = 4u);
void ReleaseTextureImpl(const std::string& name);
private: private:
void ExtractShaderSource(std::string& src, const std::string& delim); void ExtractShaderSource(std::string& src, const std::string& delim);
}; };

View file

@ -46,7 +46,7 @@
#include <imgui.h> #include <imgui.h>
// math // math
#include "Math/TempExtensions.h" #include "Math/Random.h"
// scene // scene
#include "Scene/Scene.h" #include "Scene/Scene.h"

View file

@ -3,43 +3,39 @@
namespace Light { namespace Light {
EditorLayer::EditorLayer(const std::string& name) EditorLayer::EditorLayer(const std::string& name)
: Layer(name), m_Direction(glm::vec2(0.0f, 0.0f)) : Layer(name)
{ {
m_Scene = CreateRef<Scene>(); m_Scene = CreateRef<Scene>();
m_PropertiesPanel = CreateRef<PropertiesPanel>(); m_PropertiesPanel = CreateRef<PropertiesPanel>();
m_SceneHierarchyPanel = CreateRef<SceneHierarchyPanel>(m_Scene, m_PropertiesPanel); m_SceneHierarchyPanel = CreateRef<SceneHierarchyPanel>(m_Scene, m_PropertiesPanel);
SummonAwesomeness(); 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<CameraComponent>(SceneCamera(), true); m_CameraEntity.AddComponent<CameraComponent>(SceneCamera(), true);
m_Framebuffer = std::shared_ptr<Framebuffer>(Framebuffer::Create({ 1u, 1u, 1u }, GraphicsContext::GetSharedContext())); // for native script components
// for native scripts
m_Scene->OnCreate(); m_Scene->OnCreate();
// #temp
srand(time(NULL));
} }
void EditorLayer::OnUpdate(float deltaTime) 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<TransformComponent>();
transform.translation += glm::vec3(m_Direction * m_Speed * deltaTime, 0.0);
m_Scene->OnUpdate(deltaTime); 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<TransformComponent>().translation;
cameraTranslation += glm::vec3(m_Direction * m_Speed * deltaTime, 0.0f);
} }
void EditorLayer::OnRender() void EditorLayer::OnRender()
@ -52,24 +48,23 @@ namespace Light {
UserInterface::DockspaceBegin(); UserInterface::DockspaceBegin();
ImGui::ShowDemoWindow(); ImGui::ShowDemoWindow();
if (ImGui::Begin("GameView")) if (ImGui::Begin("Game"))
{ {
Input::ReceiveGameEvents(ImGui::IsWindowFocused()); Input::ReceiveGameEvents(ImGui::IsWindowFocused());
ImVec2 regionAvail = ImGui::GetContentRegionAvail(); ImVec2 regionAvail = ImGui::GetContentRegionAvail();
if (m_AvailableContentRegionPrev != regionAvail) if(m_AvailableContentRegionPrev != regionAvail)
{ {
m_AvailableContentRegionPrev = regionAvail;
m_Framebuffer->Resize({ regionAvail.x, regionAvail.y }); m_Framebuffer->Resize({ regionAvail.x, regionAvail.y });
auto& camera = m_CameraEntity.GetComponent<CameraComponent>().camera;
camera.SetViewportSize(regionAvail.x, regionAvail.y);;
auto& cameraComp = m_CameraEntity.GetComponent<CameraComponent>(); m_AvailableContentRegionPrev = regionAvail;
cameraComp.camera.SetViewportSize(regionAvail.x, regionAvail.y);
} }
if (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX) if(GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX)
ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail); ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail);
else // opengl else
ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail, ImVec2(0, 1), ImVec2(1, 0)); ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail, ImVec2(0, 1), ImVec2(1, 0));
} ImGui::End(); } ImGui::End();
@ -84,15 +79,19 @@ namespace Light {
ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png"); ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png");
auto texture = ResourceManager::GetTexture("awesomeface"); 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 translation = Math::RandVec3(-500, 500);
const glm::vec3 scale = glm::vec3(Math::Rand(250, 350)); const glm::vec3 scale = glm::vec3(Math::Rand(125, 200));
const glm::vec3 rotation = glm::radians(Math::RandVec3(0, 360, 2));; 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 }); const glm::vec4 tint = glm::vec4(Math::RandVec3(0, 1, 2), 1.0f);
quad.AddComponent<SpriteRendererComponent>(texture);
auto& entity = m_Scene->CreateEntity("quad" + std::to_string(i), { translation, scale, rotation });
entity.AddComponent<SpriteRendererComponent>(texture, tint);
} }
ResourceManager::ReleaseTexture("awesomeface");
} }
} }