Add RenderEngine and create Vulkan instance

This commit is contained in:
Grayson Riffe 2025-02-04 13:30:06 -06:00
parent 474b096337
commit ece828d045
8 changed files with 74 additions and 8 deletions

View File

@ -1,5 +1,5 @@
# NF library CMakeLists.txt
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp")
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp" "src/client/render/RenderEngine.h" "src/client/render/RenderEngine.cpp")
# Use C++20
set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20)
@ -10,6 +10,10 @@ target_include_directories(NothinFancy PUBLIC "src" "src/include")
# Use precompiled header
target_precompile_headers(NothinFancy PUBLIC "src/pch.h")
# Link libraries
target_link_libraries(NothinFancy "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib")
target_include_directories(NothinFancy PUBLIC "$ENV{VULKAN_SDK}/Include")
# Generate version.h
find_package(Git)
execute_process(COMMAND ${GIT_EXECUTABLE} describe OUTPUT_VARIABLE NFVERSION OUTPUT_STRIP_TRAILING_WHITESPACE)

View File

@ -7,6 +7,7 @@ namespace nf::cli {
Client::Client(ClientConfig config)
: m_config(config)
, m_window(m_config.appName, m_config.display)
, m_renderEngine()
{
// Setup window and renderer
}

View File

@ -3,6 +3,7 @@
#include "nf/config.h"
#include "Window.h"
#include "render/RenderEngine.h"
namespace nf::cli {
class Client {
@ -13,5 +14,6 @@ namespace nf::cli {
private:
ClientConfig m_config;
Window m_window;
render::RenderEngine m_renderEngine;
};
}

View File

@ -17,12 +17,7 @@ namespace nf::cli {
// Disable automatic DPI upscaling
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// Register window class
WNDCLASS wc = {};
wc.lpszClassName = m_wndClassName;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.lpfnWndProc = wndProc;
RegisterClass(&wc);
registerWindowClass();
m_handle = CreateWindow(m_wndClassName, windowTitle, NULL, 0, 0, 0, 0, nullptr, nullptr, nullptr, this);
setDisplay(config);
@ -34,7 +29,7 @@ namespace nf::cli {
bool wasShown = IsWindowVisible(m_handle);
show(false);
// Temporary monitor choice
// TODO: Only use "active" monitor when starting windowed
POINT cursor = {};
GetCursorPos(&cursor);
MONITORINFO mi = {};
@ -80,6 +75,14 @@ namespace nf::cli {
ShowWindow(m_handle, show ? SW_SHOW : SW_HIDE);
}
void Window::registerWindowClass() {
WNDCLASS wc = {};
wc.lpszClassName = m_wndClassName;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.lpfnWndProc = wndProc;
RegisterClass(&wc);
}
LRESULT CALLBACK Window::wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static Window* wnd = nullptr;

View File

@ -14,6 +14,7 @@ namespace nf::cli {
void runLoop();
void show(bool show = true);
private:
void registerWindowClass();
static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
SIZE getWindowSize();

View File

@ -0,0 +1,36 @@
// RenderEngine class implementation
#include "pch.h"
#include "RenderEngine.h"
#include "util.h"
namespace nf::cli::render {
RenderEngine::RenderEngine()
: m_instance()
{
NFLog("Initializing render engine");
createInstance();
}
void RenderEngine::createInstance() {
VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
appInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo instanceCI = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
instanceCI.pApplicationInfo = &appInfo;
#ifdef _DEBUG
const char* validationLayerName = "VK_LAYER_KHRONOS_validation";
instanceCI.ppEnabledLayerNames = &validationLayerName;
instanceCI.enabledLayerCount = 1;
#endif
std::vector<const char*> instanceExtNames = { VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME };
instanceCI.ppEnabledExtensionNames = instanceExtNames.data();
instanceCI.enabledExtensionCount = instanceExtNames.size();
if (vkCreateInstance(&instanceCI, nullptr, &m_instance) != VK_SUCCESS)
NFError("Could not create Vulkan instance.");
}
RenderEngine::~RenderEngine() {
vkDestroyInstance(m_instance, nullptr);
}
}

View File

@ -0,0 +1,15 @@
// RenderEngine class header
#pragma once
namespace nf::cli::render {
class RenderEngine {
public:
RenderEngine();
~RenderEngine();
private:
void createInstance();
VkInstance m_instance;
};
}

View File

@ -37,3 +37,7 @@
// Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// Vulkan
#define VK_USE_PLATFORM_WIN32_KHR
#include <vulkan/vulkan.h>