Skip to content

Commit 2ae44c0

Browse files
committed
- adding console helper, to factor out some common things from 3ds + switch
- adding debug logger to the consoles
1 parent c39beb0 commit 2ae44c0

File tree

8 files changed

+129
-23
lines changed

8 files changed

+129
-23
lines changed

platforms/build-3ds.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ export ARCH="arm"
4343
export ARM_VERSION="arm11mpcore"
4444
export ENDIANESS="little"
4545

46-
export COMMON_FLAGS="'-march=armv6k','-mtune=mpcore','-mfloat-abi=hard', '-mtp=soft','-mword-relocations', '-ffunction-sections', '-fdata-sections'"
46+
export COMMON_FLAGS="'-march=armv6k','-mtune=mpcore','-mfloat-abi=hard','-mtp=soft','-mword-relocations','-ffunction-sections','-fdata-sections'"
4747

4848
# compat flags for some POSIX functions
4949
export EXTRA_COMPILE_FLAGS="'-DNOSTYLUS','-D_XOPEN_SOURCE'"
5050

51-
export COMPILE_FLAGS="'-D_3DS','-D__3DS__', '-isystem', '$LIBCTRU/include', '-I$PORTLIBS_PATH_3DS/include'"
51+
export COMPILE_FLAGS="'-D_3DS','-D__3DS__','-D__CONSOLE__','-D__NINTENDO_CONSOLE__','-isystem','$LIBCTRU/include','-I$PORTLIBS_PATH_3DS/include'"
5252

5353
export LINK_FLAGS="'-L$PORTLIBS_LIB','-L$LIBCTRU_LIB','-fPIE','-specs=$ARCH_DEVKIT_FOLDER/$TOOL_PREFIX/lib/3dsx.specs'"
5454

@@ -98,7 +98,7 @@ cpp_link_args = [$COMMON_FLAGS, $LINK_FLAGS]
9898
[properties]
9999
pkg_config_libdir = '$PKG_CONFIG_PATH'
100100
needs_exe_wrapper = true
101-
library_dirs= ['$LIBCTRU_LIB', '$PORTLIBS_LIB']
101+
library_dirs= ['$LIBCTRU_LIB','$PORTLIBS_LIB']
102102
libctru='$LIBCTRU'
103103
104104
APP_NAME = 'oopetris'

