vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
window.cpp
Go to the documentation of this file.
1#include <stdexcept>
2
3#define STB_IMAGE_IMPLEMENTATION
4#include "stb_image.h"
5
7
8GLFWwindow* ven::Window::createWindow(const uint32_t width, const uint32_t height, const std::string &title)
9{
10 if (glfwInit() == GLFW_FALSE) {
11 throw std::runtime_error("Failed to initialize GLFW");
12 }
13
14 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
15 glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
16
17 GLFWwindow *window = glfwCreateWindow(static_cast<int>(width), static_cast<int>(height), title.c_str(), nullptr, nullptr);
18 if (window == nullptr) {
19 glfwTerminate();
20 throw std::runtime_error("Failed to create window");
21 }
22 glfwSetWindowUserPointer(window, this);
23 glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
24 return window;
25}
26
27void ven::Window::createWindowSurface(const VkInstance instance, VkSurfaceKHR *surface) const
28{
29 if (glfwCreateWindowSurface(instance, m_window, nullptr, surface) != VK_SUCCESS) {
30 throw std::runtime_error("Failed to create window surface");
31 }
32}
33
34void ven::Window::framebufferResizeCallback(GLFWwindow *window, const int width, const int height)
35{
36 auto *app = static_cast<Window *>(glfwGetWindowUserPointer(window));
37 app->m_framebufferResized = true;
38 app->m_width = static_cast<uint32_t>(width);
39 app->m_height = static_cast<uint32_t>(height);
40}
41
42void ven::Window::setFullscreen(const bool fullscreen, const uint32_t width, const uint32_t height)
43{
44 GLFWmonitor* primaryMonitor = glfwGetPrimaryMonitor();
45 const GLFWvidmode* mode = glfwGetVideoMode(primaryMonitor);
46
47 /*
48 if (fullscreen) {
49 glfwSetWindowMonitor(m_window, primaryMonitor, 0, 0, mode->width, mode->height, mode->refreshRate);
50 } else {
51 // To restore a window that was originally windowed to its original size and position,
52 // save these before making it full screen and then pass them in as above
53 glfwSetWindowMonitor(m_window, nullptr, 0, 0, static_cast<int>(width), static_cast<int>(height), mode->refreshRate);
54
55 }
56
57 m_width = width;
58 m_height = height;
59 */
60}
61
62void ven::Window::setWindowIcon(const std::string &path)
63{
64 int width, height, channels;
65
66 if (unsigned char *pixels = stbi_load(path.c_str(), &width, &height, &channels, 4)) {
67 GLFWimage icon;
68 icon.width = width;
69 icon.height = height;
70 icon.pixels = pixels;
71
72 glfwSetWindowIcon(m_window, 1, &icon);
73 stbi_image_free(pixels);
74 } else {
75 throw std::runtime_error("Failed to load window icon with path: " + path);
76 }
77}
This file contains the Window class.
Class for window.
Definition Window.hpp:26
void createWindowSurface(VkInstance instance, VkSurfaceKHR *surface) const
Definition window.cpp:27
GLFWwindow * createWindow(uint32_t width, uint32_t height, const std::string &title)
Definition window.cpp:8
static void framebufferResizeCallback(GLFWwindow *window, int width, int height)
Definition window.cpp:34
bool m_framebufferResized
Definition Window.hpp:58
void setFullscreen(bool fullscreen, uint32_t width, uint32_t height)
Definition window.cpp:42
void setWindowIcon(const std::string &path)
Definition window.cpp:62