1#include <zen/events/ZEN_Event.h>
2#include <zen/platform/linux/ZEN_LinuxWindow.h>
5 static bool s_SDLInitialized =
false;
7 LinuxWindow::LinuxWindow(
const WindowProperties &properties) { init(properties); };
9 LinuxWindow::~LinuxWindow() { shutdown(); };
12 WindowData &LinuxWindow::getWindowData() {
return m_windowData; };
14 WindowProperties &LinuxWindow::getProperties() {
return m_windowProperties; };
17 m_windowProperties.title = properties.title;
18 m_windowProperties.width = properties.width;
19 m_windowProperties.height = properties.height;
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);
28 const char *error = SDL_GetError();
29 emitErrorMessage(error);
30 ZEN_LOG_ERROR(
"SDL was not initialized properly: {}", error);
33 ZEN_LOG_DEBUG(
"SDL Successfully Initialized!");
34 s_SDLInitialized =
true;
37 constexpr SDL_WindowFlags flags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS |
38 SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_MOUSE_CAPTURE |
44 m_windowData.window = SDL_CreateWindow(m_windowProperties.title.c_str(),
45 m_windowProperties.width,
46 m_windowProperties.height,
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);
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);
62 gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress);
64 setVSync(m_windowProperties.vsync);
66 ZEN_LOG_DEBUG(
"Window Successfully Initialized!");
69 bool LinuxWindow::onEvent(
const ZenEvent &event) {
70 if (event.header.type == EventType::WindowResize) {
71 return resizeEvent(event);
74 if (event.header.type == EventType::MouseButtonPressed) {
75 return mouseClickEvent(event);
81 void *LinuxWindow::nativeWindow()
const {
return m_windowData.window; }
83 GraphicsContext &LinuxWindow::context() {
return *m_windowData.context; }
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");
91 ZEN_LOG_DEBUG(
"SDL Quit");
94 void LinuxWindow::onUpdate() { m_windowData.context->swapBuffers(); };
96 uint32_t LinuxWindow::getWidth() {
return m_windowProperties.width; };
98 uint32_t LinuxWindow::getHeight() {
return m_windowProperties.height; };
100 void LinuxWindow::setVSync(
bool enabled) {
102 SDL_GL_SetSwapInterval(1);
103 ZEN_LOG_INFO(
"VSync On");
106 SDL_GL_SetSwapInterval(0);
107 ZEN_LOG_INFO(
"VSync Off");
111 bool LinuxWindow::isVSyncEnabled()
const {
return m_windowProperties.vsync; };
113 void LinuxWindow::toggleFullscreen() {
114 m_windowProperties.fullscreen = !m_windowProperties.fullscreen;
115 SDL_SetWindowFullscreen(m_windowData.window, m_windowProperties.fullscreen);
119 void LinuxWindow::emitErrorMessage(
const char *message) {
120 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Zen Error", message,
nullptr);
123 bool LinuxWindow::resizeEvent(
const ZenEvent &event) {
125 int newWidth =
event.windowResize.width;
126 int newHeight =
event.windowResize.height;
128 ZEN_LOG_TRACE(
"Window Width: {}", newWidth);
129 ZEN_LOG_TRACE(
"Window Height: {}", newHeight);
131 m_windowProperties.width = newWidth;
132 m_windowProperties.height = newHeight;
134 ZEN_LOG_TRACE(
"New WinProp Width: {}", m_windowProperties.width);
135 ZEN_LOG_TRACE(
"New WinProp Height: {}", m_windowProperties.height);
140 bool LinuxWindow::mouseClickEvent(
const ZenEvent &event) {
141 ZEN_LOG_TRACE(
"Mouse click at X: {}, Y: {}", event.mouseButton.x, event.mouseButton.y);
145 int LinuxWindow::getPriority()
const {
return 99; }