Skip to content

Commit

Permalink
Merge pull request #136 from JacobDomagala/135-default-window-size-to…
Browse files Browse the repository at this point in the history
…-monitor-size

[#135]: Default the size of `Window` to size of `PrimaryMonitor`
  • Loading branch information
JacobDomagala authored Jul 31, 2023
2 parents fce09e0 + f74a838 commit a0ddd47
Show file tree
Hide file tree
Showing 22 changed files with 276 additions and 269 deletions.
38 changes: 19 additions & 19 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ namespace looper {

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

InputManager::Init(m_window->GetWindowHandle());
InputManager::RegisterForKeyInput(this);
InputManager::RegisterForMouseScrollInput(this);
InputManager::RegisterForMouseButtonInput(this);
InputManager::RegisterForMouseMovementInput(this);
InputManager::RegisterForInput(this);

renderer::VulkanRenderer::Initialize(m_window->GetWindowHandle(),
renderer::ApplicationType::EDITOR);
Expand Down Expand Up @@ -76,28 +73,26 @@ Editor::HandleCamera()
void
Editor::KeyCallback(const KeyEvent& event)
{
if (event.m_action == GLFW_PRESS)
if (event.action_ == GLFW_PRESS)
{
if (event.m_key == GLFW_KEY_ESCAPE)
if (event.key_ == GLFW_KEY_ESCAPE)
{
ActionOnObject(ACTION::UNSELECT);
}

if (event.m_key == GLFW_KEY_DELETE)
if (event.key_ == GLFW_KEY_DELETE)
{
ActionOnObject(ACTION::REMOVE);
}
}
else if (event.m_action == GLFW_RELEASE)
else if (event.action_ == GLFW_RELEASE)
{
if (m_gameObjectSelected && event.m_key == GLFW_KEY_C)
if (m_gameObjectSelected && event.key_ == GLFW_KEY_C)
{
Logger::Info("Copy object!");
m_copiedGameObject = m_currentSelectedGameObject;
}
if (m_copiedGameObject && event.m_key == GLFW_KEY_V)
if (m_copiedGameObject && event.key_ == GLFW_KEY_V)
{
Logger::Info("Paste object!");
CopyGameObject(m_copiedGameObject);
}
}
Expand All @@ -108,7 +103,7 @@ Editor::MouseScrollCallback(const MouseScrollEvent& event)
{
if (!m_playGame && !EditorGUI::IsBlockingEvents() && m_levelLoaded)
{
m_camera.Zoom(static_cast< float >(event.m_xOffset + event.m_yOffset));
m_camera.Zoom(static_cast< float >(event.xOffset_ + event.yOffset_));
}
}

Expand All @@ -117,7 +112,7 @@ Editor::MouseButtonCallback(const MouseButtonEvent& event)
{
if (!m_playGame && !EditorGUI::IsBlockingEvents() && m_levelLoaded)
{
const auto mousePressed = event.m_action == GLFW_PRESS;
const auto mousePressed = event.action_ == GLFW_PRESS;
m_mousePressedLastUpdate = mousePressed;

if (mousePressed)
Expand All @@ -142,7 +137,7 @@ Editor::CursorPositionCallback(const CursorPositionEvent& event)
{
if (!m_playGame && !EditorGUI::IsBlockingEvents() && m_levelLoaded)
{
const auto currentCursorPosition = glm::vec2(event.m_xPos, event.m_yPos);
const auto currentCursorPosition = glm::vec2(event.xPos_, event.yPos_);

if (m_mousePressedLastUpdate)
{
Expand Down Expand Up @@ -809,6 +804,7 @@ Editor::LoadLevel(const std::string& levelPath)

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

Expand Down Expand Up @@ -993,8 +989,7 @@ Editor::Update()

gui_.UpdateUI();

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

Expand Down Expand Up @@ -1060,7 +1055,12 @@ Editor::MainLoop()
HandleCamera();
Update();

renderer::VulkanRenderer::Render(this);
workQueue_.RunWorkUnits();

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

timeLastFrame_ = watch.Stop();

Expand Down
2 changes: 0 additions & 2 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 <glm/matrix.hpp>
#include <utility>
Expand Down
18 changes: 7 additions & 11 deletions editor/gui/editor_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,31 @@ EditorGUI::KeyCallback(const KeyEvent& event)
io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS)
|| (glfwGetKey(window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS));

const auto imguiKey = KeyToImGuiKey(event.m_key);
io.AddKeyEvent(imguiKey, (event.m_action == GLFW_PRESS));
const auto imguiKey = KeyToImGuiKey(event.key_);
io.AddKeyEvent(imguiKey, (event.action_ == GLFW_PRESS));
}

void
EditorGUI::CharCallback(const CharEvent& event)
{
ImGuiIO& io = ImGui::GetIO();
io.AddInputCharacter(event.m_key);
io.AddInputCharacter(event.key_);
}

void
EditorGUI::MouseButtonCallback(const MouseButtonEvent& event)
{
ImGuiIO& io = ImGui::GetIO();

io.MouseDown[0] = (event.m_button == GLFW_MOUSE_BUTTON_1) && event.m_action;
io.MouseDown[1] = (event.m_button == GLFW_MOUSE_BUTTON_2) && event.m_action;
io.MouseDown[0] = (event.button_ == GLFW_MOUSE_BUTTON_1) && event.action_;
io.MouseDown[1] = (event.button_ == GLFW_MOUSE_BUTTON_2) && event.action_;
}

void
EditorGUI::CursorPositionCallback(const CursorPositionEvent& event)
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = ImVec2(static_cast< float >(event.m_xPos), static_cast< float >(event.m_yPos));
io.MousePos = ImVec2(static_cast< float >(event.xPos_), static_cast< float >(event.yPos_));
}

void
Expand All @@ -101,11 +101,7 @@ EditorGUI::MouseScrollCallback(const MouseScrollEvent& /*event*/)
void
EditorGUI::Init()
{
InputManager::RegisterForKeyInput(this);
InputManager::RegisterForCharInput(this);
InputManager::RegisterForMouseScrollInput(this);
InputManager::RegisterForMouseButtonInput(this);
InputManager::RegisterForMouseMovementInput(this);
InputManager::RegisterForInput(this);

// Setup Dear ImGui context
IMGUI_CHECKVERSION();
Expand Down
2 changes: 1 addition & 1 deletion editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
int
main(int /* argc */, char** /* argv */)
{
looper::Editor editor({1920, 1080});
looper::Editor editor(looper::USE_DEFAULT_SIZE);
editor.MainLoop();

return EXIT_SUCCESS;
Expand Down
1 change: 1 addition & 0 deletions engine/common/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ constexpr float TARGET_TIME_MS = (1.0f / TARGET_FPS) * 1000.0f; // millise
constexpr float TARGET_TIME_MICRO = (1.0f / TARGET_FPS) * 1000000.0f; // microseconds
constexpr int32_t WIDTH = 1920;
constexpr int32_t HEIGHT = 1080;
constexpr glm::ivec2 USE_DEFAULT_SIZE = glm::ivec2{-1, -1};
constexpr int32_t NUM_FRAMES_TO_SAVE = 500;

} // 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
45 changes: 45 additions & 0 deletions engine/core/work_queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <functional>
#include <vector>

namespace looper {

class WorkQueue
{
public:
using WorkUnit = std::function< void() >;
using Precondition = std::function< bool() >;
using Workers = std::vector< std::pair< Precondition, WorkUnit > >;

void
PushWorkUnit(const Precondition& prec, const WorkUnit& work)
{
queue_.push_back({prec, work});
}

void
RunWorkUnits()
{
Workers tmpQueue;

for (auto& unit : queue_)
{
if (unit.first())
{
unit.second();
}
else
{
tmpQueue.push_back(unit);
}
}

queue_.swap(tmpQueue);
}

private:
Workers queue_;
};

} // namespace looper
26 changes: 12 additions & 14 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 @@ -66,7 +70,8 @@ Game::Init(const std::string& configFile, bool loadLevel)
Logger::Fatal("Game: Can't open {}", (ASSETS_DIR / configFile).string());
}

m_window = std::make_unique< renderer::Window >(WIDTH, HEIGHT, "WindowTitle");
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 @@ -342,12 +348,6 @@ Game::IsRunning() const
return m_window->IsRunning();
}

void
Game::RegisterForKeyInput(InputListener* listener)
{
InputManager::RegisterForKeyInput(listener);
}

void
Game::ProcessInput(time::milliseconds deltaTime)
{
Expand Down Expand Up @@ -395,7 +395,6 @@ Game::HandleReverseLogic()
{
++m_frameCount;
}

}
}

Expand Down Expand Up @@ -434,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
3 changes: 0 additions & 3 deletions engine/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class Game : public Application
[[nodiscard]] glm::vec2
GetCursorScreenPosition();

static void
RegisterForKeyInput(InputListener* listener);

void
LoadLevel(const std::string& pathToLevel);

Expand Down
Loading

0 comments on commit a0ddd47

Please sign in to comment.