diff --git a/lib/cfgpath/cfgpath.h b/lib/cfgpath/cfgpath.h --- a/lib/cfgpath/cfgpath.h +++ b/lib/cfgpath/cfgpath.h @@ -43,7 +43,7 @@ #include #endif -#if defined(__linux__) || defined(BSD) +#if defined(__linux__) || defined(BSD) || defined(__EMSCRIPTEN__) #define CFGPATH_LINUX #include #include diff --git a/src/lincity/MapPoint.hpp b/src/lincity/MapPoint.hpp --- a/src/lincity/MapPoint.hpp +++ b/src/lincity/MapPoint.hpp @@ -27,9 +27,8 @@ #include // for size_t #include // for ostream -namespace std { -template struct hash; // IWYU pragma: keep -} // namespace std +#include // for hash (fredygames: libc++ keeps std::hash in an + // inline namespace, so it cannot be forward-declared) class MapPoint { public: diff --git a/src/lincity-ng/Game.cpp b/src/lincity-ng/Game.cpp --- a/src/lincity-ng/Game.cpp +++ b/src/lincity-ng/Game.cpp @@ -21,6 +21,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ** ---------------------------------------------------------------------- */ +#ifdef __EMSCRIPTEN__ +#include +#endif #include "Game.hpp" #include // for SDL_KeyCode, Uint32, SDL_... @@ -493,6 +496,10 @@ SDL_Event event; int event_timeout = next_task - SDL_GetTicks(); if(event_timeout < 0) event_timeout = 0; +#ifdef __EMSCRIPTEN__ + // fredygames: let the browser run even when a task is overdue + if(event_timeout == 0) emscripten_sleep(0); +#endif int status = SDL_WaitEventTimeout(&event, event_timeout); if(!status) break; // timed out diff --git a/src/lincity-ng/GameView.cpp b/src/lincity-ng/GameView.cpp --- a/src/lincity-ng/GameView.cpp +++ b/src/lincity-ng/GameView.cpp @@ -124,7 +124,14 @@ blankGraphicsInfo.y = blankGraphicsInfo.texture->getHeight(); stopThread = false; +#ifdef __EMSCRIPTEN__ + // fredygames: no threads in the browser build + loaderThread = NULL; + preReadImages(); + textures_ready = true; +#else loaderThread = SDL_CreateThread( gameViewThread, "Loader", this ); +#endif //GameView is resizable setFlags(FLAG_RESIZABLE); diff --git a/src/lincity-ng/main.cpp b/src/lincity-ng/main.cpp --- a/src/lincity-ng/main.cpp +++ b/src/lincity-ng/main.cpp @@ -23,6 +23,10 @@ #include "main.hpp" +#ifdef __EMSCRIPTEN__ +#include +#endif + #include // for SDL_GetError, SDL_Se... #include // for TTF_Init, TTF_Quit #include // for println @@ -237,6 +241,16 @@ MainMenu(window).run(); } +#ifdef __EMSCRIPTEN__ +// fredygames: the page calls this when the tab is hidden or closed, so the +// options reach the browser storage even if the game never quits. +static bool fredygamesReady = false; +extern "C" EMSCRIPTEN_KEEPALIVE void fredygames_save() { + if(fredygamesReady) + getConfig()->save(); +} +#endif + int main(int argc, char** argv) { // initialize XML parser early because it is needed for parsing the config @@ -289,6 +303,9 @@ initVideo(getConfig()->videoX.get(), getConfig()->videoY.get()); initLincity(); std::unique_ptr sound(new Sound()); +#ifdef __EMSCRIPTEN__ + fredygamesReady = true; +#endif // enter main loop mainLoop(); diff --git a/src/lincity-ng/MainMenu.cpp b/src/lincity-ng/MainMenu.cpp --- a/src/lincity-ng/MainMenu.cpp +++ b/src/lincity-ng/MainMenu.cpp @@ -21,6 +21,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ** ---------------------------------------------------------------------- */ +#ifdef __EMSCRIPTEN__ +#include +#endif #include "MainMenu.hpp" #include // for SDL_EventType, Uint32, SDL_Ge... @@ -869,6 +872,10 @@ SDL_Event event; int event_timeout = next_task - SDL_GetTicks(); if(event_timeout < 0) event_timeout = 0; +#ifdef __EMSCRIPTEN__ + // fredygames: let the browser run even when a task is overdue + if(event_timeout == 0) emscripten_sleep(0); +#endif int status = SDL_WaitEventTimeout(&event, event_timeout); if(!status) break; // timed out diff --git a/src/lincity-ng/Sound.cpp b/src/lincity-ng/Sound.cpp --- a/src/lincity-ng/Sound.cpp +++ b/src/lincity-ng/Sound.cpp @@ -259,7 +259,11 @@ return; } else { audioOpen = true; +#ifdef __EMSCRIPTEN__ + soundThread(this); // fredygames: no threads in the browser build +#else loaderThread = SDL_CreateThread(soundThread, "Sound", this); +#endif } musicTrack = MIX_CreateTrack(mixer);