platforms/build-android.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ c_std = 'c11'
290290
cpp_std = 'c++23'
291291
c_args = ['--sysroot=${SYS_ROOT:?}','-fPIE','-fPIC','--target=$ARM_COMPILER_TRIPLE','-DHAVE_USR_INCLUDE_MALLOC_H','-D_MALLOC_H','-D__BITNESS=$BITNESS']
292292
cpp_args = ['--sysroot=${SYS_ROOT:?}','-fPIE','-fPIC','--target=$ARM_COMPILER_TRIPLE','-D__BITNESS=$BITNESS']
293-
c_link_args = ['-fPIE', '-L$SYS_ROOT/usr/lib']
294-
cpp_link_args = ['-fPIE', '-L$SYS_ROOT/usr/lib']
293+
c_link_args = ['-fPIE','-L$SYS_ROOT/usr/lib']
294+
cpp_link_args = ['-fPIE','-L$SYS_ROOT/usr/lib']
295295
prefix = '$SYS_ROOT'
296296
libdir = '$LIB_PATH'
297297
@@ -307,7 +307,7 @@ EOF
307307
ln -s "$BASE_PATH/sources/android/cpufeatures/cpu-features.c" "$PWD/subprojects/cpu-features/src/cpu-features.c"
308308
ln -s "$BASE_PATH/sources/android/cpufeatures/cpu-features.h" "$PWD/subprojects/cpu-features/include/cpu-features.h"
309309
cat <<EOF >"$PWD/subprojects/cpu-features/meson.build"
310-
project('cpu-features', 'c')
310+
project('cpu-features','c')
311311
312312
meson.override_dependency(
313313
'cpu-features',

platforms/build-switch.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export COMMON_FLAGS="'-ftls-model=local-exec','-march=armv8-a+crc+crypto','-mtun
4646
# compat flags for some POSIX functions
4747
export EXTRA_COMPILE_FLAGS="'-DNOSTYLUS','-D_XOPEN_SOURCE'"
4848

49-
export COMPILE_FLAGS="'-D__SWITCH__','-isystem','$LIBNX/include','-I$PORTLIBS_PATH_SWITCH/include'"
49+
export COMPILE_FLAGS="'-D__SWITCH__','-D__CONSOLE__','-D__NINTENDO_CONSOLE__','-isystem','$LIBNX/include','-I$PORTLIBS_PATH_SWITCH/include'"
5050

5151
export LINK_FLAGS="'-L$PORTLIBS_LIB','-L$LIBNX_LIB','-fPIE','-specs=$DEVKITPRO/libnx/switch.specs'"
5252

@@ -96,7 +96,7 @@ cpp_link_args = [$COMMON_FLAGS, $LINK_FLAGS]
9696
[properties]
9797
pkg_config_libdir = '$PKG_CONFIG_PATH'
9898
needs_exe_wrapper = true
99-
library_dirs= ['$LIBNX_LIB', '$PORTLIBS_LIB']
99+
library_dirs= ['$LIBNX_LIB','$PORTLIBS_LIB']
100100
libnx='$LIBNX'
101101
102102
APP_NAME = 'oopetris'

src/application.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
#include <ranges>
1010
#include <stdexcept>
1111

12-
#if defined(__SWITCH__)
13-
#include <switch.h>
14-
#elif defined(__3DS__)
15-
#include <3ds.h>
12+
#if defined(__CONSOLE__)
13+
#include "helper/console_helpers.hpp"
1614
#endif
1715

1816
namespace {
@@ -65,20 +63,13 @@ void Application::run() {
6563
auto start_execution_time = std::chrono::steady_clock::now();
6664

6765
while (m_is_running
68-
//TODO factor out
69-
#if defined(__SWITCH__)
70-
// see https://switchbrew.github.io/libnx/applet_8h.html#a7ed640e5f4a81ed3960c763fdc1521c5
71-
// this checks for some other reasons why this app should quit, its switch specific
72-
and appletMainLoop()
73-
#elif defined(__3DS__)
74-
// see https://libctru.devkitpro.org/apt_8h.html#a84808c36d9a8c389896ecf241c7f89cb
75-
// this checks for some other reasons why this app should quit, its 3ds specific
76-
and aptMainLoop()
66+
67+
#if defined(__CONSOLE__)
68+
and console::inMainLoop()
7769
#endif
7870

7971
) {
8072

81-
8273
m_event_dispatcher.dispatch_pending_events();
8374
update();
8475
render();

src/helper/console_helpers.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#include "helper/errors.hpp"
3+
#if defined(__NINTENDO_CONSOLE__)
4+
5+
#include "console_helpers.hpp"
6+
7+
#if defined(__SWITCH__)
8+
#include <switch.h>
9+
#elif defined(__3DS__)
10+
#include <3ds.h>
11+
#endif
12+
13+
void console::debug_write(const char* text, size_t size) {
14+
#ifdef __3DS__
15+
svcOutputDebugString(text, size);
16+
#elif defined(__SWITCH__)
17+
svcOutputDebugString(text, size);
18+
#else
19+
#error "not implemented"
20+
#endif
21+
}
22+
23+
void console::platform_init() {
24+
#if defined(__3DS__) || defined(__SWITCH__)
25+
Result ret = romfsInit();
26+
if (R_FAILED(ret)) {
27+
throw helper::IniInitializationError(fmt::format("romfsInit() failed: {:#2x}", (unsigned int) ret));
28+
}
29+
#else
30+
#error "not implemented"
31+
#endif
32+
}
33+
34+
void console::platform_exit() {
35+
#if defined(__3DS__) || defined(__SWITCH__)
36+
Result ret = romfsExit();
37+
if (R_FAILED(ret)) {
38+
throw helper::IniInitializationError(fmt::format("romfsExit() failed: {:#2x}", (unsigned int) ret));
39+
}
40+
#else
41+
#error "not implemented"
42+
#endif
43+
}
44+
45+
bool console::inMainLoop() {
46+
#ifdef __3DS__
47+
return aptMainLoop();
48+
#elif defined(__SWITCH__)
49+
return appletMainLoop();
50+
#else
51+
#error "not implemented"
52+
#endif
53+
}
54+
55+
56+
#endif

src/helper/console_helpers.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
3+
#pragma once
4+
5+
#if defined(__CONSOLE__)
6+
7+
namespace console {
8+
9+
void debug_write(const char* text, size_t size);
10+
11+
inline void debug_print(const char* text) {
12+
debug_write(text, strlen(text));
13+
}
14+
15+
inline void debug_print(const std::string& value) {
16+
debug_write(value.c_str(), value.size());
17+
}
18+
19+
void platform_init();
20+
21+
void platform_exit();
22+
23+
[[nodiscard]] bool inMainLoop();
24+
25+
26+
template<typename Mutex>
27+
class debug_sink final : public base_sink<Mutex> {
28+
public:
29+
explicit debug_sink() { }
30+
debug_sink(const debug_sink&) = delete;
31+
debug_sink& operator=(const debug_sink&) = delete;
32+
33+
protected:
34+
void sink_it_(const details::log_msg& msg) override {
35+
memory_buf_t formatted;
36+
base_sink<Mutex>::formatter_->format(msg, formatted);
37+
debug_write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
38+
}
39+
40+
void flush_() override {
41+
// noop
42+
}
43+
44+
std::ostream& ostream_;
45+
bool force_flush_;
46+
};
47+
48+
using debug_sink_mt = debug_sink<std::mutex>;
49+
using debug_sink_st = debug_sink<details::null_mutex>;
50+
51+
52+
} // namespace console
53+
54+
55+
#endif

src/helper/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ core_src_files += files(
2929
graphics_src_files += files(
3030
'clock_source.cpp',
3131
'clock_source.hpp',
32+
'console_helpers.cpp',
33+
'console_helpers.hpp',
3234
'graphic_utils.cpp',
3335
'graphic_utils.hpp',
3436
'message_box.cpp',

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ int main(int argc, char** argv) {
2525
std::vector<spdlog::sink_ptr> sinks;
2626
#if defined(__ANDROID__)
2727
sinks.push_back(std::make_shared<spdlog::sinks::android_sink_mt>());
28+
#elif defined(__CONSOLE__)
29+
sinks.push_back(std::make_shared<console::debug_sink_mt>());
2830
#else
29-
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_st>());
31+
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
3032
#endif
3133
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
3234
fmt::format("{}/oopetris.log", logs_path.string()), 1024 * 1024 * 10, 5, true

0 commit comments

Comments
 (0)