Change mainloop for web builds
This commit is contained in:
parent
1f1feaccf3
commit
8adb68630e
1 changed files with 39 additions and 0 deletions
|
|
@ -11,6 +11,18 @@
|
||||||
#include <psemek/util/exception.hpp>
|
#include <psemek/util/exception.hpp>
|
||||||
#include <psemek/util/terminal_color.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
|
#undef main
|
||||||
|
|
||||||
int main(int argc, char ** argv) try
|
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 factory = app::make_application_factory();
|
||||||
auto const options = factory->options();
|
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);
|
sdl2::window window(options);
|
||||||
|
#endif
|
||||||
|
|
||||||
app::application::context context;
|
app::application::context context;
|
||||||
for (int i = 0; i < argc; ++i)
|
for (int i = 0; i < argc; ++i)
|
||||||
|
|
@ -61,7 +78,13 @@ int main(int argc, char ** argv) try
|
||||||
context.device = window.wgpu_device();
|
context.device = window.wgpu_device();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
em_state.application = factory->create(options, context);
|
||||||
|
auto application = em_state.application.get();
|
||||||
|
#else
|
||||||
auto application = factory->create(options, context);
|
auto application = factory->create(options, context);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!application)
|
if (!application)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
@ -75,6 +98,21 @@ int main(int argc, char ** argv) try
|
||||||
|
|
||||||
SDL_StopTextInput();
|
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())
|
while (application->running())
|
||||||
{
|
{
|
||||||
if (sdl2::poll_events(*application))
|
if (sdl2::poll_events(*application))
|
||||||
|
|
@ -84,6 +122,7 @@ int main(int argc, char ** argv) try
|
||||||
application->present();
|
application->present();
|
||||||
window.swap();
|
window.swap();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
log::info() << "Quitting";
|
log::info() << "Quitting";
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue