Change mainloop for web builds

This commit is contained in:
Nikita Lisitsa 2025-02-01 07:16:58 +01:00 committed by yulya3102
parent 0c4d49ad8d
commit 3d70c41bf4

View file

@ -11,6 +11,18 @@
#include <psemek/util/exception.hpp>
#include <psemek/util/terminal_color.hpp>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <emscripten.h>
struct em_state
{
std::unique_ptr<psemek::app::application> application;
std::unique_ptr<psemek::sdl2::window> window;
} em_state;
#endif
#undef main
int main(int argc, char ** argv) try
@ -40,7 +52,12 @@ int main(int argc, char ** argv) try
auto const factory = app::make_application_factory();
auto const options = factory->options();
#ifdef __EMSCRIPTEN__
em_state.window = std::make_unique<sdl2::window>(options);
auto & window = *em_state.window;
#else
sdl2::window window(options);
#endif
app::application::context context;
for (int i = 0; i < argc; ++i)
@ -61,7 +78,13 @@ int main(int argc, char ** argv) try
context.device = window.wgpu_device();
#endif
#ifdef __EMSCRIPTEN__
em_state.application = factory->create(options, context);
auto application = em_state.application.get();
#else
auto application = factory->create(options, context);
#endif
if (!application)
return EXIT_FAILURE;
@ -75,6 +98,21 @@ int main(int argc, char ** argv) try
SDL_StopTextInput();
#ifdef __EMSCRIPTEN__
emscripten_request_animation_frame_loop([](double, void*) -> EM_BOOL {
if (sdl2::poll_events(*em_state.application))
em_state.application->stop();
if (!em_state.application->running())
return EM_FALSE;
em_state.application->update();
em_state.application->present();
em_state.window->swap();
return EM_TRUE;
}, nullptr);
#else
while (application->running())
{
if (sdl2::poll_events(*application))
@ -84,6 +122,7 @@ int main(int argc, char ** argv) try
application->present();
window.swap();
}
#endif
log::info() << "Quitting";
return EXIT_SUCCESS;