Skip to content

Commit

Permalink
Merge branch 'TheCherno:master' into fix-c++20-support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylindsey authored Aug 28, 2022
2 parents 858aea5 + 20f940b commit a8d3eb0
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Walnut/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ project "Walnut"

includedirs
{
"src",

"../vendor/imgui",
"../vendor/glfw/include",
"../vendor/stb_image",
Expand Down
25 changes: 25 additions & 0 deletions Walnut/src/Walnut/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>
#include <glm/glm.hpp>

#include <iostream>

Expand Down Expand Up @@ -54,6 +55,8 @@ static std::vector<std::vector<std::function<void()>>> s_ResourceFreeQueue;
// and is always guaranteed to increase (eg. 0, 1, 2, 0, 1, 2)
static uint32_t s_CurrentFrameIndex = 0;

static Walnut::Application* s_Instance = nullptr;

void check_vk_result(VkResult err)
{
if (err == 0)
Expand Down Expand Up @@ -384,12 +387,21 @@ namespace Walnut {
Application::Application(const ApplicationSpecification& specification)
: m_Specification(specification)
{
s_Instance = this;

Init();
}

Application::~Application()
{
Shutdown();

s_Instance = nullptr;
}

Application& Application::Get()
{
return *s_Instance;
}

void Application::Init()
Expand Down Expand Up @@ -557,6 +569,9 @@ namespace Walnut {
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();

for (auto& layer : m_LayerStack)
layer->OnUpdate(m_TimeStep);

// Resize swap chain?
if (g_SwapChainRebuild)
{
Expand Down Expand Up @@ -659,6 +674,11 @@ namespace Walnut {
// Present Main Platform Window
if (!main_is_minimized)
FramePresent(wd);

float time = GetTime();
m_FrameTime = time - m_LastFrameTime;
m_TimeStep = glm::min<float>(m_FrameTime, 0.0333f);
m_LastFrameTime = time;
}

}
Expand All @@ -668,6 +688,11 @@ namespace Walnut {
m_Running = false;
}

float Application::GetTime()
{
return (float)glfwGetTime();
}

VkInstance Application::GetInstance()
{
return g_Instance;
Expand Down
9 changes: 9 additions & 0 deletions Walnut/src/Walnut/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace Walnut {
Application(const ApplicationSpecification& applicationSpecification = ApplicationSpecification());
~Application();

static Application& Get();

void Run();
void SetMenubarCallback(const std::function<void()>& menubarCallback) { m_MenubarCallback = menubarCallback; }

Expand All @@ -43,6 +45,9 @@ namespace Walnut {

void Close();

float GetTime();
GLFWwindow* GetWindowHandle() const { return m_WindowHandle; }

static VkInstance GetInstance();
static VkPhysicalDevice GetPhysicalDevice();
static VkDevice GetDevice();
Expand All @@ -59,6 +64,10 @@ namespace Walnut {
GLFWwindow* m_WindowHandle = nullptr;
bool m_Running = false;

float m_TimeStep = 0.0f;
float m_FrameTime = 0.0f;
float m_LastFrameTime = 0.0f;

std::vector<std::shared_ptr<Layer>> m_LayerStack;
std::function<void()> m_MenubarCallback;
};
Expand Down
50 changes: 38 additions & 12 deletions Walnut/src/Walnut/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,7 @@ namespace Walnut {

Image::~Image()
{
Application::SubmitResourceFree([sampler = m_Sampler, imageView = m_ImageView, image = m_Image,
memory = m_Memory, stagingBuffer = m_StagingBuffer, stagingBufferMemory = m_StagingBufferMemory]()
{
VkDevice device = Application::GetDevice();

vkDestroySampler(device, sampler, nullptr);
vkDestroyImageView(device, imageView, nullptr);
vkDestroyImage(device, image, nullptr);
vkFreeMemory(device, memory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vkFreeMemory(device, stagingBufferMemory, nullptr);
});
Release();
}

void Image::AllocateMemory(uint64_t size)
Expand Down Expand Up @@ -168,6 +157,29 @@ namespace Walnut {
m_DescriptorSet = (VkDescriptorSet)ImGui_ImplVulkan_AddTexture(m_Sampler, m_ImageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}

void Image::Release()
{
Application::SubmitResourceFree([sampler = m_Sampler, imageView = m_ImageView, image = m_Image,
memory = m_Memory, stagingBuffer = m_StagingBuffer, stagingBufferMemory = m_StagingBufferMemory]()
{
VkDevice device = Application::GetDevice();

vkDestroySampler(device, sampler, nullptr);
vkDestroyImageView(device, imageView, nullptr);
vkDestroyImage(device, image, nullptr);
vkFreeMemory(device, memory, nullptr);
vkDestroyBuffer(device, stagingBuffer, nullptr);
vkFreeMemory(device, stagingBufferMemory, nullptr);
});

m_Sampler = nullptr;
m_ImageView = nullptr;
m_Image = nullptr;
m_Memory = nullptr;
m_StagingBuffer = nullptr;
m_StagingBufferMemory = nullptr;
}

void Image::SetData(const void* data)
{
VkDevice device = Application::GetDevice();
Expand Down Expand Up @@ -261,4 +273,18 @@ namespace Walnut {
}
}

void Image::Resize(uint32_t width, uint32_t height)
{
if (m_Image && m_Width == width && m_Height == height)
return;

// TODO: max size?

m_Width = width;
m_Height = height;

Release();
AllocateMemory(m_Width * m_Height * Utils::BytesPerPixel(m_Format));
}

}
3 changes: 3 additions & 0 deletions Walnut/src/Walnut/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ namespace Walnut {

VkDescriptorSet GetDescriptorSet() const { return m_DescriptorSet; }

void Resize(uint32_t width, uint32_t height);

uint32_t GetWidth() const { return m_Width; }
uint32_t GetHeight() const { return m_Height; }
private:
void AllocateMemory(uint64_t size);
void Release();
private:
uint32_t m_Width = 0, m_Height = 0;

Expand Down
38 changes: 38 additions & 0 deletions Walnut/src/Walnut/Input/Input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "Input.h"

#include "Walnut/Application.h"

#include <GLFW/glfw3.h>

namespace Walnut {

bool Input::IsKeyDown(KeyCode keycode)
{
GLFWwindow* windowHandle = Application::Get().GetWindowHandle();
int state = glfwGetKey(windowHandle, (int)keycode);
return state == GLFW_PRESS || state == GLFW_REPEAT;
}

bool Input::IsMouseButtonDown(MouseButton button)
{
GLFWwindow* windowHandle = Application::Get().GetWindowHandle();
int state = glfwGetMouseButton(windowHandle, (int)button);
return state == GLFW_PRESS;
}

glm::vec2 Input::GetMousePosition()
{
GLFWwindow* windowHandle = Application::Get().GetWindowHandle();

double x, y;
glfwGetCursorPos(windowHandle, &x, &y);
return { (float)x, (float)y };
}

void Input::SetCursorMode(CursorMode mode)
{
GLFWwindow* windowHandle = Application::Get().GetWindowHandle();
glfwSetInputMode(windowHandle, GLFW_CURSOR, GLFW_CURSOR_NORMAL + (int)mode);
}

}
20 changes: 20 additions & 0 deletions Walnut/src/Walnut/Input/Input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "KeyCodes.h"

#include <glm/glm.hpp>

namespace Walnut {

class Input
{
public:
static bool IsKeyDown(KeyCode keycode);
static bool IsMouseButtonDown(MouseButton button);

static glm::vec2 GetMousePosition();

static void SetCursorMode(CursorMode mode);
};

}
Loading

0 comments on commit a8d3eb0

Please sign in to comment.