Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#98]: Add multithreading #116

Merged
merged 14 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 131 additions & 44 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "renderer/vulkan_common.hpp"
#include "renderer/window/window.hpp"
#include "utils/file_manager.hpp"
#include "utils/time/scoped_timer.hpp"
#include "utils/time/stopwatch.hpp"

#include <GLFW/glfw3.h>
Expand All @@ -22,14 +23,14 @@ namespace looper {

Editor::Editor(const glm::ivec2& screenSize) : gui_(*this)
{
m_window = std::make_unique< renderer::Window >(screenSize, "Editor");
m_window = std::make_unique< renderer::Window >(screenSize, "Editor", true);

InputManager::Init(m_window->GetWindowHandle());
InputManager::RegisterForInput(this);


renderer::VulkanRenderer::Initialize(m_window->GetWindowHandle(),
renderer::ApplicationType::EDITOR);
gui_.Init();
InputManager::RegisterForInput(m_window->GetWindowHandle(), this);
}

void
Expand Down Expand Up @@ -71,44 +72,52 @@ Editor::HandleCamera()
}

void
Editor::KeyCallback(const KeyEvent& event)
Editor::KeyCallback(KeyEvent& event)
{
if (event.action_ == GLFW_PRESS)
{
if (event.key_ == GLFW_KEY_ESCAPE)
if (IsAnyObjectSelected())
{
ActionOnObject(ACTION::UNSELECT);
}
if (event.key_ == GLFW_KEY_ESCAPE)
{
ActionOnObject(ACTION::UNSELECT);
event.handled_ = true;
}

if (event.key_ == GLFW_KEY_DELETE)
{
ActionOnObject(ACTION::REMOVE);
if (event.key_ == GLFW_KEY_DELETE)
{
ActionOnObject(ACTION::REMOVE);
event.handled_ = true;
}
}
}
else if (event.action_ == GLFW_RELEASE)
{
if (m_gameObjectSelected && event.key_ == GLFW_KEY_C)
{
m_copiedGameObject = m_currentSelectedGameObject;
event.handled_ = true;
}
if (m_copiedGameObject && event.key_ == GLFW_KEY_V)
{
CopyGameObject(m_copiedGameObject);
event.handled_ = true;
}
}
}

void
Editor::MouseScrollCallback(const MouseScrollEvent& event)
Editor::MouseScrollCallback(MouseScrollEvent& event)
{
if (!m_playGame && !EditorGUI::IsBlockingEvents() && m_levelLoaded)
{
m_camera.Zoom(static_cast< float >(event.xOffset_ + event.yOffset_));
event.handled_ = true;
}
}

void
Editor::MouseButtonCallback(const MouseButtonEvent& event)
Editor::MouseButtonCallback(MouseButtonEvent& event)
{
if (!m_playGame && !EditorGUI::IsBlockingEvents() && m_levelLoaded)
{
Expand All @@ -129,11 +138,13 @@ Editor::MouseButtonCallback(const MouseButtonEvent& event)
m_movementOnGameObject = false;
m_mouseDrag = false;
}

event.handled_ = true;
}
}

void
Editor::CursorPositionCallback(const CursorPositionEvent& event)
Editor::CursorPositionCallback(CursorPositionEvent& event)
{
if (!m_playGame && !EditorGUI::IsBlockingEvents() && m_levelLoaded)
{
Expand All @@ -150,6 +161,7 @@ Editor::CursorPositionCallback(const CursorPositionEvent& event)
}

m_lastCursorPosition = currentCursorPosition;
event.handled_ = true;
}
}

Expand Down Expand Up @@ -454,9 +466,44 @@ Editor::SetupRendererData()
renderer::VulkanRenderer::UpdateLineData();
}


void
Editor::AddAnimationPoint(const glm::vec2& position)
{
GetCamera().SetCameraAtPosition(position);
AddObject(ObjectType::ANIMATION_POINT);
SetRenderAnimationPoints(true);
}

void
Editor::SelectAnimationPoint(const AnimationPoint& node)
{
GetCamera().SetCameraAtPosition(node.m_end);
HandleObjectSelected(node.GetID(), true);
SetRenderAnimationPoints(true);
}

void
Editor::ActionOnObject(Editor::ACTION action)
Editor::AddToWorkQueue(const WorkQueue::WorkUnit& work, const WorkQueue::Precondition& prec)
{
workQueue_.PushWorkUnit(prec, work);
}

bool
Editor::IsAnyObjectSelected() const
{
return m_editorObjectSelected or m_currentEditorObjectSelected or m_gameObjectSelected
or m_currentSelectedGameObject;
}

