add window context

This commit is contained in:
lauchmelder 2025-01-07 21:14:59 +01:00
parent aada1f9636
commit 18c0a431dc
11 changed files with 105 additions and 28 deletions

View file

@ -22,5 +22,13 @@ FetchContent_Declare(
FIND_PACKAGE_ARGS
)
# TODO: compile with all rendering backends and select one at runtime
set(QUARK_RENDERING_BACKEND "OPENGL" CACHE STRING "Rendering backend to use during engine compilation")
set_property(CACHE QUARK_RENDERING_BACKEND PROPERTY STRINGS OPENGL VULKAN)
if (${QUARK_RENDERING_BACKEND} STREQUAL "VULKAN")
find_package(Vulkan REQUIRED)
endif()
add_subdirectory(quark)
add_subdirectory(sandbox)

View file

@ -16,6 +16,8 @@ target_sources(quark
src/platform/linux/LinuxWindow.cpp
src/platform/linux/LinuxInput.cpp
src/platform/opengl/OpenGLContext.cpp
src/quark/imgui/ImGuiLayer.cpp
PUBLIC FILE_SET HEADERS
@ -38,7 +40,11 @@ target_sources(quark
src/quark/events/KeyEvent.hpp
src/quark/events/MouseEvent.hpp
src/platform/opengl/OpenGLContext.hpp
src/quark/imgui/ImGuiLayer.hpp
src/quark/photon/RenderingContext.hpp
)
target_include_directories(quark INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src)
@ -54,7 +60,6 @@ target_compile_definitions(quark
target_link_libraries(quark
PRIVATE
glad
glfw
PUBLIC
@ -63,6 +68,13 @@ target_link_libraries(quark
glm
)
if (${QUARK_RENDERING_BACKEND} STREQUAL "OPENGL")
target_link_libraries(quark PRIVATE glad)
elseif (${QUARK_RENDERING_BACKEND} STREQUAL "VULKAN")
target_link_libraries(quark PRIVATE Vulkan::Vulkan)
endif()
include(GNUInstallDirs)
install(
TARGETS quark

View file

@ -1,4 +1,7 @@
#include "LinuxWindow.hpp"
#include "platform/opengl/OpenGLContext.hpp"
#include "quark/Core.hpp"
#include "quark/Logger.hpp"
#include "quark/events/ApplicationEvent.hpp"
@ -6,7 +9,6 @@
#include "quark/events/MouseEvent.hpp"
#include <GLFW/glfw3.h>
#include <glad/gl.h>
namespace qk {
static bool GLFWInitialized = false;
@ -32,6 +34,7 @@ namespace qk {
data.width = config.width;
data.height = config.height;
QK_CORE_INFO("Creating window {0} ({1}x{2})", data.title, data.width, data.height);
if (!GLFWInitialized) {
@ -47,12 +50,9 @@ namespace qk {
glfwWindowHintString(GLFW_X11_CLASS_NAME, "testclass");
window = glfwCreateWindow((int)config.width, (int)config.height, config.title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(window);
int version = gladLoadGL(glfwGetProcAddress);
QK_CORE_ASSERT(version, "Failed to load OpenGL");
QK_CORE_INFO("Loaded OpenGL {0}.{1}", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
context = std::make_unique<OpenGLContext>(window);
context->Init();
glfwSetWindowUserPointer(window, &data);
@ -145,7 +145,7 @@ namespace qk {
void LinuxWindow::OnUpdate() {
glfwPollEvents();
glfwSwapBuffers(window);
context->SwapBuffers();
}
constexpr void LinuxWindow::SetVSync(bool enabled) {

View file

@ -2,7 +2,9 @@
#include <GLFW/glfw3.h>
#include <any>
#include <memory>
#include "quark/Window.hpp"
#include "quark/photon/RenderingContext.hpp"
namespace qk {
class LinuxWindow :
@ -33,6 +35,7 @@ namespace qk {
private:
GLFWwindow* window;
std::unique_ptr<RenderingContext> context;
struct WindowData {
std::string title;

View file

@ -0,0 +1,27 @@
#include "OpenGLContext.hpp"
#include "quark/Core.hpp"
#include <GLFW/glfw3.h>
#include <glad/gl.h>
namespace qk {
OpenGLContext::OpenGLContext(GLFWwindow* window) :
window(window)
{
}
void OpenGLContext::Init() {
glfwMakeContextCurrent(window);
int version = gladLoadGL(glfwGetProcAddress);
QK_CORE_ASSERT(version, "Failed to load OpenGL");
QK_CORE_INFO("Set up OpenGL context: {0}.{1}", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
}
void OpenGLContext::SwapBuffers() {
glfwSwapBuffers(window);
}
}

View file

@ -0,0 +1,20 @@
#pragma once
#include "quark/photon/RenderingContext.hpp"
struct GLFWwindow;
namespace qk {
class OpenGLContext :
public RenderingContext
{
public:
OpenGLContext(GLFWwindow* window);
virtual void Init() override;
virtual void SwapBuffers() override;
private:
GLFWwindow* window;
};
}

View file

@ -39,7 +39,7 @@ namespace qk {
}
void Application::OnEvent(Event& e) {
QK_CORE_TRACE(e);
// QK_CORE_TRACE(e);
EventDispatcher dispatcher(e);
dispatcher.Dispatch<WindowCloseEvent>(std::bind(&Application::OnWindowClose, this, std::placeholders::_1));

View file

@ -1,7 +1,6 @@
#include "ImGuiLayer.hpp"
// TODO: Remove these, these are temporary
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <imgui.h>

View file

@ -0,0 +1,11 @@
#pragma once
namespace qk {
class RenderingContext {
public:
virtual ~RenderingContext() { }
virtual void Init() = 0;
virtual void SwapBuffers() = 0;
};
}

View file

@ -2,18 +2,15 @@
# GLAD
########################
add_library(glad
glad/src/gl.c
)
if (${QUARK_RENDERING_BACKEND} STREQUAL "OPENGL")
add_library(glad
glad/src/gl.c
)
target_include_directories(glad
PUBLIC glad/include
)
set_property(
TARGET glad
PROPERTY POSITION_INDEPENDENT_CODE ON
)
target_include_directories(glad
PUBLIC glad/include
)
endif()
########################
# ImGui
@ -27,19 +24,19 @@ add_library(imgui
imgui/imgui_demo.cpp
# TODO: this is shit
imgui/backends/imgui_impl_opengl3.cpp
imgui/backends/imgui_impl_glfw.cpp
)
if (${QUARK_RENDERING_BACKEND} STREQUAL "OPENGL")
target_sources(imgui PRIVATE imgui/backends/imgui_impl_opengl3.cpp)
elseif (${QUARK_RENDERING_BACKEND} STREQUAL "VULKAN")
target_sources(imgui PRIVATE imgui/backends/imgui_impl_vulkan.cpp)
endif()
target_include_directories(imgui
PUBLIC imgui
)
set_property(
TARGET imgui
PROPERTY POSITION_INDEPENDENT_CODE ON
)
########################
# GLM
########################

View file

@ -30,7 +30,7 @@ public:
}
void OnEvent(qk::Event& event) override {
QK_TRACE(event);
// QK_TRACE(event);
}
};