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 | ImGui_ImplSDL2_InitForOpenGL(m_pSdlWindow, m_glContextMain); |
24 | ImGui_ImplOpenGL3_Init(); |
25 |
|
26 | SDL_ShowWindow(m_pSdlWindow); |
27 | } |
28 |
|
29 | WindowSDL2::~WindowSDL2() |
30 | { |
31 | LOGGER.Log(Logger::TRACE, "~WindowSDL2() for {}", m_windowTitle); |
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 | LOGGER.Log(Logger::TRACE, "MakeNullCurrent() for '{}'", m_windowTitle); |
44 | SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, nullptr); |
45 | } |
46 |
|
47 | bool WindowSDL2::Update() |
48 | { |
49 | SDL_GL_SwapWindow(m_pSdlWindow); |
50 |
|
51 | // SDL_Delay(1); |
52 |
|
53 | return false; |
54 | } |
55 |
|
56 | void WindowSDL2::SetTitle(std::string new_title) |
57 | { |
58 | LOGGER.Log(Logger::TRACE, "SetTitle({}) for '{}'", new_title, m_windowTitle); |
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 | LOGGER.Log(Logger::TRACE, "SetFullscreen({}) for '{}'", is_fullscreen, m_windowTitle); |
66 | SDL_CallErrorReturningFunction(SDL_SetWindowFullscreen, m_pSdlWindow, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0); |
67 | } |
68 |
|
69 | void WindowSDL2::SetSize(uint64_t new_width, uint64_t new_height) |
70 | { |
71 | LOGGER.Log(Logger::TRACE, "SetSize(x: {}, y: {}) for '{}'", new_width, new_height, m_windowTitle); |
72 | SDL_SetWindowSize(m_pSdlWindow, new_width, new_height); |
73 | } |
74 |
|
75 | void WindowSDL2::SetPosition(uint64_t new_x, uint64_t new_y) |
76 | { |
77 | LOGGER.Log(Logger::TRACE, "SetPosition(x: {}, y: {}) for '{}'", new_x, new_y, m_windowTitle); |
78 | SDL_SetWindowPosition(m_pSdlWindow, new_x, new_y); |
79 | } |
80 |
|
81 | int WindowSDL2::GetWidth() |
82 | { |
83 | LOGGER.Log(Logger::TRACE, "GetWidth() for '{}'", m_windowTitle); |
84 | int w; |
85 | SDL_GL_GetDrawableSize(m_pSdlWindow, &w, nullptr); |
86 | return w; |
87 | } |
88 |
|
89 | int WindowSDL2::GetHeight() |
90 | { |
91 | LOGGER.Log(Logger::TRACE, "GetHeight() for '{}'", m_windowTitle); |
92 | int h; |
93 | SDL_GL_GetDrawableSize(m_pSdlWindow, nullptr, &h); |
94 | return h; |
95 | } |
96 |
|