Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
sdl2.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_SDL2_SDL2_H_
5#define SYMBIAN_SDL2_SDL2_H_
6
7#include <cstdint>
8#include <memory>
9#include <new>
10#include <utility>
11
12#include <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::sdl2 {
19
20inline absl::Status SdlError() {
21 return absl::InternalError(SDL_GetError());
22}
23
25class Session final {
26 public:
27 Session(const Session&) = delete;
28 Session& operator=(const Session&) = delete;
29
30 Session(Session&& other) noexcept : flags_(std::exchange(other.flags_, 0)) {}
31
33 if (flags_ != 0) {
34 SDL_QuitSubSystem(flags_);
35 }
36 }
37
38 static absl::StatusOr<Session> Start(std::uint32_t flags) {
39 if (SDL_InitSubSystem(flags) != 0) {
40 return SdlError();
41 }
42 return Session(flags);
43 }
44
45 private:
46 explicit Session(std::uint32_t flags) : flags_(flags) {}
47
48 std::uint32_t flags_;
49};
50
52class Window final {
53 public:
54 Window(const Window&) = delete;
55 Window& operator=(const Window&) = delete;
56
57 Window(Window&& other) noexcept
58 : value_(std::exchange(other.value_, nullptr)) {}
59
61 if (value_ != nullptr) {
62 SDL_DestroyWindow(value_);
63 }
64 }
65
66 static absl::StatusOr<Window> Create(const char* absl_nonnull title,
67 int width, int height,
68 std::uint32_t flags) {
69 SDL_Window* absl_nullable value =
70 SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
71 SDL_WINDOWPOS_UNDEFINED, width, height, flags);
72 if (value == nullptr) {
73 return SdlError();
74 }
75 return Window(value);
76 }
77
78 static absl::StatusOr<std::unique_ptr<Window>> CreateUnique(
79 const char* absl_nonnull title, int width, int height,
80 std::uint32_t flags) {
81 ABSL_ASSIGN_OR_RETURN(auto created, Create(title, width, height, flags));
82 std::unique_ptr<Window> owner(new (std::nothrow)
83 Window(std::move(created)));
84 if (owner == nullptr) {
85 return absl::ResourceExhaustedError("SDL window owner allocation failed");
86 }
87 return owner;
88 }
89
90 SDL_Window* absl_nullable get() const { return value_; }
91
92 private:
93 explicit Window(SDL_Window* absl_nonnull value) : value_(value) {}
94
95 SDL_Window* absl_nullable value_;
96};
97
99class Renderer final {
100 public:
101 Renderer(const Renderer&) = delete;
102 Renderer& operator=(const Renderer&) = delete;
103
104 Renderer(Renderer&& other) noexcept
105 : value_(std::exchange(other.value_, nullptr)) {}
106
107 Renderer& operator=(Renderer&& other) noexcept {
108 if (this != &other) {
109 if (value_ != nullptr) {
110 SDL_DestroyRenderer(value_);
111 }
112 value_ = std::exchange(other.value_, nullptr);
113 }
114 return *this;
115 }
116
118 if (value_ != nullptr) {
119 SDL_DestroyRenderer(value_);
120 }
121 }
122
123 static absl::StatusOr<Renderer> Create(SDL_Window* absl_nonnull window,
124 std::uint32_t flags) {
125 SDL_Renderer* absl_nullable value = SDL_CreateRenderer(window, -1, flags);
126 if (value == nullptr) {
127 return SdlError();
128 }
129 return Renderer(value);
130 }
131
132 static absl::StatusOr<std::unique_ptr<Renderer>> CreateUnique(
133 SDL_Window* absl_nonnull window, std::uint32_t flags) {
134 ABSL_ASSIGN_OR_RETURN(auto created, Create(window, flags));
135 std::unique_ptr<Renderer> owner(new (std::nothrow)
136 Renderer(std::move(created)));
137 if (owner == nullptr) {
138 return absl::ResourceExhaustedError(
139 "SDL renderer owner allocation failed");
140 }
141 return owner;
142 }
143
144 SDL_Renderer* absl_nullable get() const { return value_; }
145
146 private:
147 explicit Renderer(SDL_Renderer* absl_nonnull value) : value_(value) {}
148
149 SDL_Renderer* absl_nullable value_;
150};
151
152} // namespace symbian::sdl2
153
154#endif // SYMBIAN_SDL2_SDL2_H_
A unique SDL renderer with status checked drawing operations.
Definition sdl2.h:99
SDL_Renderer *absl_nullable get() const
Definition sdl2.h:144
static absl::StatusOr< std::unique_ptr< Renderer > > CreateUnique(SDL_Window *absl_nonnull window, std::uint32_t flags)
Definition sdl2.h:132
Renderer & operator=(const Renderer &)=delete
Renderer(Renderer &&other) noexcept
Definition sdl2.h:104
Renderer(const Renderer &)=delete
~Renderer()
Definition sdl2.h:117
static absl::StatusOr< Renderer > Create(SDL_Window *absl_nonnull window, std::uint32_t flags)
Definition sdl2.h:123
Renderer & operator=(Renderer &&other) noexcept
Definition sdl2.h:107
Owns an SDL subsystem for a lexical scope.
Definition sdl2.h:25
static absl::StatusOr< Session > Start(std::uint32_t flags)
Definition sdl2.h:38
~Session()
Definition sdl2.h:32
Session(Session &&other) noexcept
Definition sdl2.h:30
Session(const Session &)=delete
Session & operator=(const Session &)=delete
A unique SDL window.
Definition sdl2.h:52
Window & operator=(const Window &)=delete
static absl::StatusOr< Window > Create(const char *absl_nonnull title, int width, int height, std::uint32_t flags)
Definition sdl2.h:66
Window(Window &&other) noexcept
Definition sdl2.h:57
~Window()
Definition sdl2.h:60
static absl::StatusOr< std::unique_ptr< Window > > CreateUnique(const char *absl_nonnull title, int width, int height, std::uint32_t flags)
Definition sdl2.h:78
SDL_Window *absl_nullable get() const
Definition sdl2.h:90
Window(const Window &)=delete
Definition sdl2.h:18
absl::Status SdlError()
Definition sdl2.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