1 | #include "ZydecoCommon.hpp" |
2 | #include "CommonSDL2.hpp" |
3 | #include "WindowSDL2.hpp" |
4 |
|
5 |
|
6 | static Logger LOGGER("WINDOW"); |
7 |
|
8 |
|
9 | WindowSDL2::WindowSDL2(std::string title, uint64_t window_config_flags): |
10 | m_windowTitle(title) |
11 | { |
12 | LOGGER.Log(Logger::DEBUG, "WindowSDL2 creating ('{}')", title); |
13 |
|
14 | SDL_CallPointerReturningFunction(SDL_CreateWindow, m_pSdlWindow, |
15 | m_windowTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, window_config_flags); |
16 |
|
17 | SDL_CallPointerReturningFunction(SDL_GL_CreateContext, m_glContextMain, m_pSdlWindow); |
18 |
|
19 | SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_ACCELERATED_VISUAL, 1); |
20 | SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
21 | SDL_CallErrorReturningFunction(SDL_GL_SetSwapInterval, 1); |
22 |
|
23 | SDL_ShowWindow(m_pSdlWindow); |
24 |
|
25 | LOGGER.Log(Logger::DEBUG, "WindowSDL2 created", title); |
26 | } |
27 |
|
28 | WindowSDL2::~WindowSDL2() |
29 | { |
30 | LOGGER.Log(Logger::DEBUG, "Destroying window '{}'", m_windowTitle); |
31 | SDL_GL_DeleteContext(m_glContextRender); |
32 | SDL_GL_DeleteContext(m_glContextMain); |
33 | SDL_DestroyWindow(m_pSdlWindow); |
34 | } |
35 |
|
36 | void WindowSDL2::MakeContextCurrent() |
37 | { |
38 | SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, m_glContextMain); |
39 | } |
40 |
|
41 | void WindowSDL2::MakeNullCurrent() |
42 | { |
43 | SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, nullptr); |
44 | } |
45 |
|
46 | bool WindowSDL2::Update(uint64_t time_since_last_update_us) |
47 | { |
48 | LOGGER.Log(Logger::TRACE, "Refreshing SDL window {} {}", SDL_GL_GetSwapInterval(), time_since_last_update_us); |
49 |
|
50 | SDL_GL_SwapWindow(m_pSdlWindow); |
51 |
|
52 | SDL_Delay(1); |
53 |
|
54 | return false; |
55 | } |
56 |
|
57 | void WindowSDL2::SetTitle(std::string new_title) |
58 | { |
59 | m_windowTitle = new_title; |
60 | SDL_SetWindowTitle(m_pSdlWindow, m_windowTitle.c_str()); |
61 | } |
62 |
|
63 | void WindowSDL2::SetFullscreen(bool is_fullscreen) |
64 | { |
65 | SDL_CallErrorReturningFunction(SDL_SetWindowFullscreen, m_pSdlWindow, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); |
66 | } |
67 |
|
68 | void WindowSDL2::SetSize(uint64_t new_width, uint64_t new_height) |
69 | { |
70 | SDL_SetWindowSize(m_pSdlWindow, new_width, new_height); |
71 | } |
72 |
|
73 | void WindowSDL2::SetPosition(uint64_t new_x, uint64_t new_y) |
74 | { |
75 | SDL_SetWindowPosition(m_pSdlWindow, new_x, new_y); |
76 | } |
77 |
|