Skip to content

Commit

Permalink
Have the storage work for emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerdes committed Mar 8, 2022
1 parent 9086840 commit 0b151cd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
add_link_options("SHELL:-s USE_PTHREADS=1")
add_link_options("SHELL:-s WASM_MEM_MAX=100Mb")
add_link_options("SHELL:-s PTHREAD_POOL_SIZE=2")
add_link_options("SHELL:-lidbfs.js")
add_link_options("SHELL:--shell-file ../../build/emscripten/shell.html")
else()
endif()
Expand Down
25 changes: 21 additions & 4 deletions src/common/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
#include <stdio.h>

#if GOLF_PLATFORM_ANDROID

#include <sys/stat.h>
#include <android/native_activity.h>

#elif GOLF_PLATFORM_EMSCRIPTEN

#include <emscripten.h>
#include "3rd_party/sokol/sokol_time.h"

#endif

#include "3rd_party/parson/parson.h"
#include "3rd_party/sokol/sokol_app.h"
#include "common/log.h"
#include "common/map.h"

static JSON_Value *_storage_json_val;
Expand Down Expand Up @@ -49,6 +57,7 @@ static bool _get_buf_from_file(const char *file, char **buf, int *buf_len) {
}

static bool _golf_storage_get_buf(char **buf, int *buf_len) {

#if GOLF_PLATFORM_WINDOWS | GOLF_PLATFORM_LINUX

return _get_buf_from_file("storage.json", buf, buf_len);
Expand Down Expand Up @@ -79,7 +88,6 @@ static bool _golf_storage_get_buf(char **buf, int *buf_len) {

void golf_storage_init(void) {
#if GOLF_PLATFORM_ANDROID
golf_log_note("Golf storaage init android");

ANativeActivity *native_activity = sapp_android_get_native_activity();
const char *internal_path = native_activity->internalDataPath;
Expand All @@ -104,8 +112,15 @@ void golf_storage_init(void) {
});
);

// Wait for the sync to be done
while (emscripten_run_script_int("Module.syncdone") == 0);
#endif
}

bool golf_storage_finish_init(void) {
#ifdef GOLF_PLATFORM_EMSCRIPTEN

if (emscripten_run_script_int("Module.syncdone") == 0) {
return false;
}

#endif

Expand All @@ -119,6 +134,7 @@ void golf_storage_init(void) {
_storage_json_val = json_value_init_object();
}
_storage_json_obj = json_value_get_object(_storage_json_val);
return true;
}

void golf_storage_set_num(const char *key, float num) {
Expand Down Expand Up @@ -163,8 +179,9 @@ void golf_storage_save(void) {
_save_buf_to_file("/opengolf_persistent_data/storage.json", buf, buf_size - 1);

EM_ASM(
FS.syncfs(false, function (err) {
FS.syncfs(function (err) {
assert(!err);
console.log("SAVED");
});
);

Expand Down
1 change: 1 addition & 0 deletions src/common/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdbool.h>

void golf_storage_init(void);
bool golf_storage_finish_init(void);
void golf_storage_set_num(const char *key, float num);
bool golf_storage_get_num(const char *key, float *num);
void golf_storage_save(void);
Expand Down
6 changes: 6 additions & 0 deletions src/golf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void cleanup(void) {
}

static void frame(void) {
static bool storage_inited = false;
static bool inited = false;
static uint64_t last_time = 0;

Expand All @@ -54,6 +55,11 @@ static void frame(void) {
inited = true;
}

if (!storage_inited && !golf_storage_finish_init()) {
return;
}
storage_inited = true;

golf_data_update(dt);

golf_graphics_begin_frame(dt);
Expand Down

0 comments on commit 0b151cd

Please sign in to comment.