minecraftvulkan/engine/xe_window.cpp

47 lines
1.4 KiB
C++
Raw Normal View History

2022-09-19 01:20:51 +00:00
#include "xe_window.hpp"
2022-09-27 01:24:28 +00:00
#include "stb_image.h"
2022-09-19 01:20:51 +00:00
namespace xe {
2022-09-27 01:24:28 +00:00
Window::Window(int w, int h, std::string name, const char *icon) : width{w}, height{h}, windowName{name} {
2022-09-19 01:20:51 +00:00
initWindow();
2022-09-27 01:24:28 +00:00
setIcon(icon);
2022-09-19 01:20:51 +00:00
}
2022-09-25 01:16:13 +00:00
Window::~Window() {
2022-09-19 01:20:51 +00:00
glfwDestroyWindow(window);
glfwTerminate();
}
2022-09-25 01:16:13 +00:00
void Window::initWindow() {
2022-09-19 01:20:51 +00:00
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
glfwSetWindowUserPointer(window, this);
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
}
2022-09-27 01:24:28 +00:00
void Window::setIcon(const char *icon) {
if(icon == NULL) return;
GLFWimage images[1];
images[0].pixels = stbi_load(icon, &images[0].width, &images[0].height, 0, 4); //rgba channels
glfwSetWindowIcon(window, 1, images);
stbi_image_free(images[0].pixels);
}
2022-09-25 01:16:13 +00:00
void Window::createWindowSurface(VkInstance instance, VkSurfaceKHR *surface){
2022-09-19 01:20:51 +00:00
if (glfwCreateWindowSurface(instance, window, nullptr, surface) != VK_SUCCESS) {
throw std::runtime_error("failed to create window surface");
}
}
2022-09-25 01:16:13 +00:00
void Window::framebufferResizeCallback(GLFWwindow *window, int width, int height){
auto xeWindow = reinterpret_cast<Window *>(glfwGetWindowUserPointer(window));
2022-09-19 01:20:51 +00:00
xeWindow->frameBufferResized = true;
xeWindow->width = width;
xeWindow->height = height;
}
}