Skip to content

Commit

Permalink
[#138]: Correcly handle Window focus
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Jul 31, 2023
1 parent 4231918 commit b908636
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 31 deletions.
13 changes: 2 additions & 11 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ Editor::KeyCallback(const KeyEvent& event)
{
if (m_gameObjectSelected && event.key_ == GLFW_KEY_C)
{
Logger::Info("Copy object!");
m_copiedGameObject = m_currentSelectedGameObject;
}
if (m_copiedGameObject && event.key_ == GLFW_KEY_V)
{
Logger::Info("Paste object!");
CopyGameObject(m_copiedGameObject);
}
}
Expand All @@ -109,13 +107,6 @@ Editor::MouseScrollCallback(const MouseScrollEvent& event)
}
}

void
Editor::WindowFocusCallback(const WindowFocusEvent& event)
{
renderer::VulkanRenderer::GetRenderData().windowFocus_ = event.focus_;
windowInFocus_ = event.focus_;
}

void
Editor::MouseButtonCallback(const MouseButtonEvent& event)
{
Expand Down Expand Up @@ -813,8 +804,8 @@ Editor::LoadLevel(const std::string& levelPath)

m_levelLoaded = true;
gui_.LevelLoaded(m_currentLevel);

workQueue_.PushWorkUnit([this] { return windowInFocus_; }, [this] { SetupRendererData(); });
m_window->MakeFocus();
SetupRendererData();
}

void
Expand Down
9 changes: 0 additions & 9 deletions editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "object.hpp"
#include "player.hpp"
#include "utils/time/time_step.hpp"
#include "utils/time/timer.hpp"
#include "work_queue.hpp"

#include <glm/matrix.hpp>
#include <utility>
Expand Down Expand Up @@ -58,9 +56,6 @@ class Editor : public Application
void
MouseScrollCallback(const MouseScrollEvent& event) override;

void
WindowFocusCallback(const WindowFocusEvent& event) override;

void
Render(VkCommandBuffer cmdBuffer) override;

Expand Down Expand Up @@ -209,8 +204,6 @@ class Editor : public Application
bool m_mousePressedLastUpdate = false;
bool m_mouseDrag = false;

bool windowInFocus_ = true;

glm::vec2 m_lastCursorPosition = {};

// Handling game objects (which are visible in game)
Expand Down Expand Up @@ -241,8 +234,6 @@ class Editor : public Application

bool m_playGame = false;
time::TimeStep timeLastFrame_ = time::TimeStep{time::microseconds{}};

WorkQueue workQueue_ = {};
};

} // namespace looper
7 changes: 7 additions & 0 deletions engine/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ Application::ScreenToGlobal(const glm::vec2& screenPos) const
return globalPos;
}

void
Application::WindowFocusCallback(const WindowFocusEvent& event)
{
renderer::VulkanRenderer::GetRenderData().windowFocus_ = event.focus_;
windowInFocus_ = event.focus_;
}

} // namespace looper
15 changes: 12 additions & 3 deletions engine/core/application.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "renderer/camera/camera.hpp"
#include "input_listener.hpp"
#include "level.hpp"
#include "logger.hpp"
#include "utils/time/timer.hpp"
#include "logger.hpp"
#include "renderer/camera/camera.hpp"
#include "work_queue.hpp"

#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
Expand Down Expand Up @@ -47,7 +48,8 @@ class Application : public InputListener
[[nodiscard]] time::milliseconds
GetDeltaTime() const;

[[nodiscard]] int32_t GetFramesLastSecond() const
[[nodiscard]] int32_t
GetFramesLastSecond() const
{
return m_framesLastSecond;
}
Expand Down Expand Up @@ -84,11 +86,16 @@ class Application : public InputListener
[[nodiscard]] virtual float
GetZoomLevel() const = 0;

void
WindowFocusCallback(const WindowFocusEvent& event) override;

protected:
[[nodiscard]] virtual bool
IsRunning() const = 0;

bool m_isGame = false;
bool windowInFocus_ = true;

std::shared_ptr< Player > m_player = nullptr;
std::shared_ptr< Level > m_currentLevel = nullptr;

Expand All @@ -100,6 +107,8 @@ class Application : public InputListener
float m_frameTimer = 0.0f;
int32_t m_framesLastSecond = 0;
uint32_t numObjects_ = {};

WorkQueue workQueue_ = {};
};


Expand Down
18 changes: 11 additions & 7 deletions engine/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ Game::MainLoop()

while (IsRunning() and (singleFrameTimer.count() >= TARGET_TIME_MICRO))
{
m_window->Clear();
const auto dt = time::milliseconds(
TARGET_TIME_MS * static_cast< float >(time::Timer::AreTimersRunning()));
ProcessInput(dt);

renderer::VulkanRenderer::Render(this);
workQueue_.RunWorkUnits();
if (windowInFocus_)
{
renderer::VulkanRenderer::Render(this);
}

if (m_frameTimer > 1.0f)
{
m_framesLastSecond = m_frames;
Expand Down Expand Up @@ -67,6 +71,7 @@ Game::Init(const std::string& configFile, bool loadLevel)
}

m_window = std::make_unique< renderer::Window >(USE_DEFAULT_SIZE, "WindowTitle");
m_window->MakeFocus();

renderer::VulkanRenderer::Initialize(m_window->GetWindowHandle(),
renderer::ApplicationType::GAME);
Expand Down Expand Up @@ -297,7 +302,8 @@ Game::LoadLevel(const std::string& pathToLevel)
m_camera.Create(glm::vec3(m_player->GetCenteredPosition(), 0.0f), m_window->GetSize());
m_camera.SetLevelSize(m_currentLevel->GetSize());

renderer::VulkanRenderer::SetupData();
workQueue_.PushWorkUnit([this] { return windowInFocus_; },
[] { renderer::VulkanRenderer::SetupData(); });
}

glm::vec2
Expand Down Expand Up @@ -389,7 +395,6 @@ Game::HandleReverseLogic()
{
++m_frameCount;
}

}
}

Expand Down Expand Up @@ -428,10 +433,9 @@ Game::Render(VkCommandBuffer cmdBuffer)
// const auto numObjects =
// renderer::VulkanRenderer::GetNumMeshes(renderer::ApplicationType::GAME); numObjects_ =
// numObjects.second - numObjects.first;
vkCmdDrawIndexed(cmdBuffer, renderData.numMeshes.at(idx) * renderer::INDICES_PER_SPRITE, 1,
0, 0, 0);
vkCmdDrawIndexed(cmdBuffer, renderData.numMeshes.at(idx) * renderer::INDICES_PER_SPRITE, 1, 0,
0, 0);
}

}

} // namespace looper
1 change: 0 additions & 1 deletion engine/logger/logger.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "formatter_types.hpp"
#include "utils/time/timer.hpp"

#include <fmt/color.h>

Expand Down
1 change: 1 addition & 0 deletions engine/logger/logger.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#undef GetCurrentTime

#include "logger.hpp"
#include "time/timer.hpp"

#include <fmt/format.h>

Expand Down
8 changes: 8 additions & 0 deletions engine/renderer/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ Window::Resize(const glm::ivec2& newSize)
size_ = newSize;
}

void
Window::MakeFocus()
{
glfwRestoreWindow(window_);
glfwShowWindow(window_);
glfwFocusWindow(window_);
}

void
Window::Clear()
{
Expand Down
2 changes: 2 additions & 0 deletions engine/renderer/window/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Window
void
ShutDown();

void MakeFocus();

[[nodiscard]] glm::ivec2
GetSize() const
{
Expand Down

0 comments on commit b908636

Please sign in to comment.