Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
sdl3.h
Go to the documentation of this file.
1// Copyright 2026 The Symbian SDK Authors.
2// Licensed under the Apache License, Version 2.0.
3
4#ifndef SYMBIAN_SDL3_SDL3_H_
5#define SYMBIAN_SDL3_SDL3_H_
6
7#include <cstdint>
8#include <memory>
9#include <new>
10#include <utility>
11
12#include <SDL3/SDL.h>
13#include <absl/status/status_macros.h>
14#include <absl/base/nullability.h>
15#include <absl/status/status.h>
16#include <absl/status/statusor.h>
17
18namespace symbian::sdl3 {
19
20inline absl::Status SdlError() {
21 return absl::InternalError(SDL_GetError());
22}
23
24class Session final {
25 public:
26 Session(const Session&) = delete;
27 Session& operator=(const Session&) = delete;
28
29 Session(Session&& other) noexcept : flags_(std::exchange(other.flags_, 0)) {}
30
32 if (flags_ != 0) {
33 SDL_QuitSubSystem(flags_);
34 }
35 }
36
37 static absl::StatusOr<Session> Start(std::uint32_t flags) {
38 if (!SDL_InitSubSystem(flags)) {
39 return SdlError();
40 }
41 return Session(flags);
42 }
43
44 private:
45 explicit Session(std::uint32_t flags) : flags_(flags) {}
46
47 std::uint32_t flags_;
48};
49
50class Window final {
51 public:
52 Window(const Window&) = delete;
53 Window& operator=(const Window&) = delete;
54
55 Window(Window&& other) noexcept
56 : value_(std::exchange(other.value_, nullptr)) {}
57
59 if (value_ != nullptr) {
60 SDL_DestroyWindow(value_);
61 }
62 }
63
64 static absl::StatusOr<Window> Create(const char* absl_nonnull title,
65 int width, int height,
66 std::uint64_t flags) {
67 SDL_Window* absl_nullable value =
68 SDL_CreateWindow(title, width, height, flags);
69 if (value == nullptr) {
70 return SdlError();
71 }
72 return Window(value);
73 }
74
75 static absl::StatusOr<std::unique_ptr<Window>> CreateUnique(
76 const char* absl_nonnull title, int width, int height,
77 std::uint64_t flags) {
78 ABSL_ASSIGN_OR_RETURN(auto created, Create(title, width, height, flags));
79 std::unique_ptr<Window> owner(new (std::nothrow)
80 Window(std::move(created)));
81 if (owner == nullptr) {
82 return absl::ResourceExhaustedError("SDL window owner allocation failed");
83 }
84 return owner;
85 }
86
87 SDL_Window* absl_nullable get() const { return value_; }
88
89 private:
90 explicit Window(SDL_Window* absl_nonnull value) : value_(value) {}
91
92 SDL_Window* absl_nullable value_;
93};
94
95class Renderer final {
96 public:
97 Renderer(const Renderer&) = delete;
98 Renderer& operator=(const Renderer&) = delete;
99
100 Renderer(Renderer&& other) noexcept
101 : value_(std::exchange(other.value_, nullptr)) {}
102
103 Renderer& operator=(Renderer&& other) noexcept {
104 if (this != &other) {
105 if (value_ != nullptr) {
106 SDL_DestroyRenderer(value_);
107 }
108 value_ = std::exchange(other.value_, nullptr);
109 }
110 return *this;
111 }
112
114 if (value_ != nullptr) {
115 SDL_DestroyRenderer(value_);
116 }
117 }
118
119 static absl::StatusOr<Renderer> Create(
120 SDL_Window* absl_nonnull window,
121 const char* absl_nonnull driver_name = "software") {
122 SDL_Renderer* absl_nullable value = SDL_CreateRenderer(window, driver_name);
123 if (value == nullptr) {
124 return SdlError();
125 }
126 return Renderer(value);
127 }
128
129 static absl::StatusOr<std::unique_ptr<Renderer>> CreateUnique(
130 SDL_Window* absl_nonnull window,
131 const char* absl_nonnull driver_name = "software") {
132 ABSL_ASSIGN_OR_RETURN(auto created, Create(window, driver_name));
133 std::unique_ptr<Renderer> owner(new (std::nothrow)
134 Renderer(std::move(created)));
135 if (owner == nullptr) {
136 return absl::ResourceExhaustedError(
137 "SDL renderer owner allocation failed");
138 }
139 return owner;
140 }
141
142 SDL_Renderer* absl_nullable get() const { return value_; }
143
144 private:
145 explicit Renderer(SDL_Renderer* absl_nonnull value) : value_(value) {}
146
147 SDL_Renderer* absl_nullable value_;
148};
149
150} // namespace symbian::sdl3
151
152#endif // SYMBIAN_SDL3_SDL3_H_
Definition sdl3.h:95
Renderer(const Renderer &)=delete
Renderer & operator=(Renderer &&other) noexcept
Definition sdl3.h:103
~Renderer()
Definition sdl3.h:113
Renderer(Renderer &&other) noexcept
Definition sdl3.h:100
Renderer & operator=(const Renderer &)=delete
static absl::StatusOr< std::unique_ptr< Renderer > > CreateUnique(SDL_Window *absl_nonnull window, const char *absl_nonnull driver_name="software")
Definition sdl3.h:129
SDL_Renderer *absl_nullable get() const
Definition sdl3.h:142
static absl::StatusOr< Renderer > Create(SDL_Window *absl_nonnull window, const char *absl_nonnull driver_name="software")
Definition sdl3.h:119
Definition sdl3.h:24
Session & operator=(const Session &)=delete
static absl::StatusOr< Session > Start(std::uint32_t flags)
Definition sdl3.h:37
Session(const Session &)=delete
~Session()
Definition sdl3.h:31
Session(Session &&other) noexcept
Definition sdl3.h:29
Definition sdl3.h:50
static absl::StatusOr< std::unique_ptr< Window > > CreateUnique(const char *absl_nonnull title, int width, int height, std::uint64_t flags)
Definition sdl3.h:75
Window & operator=(const Window &)=delete
SDL_Window *absl_nullable get() const
Definition sdl3.h:87
Window(const Window &)=delete
Window(Window &&other) noexcept
Definition sdl3.h:55
~Window()
Definition sdl3.h:58
static absl::StatusOr< Window > Create(const char *absl_nonnull title, int width, int height, std::uint64_t flags)
Definition sdl3.h:64
Definition sdl3.h:18
absl::Status SdlError()
Definition sdl3.h:20
libusb_context *absl_nullable value
Definition usb.cc:37
std::uint32_t height
Definition window_surface.cc:109
std::uint32_t width
Definition window_surface.cc:108