Add Application class
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
#include "copium/app/Application.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "copium/event/EventDispatcher.h"
|
||||
#include "copium/event/Input.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
void Application::Run()
|
||||
{
|
||||
while (Update())
|
||||
{
|
||||
Input::Update();
|
||||
glfwPollEvents();
|
||||
EventDispatcher::Dispatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
class Application
|
||||
{
|
||||
public:
|
||||
Application() = default;
|
||||
virtual ~Application() = default;
|
||||
void Run();
|
||||
virtual bool Update() = 0;
|
||||
|
||||
private:
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(Application);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
#include "copium/app/Basic2DApplication.h"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <thread>
|
||||
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/event/EventDispatcher.h"
|
||||
#include "copium/event/WindowResizeEvent.h"
|
||||
#include "copium/renderer/RendererVertex.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Basic2DApplication::Basic2DApplication(bool realTimeApplication,
|
||||
const std::string& vertexShader,
|
||||
const std::string& fragmentShader)
|
||||
: realTimeApplication{realTimeApplication}
|
||||
{
|
||||
EventDispatcher::AddEventHandler(this);
|
||||
|
||||
PipelineCreator pipelineCreator{Vulkan::GetSwapChain().GetRenderPass(), vertexShader, fragmentShader};
|
||||
pipelineCreator.SetVertexDescriptor(RendererVertex::GetDescriptor());
|
||||
renderer = std::make_unique<Renderer>(AssetRef<Pipeline>(std::make_unique<Pipeline>(pipelineCreator)));
|
||||
|
||||
Pipeline& pl = renderer->GetGraphicsPipeline();
|
||||
|
||||
commandBuffer = std::make_unique<CommandBuffer>(CommandBufferType::Dynamic);
|
||||
descriptorPool = std::make_unique<DescriptorPool>(SwapChain::MAX_FRAMES_IN_FLIGHT * 2, 0);
|
||||
|
||||
projectionDescriptorSet = pl.CreateDescriptorSet(*descriptorPool, 1);
|
||||
viewDescriptorSet = pl.CreateDescriptorSet(*descriptorPool, 2);
|
||||
|
||||
float ratio = Vulkan::GetWindow().GetWidth() / (float)Vulkan::GetWindow().GetHeight();
|
||||
SetViewMatrixStatic(glm::translate(glm::mat4{1}, glm::vec3{0, 0, 0}));
|
||||
SetProjectionMatrixStatic(glm::ortho<float>(0, ratio, 0, 1));
|
||||
|
||||
pl.SetDescriptorSet(*viewDescriptorSet);
|
||||
pl.SetDescriptorSet(*projectionDescriptorSet);
|
||||
}
|
||||
|
||||
Basic2DApplication::~Basic2DApplication()
|
||||
{
|
||||
EventDispatcher::RemoveEventHandler(this);
|
||||
}
|
||||
|
||||
EventResult Basic2DApplication::OnEvent(const Event& event)
|
||||
{
|
||||
switch (event.GetType())
|
||||
{
|
||||
case Copium::EventType::WindowResize:
|
||||
{
|
||||
const WindowResizeEvent& windowResize = static_cast<const WindowResizeEvent&>(event);
|
||||
float ratio = windowResize.GetWidth() / (float)windowResize.GetHeight();
|
||||
SetProjectionMatrixStatic(glm::ortho<float>(0, ratio, 0, 1));
|
||||
Redraw();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return EventResult::Continue;
|
||||
}
|
||||
|
||||
void Basic2DApplication::PreRender()
|
||||
{
|
||||
}
|
||||
|
||||
void Basic2DApplication::PostRender()
|
||||
{
|
||||
}
|
||||
|
||||
bool Basic2DApplication::Update()
|
||||
{
|
||||
PreRender();
|
||||
if (!realTimeApplication && redrawCount <= 0)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
return !Vulkan::GetWindow().ShouldClose();
|
||||
}
|
||||
|
||||
if (!Vulkan::GetSwapChain().BeginPresent())
|
||||
return true;
|
||||
|
||||
commandBuffer->Begin();
|
||||
|
||||
Vulkan::GetSwapChain().BeginFrameBuffer(*commandBuffer);
|
||||
renderer->Begin(*commandBuffer);
|
||||
Render(*renderer);
|
||||
renderer->End();
|
||||
Vulkan::GetSwapChain().EndFrameBuffer(*commandBuffer);
|
||||
|
||||
commandBuffer->End();
|
||||
|
||||
Vulkan::GetSwapChain().SubmitToGraphicsQueue(*commandBuffer);
|
||||
|
||||
Vulkan::GetSwapChain().EndPresent();
|
||||
if (!realTimeApplication)
|
||||
redrawCount--;
|
||||
PostRender();
|
||||
return !Vulkan::GetWindow().ShouldClose();
|
||||
}
|
||||
|
||||
void Basic2DApplication::SetViewMatrix(const glm::mat4& viewMatrix)
|
||||
{
|
||||
UniformBuffer& viewUbo = viewDescriptorSet->GetUniformBuffer("viewUbo");
|
||||
viewUbo.Set("view", glm::translate(glm::mat4{1}, glm::vec3{0, 0, 0}));
|
||||
viewUbo.Update();
|
||||
}
|
||||
|
||||
void Basic2DApplication::SetViewMatrixStatic(const glm::mat4& viewMatrix)
|
||||
{
|
||||
UniformBuffer& viewUbo = viewDescriptorSet->GetUniformBuffer("viewUbo");
|
||||
viewUbo.Set("view", glm::translate(glm::mat4{1}, glm::vec3{0, 0, 0}));
|
||||
viewUbo.UpdateStatic();
|
||||
}
|
||||
|
||||
void Basic2DApplication::SetProjectionMatrix(const glm::mat4& projectionMatrix)
|
||||
{
|
||||
UniformBuffer& projectionUbo = projectionDescriptorSet->GetUniformBuffer("projectionUbo");
|
||||
projectionUbo.Set("projection", projectionMatrix);
|
||||
projectionUbo.Update();
|
||||
}
|
||||
|
||||
void Basic2DApplication::SetProjectionMatrixStatic(const glm::mat4& projectionMatrix)
|
||||
{
|
||||
UniformBuffer& projectionUbo = projectionDescriptorSet->GetUniformBuffer("projectionUbo");
|
||||
projectionUbo.Set("projection", projectionMatrix);
|
||||
projectionUbo.UpdateStatic();
|
||||
}
|
||||
|
||||
void Basic2DApplication::Redraw()
|
||||
{
|
||||
redrawCount = SwapChain::MAX_FRAMES_IN_FLIGHT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
#include "copium/app/Application.h"
|
||||
#include "copium/buffer/CommandBuffer.h"
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/event/EventHandler.h"
|
||||
#include "copium/renderer/Renderer.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
// TODO: Better name?
|
||||
class Basic2DApplication : public Application, public EventHandler
|
||||
{
|
||||
private:
|
||||
struct VulkanWrapper
|
||||
{
|
||||
VulkanWrapper()
|
||||
{
|
||||
Vulkan::Initialize();
|
||||
}
|
||||
|
||||
~VulkanWrapper()
|
||||
{
|
||||
Vulkan::Destroy();
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
Basic2DApplication(bool realTimeApplication, const std::string& vertexShader, const std::string& fragmentShader);
|
||||
~Basic2DApplication();
|
||||
|
||||
virtual EventResult OnEvent(const Event& event) override;
|
||||
|
||||
virtual void PreRender();
|
||||
virtual void Render(Renderer& renderer) = 0;
|
||||
virtual void PostRender();
|
||||
bool Update() override;
|
||||
|
||||
void SetViewMatrix(const glm::mat4& viewMatrix);
|
||||
void SetViewMatrixStatic(const glm::mat4& viewMatrix);
|
||||
void SetProjectionMatrix(const glm::mat4& projectionMatrix);
|
||||
void SetProjectionMatrixStatic(const glm::mat4& projectionMatrix);
|
||||
void Redraw();
|
||||
|
||||
private:
|
||||
bool realTimeApplication;
|
||||
int redrawCount{2};
|
||||
|
||||
VulkanWrapper vulkan;
|
||||
std::unique_ptr<Renderer> renderer;
|
||||
std::unique_ptr<CommandBuffer> commandBuffer;
|
||||
std::unique_ptr<Copium::DescriptorPool> descriptorPool;
|
||||
std::unique_ptr<Copium::DescriptorSet> projectionDescriptorSet;
|
||||
std::unique_ptr<Copium::DescriptorSet> viewDescriptorSet;
|
||||
};
|
||||
}
|
||||
@@ -73,6 +73,12 @@ namespace Copium
|
||||
return *id != NULL_ASSET_ID;
|
||||
}
|
||||
|
||||
void Reset()
|
||||
{
|
||||
AssetManager::UnloadAsset(*id);
|
||||
*id = NULL_ASSET_ID;
|
||||
}
|
||||
|
||||
private:
|
||||
struct AssetIdUnloader
|
||||
{
|
||||
|
||||
@@ -66,6 +66,7 @@ namespace Copium
|
||||
device.reset();
|
||||
window.reset();
|
||||
instance.reset();
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
Instance& Vulkan::GetInstance()
|
||||
|
||||
Reference in New Issue
Block a user