Skip to content

Commit 4a629a1

Browse files
committed
port to SDL3 (start)
1 parent 322ebb0 commit 4a629a1

21 files changed

+316
-317
lines changed

include/gf2/core/Event.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <cstdint>
77

88
#include <array>
9+
#include <string_view>
910
#include <variant>
1011

1112
#include "CoreApi.h"
@@ -30,7 +31,7 @@ namespace gf {
3031
WindowExposed,
3132
WindowMoved,
3233
WindowResized,
33-
// WindowPixelSizeChanged, // TODO: SDL3
34+
WindowPixelSizeChanged,
3435
WindowMinimized,
3536
WindowMaximized,
3637
WindowRestored,
@@ -45,7 +46,7 @@ namespace gf {
4546
KeyReleased,
4647

4748
// TextEdited, // TODO: SDL3
48-
TextEntered,
49+
TextInput,
4950
// TextCandidatesShown, // TODO: SDL3
5051

5152
MouseMoved,
@@ -91,10 +92,10 @@ namespace gf {
9192
Vec2I size;
9293
};
9394

94-
// struct GF_CORE_API WindowPixelSizeChangedEvent {
95-
// WindowId window_id;
96-
// Vec2I size;
97-
// };
95+
struct GF_CORE_API WindowPixelSizeChangedEvent {
96+
WindowId window_id;
97+
Vec2I size;
98+
};
9899

99100
struct GF_CORE_API WindowMinimizedEvent {
100101
WindowId window_id;
@@ -157,9 +158,9 @@ namespace gf {
157158
// int32_t length;
158159
// };
159160

160-
struct GF_CORE_API TextEnteredEvent {
161+
struct GF_CORE_API TextInputEvent {
161162
WindowId window_id;
162-
std::array<char, 32> text; // TODO: SDL3: changed to a std::string_view
163+
std::string_view text;
163164
};
164165

165166
// struct GF_CORE_API TextCandidatesShownEvent {
@@ -181,23 +182,24 @@ namespace gf {
181182
WindowId window_id;
182183
MouseId mouse_id;
183184
MouseButton button;
184-
Vec2I position;
185+
Vec2F position;
185186
uint8_t clicks;
186187
};
187188

188189
struct GF_CORE_API MouseButtonReleasedEvent {
189190
WindowId window_id;
190191
MouseId mouse_id;
191192
MouseButton button;
192-
Vec2I position;
193+
Vec2F position;
193194
uint8_t clicks;
194195
};
195196

196197
struct GF_CORE_API MouseWheelScrolledEvent {
197198
WindowId window_id;
198199
MouseId mouse_id;
199-
Vec2I offset;
200+
Vec2F offset;
200201
MouseWheelDirection direction;
202+
Vec2F position;
201203
};
202204

203205
struct GF_CORE_API GamepadAxisMovedEvent {
@@ -217,7 +219,7 @@ namespace gf {
217219
};
218220

219221
struct GF_CORE_API GamepadConnectedEvent {
220-
GamepadHwId gamepad_hw_id;
222+
GamepadId gamepad_id;
221223
};
222224

223225
struct GF_CORE_API GamepadDisconnectedEvent {
@@ -315,7 +317,7 @@ namespace gf {
315317
WindowExposedEvent,
316318
WindowMovedEvent,
317319
WindowResizedEvent,
318-
// WindowPixelSizeChangedEvent,
320+
WindowPixelSizeChangedEvent,
319321
WindowMinimizedEvent,
320322
WindowMaximizedEvent,
321323
WindowRestoredEvent,
@@ -330,7 +332,7 @@ namespace gf {
330332
KeyReleasedEvent,
331333

332334
// TextEditedEvent,
333-
TextEnteredEvent,
335+
TextInputEvent,
334336
// TextCandidatesShownEvent,
335337

336338
MouseMovedEvent,

include/gf2/core/GamepadTypes.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66
#include <cstdint>
77

8+
#include <limits>
9+
#include <type_traits>
10+
811
namespace gf {
912

1013
enum class GamepadButton : int32_t { // NOLINT(performance-enum-size)
1114
Invalid = -1,
12-
A,
13-
B,
14-
X,
15-
Y,
15+
South,
16+
East,
17+
West,
18+
North,
1619
Back,
1720
Guide,
1821
Start,
@@ -50,13 +53,10 @@ namespace gf {
5053
Negative,
5154
};
5255

53-
enum class GamepadHwId : int { // NOLINT(performance-enum-size)
54-
};
55-
56-
enum class GamepadId : int32_t { // NOLINT(performance-enum-size)
56+
enum class GamepadId : uint32_t { // NOLINT(performance-enum-size)
5757
};
5858

59-
constexpr GamepadId AnyGamepad = static_cast<GamepadId>(INT32_C(-1));
59+
constexpr GamepadId AnyGamepad = static_cast<GamepadId>(std::numeric_limits<std::underlying_type_t<GamepadId>>::max());
6060

6161
}
6262

include/gf2/core/Keycode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace gf {
2424
Tab = '\t',
2525
Space = ' ',
2626
Exclaim = '!',
27-
QuoteDbl = '"',
27+
DoubleApostrophe = '"',
2828
Hash = '#',
2929
Percent = '%',
3030
Dollar = '$',
@@ -60,7 +60,7 @@ namespace gf {
6060
RightBracket = ']',
6161
Caret = '^',
6262
Underscore = '_',
63-
Backquote = '`',
63+
Grave = '`',
6464
A = 'a',
6565
B = 'b',
6666
C = 'c',

include/gf2/core/TouchTypes.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
#include <cstdint>
77

8+
#include <limits>
9+
#include <type_traits>
10+
811
namespace gf {
912

10-
enum class TouchId : int64_t;
13+
enum class TouchId : uint64_t;
1114

12-
constexpr TouchId MouseTouchId = TouchId{ -1 };
15+
constexpr TouchId MouseTouchId = TouchId{ std::numeric_limits<std::underlying_type_t<TouchId>>::max() };
1316

14-
enum class FingerId : int64_t;
17+
enum class FingerId : uint64_t;
1518

1619
} // namespace gf
1720

include/gf2/graphics/Cursor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ struct SDL_Cursor;
1212
namespace gf {
1313

1414
enum class CursorType : uint8_t {
15-
Arrow,
15+
Default,
1616
Text,
1717
Wait,
1818
Cross,
19-
ArrowWait,
19+
Progress,
2020
SizeTopLeftBottomRight,
2121
SizeBottomLeftTopRight,
2222
SizeHorizontal,
2323
SizeVertical,
24-
SizeAll,
24+
Move,
2525
NotAllowed,
26-
Hand,
26+
Pointer,
2727
};
2828

2929
class GF_GRAPHICS_API Cursor {

include/gf2/graphics/Gamepad.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace gf {
1515
static const char* button_name(GamepadButton button);
1616
static const char* gamepad_name(GamepadId id);
1717

18-
static GamepadId open(GamepadHwId hwid);
18+
static void open(GamepadId hwid);
1919
static bool attached(GamepadId id);
2020
static void close(GamepadId id);
2121
};

include/gf2/imgui/ImguiManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace gf {
3737
void update_mouse_cursor();
3838

3939
Texture m_font_texture;
40-
CursorType m_cursor_type = CursorType::Arrow;
40+
CursorType m_cursor_type = CursorType::Default;
4141
Cursor m_cursor;
4242
};
4343

library/core/Event.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace gf {
2626
event_data_check<gf::WindowExposedEvent, gf::EventType::WindowExposed>();
2727
event_data_check<gf::WindowMovedEvent, gf::EventType::WindowMoved>();
2828
event_data_check<gf::WindowResizedEvent, gf::EventType::WindowResized>();
29+
event_data_check<gf::WindowPixelSizeChangedEvent, gf::EventType::WindowPixelSizeChanged>();
2930
event_data_check<gf::WindowMinimizedEvent, gf::EventType::WindowMinimized>();
3031
event_data_check<gf::WindowMaximizedEvent, gf::EventType::WindowMaximized>();
3132
event_data_check<gf::WindowRestoredEvent, gf::EventType::WindowRestored>();
@@ -37,7 +38,7 @@ namespace gf {
3738
event_data_check<gf::KeyPressedEvent, gf::EventType::KeyPressed>();
3839
event_data_check<gf::KeyRepeatedEvent, gf::EventType::KeyRepeated>();
3940
event_data_check<gf::KeyReleasedEvent, gf::EventType::KeyReleased>();
40-
event_data_check<gf::TextEnteredEvent, gf::EventType::TextEntered>();
41+
event_data_check<gf::TextInputEvent, gf::EventType::TextInput>();
4142
event_data_check<gf::MouseMovedEvent, gf::EventType::MouseMoved>();
4243
event_data_check<gf::MouseButtonPressedEvent, gf::EventType::MouseButtonPressed>();
4344
event_data_check<gf::MouseButtonReleasedEvent, gf::EventType::MouseButtonReleased>();

library/graphics/Clipboard.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@
33

44
#include <gf2/graphics/Clipboard.h>
55

6-
#include <SDL2/SDL.h>
6+
#include <memory>
7+
8+
#include <SDL3/SDL.h>
79

810
#include <gf2/core/Log.h>
911

1012
namespace gf {
1113

14+
namespace {
15+
16+
struct ClipboardStringDeleter {
17+
void operator()(char* str) const noexcept
18+
{
19+
SDL_free(str);
20+
}
21+
};
22+
23+
}
24+
1225
std::string Clipboard::text()
1326
{
14-
if (SDL_HasClipboardText() == SDL_FALSE) {
27+
if (!SDL_HasClipboardText()) {
1528
return "";
1629
}
1730

18-
char* raw_text = SDL_GetClipboardText();
31+
std::unique_ptr<char[], ClipboardStringDeleter> raw_text(SDL_GetClipboardText());
1932

2033
if (raw_text == nullptr) {
2134
Log::error("Unable to get clipboard text: {}.", SDL_GetError());
2235
return "";
2336
}
2437

25-
std::string result(raw_text);
26-
SDL_free(raw_text);
27-
return result;
38+
return raw_text.get();
2839
}
2940

3041
void Clipboard::set_text(const std::string& text)

library/graphics/Cursor.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <utility>
77

8-
#include <SDL2/SDL.h>
8+
#include <SDL3/SDL.h>
99

1010
#include <gf2/core/Log.h>
1111

@@ -21,18 +21,18 @@ namespace gf {
2121

2222
[[maybe_unused]] constexpr void cursor_check()
2323
{
24-
cursor_type_check<CursorType::Arrow, SDL_SYSTEM_CURSOR_ARROW>();
25-
cursor_type_check<CursorType::Text, SDL_SYSTEM_CURSOR_IBEAM>();
24+
cursor_type_check<CursorType::Default, SDL_SYSTEM_CURSOR_DEFAULT>();
25+
cursor_type_check<CursorType::Text, SDL_SYSTEM_CURSOR_TEXT>();
2626
cursor_type_check<CursorType::Wait, SDL_SYSTEM_CURSOR_WAIT>();
2727
cursor_type_check<CursorType::Cross, SDL_SYSTEM_CURSOR_CROSSHAIR>();
28-
cursor_type_check<CursorType::ArrowWait, SDL_SYSTEM_CURSOR_WAITARROW>();
29-
cursor_type_check<CursorType::SizeTopLeftBottomRight, SDL_SYSTEM_CURSOR_SIZENWSE>();
30-
cursor_type_check<CursorType::SizeBottomLeftTopRight, SDL_SYSTEM_CURSOR_SIZENESW>();
31-
cursor_type_check<CursorType::SizeHorizontal, SDL_SYSTEM_CURSOR_SIZEWE>();
32-
cursor_type_check<CursorType::SizeVertical, SDL_SYSTEM_CURSOR_SIZENS>();
33-
cursor_type_check<CursorType::SizeAll, SDL_SYSTEM_CURSOR_SIZEALL>();
34-
cursor_type_check<CursorType::Hand, SDL_SYSTEM_CURSOR_HAND>();
35-
cursor_type_check<CursorType::NotAllowed, SDL_SYSTEM_CURSOR_NO>();
28+
cursor_type_check<CursorType::Progress, SDL_SYSTEM_CURSOR_PROGRESS>();
29+
cursor_type_check<CursorType::SizeTopLeftBottomRight, SDL_SYSTEM_CURSOR_NWSE_RESIZE>();
30+
cursor_type_check<CursorType::SizeBottomLeftTopRight, SDL_SYSTEM_CURSOR_NESW_RESIZE>();
31+
cursor_type_check<CursorType::SizeHorizontal, SDL_SYSTEM_CURSOR_EW_RESIZE>();
32+
cursor_type_check<CursorType::SizeVertical, SDL_SYSTEM_CURSOR_NS_RESIZE>();
33+
cursor_type_check<CursorType::Move, SDL_SYSTEM_CURSOR_MOVE>();
34+
cursor_type_check<CursorType::Pointer, SDL_SYSTEM_CURSOR_POINTER>();
35+
cursor_type_check<CursorType::NotAllowed, SDL_SYSTEM_CURSOR_NOT_ALLOWED>();
3636
}
3737

3838
}
@@ -53,7 +53,7 @@ namespace gf {
5353
Cursor::~Cursor()
5454
{
5555
if (m_cursor != nullptr) {
56-
SDL_FreeCursor(m_cursor);
56+
SDL_DestroyCursor(m_cursor);
5757
}
5858
}
5959

@@ -65,7 +65,12 @@ namespace gf {
6565

6666
void Cursor::set_mouse_cursor_visible(bool visible)
6767
{
68-
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
68+
// TODO: [SDL3] check return
69+
if (visible) {
70+
SDL_ShowCursor();
71+
} else {
72+
SDL_HideCursor();
73+
}
6974
}
7075

7176
void Cursor::set_mouse_cursor(const Cursor* cursor)

0 commit comments

Comments
 (0)