From 89f5044f4233a010d4ff5e745af54f72b1dbfbb3 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 3 Jun 2024 03:24:58 +0200 Subject: [PATCH 1/7] platforms: add support for serenityOS - add package.sh port script, that has to be placed inside the serenity repos Ports/ dir - detect serenity os and add define to use in code - add some small functionality that every new platform requires --- platforms/serenity/package.sh | 36 +++++++++++++++++++++++++++++++++++ src/helper/platform.cpp | 6 ++++++ src/libs/core/helper/date.cpp | 2 +- tools/options/meson.build | 7 +++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 platforms/serenity/package.sh diff --git a/platforms/serenity/package.sh b/platforms/serenity/package.sh new file mode 100644 index 00000000..374b6be9 --- /dev/null +++ b/platforms/serenity/package.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env -S bash ../.port_include.sh +port='oopetris' +version='0.5.6' +files=( + "git+https://github.com/OpenBrickProtocolFoundation/oopetris#${version}" +) +useconfigure='true' +configopts=( + '--cross-file' + "${SERENITY_BUILD_DIR}/meson-cross-file.txt" +) +depends=( + 'SDL2' + 'SDL2_image' + 'SDL2_mixer' + 'SDL2_ttf' + 'nlohmann-json' +) + +configure() { + # TODO: Figure out why GCC doesn't autodetect that libgcc_s is needed. (stolen from glib port) + if [ "${SERENITY_TOOLCHAIN}" = 'GNU' ]; then + export LDFLAGS='-lgcc_s' + fi + + run meson setup _build "${configopts[@]}" +} + +build() { + run meson compile -C _build +} + +install() { + export DESTDIR="${SERENITY_INSTALL_ROOT}" + run meson install -C _build +} diff --git a/src/helper/platform.cpp b/src/helper/platform.cpp index 9922cc96..9f3cd49c 100644 --- a/src/helper/platform.cpp +++ b/src/helper/platform.cpp @@ -1,5 +1,6 @@ #include "platform.hpp" +#include "helper/utils.hpp" #if defined(__CONSOLE__) #include "helper/console_helpers.hpp" @@ -51,6 +52,8 @@ namespace { return "Windows"; #elif defined(__APPLE__) return "MacOS"; +#elif defined(__SERENITY__) + return "Serenity OS"; #else #error "Unsupported platform" #endif @@ -71,6 +74,9 @@ namespace { auto result = console::open_url(url); spdlog::info("Returned string from url open was: {}", result); return true; +#elif defined(__SERENITY__) + UNUSED(url); + return false; #else //TODO(Totto): this is dangerous, if we supply user input, so use SDL_OpenURL preferably diff --git a/src/libs/core/helper/date.cpp b/src/libs/core/helper/date.cpp index dc78b9e8..b4b5368d 100644 --- a/src/libs/core/helper/date.cpp +++ b/src/libs/core/helper/date.cpp @@ -32,7 +32,7 @@ helper::expected date::ISO8601Date::from_string( std::tm tm = {}; -#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) +#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__SERENITY__) std::istringstream input_stream{ input }; diff --git a/tools/options/meson.build b/tools/options/meson.build index 77687208..2038cd8b 100644 --- a/tools/options/meson.build +++ b/tools/options/meson.build @@ -25,6 +25,13 @@ graphics_lib = { 'deps': [], } +if meson.is_cross_build() and host_machine.system() == 'serenity' + core_lib += { + 'compile_args': [core_lib.get('compile_args'), '-D__SERENITY__'], + } +endif + + cpp = meson.get_compiler('cpp') build_with_libcpp = false From 3ae6bce0a5a0d7df356ec3a1a9e615512c6ae1b7 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 3 Jun 2024 03:27:45 +0200 Subject: [PATCH 2/7] serenity: fix compilation errors --- src/helper/platform.cpp | 3 ++- src/libs/core/helper/date.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/helper/platform.cpp b/src/helper/platform.cpp index 9f3cd49c..dd464240 100644 --- a/src/helper/platform.cpp +++ b/src/helper/platform.cpp @@ -1,6 +1,7 @@ #include "platform.hpp" -#include "helper/utils.hpp" + +#include #if defined(__CONSOLE__) #include "helper/console_helpers.hpp" diff --git a/src/libs/core/helper/date.cpp b/src/libs/core/helper/date.cpp index b4b5368d..2d5e9bac 100644 --- a/src/libs/core/helper/date.cpp +++ b/src/libs/core/helper/date.cpp @@ -32,7 +32,8 @@ helper::expected date::ISO8601Date::from_string( std::tm tm = {}; -#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__SERENITY__) +#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) \ + || defined(__SERENITY__) std::istringstream input_stream{ input }; From 2371a79512c8817f93a98683fd175075e0725c43 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 18 Jun 2024 19:12:00 +0200 Subject: [PATCH 3/7] serenity: fix runtime crashes and errors: - use software rendering, since hardware rendering isn't supported - fix installation of files --- platforms/serenity/package.sh | 1 + src/executables/game/main.cpp | 2 +- src/graphics/renderer.cpp | 2 +- src/helper/graphic_utils.cpp | 45 ++++++++++++++++++++++++++++++++++- tools/options/meson.build | 8 +++++-- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/platforms/serenity/package.sh b/platforms/serenity/package.sh index 374b6be9..36143a43 100644 --- a/platforms/serenity/package.sh +++ b/platforms/serenity/package.sh @@ -8,6 +8,7 @@ useconfigure='true' configopts=( '--cross-file' "${SERENITY_BUILD_DIR}/meson-cross-file.txt" + "-Dbuildtype=release" ) depends=( 'SDL2' diff --git a/src/executables/game/main.cpp b/src/executables/game/main.cpp index a58daae8..46acced5 100644 --- a/src/executables/game/main.cpp +++ b/src/executables/game/main.cpp @@ -92,7 +92,7 @@ namespace { try { -#if defined(__ANDROID__) or defined(__CONSOLE__) +#if defined(__ANDROID__) or defined(__CONSOLE__) or defined(__SERENITY__) window = std::make_shared(window_name, WindowPosition::Centered); #else [[maybe_unused]] static constexpr int width = 1280; diff --git a/src/graphics/renderer.cpp b/src/graphics/renderer.cpp index b5f71f80..78a8cc46 100644 --- a/src/graphics/renderer.cpp +++ b/src/graphics/renderer.cpp @@ -10,7 +10,7 @@ Renderer::Renderer(const Window& window, const VSync v_sync) window.get_sdl_window(), -1, (v_sync == VSync::Enabled ? SDL_RENDERER_PRESENTVSYNC : 0) | SDL_RENDERER_TARGETTEXTURE -#if defined(__3DS__) +#if defined(__3DS__) || defined(__SERENITY__) | SDL_RENDERER_SOFTWARE #else | SDL_RENDERER_ACCELERATED diff --git a/src/helper/graphic_utils.cpp b/src/helper/graphic_utils.cpp index 080969c2..d387f099 100644 --- a/src/helper/graphic_utils.cpp +++ b/src/helper/graphic_utils.cpp @@ -48,7 +48,7 @@ std::vector utils::supported_features() { throw std::runtime_error{ "Failed to get flatpak data directory (XDG_DATA_HOME)" }; } - return std::filesystem::path{ data_home }; + return std::filesystem::path{ data_home } / "share" / constants::program_name; #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) char* pref_path = SDL_GetPrefPath(constants::author, constants::program_name); if (!pref_path) { @@ -58,6 +58,33 @@ std::vector utils::supported_features() { #else #error "unrecognized installer build" #endif +#elif defined(INSTALL_FILES) +#if !defined(INSTALL_LOCATION) +#error "missing 'INSTALL_LOCATION' define" +#endif + +#if defined(__SERENITY__) + + + // this is a read write location in the serenityos case build, see https://docs.flatpak.org/en/latest/conventions.html + const char* data_home = std::getenv("HOME"); + if (data_home == nullptr) { + throw std::runtime_error{ "Failed to get flatpak data directory (XDG_DATA_HOME)" }; + } + + return std::filesystem::path{ data_home } / "share" / constants::program_name; + +#else + +#define STRINGIFY(a) STRINGIFY_HELPER_(a) //NOLINT(cppcoreguidelines-macro-usage) +#define STRINGIFY_HELPER_(a) #a + + return std::filesystem::path{ STRINGIFY(INSTALL_LOCATION) } / "share" / constants::program_name; + + +#undef STRINGIFY +#undef STRINGIFY_HELPER_ +#endif #else // this is only used in local build for debugging, when compiling in release mode the path is real path where the app can store many things without interfering with other things (eg. AppData\Roaming\... on Windows or .local/share/... on Linux ) return std::filesystem::path{ "." }; @@ -86,7 +113,23 @@ std::vector utils::supported_features() { #else #error "unrecognized installer build" #endif +#elif defined(INSTALL_FILES) +#if !defined(INSTALL_LOCATION) +#error "missing 'INSTALL_LOCATION' define" +#endif + +#define STRINGIFY(a) STRINGIFY_HELPER_(a) //NOLINT(cppcoreguidelines-macro-usage) +#define STRINGIFY_HELPER_(a) #a + + return std::filesystem::path{ STRINGIFY(INSTALL_LOCATION) } / "share" / "oopetris" / "assets"; + + +#undef STRINGIFY +#undef STRINGIFY_HELPER_ + #else + + return std::filesystem::path{ "assets" }; #endif } diff --git a/tools/options/meson.build b/tools/options/meson.build index 2038cd8b..ff153c1d 100644 --- a/tools/options/meson.build +++ b/tools/options/meson.build @@ -27,11 +27,15 @@ graphics_lib = { if meson.is_cross_build() and host_machine.system() == 'serenity' core_lib += { - 'compile_args': [core_lib.get('compile_args'), '-D__SERENITY__'], + 'compile_args': [ + core_lib.get('compile_args'), + '-D__SERENITY__', + '-DINSTALL_FILES', + '-DINSTALL_LOCATION=' + get_option('prefix'), + ], } endif - cpp = meson.get_compiler('cpp') build_with_libcpp = false From 70f1f44df9d1d088ce154b7b1ef5a664ffb1ded2 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 18 Jun 2024 19:16:48 +0200 Subject: [PATCH 4/7] Serenity OS: add 60 fps as default, since VSync doesn't work that well --- src/executables/game/parser.cpp | 40 ++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/executables/game/parser.cpp b/src/executables/game/parser.cpp index 591a5e31..8f759c27 100644 --- a/src/executables/game/parser.cpp +++ b/src/executables/game/parser.cpp @@ -41,23 +41,31 @@ helper::expected helper::parse_args(const std spdlog::error("invalid value for target fps ({}), using default value instead (VSYNC)", fps.value()); } } - - const auto level = parser.get("--level"); - if (level <= 30) { - result.starting_level = level; - } else { - spdlog::error( - "invalid value for starting level ({}), using default value instead ({})", level, - CommandLineArguments::default_starting_level - ); - result.starting_level = CommandLineArguments::default_starting_level; +#if defined(__SERENITY__) + // serenity OS can#t handle vsync very well (Since it's inside qemu), so setting the target_fps value to 60 per default + if (not result.target_fps.has_value()) { + result.target_fps = 60; } + } +#endif + + + const auto level = parser.get("--level"); + if (level <= 30) { + result.starting_level = level; + } else { + spdlog::error( + "invalid value for starting level ({}), using default value instead ({})", level, + CommandLineArguments::default_starting_level + ); + result.starting_level = CommandLineArguments::default_starting_level; + } - result.silent = parser.get("--silent"); - - return result; + result.silent = parser.get("--silent"); - } catch (const std::exception& error) { - return helper::unexpected{ error.what() }; - } + return result; +} +catch (const std::exception& error) { + return helper::unexpected{ error.what() }; +} } From 2074153509cf3b846a896cf89f1873c5adc96ddf Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 18 Jun 2024 19:17:35 +0200 Subject: [PATCH 5/7] serenity: remove unnecessary "-lgcc_s" link option --- platforms/serenity/package.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/platforms/serenity/package.sh b/platforms/serenity/package.sh index 36143a43..e8ccccda 100644 --- a/platforms/serenity/package.sh +++ b/platforms/serenity/package.sh @@ -19,11 +19,6 @@ depends=( ) configure() { - # TODO: Figure out why GCC doesn't autodetect that libgcc_s is needed. (stolen from glib port) - if [ "${SERENITY_TOOLCHAIN}" = 'GNU' ]; then - export LDFLAGS='-lgcc_s' - fi - run meson setup _build "${configopts[@]}" } From 186d23f4565b7fcbb41bdf856c9cf22d6f63d546 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 18 Jun 2024 19:23:02 +0200 Subject: [PATCH 6/7] serenity os: fix small syntax error --- src/executables/game/parser.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/executables/game/parser.cpp b/src/executables/game/parser.cpp index 8f759c27..b05618f6 100644 --- a/src/executables/game/parser.cpp +++ b/src/executables/game/parser.cpp @@ -46,26 +46,24 @@ helper::expected helper::parse_args(const std if (not result.target_fps.has_value()) { result.target_fps = 60; } - } #endif - const auto level = parser.get("--level"); - if (level <= 30) { - result.starting_level = level; - } else { - spdlog::error( - "invalid value for starting level ({}), using default value instead ({})", level, - CommandLineArguments::default_starting_level - ); - result.starting_level = CommandLineArguments::default_starting_level; - } + const auto level = parser.get("--level"); + if (level <= 30) { + result.starting_level = level; + } else { + spdlog::error( + "invalid value for starting level ({}), using default value instead ({})", level, + CommandLineArguments::default_starting_level + ); + result.starting_level = CommandLineArguments::default_starting_level; + } - result.silent = parser.get("--silent"); + result.silent = parser.get("--silent"); - return result; -} -catch (const std::exception& error) { - return helper::unexpected{ error.what() }; -} + return result; + } catch (const std::exception& error) { + return helper::unexpected{ error.what() }; + } } From fd0f72ee4cd7b92c87c0da011fc0a954b728fe31 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 18 Jun 2024 19:39:05 +0200 Subject: [PATCH 7/7] Serenity OS: add to the readme's --- README.md | 2 +- docs/develop.md | 5 +++++ docs/serenity.md | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 docs/serenity.md diff --git a/README.md b/README.md index cc7d9d42..2ce78e93 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ It is written in [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming ## Platform support -This officially supports Linux, Windows, MacOS, Android, Nintendo Switch and Nintendo 3DS. +This officially supports Linux, Windows, MacOS, Android, Serenity OS, Nintendo Switch and Nintendo 3DS. Why these? Because it was fun to port the application to those platforms 😋 diff --git a/docs/develop.md b/docs/develop.md index 2a574225..ed263bb9 100644 --- a/docs/develop.md +++ b/docs/develop.md @@ -10,6 +10,11 @@ For concrete instructions, see the list below: [linux](linux.md) +## Serenity OS + +[serenity](serenity.md) + + ## Windows [windows](windows.md) diff --git a/docs/serenity.md b/docs/serenity.md new file mode 100644 index 00000000..aac79f01 --- /dev/null +++ b/docs/serenity.md @@ -0,0 +1,15 @@ +# Serenity OS build + +## How to build + +To add this to the serenity OS system, you have to build the port for it. + + +Copy `platforms/serenity/package.sh` to `Ports/oopetris/package.sh`, create the directory `Ports/oopetris/` and add the file to it. + +Then just `cd` into the directory and run the `package.sh`, like with all other Serenity OS ports. + + +## NOTE + +We don't have a CI for this port, so please report errors, if you find some.