1 | #include "ZydecoCommon.hpp" |
2 | #include "CommonSDL2.hpp" |
3 | #include "WindowSDL2.hpp" |
4 |
|
5 |
|
6 | static Logger LOGGER("WindowSDL2"); |
7 |
|
8 |
|
9 | WindowSDL2::WindowSDL2(std::string title, uint64_t window_config_flags): |
10 | m_windowTitle(title) |
11 | { |
12 | LOGGER.Log(Logger::TRACE, "WindowSDL2() for '{}'", 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 |
|
26 | WindowSDL2::~WindowSDL2() |
27 | { |
28 | LOGGER.Log(Logger::TRACE, "~WindowSDL2() for {}", m_windowTitle); |
29 | SDL_GL_DeleteContext(m_glContextRender); |
30 | SDL_GL_DeleteContext(m_glContextMain); |
31 | SDL_DestroyWindow(m_pSdlWindow); |
32 | } |
33 |
|
34 | void WindowSDL2::MakeContextCurrent() |
35 | { |
36 | SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, m_glContextMain); |
37 | } |
38 |
|
39 | void WindowSDL2::MakeNullCurrent() |
40 | { |
41 | LOGGER.Log(Logger::TRACE, "MakeNullCurrent() for '{}'", m_windowTitle); |
42 | SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, nullptr); |
43 | } |
44 |
|
45 | bool WindowSDL2::Update() |
46 | { |
47 | SDL_GL_SwapWindow(m_pSdlWindow); |
48 |
|
49 | SDL_Delay(1); |
50 |
|
51 | return false; |
52 | } |
53 |
|
54 | void WindowSDL2::SetTitle(std::string new_title) |
55 | { |
56 | LOGGER.Log(Logger::TRACE, "SetTitle({}) for '{}'", new_title, m_windowTitle); |
57 | m_windowTitle = new_title; |
58 | SDL_SetWindowTitle(m_pSdlWindow, m_windowTitle.c_str()); |
59 | } |
60 |
|
61 | void WindowSDL2::SetFullscreen(bool is_fullscreen) |
62 | { |
63 | LOGGER.Log(Logger::TRACE, "SetFullscreen({}) for '{}'", is_fullscreen, m_windowTitle); |
64 | SDL_CallErrorReturningFunction(SDL_SetWindowFullscreen, m_pSdlWindow, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); |
65 | } |
66 |
|
67 | void WindowSDL2::SetSize(uint64_t new_width, uint64_t new_height) |
68 | { |
69 | LOGGER.Log(Logger::TRACE, "SetSize(x: {}, y: {}) for '{}'", new_width, new_height, m_windowTitle); |
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 | LOGGER.Log(Logger::TRACE, "SetPosition(x: {}, y: {}) for '{}'", new_x, new_y, m_windowTitle); |
76 | SDL_SetWindowPosition(m_pSdlWindow, new_x, new_y); |
77 | } |
78 |
|
79 | int WindowSDL2::GetWidth() |
80 | { |
81 | LOGGER.Log(Logger::TRACE, "GetWidth() for '{}'", m_windowTitle); |
82 | int w; |
83 | SDL_GL_GetDrawableSize(m_pSdlWindow, &w, nullptr); |
84 | return w; |
85 | } |
86 |
|
87 | int WindowSDL2::GetHeight() |
88 | { |
89 | LOGGER.Log(Logger::TRACE, "GetHeight() for '{}'", m_windowTitle); |
90 | int h; |
91 | SDL_GL_GetDrawableSize(m_pSdlWindow, nullptr, &h); |
92 | return h; |
93 | } |
94 |
|