Index

zydeco / fractal

Experiment in graphics programming, C++, OpenGL, simulation techniques.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
916 Nov 2023 11:11a4d3636Fractal ViewerJosh Stockin181G

Blob @ zydeco / src / sdl2 / WindowSDL2.cpp

text/plain3375 bytesdownload raw
1#include "ZydecoCommon.hpp"
2#include "CommonSDL2.hpp"
3#include "WindowSDL2.hpp"
4
5
6static Logger LOGGER("WindowSDL2");
7
8
9WindowSDL2::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_RED_SIZE, 8);
20 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_GREEN_SIZE, 8);
21 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_BLUE_SIZE, 8);
22 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_ALPHA_SIZE, 8);
23 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_MULTISAMPLEBUFFERS, 1);
24 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_MULTISAMPLESAMPLES, 4);
25
26 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_ACCELERATED_VISUAL, 1);
27 SDL_CallErrorReturningFunction(SDL_GL_SetAttribute, SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
28 SDL_CallErrorReturningFunction(SDL_GL_SetSwapInterval, 1);
29
30 ImGui_ImplSDL2_InitForOpenGL(m_pSdlWindow, m_glContextMain);
31 ImGui_ImplOpenGL3_Init();
32
33 SDL_ShowWindow(m_pSdlWindow);
34}
35
36WindowSDL2::~WindowSDL2()
37{
38 LOGGER.Log(Logger::TRACE, "~WindowSDL2() for {}", m_windowTitle);
39 SDL_GL_DeleteContext(m_glContextMain);
40 SDL_DestroyWindow(m_pSdlWindow);
41}
42
43void WindowSDL2::MakeContextCurrent()
44{
45 SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, m_glContextMain);
46}
47
48void WindowSDL2::MakeNullCurrent()
49{
50 LOGGER.Log(Logger::TRACE, "MakeNullCurrent() for '{}'", m_windowTitle);
51 SDL_CallErrorReturningFunction(SDL_GL_MakeCurrent, m_pSdlWindow, nullptr);
52}
53
54bool WindowSDL2::Update()
55{
56 SDL_GL_SwapWindow(m_pSdlWindow);
57
58 SDL_Delay(1);
59
60 return false;
61}
62
63void WindowSDL2::SetTitle(std::string new_title)
64{
65 LOGGER.Log(Logger::TRACE, "SetTitle({}) for '{}'", new_title, m_windowTitle);
66 m_windowTitle = new_title;
67 SDL_SetWindowTitle(m_pSdlWindow, m_windowTitle.c_str());
68}
69
70void WindowSDL2::SetFullscreen(bool is_fullscreen)
71{
72 LOGGER.Log(Logger::TRACE, "SetFullscreen({}) for '{}'", is_fullscreen, m_windowTitle);
73 SDL_CallErrorReturningFunction(SDL_SetWindowFullscreen, m_pSdlWindow, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
74}
75
76void WindowSDL2::SetSize(uint64_t new_width, uint64_t new_height)
77{
78 LOGGER.Log(Logger::TRACE, "SetSize(x: {}, y: {}) for '{}'", new_width, new_height, m_windowTitle);
79 SDL_SetWindowSize(m_pSdlWindow, new_width, new_height);
80}
81
82void WindowSDL2::SetPosition(uint64_t new_x, uint64_t new_y)
83{
84 LOGGER.Log(Logger::TRACE, "SetPosition(x: {}, y: {}) for '{}'", new_x, new_y, m_windowTitle);
85 SDL_SetWindowPosition(m_pSdlWindow, new_x, new_y);
86}
87
88int WindowSDL2::GetWidth()
89{
90 LOGGER.Log(Logger::TRACE, "GetWidth() for '{}'", m_windowTitle);
91 int w;
92 SDL_GL_GetDrawableSize(m_pSdlWindow, &w, nullptr);
93 return w;
94}
95
96int WindowSDL2::GetHeight()
97{
98 LOGGER.Log(Logger::TRACE, "GetHeight() for '{}'", m_windowTitle);
99 int h;
100 SDL_GL_GetDrawableSize(m_pSdlWindow, nullptr, &h);
101 return h;
102}
103