void
Editor::ActionOnObject(Editor::ACTION action, const std::optional< Object::ID >& object)
{
if (object)
{
HandleObjectSelected(object.value(), true);
}

switch (action)
{
case ACTION::UNSELECT:
Expand Down Expand Up @@ -700,11 +747,23 @@ Editor::GetGridData() const
}

time::TimeStep
Editor::GetRenderTime() const
Editor::GetFrameTime() const
{
return timeLastFrame_;
}

time::TimeStep
Editor::GetUpdateUITime() const
{
return uiTime_;
}

time::TimeStep
Editor::GetRenderTime() const
{
return renderTime_;
}

std::pair< uint32_t, uint32_t >
Editor::GetRenderOffsets() const
{
Expand All @@ -714,6 +773,8 @@ Editor::GetRenderOffsets() const
void
Editor::SetupPathfinderNodes()
{
SCOPED_TIMER("SetupPathfinderNodes");

const auto& pathfinderNodes = m_currentLevel->GetPathfinder().GetAllNodes();
std::ranges::transform(
pathfinderNodes, std::back_inserter(pathfinderNodes_), [this](const auto& node) {
Expand Down Expand Up @@ -771,40 +832,52 @@ Editor::LoadLevel(const std::string& levelPath)

renderer::VulkanRenderer::SetAppMarker(renderer::ApplicationType::EDITOR);

m_levelFileName = levelPath;
m_currentLevel = std::make_shared< Level >();
m_currentLevel->Load(this, levelPath);

SetupPathfinderNodes();

const auto& gameObjects = m_currentLevel->GetObjects();
for (const auto& object : gameObjects)
{
const auto animatablePtr = std::dynamic_pointer_cast< Animatable >(object);
SCOPED_TIMER("Total level load");

m_levelFileName = levelPath;
m_currentLevel = std::make_shared< Level >();
m_currentLevel->Load(this, levelPath);

SetupPathfinderNodes();

if (animatablePtr)
{
const auto& animationPoints = animatablePtr->GetAnimationKeypoints();
SCOPED_TIMER("Animation points setup");

for (const auto& point : animationPoints)
const auto& gameObjects = m_currentLevel->GetObjects();
for (const auto& object : gameObjects)
{
auto editorObject = std::make_shared< EditorObject >(
*this, point.m_end, glm::vec2(20, 20), "NodeSprite.png", point.GetID());
editorObject->SetName(fmt::format("AnimationPoint{}", object->GetName()));
editorObject->SetVisible(false);
editorObject->Render();
animationPoints_.push_back(editorObject);
const auto animatablePtr = std::dynamic_pointer_cast< Animatable >(object);

if (animatablePtr)
{
const auto& animationPoints = animatablePtr->GetAnimationKeypoints();

for (const auto& point : animationPoints)
{
auto editorObject = std::make_shared< EditorObject >(
*this, point.m_end, glm::vec2(20, 20), "NodeSprite.png", point.GetID());
editorObject->SetName(fmt::format("AnimationPoint{}", object->GetName()));
editorObject->SetVisible(false);
editorObject->Render();
animationPoints_.push_back(editorObject);
}
}
}
}
}

m_camera.Create(glm::vec3(m_currentLevel->GetPlayer()->GetPosition(), 0.0f),
m_window->GetSize());
m_camera.SetLevelSize(m_currentLevel->GetSize());
m_camera.Create(glm::vec3(m_currentLevel->GetPlayer()->GetPosition(), 0.0f),
m_window->GetSize());
m_camera.SetLevelSize(m_currentLevel->GetSize());

m_levelLoaded = true;
gui_.LevelLoaded(m_currentLevel);
m_window->MakeFocus();
m_levelLoaded = true;

gui_.LevelLoaded(m_currentLevel);

m_window->MakeFocus();

}

SetupRendererData();
}

Expand Down Expand Up @@ -972,6 +1045,8 @@ Editor::SetLockAnimationPoints(bool lock)
void
Editor::Update()
{
HandleCamera();

if (m_animateGameObject && m_currentSelectedGameObject)
{
auto moveBy = std::dynamic_pointer_cast< Animatable >(m_currentSelectedGameObject)
Expand All @@ -987,13 +1062,16 @@ Editor::Update()
}
}

gui_.UpdateUI();

auto& renderData = renderer::VulkanRenderer::GetRenderData();
renderData.viewMat = m_camera.GetViewMatrix();
renderData.projMat = m_camera.GetProjectionMatrix();

DrawBoundingBoxes();

{
const time::ScopedTimer uiTImer(&uiTime_);
gui_.UpdateUI();
}
}

void
Expand Down Expand Up @@ -1037,6 +1115,12 @@ Editor::IsRunning() const
return m_isRunning;
}

void
Editor::Shutdown()
{
m_isRunning = false;
}

void
Editor::MainLoop()
{
Expand All @@ -1050,15 +1134,18 @@ Editor::MainLoop()

while (IsRunning() and (singleFrameTimer.count() >= TARGET_TIME_MICRO))
{
const time::ScopedTimer frameTimer(&timeLastFrame_);
InputManager::PollEvents();

// Run all deffered work units
workQueue_.RunWorkUnits();

HandleCamera();
Update();

workQueue_.RunWorkUnits();

if (windowInFocus_)
{
const time::ScopedTimer renderTimer(&renderTime_);
renderer::VulkanRenderer::Render(this);
}

Expand Down
Loading