WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
AppWindow.cpp
Go to the documentation of this file.
1#include "AppWindow.h"
2
3#ifdef _WIN32
4#include <windows.h>
5#endif
6
7#include <cstdio>
8#include <filesystem>
9
10#include <glad/gl.h>
11#include <GLFW/glfw3.h>
12
13#include "stb_image.h"
14
15// ---- Static error callback ------------------------------------------------
16
17void AppWindow::errorCallback(int error, const char* description)
18{
19 fprintf(stderr, "GLFW Error %d: %s\n", error, description);
20}
21
22// ---- Lifecycle ------------------------------------------------------------
23
25{
26 if (m_window)
27 {
28 glfwDestroyWindow(m_window);
29 m_window = nullptr;
30 }
31 glfwTerminate();
32}
33
34bool AppWindow::init(int width, int height, const char* title)
35{
36 glfwSetErrorCallback(errorCallback);
37 if (!glfwInit())
38 return false;
39
40 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
41 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
42 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
43 glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
44
45 m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
46 if (!m_window)
47 {
48 glfwTerminate();
49 return false;
50 }
51
52 glfwMakeContextCurrent(m_window);
53 glfwSwapInterval(1);
54
55 // Load OpenGL function pointers via glad
56 if (!gladLoadGL(glfwGetProcAddress))
57 {
58 fprintf(stderr, "Failed to initialise OpenGL loader (glad)\n");
59 glfwDestroyWindow(m_window);
60 m_window = nullptr;
61 glfwTerminate();
62 return false;
63 }
64
65 return true;
66}
67
68// ---- Icon -----------------------------------------------------------------
69
70void AppWindow::setIcon(const char* fallbackPath)
71{
72 int iw = 0, ih = 0, ic = 0;
73 unsigned char* px = nullptr;
74
75#ifdef _WIN32
76 {
77 wchar_t exePath[MAX_PATH]{};
78 GetModuleFileNameW(nullptr, exePath, MAX_PATH);
79 auto iconPath = std::filesystem::path(exePath).parent_path() / "wmv_16.png";
80 px = stbi_load(iconPath.string().c_str(), &iw, &ih, &ic, 4);
81 }
82#endif
83
84 if (!px && fallbackPath)
85 px = stbi_load(fallbackPath, &iw, &ih, &ic, 4);
86
87 if (px)
88 {
89 GLFWimage img{ iw, ih, px };
90 glfwSetWindowIcon(m_window, 1, &img);
91 stbi_image_free(px);
92 }
93}
94
95// ---- DPI ------------------------------------------------------------------
96
98{
99 float xscale = 1.0f, yscale = 1.0f;
100 if (m_window)
101 glfwGetWindowContentScale(m_window, &xscale, &yscale);
102 return (xscale > yscale) ? xscale : yscale;
103}
104
105// ---- Per-frame helpers ----------------------------------------------------
106
108{
109 glfwPollEvents();
110}
111
113{
114 glfwSwapBuffers(m_window);
115}
116
118{
119 return m_window && glfwWindowShouldClose(m_window);
120}
121
123{
124 if (m_window)
125 glfwSetWindowShouldClose(m_window, GLFW_TRUE);
126}
127
128void AppWindow::framebufferSize(int& w, int& h) const
129{
130 if (m_window)
131 glfwGetFramebufferSize(m_window, &w, &h);
132 else
133 {
134 w = 0;
135 h = 0;
136 }
137}
void pollEvents()
Poll platform events (must be called once per frame).
void setIcon(const char *fallbackPath)
Load and set the window icon from a PNG file.
Definition AppWindow.cpp:70
float queryDpiScale() const
Query the monitor content-scale (DPI).
Definition AppWindow.cpp:97
void requestClose()
Signal the window to close.
static void errorCallback(int error, const char *description)
Definition AppWindow.cpp:17
void framebufferSize(int &w, int &h) const
Retrieve the framebuffer size in pixels.
bool init(int width, int height, const char *title)
Definition AppWindow.cpp:34
GLFWwindow * m_window
Definition AppWindow.h:50
void swapBuffers()
Swap the front/back framebuffers.
bool shouldClose() const
Returns true when the user (or code) has requested the window to close.