--- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,18 @@ #include "editor.h" #include "view.h" +#ifdef __EMSCRIPTEN__ +#include + +static View *fredygames_view = NULL; + +extern "C" EMSCRIPTEN_KEEPALIVE void fredygames_save() +{ + if (fredygames_view) + fredygames_view->fredygamesSave(); +} +#endif + int main(int argc, char **argv) { /* i18n */ @@ -50,6 +62,12 @@ Config config; ClientGame cgame(config); View view(config, cgame); +#ifdef __EMSCRIPTEN__ + fredygames_view = &view; +#endif view.runMenu(); +#ifdef __EMSCRIPTEN__ + fredygames_view = NULL; +#endif return 0; } --- a/src/sdl.h +++ b/src/sdl.h @@ -22,6 +22,32 @@ #include #include "tools.h" +#ifdef __EMSCRIPTEN__ +/* In the browser, events are queued while a loop yields (SDL_Delay or + * SDL_RenderPresent), right before the loops flush mouse motion, so the + * flush would drop every motion event and menus would never see the + * pointer. Keep the newest motion event, which still prevents a backlog. */ +static inline void fredygames_FlushEvent(Uint32 type) +{ + if (type != SDL_MOUSEMOTION) { + SDL_FlushEvent(type); + return; + } + SDL_Event ev[16], last; + int n; + bool have = false; + while ((n = SDL_PeepEvents(ev, 16, SDL_GETEVENT, + SDL_MOUSEMOTION, SDL_MOUSEMOTION)) > 0) { + last = ev[n - 1]; + have = true; + } + if (have) + SDL_PeepEvents(&last, 1, SDL_ADDEVENT, + SDL_MOUSEMOTION, SDL_MOUSEMOTION); +} +#define SDL_FlushEvent(type) fredygames_FlushEvent(type) +#endif + class MainWindow { public: SDL_Window* mw; @@ -40,6 +66,13 @@ void refresh(); static int getModeNames(vector &list) { +#ifdef __EMSCRIPTEN__ + /* the web page scales the canvas, so only the render size is chosen */ + list.clear(); + list.push_back(_("1080p")); + list.push_back(_("720p")); + return list.size(); +#endif SDL_DisplayMode mode; SDL_GetCurrentDisplayMode(0,&mode); list.clear(); @@ -52,6 +85,9 @@ return list.size(); } static int getModeResolution(int mode) { +#ifdef __EMSCRIPTEN__ + return mode == 1 ? 720 : 1080; +#endif switch (mode) { case 0: return 0; case 1: return 480; --- a/src/view.h +++ b/src/view.h @@ -70,6 +70,9 @@ Uint32 brickScreenWidth, brickScreenHeight; int scaleFactor; // *100, e.g., 140 means 1.4 bool quitReceived; +#ifdef __EMSCRIPTEN__ + bool playing = false; /* inside run(), for fredygamesSave() */ +#endif /* render parts */ Label lblTitle; SmoothCounter lblTitleCounter; @@ -139,6 +142,9 @@ void render(bool clear=false); void runMenu(); void renderMenu(); +#ifdef __EMSCRIPTEN__ + void fredygamesSave(); +#endif }; #endif /* VIEW_H_ */ --- a/src/view.cpp +++ b/src/view.cpp @@ -336,6 +336,9 @@ grabInput(1); +#ifdef __EMSCRIPTEN__ + playing = true; +#endif while (!leave) { flags = 0; @@ -570,6 +573,9 @@ SDL_Delay(frameDelay); SDL_FlushEvent(SDL_MOUSEMOTION); /* prevent event loop from dying */ } +#ifdef __EMSCRIPTEN__ + playing = false; +#endif if (editor.testRequested()) { /* do nothing */ @@ -1726,6 +1732,19 @@ } } +#ifdef __EMSCRIPTEN__ +/** Called by the web page when the tab is hidden or closed: a browser tab + * is usually closed without quitting, so the destructors that save config + * and hiscores never run. A running game is saved to be resumed later. */ +void View::fredygamesSave() +{ + if (playing) + saveGame(); + cgame.saveHiscores(); + config.save(); +} +#endif + void View::saveGame() { vector> &players = cgame.getPlayers(); --- a/src/levels/Makefile.am +++ b/src/levels/Makefile.am @@ -1,20 +1,15 @@ # Use ls | grep -v Makefile | sed -e "s/$/ \\\\/" for update EXTRA_DIST = 1010011010 \ Abc \ -Afl \ Africa \ Alex \ Alexandre \ -AllNighter \ AllWorld \ America \ Arcade \ -Arkanoid \ Asia \ Astra \ Astro \ -Belief \ -BeOS-4ever \ Bertrand \ BigBang \ Biolab428 \ @@ -26,20 +21,14 @@ Choices \ Circle \ Classique \ -Color \ Composers \ Cool \ Crossing \ D2K \ Daves \ -Demons \ Europe \ Family \ -Fantaisie \ -FatalDoomsday \ -FatalDoomsday2 \ Flower \ -Fly \ Football \ Freek \ Frozen \ @@ -47,95 +36,62 @@ Gatinha \ Germany \ GreenWall \ -GreenWall2 \ -GreenWall3 \ -GreenWall4 \ Hans \ -HartzIV \ HereWeGo \ -Heroes \ Hieroglyphs \ HighBall \ Holidays \ -Hommage \ Horror \ Impossibility \ Invisible \ -IoriYagami \ Iris \ -IrrenhausAh \ -Izusiowe \ -JediAdventure \ JustForFun \ -JustGoForIt \ Kazan-1 \ -Kevin \ Karnickel \ KungFu \ -Lapis \ Lattices \ Lattsville \ LBreakout1 \ LBreakout2 \ LBreakoutHD \ -LinuxFun \ -Lord \ Madness \ Maze \ Megadoomer \ -MegaMan2 \ Memory \ MontyHall \ Nations \ Negundoid \ -NES \ NewBricks \ -NewOERG1 \ -ObiWan \ Oceania \ -OpenSource \ -OurWorld \ -Pabelo \ Patience \ Pawset \ -PCGames \ Ph33r \ Picky \ -P.I.T.A. \ PlayAgain \ Popcorn \ RichQuick \ Runes \ R-World \ Sadistic \ -Shimitar \ Silly \ SkillShot \ Smilies \ SnyMaxx \ -StarWars \ Symmetry \ Symmetry-MKII \ T.C.S. \ -TheEnd \ TheGauntlet \ TimeWaster \ TrickShot \ Tricky \ -Twilight \ -Vader \ WallBricks \ Weirdness \ Wizi \ -Wolvie \ WonderWalls \ -WTF \ X \ Yexter \ -Yoda \ Zigies \ -Zooligan \ -Zufallswelt +Zooligan install-data-local: $(mkinstalldirs) $(DESTDIR)$(datdir)/levels --- a/src/levels/Makefile.in +++ b/src/levels/Makefile.in @@ -249,20 +249,15 @@ # Use ls | grep -v Makefile | sed -e "s/$/ \\\\/" for update EXTRA_DIST = 1010011010 \ Abc \ -Afl \ Africa \ Alex \ Alexandre \ -AllNighter \ AllWorld \ America \ Arcade \ -Arkanoid \ Asia \ Astra \ Astro \ -Belief \ -BeOS-4ever \ Bertrand \ BigBang \ Biolab428 \ @@ -274,20 +269,14 @@ Choices \ Circle \ Classique \ -Color \ Composers \ Cool \ Crossing \ D2K \ Daves \ -Demons \ Europe \ Family \ -Fantaisie \ -FatalDoomsday \ -FatalDoomsday2 \ Flower \ -Fly \ Football \ Freek \ Frozen \ @@ -295,95 +284,62 @@ Gatinha \ Germany \ GreenWall \ -GreenWall2 \ -GreenWall3 \ -GreenWall4 \ Hans \ -HartzIV \ HereWeGo \ -Heroes \ Hieroglyphs \ HighBall \ Holidays \ -Hommage \ Horror \ Impossibility \ Invisible \ -IoriYagami \ Iris \ -IrrenhausAh \ -Izusiowe \ -JediAdventure \ JustForFun \ -JustGoForIt \ Kazan-1 \ -Kevin \ Karnickel \ KungFu \ -Lapis \ Lattices \ Lattsville \ LBreakout1 \ LBreakout2 \ LBreakoutHD \ -LinuxFun \ -Lord \ Madness \ Maze \ Megadoomer \ -MegaMan2 \ Memory \ MontyHall \ Nations \ Negundoid \ -NES \ NewBricks \ -NewOERG1 \ -ObiWan \ Oceania \ -OpenSource \ -OurWorld \ -Pabelo \ Patience \ Pawset \ -PCGames \ Ph33r \ Picky \ -P.I.T.A. \ PlayAgain \ Popcorn \ RichQuick \ Runes \ R-World \ Sadistic \ -Shimitar \ Silly \ SkillShot \ Smilies \ SnyMaxx \ -StarWars \ Symmetry \ Symmetry-MKII \ T.C.S. \ -TheEnd \ TheGauntlet \ TimeWaster \ TrickShot \ Tricky \ -Twilight \ -Vader \ WallBricks \ Weirdness \ Wizi \ -Wolvie \ WonderWalls \ -WTF \ X \ Yexter \ -Yoda \ Zigies \ -Zooligan \ -Zufallswelt +Zooligan all: all-am --- a/src/themes/Standard/Makefile.am +++ b/src/themes/Standard/Makefile.am @@ -1,7 +1,7 @@ EXTRA_DIST = back0.jpg back1.jpg back2.jpg back3.jpg back4.jpg \ CREDITS SIL-OFL.txt shot.png ball.png paddle.png weapon.png \ bricks.png extras.png life.png fsmall.otf fnormal.otf theme.ini \ -menuback.jpg cursor.png explosions.png frame.png warp.png shine.png \ +menuback.jpg cursor.png explosions.png warp.png shine.png \ attach.wav energyhit.wav goldshower.wav shot.wav \ bonusmagnet.wav excellent.wav joker.wav shrink.wav \ brickhit.wav expand.wav looselife.wav speeddown.wav \ --- a/src/themes/Standard/Makefile.in +++ b/src/themes/Standard/Makefile.in @@ -248,7 +248,7 @@ EXTRA_DIST = back0.jpg back1.jpg back2.jpg back3.jpg back4.jpg \ CREDITS SIL-OFL.txt shot.png ball.png paddle.png weapon.png \ bricks.png extras.png life.png fsmall.otf fnormal.otf theme.ini \ -menuback.jpg cursor.png explosions.png frame.png warp.png shine.png \ +menuback.jpg cursor.png explosions.png warp.png shine.png \ attach.wav energyhit.wav goldshower.wav shot.wav \ bonusmagnet.wav excellent.wav joker.wav shrink.wav \ brickhit.wav expand.wav looselife.wav speeddown.wav \