Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_LinuxWindow.cpp
1#include <zen/events/ZEN_Event.h>
2#include <zen/platform/linux/ZEN_LinuxWindow.h>
3
4namespace Zen {
5 static bool s_SDLInitialized = false;
6
7 LinuxWindow::LinuxWindow(const WindowProperties &properties) { init(properties); };
8
9 LinuxWindow::~LinuxWindow() { shutdown(); };
10
11 // TEMP MUST CHANGE
12 WindowData &LinuxWindow::getWindowData() { return m_windowData; };
13
14 WindowProperties &LinuxWindow::getProperties() { return m_windowProperties; };
15
16 void LinuxWindow::init(const WindowProperties &properties) {
17 m_windowProperties.title = properties.title;
18 m_windowProperties.width = properties.width;
19 m_windowProperties.height = properties.height;
20
21 ZEN_LOG_INFO("Creating new SDL Window: {}, W:{} H: {}",
22 m_windowProperties.title,
23 m_windowProperties.width,
24 m_windowProperties.height);
25 if (!s_SDLInitialized) {
26 bool success = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
27 if (!success) {
28 const char *error = SDL_GetError();
29 emitErrorMessage(error);
30 ZEN_LOG_ERROR("SDL was not initialized properly: {}", error);
31 };
32
33 ZEN_LOG_DEBUG("SDL Successfully Initialized!");
34 s_SDLInitialized = true;
35 };
36
37 constexpr SDL_WindowFlags flags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS |
38 SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_MOUSE_CAPTURE |
39 SDL_WINDOW_RESIZABLE;
40
41 // UNCOMMENT BELOW TO FORCE ERROR
42 // SDL_WindowFlags flags;
43
44 m_windowData.window = SDL_CreateWindow(m_windowProperties.title.c_str(),
45 m_windowProperties.width,
46 m_windowProperties.height,
47 flags);
48 if (m_windowData.window == nullptr) {
49 const char *error = SDL_GetError();
50 emitErrorMessage(error);
51 ZEN_LOG_ERROR("The SDL Window could not be initialized. {}", error);
52 };
53
54 m_windowData.context = GraphicsContext::Create(m_windowData.window);
55 m_windowData.context->init();
56 if (m_windowData.context == nullptr) {
57 const char *error = SDL_GetError();
58 emitErrorMessage(error);
59 ZEN_LOG_ERROR("The SDL OpenGL context could not be initialized: {}", error);
60 };
61
62 gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress);
63
64 setVSync(m_windowProperties.vsync);
65
66 ZEN_LOG_DEBUG("Window Successfully Initialized!");
67 };
68
69 bool LinuxWindow::onEvent(const ZenEvent &event) {
70 if (event.header.type == EventType::WindowResize) {
71 return resizeEvent(event);
72 }
73
74 if (event.header.type == EventType::MouseButtonPressed) {
75 return mouseClickEvent(event);
76 }
77
78 return false;
79 }
80
81 void *LinuxWindow::nativeWindow() const { return m_windowData.window; }
82
83 GraphicsContext &LinuxWindow::context() { return *m_windowData.context; }
84
85 const GraphicsContext &LinuxWindow::context() const { return *m_windowData.context; };
86 void LinuxWindow::shutdown() {
87 m_windowData.context->shutdown();
88 SDL_DestroyWindow(m_windowData.window);
89 ZEN_LOG_DEBUG("SDL Window destroyed");
90 SDL_Quit();
91 ZEN_LOG_DEBUG("SDL Quit");
92 };
93
94 void LinuxWindow::onUpdate() { m_windowData.context->swapBuffers(); };
95
96 uint32_t LinuxWindow::getWidth() { return m_windowProperties.width; };
97
98 uint32_t LinuxWindow::getHeight() { return m_windowProperties.height; };
99
100 void LinuxWindow::setVSync(bool enabled) {
101 if (enabled) {
102 SDL_GL_SetSwapInterval(1);
103 ZEN_LOG_INFO("VSync On");
104
105 } else {
106 SDL_GL_SetSwapInterval(0);
107 ZEN_LOG_INFO("VSync Off");
108 };
109 };
110
111 bool LinuxWindow::isVSyncEnabled() const { return m_windowProperties.vsync; };
112
113 void LinuxWindow::toggleFullscreen() {
114 m_windowProperties.fullscreen = !m_windowProperties.fullscreen;
115 SDL_SetWindowFullscreen(m_windowData.window, m_windowProperties.fullscreen);
116 };
117
118 // This should ONLY BE CALLED ON THE MAIN THREAD
119 void LinuxWindow::emitErrorMessage(const char *message) {
120 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Zen Error", message, nullptr);
121 };
122
123 bool LinuxWindow::resizeEvent(const ZenEvent &event) {
124
125 int newWidth = event.windowResize.width;
126 int newHeight = event.windowResize.height;
127
128 ZEN_LOG_TRACE("Window Width: {}", newWidth);
129 ZEN_LOG_TRACE("Window Height: {}", newHeight);
130
131 m_windowProperties.width = newWidth;
132 m_windowProperties.height = newHeight;
133
134 ZEN_LOG_TRACE("New WinProp Width: {}", m_windowProperties.width);
135 ZEN_LOG_TRACE("New WinProp Height: {}", m_windowProperties.height);
136
137 return false;
138 };
139
140 bool LinuxWindow::mouseClickEvent(const ZenEvent &event) {
141 ZEN_LOG_TRACE("Mouse click at X: {}, Y: {}", event.mouseButton.x, event.mouseButton.y);
142 return false;
143 };
144
145 int LinuxWindow::getPriority() const { return 99; }
146}; // namespace Zen