1 | #include <SDL2/SDL.h> |
2 | #include "ZydecoCommon.hpp" |
3 | #include "Window.hpp" |
4 | #include <GL/gl.h> |
5 |
|
6 | static Logger LOGGER("WINDOW"); |
7 |
|
8 | #define SDL_CallErrorReturningFunction(FUNC_NAME, PARAMS...) \ |
9 | if (FUNC_NAME(PARAMS) < 0) \ |
10 | { \ |
11 | std::string fault_message = fmt::format(#FUNC_NAME " error: {}", SDL_GetError()); \ |
12 | ZydecoFault(fault_message); \ |
13 | } |
14 |
|
15 | #define SDL_CallPointerReturningFunction(FUNC_NAME, NEW_PTR, PARAMS...) \ |
16 | NEW_PTR = FUNC_NAME(PARAMS); \ |
17 | if (NEW_PTR == nullptr) \ |
18 | { \ |
19 | std::string fault_message = fmt::format(#FUNC_NAME " error: {}", SDL_GetError()); \ |
20 | ZydecoFault(fault_message); \ |
21 | } |
22 |
|
23 | // Static initialize |
24 | bool Window::s_sdlInitialized = false; |
25 |
|
26 | Window::Window(std::string title, uint64_t sdl_window_flags): |
27 | m_windowTitle(title) |
28 | { |
29 | _SdlInitialize(); |
30 |
|
31 | LOGGER.Log(Logger::INFO, fmt::format("SDL Creating OpenGL Window (\"{}\")", title)); |
32 |
|
33 | SDL_CallPointerReturningFunction(SDL_CreateWindow, m_pSdlWindow, |
34 | m_windowTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, sdl_window_flags); |
35 |
|
36 | SDL_CallPointerReturningFunction(SDL_CreateRenderer, m_pSdlRenderer, |
37 | m_pSdlWindow, -1, SDL_RENDERER_ACCELERATED); |
38 | |
39 | SDL_CallPointerReturningFunction(SDL_GL_CreateContext, m_pGlContext, m_pSdlWindow); |
40 |
|
41 | Update(0); |
42 |
|
43 | SDL_ShowWindow(m_pSdlWindow); |
44 |
|
45 | SDL_CallErrorReturningFunction(SDL_GL_SetSwapInterval, 1); |
46 | } |
47 |
|
48 | Window::~Window() |
49 | { |
50 | LOGGER.Log(Logger::DEBUG, fmt::format("Destroying window {}", m_windowTitle)); |
51 | SDL_GL_DeleteContext(m_pGlContext); |
52 | SDL_DestroyRenderer(m_pSdlRenderer); |
53 | SDL_DestroyWindow(m_pSdlWindow); |
54 | } |
55 |
|
56 | bool Window::Update(uint64_t time_since_last_update_us) |
57 | { |
58 | // Update SDL renderer display |
59 | SDL_CallErrorReturningFunction(SDL_RenderClear, m_pSdlRenderer); |
60 |
|
61 | // Update OpenGL context display |
62 | glClearColor(0.18, 0.1, 0.18, 1.); |
63 | SDL_GL_SwapWindow(m_pSdlWindow); |
64 |
|
65 | SDL_RenderPresent(m_pSdlRenderer); |
66 |
|
67 | return HandleEvents(); |
68 | } |
69 |
|
70 | void Window::SetTitle(std::string new_title) |
71 | { |
72 | m_windowTitle = new_title; |
73 | SDL_SetWindowTitle(m_pSdlWindow, m_windowTitle.c_str()); |
74 | } |
75 |
|
76 | void Window::SetFullscreen(bool is_fullscreen) |
77 | { |
78 | SDL_CallErrorReturningFunction(SDL_SetWindowFullscreen, m_pSdlWindow, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); |
79 | } |
80 |
|
81 | void Window::SetSize(uint64_t new_width, uint64_t new_height) |
82 | { |
83 | SDL_SetWindowSize(m_pSdlWindow, new_width, new_height); |
84 | } |
85 |
|
86 | void Window::SetPosition(uint64_t new_x, uint64_t new_y) |
87 | { |
88 | SDL_SetWindowPosition(m_pSdlWindow, new_x, new_y); |
89 | } |
90 |
|
91 | bool Window::HandleEvents() |
92 | { |
93 | SDL_Event event; |
94 | while (SDL_PollEvent(&event)) |
95 | { |
96 | LOGGER.Log(Logger::TRACE, fmt::format("Received event: {}", event.type)); |
97 | switch (event.type) |
98 | { |
99 | case SDL_QUIT: |
100 | LOGGER.Log(Logger::DEBUG, fmt::format("Received SDL_QUIT event")); |
101 | return false; |
102 | } |
103 | } |
104 | return true; |
105 | } |
106 |
|
107 | void Window::_SdlInitialize() |
108 | { |
109 | if (s_sdlInitialized) { return; } |
110 |
|
111 | LOGGER.Log(Logger::INFO, "SDL Init"); |
112 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) |
113 | { |
114 | std::string fault_message = fmt::format("SDL_Init error: {}", SDL_GetError()); |
115 | ZydecoFault(fault_message); |
116 | } |
117 |
|
118 | s_sdlInitialized = true; |
119 | } |
120 |
|