copying code over from alphane engine

This commit is contained in:
2026-08-14 06:09:53 +00:00
parent 108341158e
commit 263b6da528
13 changed files with 1203 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
#include "window.hpp"
#include "key_event.hpp"
#include "util/logger.hpp"
namespace hep {
Window::Window(int width, int height, const std::string& name)
: width{width}, height{height}, name{name} {
initialize();
}
Window::~Window() {
glfwDestroyWindow(window);
glfwTerminate();
}
void Window::createSurface(const vk::Instance& instance,
vk::SurfaceKHR& surface) {
VkSurfaceKHR rawSurface;
if (glfwCreateWindowSurface(static_cast<VkInstance>(instance), window,
nullptr, &rawSurface) != VK_SUCCESS) {
log::fatal("Failed to create vk::SurfaceKHR.");
throw std::exception();
}
surface = vk::SurfaceKHR(rawSurface);
log::trace("Created vk::SurfaceKHR.");
}
void Window::keyEventCallback(GLFWwindow* window,
int key,
int scancode,
int action,
int mods) {
if (action == GLFW_PRESS) {
KeyPressedEvent event{static_cast<KeyCode>(key)};
EventSystem::get().dispatch(event);
} else if (action == GLFW_RELEASE) {
KeyReleasedEvent event{static_cast<KeyCode>(key)};
EventSystem::get().dispatch(event);
}
}
void Window::resizeCallback(GLFWwindow* window, int width, int height) {
auto newWindow = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
newWindow->resized = true;
newWindow->width = width;
newWindow->height = height;
}
void Window::initialize() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
this->window =
glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr);
glfwSetWindowUserPointer(this->window, this);
glfwSetFramebufferSizeCallback(this->window, resizeCallback);
glfwSetKeyCallback(this->window, keyEventCallback);
}
} // namespace hep