Skip to content

Commit 4f88763

Browse files
committed
- fix compilation errors
- factor more switch and 3ds functions out into console helpers
1 parent 9ba9b35 commit 4f88763

File tree

14 files changed

+107
-49
lines changed

14 files changed

+107
-49
lines changed

src/game/input_creator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include "platform/replay_input.hpp"
88
#include <stdexcept>
99

10-
//TODO: support 3ds
10+
1111
#if defined(__ANDROID__)
1212
#include "platform/android_input.hpp"
13-
#elif defined(__SWITCH__)
14-
#include "platform/switch_input.hpp"
13+
#elif defined(__CONSOLE__)
14+
#include "platform/console_input.hpp"
1515
#else
1616
#include "platform/keyboard_input.hpp"
1717
#endif
@@ -28,7 +28,7 @@ namespace {
2828
auto* const event_dispatcher = &(service_provider->event_dispatcher());
2929
#if defined(__ANDROID__)
3030
auto input = std::make_unique<TouchInput>(event_dispatcher);
31-
#elif defined(__SWITCH__)
31+
#elif defined(__CONSOLE__)
3232
auto input = std::make_unique<JoystickInput>(event_dispatcher);
3333
#else
3434
auto input = std::make_unique<KeyboardInput>(keyboard_controls, event_dispatcher);

src/graphics/sdl_context.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
#include <SDL_ttf.h>
66
#include <fmt/format.h>
77

8-
#if defined(__SWITCH__)
9-
#include <switch.h>
10-
#elif defined(__3DS__)
11-
#include <3ds.h>
8+
#if defined(__CONSOLE__)
9+
#include "helper/console_helpers.hpp"
1210
#endif
1311

1412
#if defined(_HAVE_FILE_DIALOGS)
@@ -26,11 +24,10 @@ SdlContext::SdlContext() {
2624
throw helper::InitializationError{ fmt::format("Failed in initializing sdl ttf: {}", TTF_GetError()) };
2725
}
2826

29-
#if defined(__SWITCH__) or defined(__3DS__)
30-
// based on: https://github.com/carstene1ns/switch-sdl2-demo
27+
#if defined(__CONSOLE__)
28+
console::platform_init();
3129

32-
// mount the romfs in the executable as "romfs:/" (this is fine since only one app can run at the time on the switch)
33-
romfsInit();
30+
//TODO: factor out
3431

3532
// init joystick and other nintendo switch / 3ds specific things
3633
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
@@ -58,9 +55,8 @@ SdlContext::~SdlContext() {
5855
NFD::Quit();
5956
#endif
6057

61-
#if defined(__SWITCH__)
62-
// unmount romfs again
63-
romfsExit();
58+
#if defined(__CONSOLE__)
59+
console::platform_exit();
6460
#endif
6561

6662
TTF_Quit();

src/helper/console_helpers.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
#include "console_helpers.hpp"
66

7+
#include "helper/utils.hpp"
8+
79
#if defined(__SWITCH__)
810
#include <switch.h>
911
#elif defined(__3DS__)
1012
#include <3ds.h>
1113
#endif
1214

15+
#include <fmt/format.h>
16+
1317
void console::debug_write(const char* text, size_t size) {
1418
#ifdef __3DS__
1519
svcOutputDebugString(text, size);
@@ -20,11 +24,38 @@ void console::debug_write(const char* text, size_t size) {
2024
#endif
2125
}
2226

27+
#define IN_OUT_BUF_SIZE 0x100
28+
29+
[[nodiscard]] std::string console::open_url(const std::string& url) {
30+
#if defined(__3DS__)
31+
const auto size = url.size() >= 0x100 ? url.size() + 1 : 0x100;
32+
33+
char* buffer = malloc(size);
34+
35+
memcpy(buffer, url.c_str(), url.size());
36+
buffer[url.size()] = '\0';
37+
38+
aptLaunchLibraryApplet(APPID_WEB, buffer, IN_OUT_BUF_SIZE, 0);
39+
40+
const auto result = std::string{ buffer };
41+
42+
free(buffer);
43+
44+
return result;
45+
#elif defined(__SWITCH__)
46+
UNUSED(url);
47+
return "";
48+
#else
49+
#error "not implemented"
50+
#endif
51+
}
52+
53+
2354
void console::platform_init() {
2455
#if defined(__3DS__) || defined(__SWITCH__)
2556
Result ret = romfsInit();
2657
if (R_FAILED(ret)) {
27-
throw helper::IniInitializationError(fmt::format("romfsInit() failed: {:#2x}", (unsigned int) ret));
58+
throw helper::InitializationError(fmt::format("romfsInit() failed: {:#2x}", static_cast<unsigned int>(ret)));
2859
}
2960
#else
3061
#error "not implemented"
@@ -35,7 +66,7 @@ void console::platform_exit() {
3566
#if defined(__3DS__) || defined(__SWITCH__)
3667
Result ret = romfsExit();
3768
if (R_FAILED(ret)) {
38-
throw helper::IniInitializationError(fmt::format("romfsExit() failed: {:#2x}", (unsigned int) ret));
69+
throw helper::InitializationError(fmt::format("romfsExit() failed: {:#2x}", static_cast<unsigned int>(ret)));
3970
}
4071
#else
4172
#error "not implemented"

src/helper/console_helpers.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#if defined(__CONSOLE__)
66

7+
#include <spdlog/sinks/base_sink.h>
8+
79
namespace console {
810

911
void debug_write(const char* text, size_t size);
@@ -16,6 +18,8 @@ namespace console {
1618
debug_write(value.c_str(), value.size());
1719
}
1820

21+
[[nodiscard]] std::string open_url(const std::string& url);
22+
1923
void platform_init();
2024

2125
void platform_exit();
@@ -24,29 +28,26 @@ namespace console {
2428

2529

2630
template<typename Mutex>
27-
class debug_sink final : public base_sink<Mutex> {
31+
class debug_sink final : public spdlog::sinks::base_sink<Mutex> {
2832
public:
2933
explicit debug_sink() { }
3034
debug_sink(const debug_sink&) = delete;
3135
debug_sink& operator=(const debug_sink&) = delete;
3236

3337
protected:
34-
void sink_it_(const details::log_msg& msg) override {
35-
memory_buf_t formatted;
36-
base_sink<Mutex>::formatter_->format(msg, formatted);
38+
void sink_it_(const spdlog::details::log_msg& msg) override {
39+
spdlog::memory_buf_t formatted;
40+
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
3741
debug_write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
3842
}
3943

4044
void flush_() override {
4145
// noop
4246
}
43-
44-
std::ostream& ostream_;
45-
bool force_flush_;
4647
};
4748

4849
using debug_sink_mt = debug_sink<std::mutex>;
49-
using debug_sink_st = debug_sink<details::null_mutex>;
50+
using debug_sink_st = debug_sink<spdlog::details::null_mutex>;
5051

5152

5253
} // namespace console

src/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
#include <spdlog/sinks/android_sink.h>
1212
#endif
1313

14+
#if defined(__CONSOLE__)
15+
#include "helper/console_helpers.hpp"
16+
#endif
17+
18+
1419
#include <spdlog/sinks/basic_file_sink.h>
1520
#include <spdlog/sinks/rotating_file_sink.h>
1621
#include <spdlog/sinks/stdout_sinks.h>

src/platform/capabilities.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include "helper/utils.hpp"
66
#include "platform/capabilities.hpp"
77

8-
//TODO: support 3ds too
9-
#if defined(__SWITCH__)
10-
#include "platform/switch_buttons.hpp"
8+
#if defined(__CONSOLE__)
9+
#include "helper/console_helpers.hpp"
10+
#include "platform/console_buttons.hpp"
1111
#endif
1212

1313

@@ -67,10 +67,12 @@ namespace {
6767

6868
return true;
6969

70-
#elif defined(__SWITCH__) || defined(__3DS__)
71-
UNUSED(url);
72-
return false;
70+
#elif defined(__CONSOLE__)
71+
auto result = console::open_url(url);
72+
spdlog::info("Returned string from url open was: {}", result);
73+
return true;
7374
#else
75+
//TODO: this is dangerous, if we supply user input, so use SDL_OpenURL preferably
7476

7577
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
7678
const std::string shellCommand = "start " + url;

src/platform/capabilities.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
#include "helper/types.hpp"
1515
#include "helper/utils.hpp"
1616

17-
#if defined(__SWITCH__)
18-
#include "platform/switch_buttons.hpp"
17+
#if defined(__CONSOLE__)
18+
#include "platform/console_buttons.hpp"
1919
#endif
2020

21-
//TODO factor this file into mulitple ones, according to logic distribution of what the functions are doing
21+
//TODO factor this file into mulitple ones, according to logic distribution of what the functions are doing, remove and refacator the input helper functions
2222

2323
namespace utils {
2424

@@ -53,7 +53,7 @@ namespace utils {
5353
{static_cast<u8>(CrossPlatformAction::OPEN_SETTINGS), { 0 }},
5454
{ static_cast<u8>(CrossPlatformAction::TAB), { 0 }}
5555
};
56-
#elif defined(__SWITCH__)
56+
#elif defined(__CONSOLE__)
5757
{
5858
{ static_cast<u8>(CrossPlatformAction::OK),{ JOYCON_A } },
5959
{ static_cast<u8>(CrossPlatformAction::PAUSE), { JOYCON_PLUS }},
@@ -90,7 +90,7 @@ namespace utils {
9090
[[nodiscard]] constexpr Capabilities get_capabilities() {
9191
#if defined(__ANDROID__)
9292
return Capabilities{ false, true, Orientation::Portrait };
93-
#elif defined(__SWITCH__)
93+
#elif defined(__SWITCH__) or defined(__3DS__)
9494
return Capabilities{ true, false, Orientation::Landscape };
9595
#else
9696
return Capabilities{ true, true, Orientation::Landscape };

src/platform/switch_buttons.hpp renamed to src/platform/console_buttons.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#pragma once
44

5+
#if defined(__CONSOLE__)
6+
57
#if defined(__SWITCH__)
68

79

@@ -47,4 +49,8 @@ static_assert(BITL(JOYCON_B) == HidNpadButton_B);
4749
static_assert(BITL(JOYCON_PLUS) == HidNpadButton_Plus);
4850

4951

52+
#endif
53+
54+
//TODO: implement for 3ds
55+
5056
#endif

src/platform/switch_input.cpp renamed to src/platform/console_input.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
#if defined(__SWITCH__)
3-
#include "switch_input.hpp"
2+
#if defined(__CONSOLE__)
3+
#include "console_input.hpp"
44

55

66
void JoystickInput::handle_event(const SDL_Event& event, const Window*) {
@@ -19,6 +19,9 @@ void JoystickInput::update(SimulationStep simulation_step_index) {
1919
Input::update(simulation_step_index);
2020
}
2121

22+
#if defined(__SWITCH__)
23+
24+
2225
helper::optional<InputEvent> JoystickInput::sdl_event_to_input_event(const SDL_Event& event) const {
2326
if (event.type == SDL_JOYBUTTONDOWN) {
2427
const auto button = event.jbutton.button;
@@ -69,5 +72,14 @@ helper::optional<InputEvent> JoystickInput::sdl_event_to_input_event(const SDL_E
6972
}
7073
return helper::nullopt;
7174
}
75+
#elif defined(__3DS__)
76+
77+
helper::optional<InputEvent> JoystickInput::sdl_event_to_input_event(const SDL_Event& event) const {
78+
return helper::nullopt;
79+
}
80+
81+
//TODO: implement for 3ds
82+
#endif
83+
7284

7385
#endif

src/platform/switch_input.hpp renamed to src/platform/console_input.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
#pragma once
44

5-
#if defined(__SWITCH__)
5+
#if defined(__CONSOLE__)
66

7+
#include "console_buttons.hpp"
78
#include "input.hpp"
89
#include "manager/event_dispatcher.hpp"
9-
#include "switch_buttons.hpp"
1010

1111
struct JoystickInput : public Input, public EventListener {
1212
private:

0 commit comments

Comments
 (0)