Skip to content

Commit

Permalink
Make bare-minimum changes to get SDL3 compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Nov 7, 2024
1 parent c49de80 commit c371e0d
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 364 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Upcoming Release]

- A CC-BY license was added to `resources/icons` (#944)
- "Windows Fullscreen" was dropped from the about page: the "Fullscreen" button now
always follows "windowed fullscreen" behavior (exclusive fullscreen usage was deemed
to be niche).
- Internal: SDL2 was upgraded to SDL3

## [0.5.16] - 2024/11/04

Expand Down
10 changes: 2 additions & 8 deletions src/OpenSimCreator/UI/Shared/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,10 @@ void osc::MainMenuAboutTab::onDraw()
ui::draw_text_unformatted("window");
ui::next_column();

if (ui::draw_button(OSC_ICON_EXPAND " fullscreen"))
{
App::upd().make_fullscreen();
}
if (ui::draw_button(OSC_ICON_EXPAND " windowed fullscreen"))
{
if (ui::draw_button(OSC_ICON_EXPAND " fullscreen")) {
App::upd().make_windowed_fullscreen();
}
if (ui::draw_button(OSC_ICON_WINDOW_RESTORE " windowed"))
{
if (ui::draw_button(OSC_ICON_WINDOW_RESTORE " windowed")) {
App::upd().make_windowed();
}
ui::next_column();
Expand Down
5 changes: 2 additions & 3 deletions src/oscar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (NOT ${OSC_EMSCRIPTEN})
# source code).
find_package(OpenGL REQUIRED)
find_package(glew REQUIRED CONFIG)
find_package(SDL2 REQUIRED CONFIG)
find_package(SDL3 REQUIRED CONFIG)
find_package(nativefiledialog REQUIRED CONFIG)
endif()
find_package(imgui REQUIRED CONFIG)
Expand Down Expand Up @@ -268,7 +268,6 @@ add_library(oscar STATIC
Maths/Vec3.h
Maths/Vec4.h

Platform/Detail/SDL2Helpers.h
Platform/App.cpp
Platform/App.h
Platform/AppClock.h
Expand Down Expand Up @@ -647,7 +646,7 @@ target_link_libraries(oscar PRIVATE
$<$<NOT:$<BOOL:${OSC_EMSCRIPTEN}>>:
OpenGL::GL
GLEW::glew_s
SDL2::SDL2
SDL3::SDL3
nativefiledialog
>

Expand Down
65 changes: 58 additions & 7 deletions src/oscar/Graphics/GraphicsImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#include <oscar/Maths/Vec4.h>
#include <oscar/Maths/VecFunctions.h>
#include <oscar/Platform/App.h>
#include <oscar/Platform/Detail/SDL2Helpers.h>
#include <oscar/Platform/Log.h>
#include <oscar/Utils/Algorithms.h>
#include <oscar/Utils/Assertions.h>
Expand All @@ -86,8 +85,10 @@
#include <oscar/Utils/TransparentStringHasher.h>
#include <oscar/Utils/UID.h>

#include <GL/glew.h>
#include <ankerl/unordered_dense.h>
#include <GL/glew.h>
#include <SDL3/SDL.h>
#undef main

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -115,6 +116,56 @@ using namespace osc::literals;
using namespace osc;
namespace rgs = std::ranges;

namespace osc::sdl
{
// RAII wrapper around `SDL_GLContext` that calls `SDL_GL_DeleteContext` on dtor
// https://wiki.libsdl.org/SDL_GL_DeleteContext
class GLContext final {
public:
GLContext(const GLContext&) = delete;
GLContext(GLContext&& tmp) noexcept :
context_handle_{std::exchange(tmp.context_handle_, nullptr)}
{}
GLContext& operator=(const GLContext&) = delete;
GLContext& operator=(GLContext&&) = delete;
~GLContext() noexcept
{
if (context_handle_) {
SDL_GL_DestroyContext(context_handle_);
}
}

SDL_GLContext get() { return context_handle_; }

private:
friend GLContext GL_CreateContext(SDL_Window* w);
GLContext(SDL_GLContext _ctx) :
context_handle_{_ctx}
{}

SDL_GLContext context_handle_;
};

// https://wiki.libsdl.org/SDL_GL_CreateContext
inline GLContext GL_CreateContext(SDL_Window* w)
{
const SDL_GLContext ctx = SDL_GL_CreateContext(w);

if (ctx == nullptr) {
throw std::runtime_error{std::string{"SDL_GL_CreateContext failed: "} + SDL_GetError()};
}

return GLContext{ctx};
}

inline int GetOpenGLSwapInterval()
{
int rv = 0;
SDL_GL_GetSwapInterval(&rv);
return rv;
}
}

// shader source
namespace
{
Expand Down Expand Up @@ -5872,15 +5923,15 @@ namespace
sdl::GLContext ctx = sdl::GL_CreateContext(&window);

// enable the OpenGL context
if (SDL_GL_MakeCurrent(&window, ctx.get()) != 0) {
if (not SDL_GL_MakeCurrent(&window, ctx.get())) {
throw std::runtime_error{std::string{"SDL_GL_MakeCurrent failed: "} + SDL_GetError()};
}

// enable vsync by default
//
// vsync can feel a little laggy on some systems, but vsync reduces CPU usage
// on *constrained* systems (e.g. laptops, which the majority of users are using)
if (SDL_GL_SetSwapInterval(-1) != 0) {
if (not SDL_GL_SetSwapInterval(-1)) {
SDL_GL_SetSwapInterval(1);
}

Expand Down Expand Up @@ -6118,13 +6169,13 @@ class osc::GraphicsContext::Impl final {
}

// always read the vsync state back from SDL
vsync_enabled_ = SDL_GL_GetSwapInterval() != 0;
vsync_enabled_ = sdl::GetOpenGLSwapInterval();
}
else {
// try to disable vsync

SDL_GL_SetSwapInterval(0);
vsync_enabled_ = SDL_GL_GetSwapInterval() != 0;
vsync_enabled_ = sdl::GetOpenGLSwapInterval();
}
}

Expand Down Expand Up @@ -6260,7 +6311,7 @@ class osc::GraphicsContext::Impl final {
// maximum number of antiAliasingLevel supported by this hardware's OpenGL MSXAA API
AntiAliasingLevel max_aa_level_ = get_opengl_max_aa_level(opengl_context_);

bool vsync_enabled_ = SDL_GL_GetSwapInterval() != 0;
bool vsync_enabled_ = sdl::GetOpenGLSwapInterval();

// true if OpenGL's debug mode is enabled
bool debug_mode_enabled_ = false;
Expand Down
Loading

0 comments on commit c371e0d

Please sign in to comment.