mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-07 03:25:10 +00:00
Merging with develop and reworking all systems
This commit is contained in:
+35
-1
@@ -14,6 +14,10 @@
|
||||
#include "Models/Callback.hpp"
|
||||
#include "Scene/Scene.hpp"
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
#include <emscripten/emscripten.h>
|
||||
#endif
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
class Entity;
|
||||
@@ -47,7 +51,7 @@ namespace WAL
|
||||
});
|
||||
if (existing != this->_systems.end())
|
||||
throw DuplicateError("A system of the type \"" + std::string(type.name()) + "\" already exists.");
|
||||
this->_systems.push_back(std::make_unique<T>(std::forward<Types>(params)...));
|
||||
this->_systems.push_back(std::make_unique<T>(*this, std::forward<Types>(params)...));
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -102,7 +106,13 @@ namespace WAL
|
||||
void run(const std::function<void (Wal &, T &)> &callback, T state = T())
|
||||
{
|
||||
Callback<Wal &, T &> update(callback);
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
std::tuple iterationParams(this, &update, &state);
|
||||
return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration<T>, (void *)&iterationParams, 0, 1);
|
||||
#else
|
||||
return this->run(update, state);
|
||||
#endif
|
||||
}
|
||||
|
||||
//! @brief Start the game loop
|
||||
@@ -137,6 +147,30 @@ namespace WAL
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
template<typename T>
|
||||
static void runIteration(void *param)
|
||||
{
|
||||
static auto iterationParams = reinterpret_cast<std::tuple<Wal *, Callback<Wal &, T &> *, T *> *>(param);
|
||||
static const Callback<Wal &, T &> callback = *((Callback<Wal &, T &> *)std::get<1>(*iterationParams));
|
||||
static T *state = (T *)std::get<2>(*iterationParams);
|
||||
static Wal *wal = (Wal *)std::get<0>(*iterationParams);
|
||||
static auto lastTick = std::chrono::steady_clock::now();
|
||||
static std::chrono::nanoseconds fBehind(0);
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
std::chrono::nanoseconds dtime = now - lastTick;
|
||||
fBehind += dtime;
|
||||
lastTick = now;
|
||||
while (fBehind > Wal::timestep) {
|
||||
fBehind -= Wal::timestep;
|
||||
wal->_fixedUpdate();
|
||||
}
|
||||
wal->_update(dtime);
|
||||
callback(*wal, *state);
|
||||
}
|
||||
#endif
|
||||
|
||||
//! @brief A default constructor
|
||||
Wal() = default;
|
||||
//! @brief A WAL can't be copy constructed
|
||||
|
||||
Reference in New Issue
Block a user