File tree Expand file tree Collapse file tree 5 files changed +91
-8
lines changed Expand file tree Collapse file tree 5 files changed +91
-8
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ src_files += files(
5252 ' types.hpp' ,
5353 ' utils.cpp' ,
5454 ' utils.hpp' ,
55+ ' web_utils.cpp' ,
5556 ' window.cpp' ,
5657 ' window.hpp' ,
5758)
Original file line number Diff line number Diff line change 22#include " utils.hpp"
33#include < tl/optional.hpp>
44
5+ #if defined(__EMSCRIPTEN__)
6+ #include " web_utils.hpp"
7+ #endif
8+
59TetrisApplication::TetrisApplication (CommandLineArguments command_line_arguments)
610#if defined(__ANDROID__)
711 : Application{ " OOPetris" , WindowPosition::Centered, std::move (command_line_arguments) } {
@@ -135,13 +139,19 @@ void TetrisApplication::render() const {
135139 }
136140}
137141
138- void TetrisApplication::try_load_settings () try {
139- std::ifstream settings_file{ settings_filename };
140- m_settings = nlohmann::json::parse (settings_file);
141- spdlog::info (" settings loaded" );
142- } catch (...) {
143- spdlog::error (" unable to load settings from \" {}\" " , settings_filename);
144- spdlog::warn (" applying default settings" );
142+ void TetrisApplication::try_load_settings () {
143+ try {
144+ #if defined(__EMSCRIPTEN__)
145+ const std::string settings_string = utils::LocalStorage::get_item (settings_filename);
146+ m_settings = nlohmann::json::parse (settings_string);
147+ #else
148+ m_settings = nlohmann::json::parse (settings_file);
149+ #endif
150+ spdlog::info (" settings loaded" );
151+ } catch (...) {
152+ spdlog::error (" unable to load settings from \" {}\" " , settings_filename);
153+ spdlog::warn (" applying default settings" );
154+ }
145155}
146156
147157[[nodiscard]] bool TetrisApplication::is_replay_mode () const {
Original file line number Diff line number Diff line change @@ -16,8 +16,11 @@ struct TetrisApplication : public Application {
1616private:
1717 using TetrionHeaders = std::vector<Recording::TetrionHeader>;
1818
19+ #if defined(__EMSCRIPTEN__)
20+ static constexpr auto settings_filename = " settings_key" ;
21+ #else
1922 static constexpr auto settings_filename = " settings.json" ;
20-
23+ # endif
2124 std::vector<std::unique_ptr<ClockSource>> m_clock_sources;
2225 std::vector<SimulationStep> m_simulation_step_indices;
2326 std::vector<std::unique_ptr<Tetrion>> m_tetrions;
Original file line number Diff line number Diff line change 1+
2+ #if defined(__EMSCRIPTEN__)
3+ #include " web_utils.hpp"
4+ #include < emscripten.h>
5+ #include < string>
6+ #include < tl/optional.hpp>
7+
8+ tl::optional<std::string> utils::LocalStorage::get_item (const std::string& key) {
9+
10+
11+ thread_local const emscripten::val localStorage = emscripten::val::global (" localStorage" );
12+
13+ emscripten::val value localStorage.getItem (key.c_str ());
14+
15+ if (value.isUndefined ()) {
16+ return tl::nullopt ;
17+ }
18+
19+ return value.as <std::string | undefined>();
20+ }
21+
22+ void utils::LocalStorage::set_item (const std::string& key, const std::string& value) {
23+
24+ thread_local const emscripten::val localStorage = emscripten::val::global (" localStorage" );
25+
26+ localStorage.setItem (key.c_str (), value.c_str ());
27+ }
28+
29+ void utils::LocalStorage::remove_item (const std::string& key) {
30+
31+ thread_local const emscripten::val localStorage = emscripten::val::global (" localStorage" );
32+
33+ localStorage.removeItem (key.c_str ());
34+ }
35+
36+ void utils::LocalStorage::clear () {
37+
38+ thread_local const emscripten::val localStorage = emscripten::val::global (" localStorage" );
39+
40+ localStorage.clear ();
41+ }
42+
43+
44+ #endif
Original file line number Diff line number Diff line change 1+
2+
3+ #pragma once
4+
5+ #include < string>
6+ #include < tl/optional.hpp>
7+
8+
9+ #if not defined(__EMSCRIPTEN__)
10+ #error this header is for emscripten only
11+ #endif
12+
13+ namespace utils {
14+
15+
16+ struct LocalStorage {
17+
18+ static tl::optional<std::string> [[nodiscard]] get_item(const std::string& key);
19+ static void set_item (const std::string& key, const std::string& value);
20+ static void remove_item (const std::string& key);
21+ static void clear ();
22+ };
23+
24+
25+ }; // namespace utils
You can’t perform that action at this time.
0 commit comments