| 1 | #include "ZydecoCommon.hpp" |
| 2 | #include "CommonSDL2.hpp" |
| 3 | #include "WindowSDL2.hpp" |
| 4 |
|
| 5 |
|
| 6 | static Logger LOGGER("WINDOW"); |
| 7 |
|
| 8 |
|
| 9 | // Static initialize |
| 10 | bool WindowSDL2::s_sdlInitialized = false; |
| 11 |
|
| 12 |
|
| 13 | WindowSDL2::WindowSDL2(std::string title, uint64_t window_config_flags): |
| 14 | m_windowTitle(title) |
| 15 | { |
| 16 | _SdlInitialize(); |
| 17 |
|
| 18 | LOGGER.Log(Logger::INFO, "SDL Creating OpenGL Window ('{}')", title); |
| 19 |
|
| 20 | SDL_CallPointerReturningFunction(SDL_CreateWindow, m_pSdlWindow, |
| 21 | m_windowTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, window_config_flags); |
| 22 |
|
| 23 | SDL_CallPointerReturningFunction(SDL_CreateRenderer, m_pSdlRenderer, |
| 24 | m_pSdlWindow, -1, SDL_RENDERER_ACCELERATED); |
| 25 | |
| 26 | SDL_CallPointerReturningFunction(SDL_GL_CreateContext, m_pGlContext, m_pSdlWindow); |
| 27 |
|
| 28 | SDL_ShowWindow(m_pSdlWindow); |
| 29 | } |
| 30 |
|
| 31 | WindowSDL2::~WindowSDL2() |
| 32 | { |
| 33 | LOGGER.Log(Logger::DEBUG, "Destroying window {}", m_windowTitle); |
| 34 | SDL_GL_DeleteContext(m_pGlContext); |
| 35 | SDL_DestroyRenderer(m_pSdlRenderer); |
| 36 | SDL_DestroyWindow(m_pSdlWindow); |
| 37 | } |
| 38 |
|
| 39 | bool WindowSDL2::Update(uint64_t time_since_last_update_us) |
| 40 | { |
| 41 | LOGGER.Log(Logger::TRACE, "Refreshing SDL window {}", time_since_last_update_us); |
| 42 |
|
| 43 | // Update SDL renderer display |
| 44 | SDL_CallErrorReturningFunction(SDL_RenderClear, m_pSdlRenderer); |
| 45 |
|
| 46 | // Update OpenGL context display |
| 47 | SDL_GL_SwapWindow(m_pSdlWindow); |
| 48 |
|
| 49 | SDL_RenderPresent(m_pSdlRenderer); |
| 50 |
|
| 51 | return false; |
| 52 | } |
| 53 |
|
| 54 | void WindowSDL2::SetTitle(std::string new_title) |
| 55 | { |
| 56 | m_windowTitle = new_title; |
| 57 | SDL_SetWindowTitle(m_pSdlWindow, m_windowTitle.c_str()); |
| 58 | } |
| 59 |
|
| 60 | void WindowSDL2::SetFullscreen(bool is_fullscreen) |
| 61 | { |
| 62 | SDL_CallErrorReturningFunction(SDL_SetWindowFullscreen, m_pSdlWindow, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); |
| 63 | } |
| 64 |
|
| 65 | void WindowSDL2::SetSize(uint64_t new_width, uint64_t new_height) |
| 66 | { |
| 67 | SDL_SetWindowSize(m_pSdlWindow, new_width, new_height); |
| 68 | } |
| 69 |
|
| 70 | void WindowSDL2::SetPosition(uint64_t new_x, uint64_t new_y) |
| 71 | { |
| 72 | SDL_SetWindowPosition(m_pSdlWindow, new_x, new_y); |
| 73 | } |
| 74 |
|
| 75 | void WindowSDL2::_SdlInitialize() |
| 76 | { |
| 77 | if (s_sdlInitialized) { return; } |
| 78 |
|
| 79 | LOGGER.Log(Logger::INFO, "SDL Init"); |
| 80 | SDL_CallErrorReturningFunction(SDL_Init, SDL_INIT_VIDEO | SDL_INIT_EVENTS); |
| 81 |
|
| 82 | s_sdlInitialized = true; |
| 83 | } |
| 84 |
|