Skip to content

Commit

Permalink
feat(ecs): use proper main loop on Cubos::run with Emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Jun 14, 2024
1 parent 2b0818b commit 02a719e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/src/ecs/cubos.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <utility>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include <cubos/core/ecs/command_buffer.hpp>
#include <cubos/core/ecs/cubos.hpp>
#include <cubos/core/ecs/name.hpp>
Expand Down Expand Up @@ -306,9 +310,22 @@ bool Cubos::update()
void Cubos::run()
{
this->start();
#ifndef __EMSCRIPTEN__
while (this->update())
{
}
#else
auto* cubos = new Cubos(std::move(*this)); // Move the Cubos object to the heap so that it won't be destroyed.
emscripten_set_main_loop_arg(
[](void* cubos) {
if (static_cast<Cubos*>(cubos)->update() == 0)
{
emscripten_cancel_main_loop();
delete static_cast<Cubos*>(cubos);
}
},
cubos, 0, 1);
#endif
}

bool Cubos::isRegistered(const reflection::Type& type) const
Expand Down

0 comments on commit 02a719e

Please sign in to comment.