adopt engine stuff to new update strategy

This commit is contained in:
Green Sky 2020-12-26 19:29:39 +01:00
parent c1ae30c89c
commit 840b663d5e
28 changed files with 351 additions and 324 deletions

View File

@ -32,8 +32,8 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL
-Wall -Wextra # Reasonable and standard
-Wpedantic # Warn if non-standard C++ is used
-Wunused # Warn on anything being unused
-Wconversion # Warn on type conversions that may lose data
-Wsign-conversion # Warn on sign conversions
#-Wconversion # Warn on type conversions that may lose data
#-Wsign-conversion # Warn on sign conversions
-Wshadow # Warn if a variable declaration shadows one from a parent context
)

View File

@ -3,6 +3,8 @@
#include <imgui/imgui.h>
#include <imgui_impl_sdl.h>
#include <entt/core/hashed_string.hpp>
#ifdef MM_OPENGL_3
#include <imgui_impl_opengl3.h>
#endif
@ -102,12 +104,6 @@ bool ImGuiService::enable(Engine& engine) {
return false;
});
_new_frame_handle = engine.addUpdate([this](Engine& e) { this->imgui_new_frame(e); });
assert(!_new_frame_handle.expired());
auto tmp_lock = _new_frame_handle.lock();
tmp_lock->priority = 90; // after sdl events (100)
tmp_lock->name = "imgui new frame";
return true;
}
@ -115,8 +111,6 @@ void ImGuiService::disable(Engine& engine) {
auto& sdl_ss = engine.getService<MM::Services::SDLService>();
sdl_ss.removeEventHandler(_event_handle);
engine.removeUpdate(_new_frame_handle);
ImGui::EndFrame(); // making sure, does not work????
#ifdef MM_OPENGL_3
@ -126,6 +120,19 @@ void ImGuiService::disable(Engine& engine) {
ImGui::DestroyContext();
}
std::vector<UpdateStrategies::UpdateCreationInfo> ImGuiService::registerUpdates(void) {
return {
{
"ImGuiService::new_frame"_hs,
"ImGuiService::new_frame",
[this](Engine& e) { this->imgui_new_frame(e); },
UpdateStrategies::update_phase_t::PRE,
true,
{"SDLService::events"_hs}
}
};
}
void ImGuiService::imgui_new_frame(Engine& engine) {
ZoneScopedN("MM::Services::ImGuiService::imgui_new_frame");

View File

@ -12,7 +12,6 @@ namespace MM::Services {
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
// new frame needs to start AFTER the events have been processed (and obv bf rendt)
MM::Engine::FunctionDataHandle _new_frame_handle;
public:
bool enable(Engine& engine) override;
@ -20,6 +19,8 @@ namespace MM::Services {
const char* name(void) override { return "ImGuiService"; }
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
private:
void imgui_new_frame(Engine& engine);
};

View File

@ -179,8 +179,6 @@ namespace MM::Services {
_entity_editor.registerComponent<MM::Components::OpenGL::Texture>("Texture");
}
_render_handle = engine.addUpdate([this](Engine& e){ this->renderImGui(e); });
auto* sdl_ss = engine.tryService<MM::Services::SDLService>();
if (sdl_ss) {
_event_handle = sdl_ss->addEventHandler([this](const SDL_Event& e) -> bool {
@ -197,11 +195,6 @@ namespace MM::Services {
}
void ImGuiSceneToolsService::disable(Engine& engine) {
if (!_render_handle.expired()) {
engine.removeUpdate(_render_handle);
_render_handle.reset();
}
if (_event_handle) {
auto* sdl_ss = engine.tryService<MM::Services::SDLService>();
sdl_ss->removeEventHandler(_event_handle);
@ -209,5 +202,14 @@ namespace MM::Services {
}
}
std::vector<UpdateStrategies::UpdateCreationInfo> ImGuiSceneToolsService::registerUpdates(void) {
return {
{
"ImGuiSceneToolsService::render"_hs,
"ImGuiSceneToolsService::render",
[this](Engine& e){ renderImGui(e); }
}
};
}
} // namespace MM::Services

View File

@ -32,7 +32,6 @@ namespace MM::Services {
std::vector<MM::FileTextEditor> _text_editor_list;
Engine::FunctionDataHandle _render_handle;
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
private:
@ -47,6 +46,9 @@ namespace MM::Services {
void disable(Engine& engine) override;
const char* name(void) override { return "ImGuiSceneToolsService"; }
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
};
} // namespace MM::Services

View File

@ -12,8 +12,6 @@
#include <mm/services/opengl_renderer.hpp>
#include <mm/opengl/render_tasks/imgui.hpp>
#include <mm/imgui/fps_overlay.hpp>
#include <mm/services/scene_tools.hpp>
static char* argv0;
@ -39,6 +37,7 @@ TEST(imgui_scene_tools, it) {
ASSERT_TRUE(engine.enableService<MM::Services::ImGuiService>());
engine.addService<MM::Services::ImGuiSceneToolsService>();
engine.getUpdateStrategy().depend("ImGuiSceneToolsService::render"_hs, "SimpleSceneService::scene_tick"_hs);
auto& rs = engine.addService<MM::Services::OpenGLRenderer>();
ASSERT_TRUE(engine.enableService<MM::Services::OpenGLRenderer>());
@ -49,16 +48,7 @@ TEST(imgui_scene_tools, it) {
//InitializeYojimbo();
{
MM::ImGuiSimpleFPSOverlay fps_overlay;
engine.addUpdate([&](MM::Engine&) {
fps_overlay.renderImGui();
}
);
engine.run();
}
engine.run();
// TODO: clear asset manager

View File

@ -2,6 +2,8 @@
#include <mm/engine.hpp>
#include <entt/core/hashed_string.hpp>
// services
#include <mm/services/sdl_service.hpp>
#include <mm/services/filesystem.hpp>
@ -25,7 +27,7 @@
const char* argv0;
class ImGuiSpeechy {
class ImGuiSpeechy : public MM::Services::Service {
private:
SoLoud::Speech speech;
SoLoud::Sfxr sfxr;
@ -35,13 +37,34 @@ class ImGuiSpeechy {
SoLoud::LofiFilter lofi;
public:
explicit ImGuiSpeechy(SoLoud::Soloud& sound) {
const char* name(void) override { return "TestWindow"; }
bool enable(MM::Engine& engine) override {
auto& sound = engine.getService<MM::Services::SoundService>().engine;
speech.setText("Test text. 1. 2. 3.");
sfxr.loadPreset(SoLoud::Sfxr::COIN, 0);
sound.setGlobalFilter(0, &lofi);
sound.setGlobalFilter(1, &echo);
sound.setGlobalFilter(2, &freeverb);
return true;
}
void disable(MM::Engine&) override {}
std::vector<MM::UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override {
return {{
"testwindow"_hs,
"testwindow",
[this](MM::Engine& engine) {
MM::ImGuiSoundInfo(engine);
MM::ImGuiSoundPref(engine);
renderImGui(engine);
}
}};
}
void renderImGui(MM::Engine& engine) {
@ -129,19 +152,10 @@ TEST(imgui_sound, basic) {
rs.addRenderTask<MM::OpenGL::RenderTasks::ImGuiRT>(engine);
{
ImGuiSpeechy speechy(sound.engine);
engine.addService<ImGuiSpeechy>();
ASSERT_TRUE(engine.enableService<ImGuiSpeechy>());
engine.addUpdate([&](MM::Engine& engine) {
MM::ImGuiSoundInfo(engine);
MM::ImGuiSoundPref(engine);
speechy.renderImGui(engine);
}
);
engine.run();
}
engine.run();
sdl_ss.destroyWindow();
}

View File

@ -1,3 +1,4 @@
#include <functional>
#include <gtest/gtest.h>
#include <mm/resource_manager.hpp>
@ -21,6 +22,25 @@
static char* argv0;
class TemplateUpdateMainService : public MM::Services::Service {
std::function<void(MM::Engine&)> _fn;
public:
explicit TemplateUpdateMainService(std::function<void(MM::Engine&)> fn) : _fn(fn) {}
const char* name(void) override { return "TemplateUpdateMainService"; }
bool enable(MM::Engine&) override { return true; }
void disable(MM::Engine&) override {}
std::vector<MM::UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override {
return {{
"TemplateUpdateMainService::fn"_hs,
"TemplateUpdateMainService::fn",
_fn
}};
}
};
TEST(imgui_text_edit, it) {
MM::Engine engine;
@ -50,17 +70,11 @@ TEST(imgui_text_edit, it) {
ASSERT_TRUE(engine.enableService<MM::Services::ImGuiSceneToolsService>());
MM::ImGuiSimpleFPSOverlay fps_overlay;
engine.addUpdate([&](MM::Engine&) {
fps_overlay.renderImGui();
}
);
MM::FileTextEditor fte{engine};
engine.addUpdate([&](MM::Engine&) {
fte.renderImGui();
}
);
engine.addService<TemplateUpdateMainService>([](MM::Engine& e) {
static MM::FileTextEditor fte{e};
fte.renderImGui();
});
ASSERT_TRUE(engine.enableService<TemplateUpdateMainService>());
engine.run();
@ -98,26 +112,16 @@ TEST(imgui_text_edit, shader) {
ASSERT_TRUE(engine.enableService<MM::Services::ImGuiSceneToolsService>());
MM::ImGuiSimpleFPSOverlay fps_overlay;
engine.addUpdate([&](MM::Engine&) {
fps_overlay.renderImGui();
}
);
//auto& rc = engine.getScene().ctx<MM::OpenGL::RenderController>();
//rc.registerRenderer<MM::OpenGL::Renderers::QuadRenderer>();
MM::FileTextEditor fte{engine};
engine.addUpdate([&](MM::Engine&) {
fte.renderImGui();
}
);
MM::FileShaderEditor fse{engine};
engine.addUpdate([&](MM::Engine&) {
fse.renderImGui();
}
);
engine.addService<TemplateUpdateMainService>([&](MM::Engine&) {
fte.renderImGui();
fse.renderImGui();
});
ASSERT_TRUE(engine.enableService<TemplateUpdateMainService>());
fte.open("shader/quad_renderer/vert.glsl");
fse.open("shader/quad_renderer/frag.glsl");

View File

@ -2,6 +2,8 @@
#include <mm/engine.hpp>
#include <entt/core/hashed_string.hpp>
// services
#include <mm/services/sdl_service.hpp>
#include <mm/services/filesystem.hpp>
@ -16,35 +18,6 @@
const char* argv0;
//class ImGuiSpeechy {
//private:
//SoLoud::Speech speech;
//SoLoud::Sfxr sfxr;
//public:
//ImGuiSpeechy(void) {
//speech.setText("Test text. 1. 2. 3.");
//sfxr.loadPreset(SoLoud::Sfxr::COIN, 0);
//}
//void renderImGui(MM::Engine& engine) {
//if (ImGui::Begin("Inputs")) {
//auto& sound = *engine.tryGetService<MM::Services::SoundService>();
//if (ImGui::Button("play sfx")) {
//sound.engine.play(sfxr);
//}
////ImGui::Text("Active Voice Count: %d", sound.engine.getActiveVoiceCount());
//if (ImGui::Button("Read")) {
//sound.engine.play(speech);
//}
//}
//ImGui::End();
//}
//};
TEST(imgui_widgets, basic) {
MM::Engine engine;
@ -64,23 +37,35 @@ TEST(imgui_widgets, basic) {
rs.addRenderTask<MM::OpenGL::RenderTasks::ImGuiRT>(engine);
{
//ImGuiSpeechy speechy;
class TestWindow : public MM::Services::Service {
public:
const char* name(void) override { return "TestWindow"; }
bool enable(MM::Engine&) override { return true; }
void disable(MM::Engine&) override {}
engine.addUpdate([&](MM::Engine&) {
if (ImGui::Begin("test window")) {
std::vector<MM::UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override {
return {{
"testwindow"_hs,
"testwindow",
[](MM::Engine&) {
if (ImGui::Begin("test window")) {
static float knob_test = 0.f;
MM::ImGuiWidgets::KnobFloat("knob1", &knob_test, 0.f, 1.f);
ImGui::SameLine();
MM::ImGuiWidgets::KnobFloat("knob2", &knob_test, 0.f, 1.f, 0.f, false);
static float knob_test = 0.f;
MM::ImGuiWidgets::KnobFloat("knob1", &knob_test, 0.f, 1.f);
ImGui::SameLine();
MM::ImGuiWidgets::KnobFloat("knob2", &knob_test, 0.f, 1.f, 0.f, false);
}
ImGui::End();
}
}};
}
ImGui::End();
});
};
engine.run();
}
engine.addService<TestWindow>();
ASSERT_TRUE(engine.enableService<TestWindow>());
engine.run();
sdl_ss.destroyWindow();
}

View File

@ -1,5 +1,7 @@
#include "./input_service.hpp"
#include <entt/core/hashed_string.hpp>
#include <glm/common.hpp>
#include <glm/geometric.hpp>
#include <glm/trigonometric.hpp>
@ -88,24 +90,6 @@ bool InputService::enable(Engine& engine) {
return false;
}
_update_handle = engine.addFixedUpdate([this](MM::Engine&) {
for (size_t i = 0; i < 4; i++) {
if (!_player_active[i])
continue;
auto& p = _player[i];
if (p.is_controller) {
} else {
updateKPlayerDirs(p);
}
}
});
if (_update_handle.expired()) {
LOG_ERROR("couldnt register update handler!");
return false;
}
_update_handle.lock()->priority = 1; // scene is 0
return true;
}
@ -119,12 +103,27 @@ void InputService::disable(Engine& engine) {
sdl_ss->removeEventHandler(_event_handle);
}
}
if (!_update_handle.expired()) {
engine.removeFixedUpdate(_update_handle);
_update_handle.reset();
}
std::vector<UpdateStrategies::UpdateCreationInfo> InputService::registerUpdates(void) {
return {
{
"InputService::update"_hs,
"InputService::update",
[this](MM::Engine&) {
for (size_t i = 0; i < 4; i++) {
if (!_player_active[i])
continue;
auto& p = _player[i];
if (p.is_controller) {
} else {
updateKPlayerDirs(p);
}
}
}
}
};
}
bool InputService::handleSDL_Event(const SDL_Event& e, MM::Engine& engine) {

View File

@ -16,7 +16,7 @@ namespace MM::Services {
class InputService : public Service {
public:
// TDO: rework
// TODO: rework
enum input_action_t {
SPELL_WEAPON,
SPELL_1,
@ -106,8 +106,6 @@ namespace MM::Services {
private:
SDLService::EventHandlerHandle _event_handle = nullptr;
MM::Engine::FunctionDataHandle _update_handle;
public:
bool enable(Engine& engine) override;
@ -115,6 +113,10 @@ namespace MM::Services {
const char* name(void) override { return "InputService"; }
// you will likely want to make the scene depend on this
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
public:
// returns true if event was relevant
bool handleSDL_Event(const SDL_Event& e, MM::Engine& engine);

View File

@ -1,7 +1,4 @@
#include <gtest/gtest.h>
#include <mm/engine.hpp>
#include <mm/services/input_service.hpp>
#include <mm/services/sdl_service.hpp>
#include <mm/services/filesystem.hpp>
@ -10,7 +7,7 @@
#include <mm/opengl/render_tasks/imgui.hpp>
#include <mm/imgui/fps_overlay.hpp>
#include <entt/core/hashed_string.hpp>
#include <imgui/imgui.h>
@ -18,10 +15,11 @@
class InputVisualizer : public MM::Services::Service {
private:
MM::Input::PlayerID _player_id;
MM::Engine::FunctionDataHandle _render_handle;
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
public:
const char* name(void) override { return "InputVisualizer"; }
bool enable(MM::Engine& engine) override {
_player_id = UINT16_MAX;
@ -42,19 +40,26 @@ class InputVisualizer : public MM::Services::Service {
}
}
_render_handle = engine.addUpdate([this](MM::Engine& e){ this->renderImGui(e); });
return true;
}
void disable(MM::Engine& engine) override {
if (!_render_handle.expired()) {
engine.removeUpdate(_render_handle);
_render_handle.reset();
}
void disable(MM::Engine&) override {
}
std::vector<MM::UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override {
return {
{
"InputVisualizer::render"_hs,
"InputVisualizer::render",
[this](MM::Engine& e){ this->renderImGui(e); },
MM::UpdateStrategies::update_phase_t::MAIN,
true,
{ "InputService::update"_hs }
}
};
}
private:
void renderImGui(MM::Engine& engine) {
ImGui::Begin("InputVisualizer", NULL, ImGuiWindowFlags_AlwaysAutoResize);
@ -146,16 +151,7 @@ TEST(input_service, input_visualizer) {
rs.addRenderTask<MM::OpenGL::RenderTasks::ImGuiRT>(engine);
{
MM::ImGuiSimpleFPSOverlay fps_overlay;
engine.addUpdate([&](MM::Engine&) {
fps_overlay.renderImGui();
}
);
engine.run();
}
engine.run();
sdl_ss.destroyWindow();
}

View File

@ -10,6 +10,7 @@
#include <mm/opengl/texture_loader.hpp>
#include "../opengl/res/default_texture.h" // data
#include "../opengl/res/errig_texture.h" // data
#include "mm/update_strategies/update_strategy.hpp"
#include <tracy/Tracy.hpp>
#ifndef MM_OPENGL_3_GLES
@ -67,18 +68,6 @@ bool OpenGLRenderer::enable(Engine& engine) {
targets["display"]->_resize = false; // its done for us
}
_render_handle = engine.addUpdate([this](Engine& e){ this->render(e); });
if (_render_handle.expired()) {
LOG_ERROR("couldn't add update function!");
return false;
}
{
auto tmp_lock = _render_handle.lock();
tmp_lock->priority = -10;
tmp_lock->name = "render";
}
_sdl_event_handle = sdl_s.addEventHandler([this, &engine](const SDL_Event& e) -> bool {
if (e.type == SDL_WINDOWEVENT) {
if (e.window.event == SDL_WINDOWEVENT_RESIZED || e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
@ -138,12 +127,7 @@ bool OpenGLRenderer::enable(Engine& engine) {
return true;
}
void OpenGLRenderer::disable(Engine& engine) {
if (!_render_handle.expired()) {
engine.removeUpdate(_render_handle);
_render_handle.reset();
}
void OpenGLRenderer::disable(Engine&) {
// TODO: do we need this??
targets.clear();
render_tasks.clear();
@ -152,6 +136,17 @@ void OpenGLRenderer::disable(Engine& engine) {
MM::ResourceManager<MM::OpenGL::Texture>::ref().clear();
}
std::vector<UpdateStrategies::UpdateCreationInfo> OpenGLRenderer::registerUpdates(void) {
return {
{
"OpenGLRenderer::render"_hs,
"OpenGLRenderer::render",
[this](Engine& e){ this->render(e); },
UpdateStrategies::update_phase_t::POST
}
};
}
void OpenGLRenderer::render(Engine& engine) {
ZoneScopedN("MM::Services::OpenGLRenderer::render");

View File

@ -28,7 +28,6 @@ namespace MM::Services {
}
private:
Engine::FunctionDataHandle _render_handle;
SDLService::EventHandlerHandle _sdl_event_handle = nullptr;
public:
@ -37,6 +36,9 @@ namespace MM::Services {
const char* name(void) override { return "OpenGLRendererService"; }
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
private:
void render(Engine& engine);
};

View File

@ -116,11 +116,11 @@ TEST(blur_render_task, it) {
for (int i = 0; i < 10; i++) {
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = i * 9.f - 40;
t.position.x = float(i) * 9.f - 40.f;
t.scale = {5,5};
auto& v = scene.emplace<MM::Components::Velocity2D>(e);
v.rotation = i * 0.3f;
v.rotation = float(i) * 0.3f;
if (mt() % 2) {
auto& col = scene.emplace<MM::Components::Color>(e);
@ -132,12 +132,6 @@ TEST(blur_render_task, it) {
}
engine.addUpdate(
[&](MM::Engine&) {
ImGui::ColorEdit4("rect_col", &bsrr_rend.default_color[0]);
}
);
engine.run();
}

View File

@ -1,3 +1,4 @@
#include "mm/services/service.hpp"
#include <gtest/gtest.h>
#include <mm/engine.hpp>
@ -7,6 +8,8 @@
#include <mm/services/imgui_s.hpp>
#include <mm/opengl/render_tasks/imgui.hpp>
#include <entt/core/hashed_string.hpp>
#include <imgui/imgui.h>
TEST(imgui_render_task, demowindow) {
@ -22,19 +25,26 @@ TEST(imgui_render_task, demowindow) {
engine.addService<MM::Services::ImGuiService>();
ASSERT_TRUE(engine.enableService<MM::Services::ImGuiService>());
class ImGuiDemoWindowService : public MM::Services::Service {
public:
const char* name(void) override { return "ImGuiDemoWindowService"; }
bool enable(MM::Engine&) override { return true; }
void disable(MM::Engine&) override {}
std::vector<MM::UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override {
return {{
"ImGuiDemoWindow"_hs,
"ImGuiDemoWindow",
[](MM::Engine&) { ImGui::ShowDemoWindow(); }
}};
}
};
engine.addService<ImGuiDemoWindowService>();
ASSERT_TRUE(engine.enableService<ImGuiDemoWindowService>());
rs.addRenderTask<MM::OpenGL::RenderTasks::ImGuiRT>(engine);
auto handle = engine.addUpdate([](MM::Engine&) {
ImGui::ShowDemoWindow();
}
);
{
auto tmp_lock = handle.lock();
tmp_lock->priority = 0;
tmp_lock->name = "imgui demo window";
}
engine.run();
}

View File

@ -1,5 +1,7 @@
#include "./screen_director.hpp"
#include <entt/core/hashed_string.hpp>
#include <tracy/Tracy.hpp>
#include <mm/logger.hpp>
@ -7,17 +9,6 @@
namespace MM::Services {
bool ScreenDirector::enable(MM::Engine& engine) {
if (!_update_handle.expired())
return false;
{
_update_handle = engine.addFixedUpdate([this](Engine& e) { this->update(e); });
auto tmp_lock = _update_handle.lock();
tmp_lock->priority = -100; // post everything ?
tmp_lock->name = "ScreenDirector::update";
}
// start initial screen
if (!queued_screen_id.empty()) {
auto next_screen_id = queued_screen_id;
@ -31,15 +22,24 @@ bool ScreenDirector::enable(MM::Engine& engine) {
return true;
}
void ScreenDirector::disable(MM::Engine& engine) {
engine.removeFixedUpdate(_update_handle);
_update_handle.reset();
void ScreenDirector::disable(MM::Engine&) {
}
std::vector<UpdateStrategies::UpdateCreationInfo> ScreenDirector::registerUpdates(void) {
return {
{
"ScreenDirector::update"_hs,
"ScreenDirector::update",
[this](Engine& engine) { update(engine); },
UpdateStrategies::update_phase_t::POST
}
};
}
void ScreenDirector::update(MM::Engine& engine) {
if (curr_screen_id != queued_screen_id) {
engine.addFixedDefer([this](MM::Engine& engine) {
changeScreenTo(engine, queued_screen_id);
engine.getUpdateStrategy().addDefered([this](MM::Engine& e) {
changeScreenTo(e, queued_screen_id);
});
}
}

View File

@ -15,6 +15,8 @@ class ScreenDirector : public Service {
bool enable(MM::Engine&) override;
void disable(MM::Engine&) override;
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
public:
struct Screen {
// lists of services relevant for this screen, disable and enable are called when its changed to
@ -47,7 +49,6 @@ class ScreenDirector : public Service {
std::string queued_screen_id = "";
private:
MM::Engine::FunctionDataHandle _update_handle;
void update(MM::Engine& engine);
private:

View File

@ -1,5 +1,7 @@
#include "./sdl_service.hpp"
#include <entt/core/hashed_string.hpp>
#include <tracy/Tracy.hpp>
#ifdef MM_OPENGL_3_GLES
@ -58,37 +60,23 @@ SDLService::~SDLService(void) {
SDL_Quit();
}
bool SDLService::enable(Engine& engine) {
std::vector<UpdateStrategies::UpdateCreationInfo> SDLService::registerUpdates(void) {
return {
{
"SDLService::events"_hs,
"SDLService::events",
[this](Engine& e) { this->processEvents(e); },
UpdateStrategies::update_phase_t::PRE
}
};
}
bool SDLService::enable(Engine&) {
bool succ = true;
_func_handle = engine.addUpdate([this](Engine& e) { this->processEvents(e); });
if (!_func_handle.expired()) {
auto tmp_lock = _func_handle.lock();
tmp_lock->priority = 100; // before the calls before the scene
tmp_lock->name = "sdl events";
} else {
succ = false;
}
_f_func_handle = engine.addFixedUpdate([this](Engine& e) { this->processEvents(e); });
if (!_f_func_handle.expired()) {
auto tmp_lock = _f_func_handle.lock();
tmp_lock->priority = 100; // before the calls before the scene
tmp_lock->name = "sdl events";
} else {
succ = false;
}
return succ;
}
void SDLService::disable(Engine& engine) {
// remove update hooks
if (!_func_handle.expired())
engine.removeUpdate(_func_handle);
if (!_f_func_handle.expired())
engine.removeFixedUpdate(_f_func_handle);
void SDLService::disable(Engine&) {
// destroy stuff
if (gl_context) {
SDL_GL_DeleteContext(gl_context);

View File

@ -22,14 +22,13 @@ namespace MM::Services {
private:
std::vector<std::unique_ptr<EventHandlerType>> _event_handler_list;
Engine::FunctionDataHandle _func_handle;
Engine::FunctionDataHandle _f_func_handle;
public:
SDLService(uint32_t sdl_init_flags = SDL_INIT_EVERYTHING);
~SDLService(void);
public:
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
bool enable(Engine& engine) override;
void disable(Engine& engine) override; // destroys windows and ass contexts

View File

@ -103,9 +103,8 @@ TEST(sdl_service, event_handle_reg) {
ASSERT_EQ(SDL_PushEvent(&sdlevent), 1);
};
// register forge for fixedUpdate
auto event_forge_handle = engine.addFixedUpdate(event_forge_f);
ASSERT_NE(event_forge_handle.lock(), nullptr);
// register forge
engine.getUpdateStrategy().addDefered(event_forge_f);
// register sdl event handler to just stop the engine
auto* stop_event_hook_h = sdl_ss_ptr->addEventHandler([&engine](const SDL_Event&){ engine.stop(); return true; });

View File

@ -1,9 +1,11 @@
#include "./simple_scene.hpp"
#include "spdlog/fmt/bundled/core.h"
//#include "../systems_container.hpp"
#include <entt/entity/registry.hpp>
#include <tracy/Tracy.hpp>
#include <mm/logger.hpp>
@ -18,50 +20,61 @@ bool SimpleSceneService::enable(Engine& engine) {
_scene->set<MM::Engine*>(&engine);
}
{
_f_update_handle = engine.addFixedUpdate([this](Engine& e) { this->sceneFixedUpdate(e); });
if (_f_update_handle.expired()) {
return false;
}
auto tmp_lock = _f_update_handle.lock();
tmp_lock->priority = 0;
}
{
_f_update_changer_handle = engine.addFixedUpdate([this](Engine& e) { this->changeSceneFixedUpdate(e); });
if (_f_update_changer_handle.expired()) {
return false;
}
auto tmp_lock = _f_update_changer_handle.lock();
tmp_lock->priority = -100;
}
resetTime();
return true;
}
void SimpleSceneService::disable(Engine& engine) {
if (!_f_update_handle.expired()) {
engine.removeFixedUpdate(_f_update_handle);
_f_update_handle.reset();
}
if (!_f_update_changer_handle.expired()) {
engine.removeFixedUpdate(_f_update_changer_handle);
_f_update_changer_handle.reset();
}
void SimpleSceneService::disable(Engine&) {
}
void SimpleSceneService::sceneFixedUpdate(Engine& engine) {
std::vector<UpdateStrategies::UpdateCreationInfo> SimpleSceneService::registerUpdates(void) {
return {
{
"SimpleSceneService::scene_tick"_hs,
"SimpleSceneService::scene_tick",
[this](Engine& e) { this->sceneFixedUpdate(e); },
UpdateStrategies::update_phase_t::MAIN,
true,
{} // no dependencies"
},
{
"SimpleSceneService::scene_change"_hs,
"SimpleSceneService::scene_change",
[this](Engine& e) { this->changeSceneFixedUpdate(e); },
UpdateStrategies::update_phase_t::MAIN,
true,
{"SimpleSceneService::scene_update"_hs} // first update, than change????
}
};
}
void SimpleSceneService::sceneFixedUpdate(Engine&) {
ZoneScoped;
const auto f_delta = engine.getFixedDeltaTime();
::MM::EachSystemInScene(*_scene, [&](::MM::Scene& s, ::MM::System& fn) {
fn(s, f_delta);
});
auto newNow = clock::now();
auto deltaTime = std::chrono::duration_cast<std::chrono::nanoseconds>(newNow - _last_time);
_last_time = newNow;
_accumulator += deltaTime.count();
const double dt = f_delta * 1'000'000'000.0;
TracyPlot("MM::Services::SimpleSceneService::_scene.alive", (int64_t)_scene->alive());
size_t continuous_counter = 0;
// TODO: this is just cancer
while (_accumulator >= static_cast<decltype(_accumulator)>(dt)){
_accumulator -= static_cast<decltype(_accumulator)>(dt);
continuous_counter++;
::MM::EachSystemInScene(*_scene, [&](::MM::Scene& s, ::MM::System& fn) {
fn(s, f_delta * delta_factor);
});
TracyPlot("MM::Services::SimpleSceneService::_scene.alive", (int64_t)_scene->alive());
}
if (continuous_counter > 2) {
LOG_SSS(fmt::format("had {} contiguous scene ticks!", continuous_counter));
}
}
void SimpleSceneService::changeSceneFixedUpdate(Engine& engine) {
@ -95,5 +108,11 @@ void SimpleSceneService::changeSceneNow(std::unique_ptr<Scene>&& new_scene) {
//}
}
void SimpleSceneService::resetTime(void) {
_last_time = clock::now();
_accumulator = 0;
}
} // MM::Services

View File

@ -2,6 +2,8 @@
#include <mm/services/scene_service_interface.hpp>
#include <chrono>
namespace MM::Services {
// provides an implementation for SceneServiceInterface
@ -10,15 +12,26 @@ namespace MM::Services {
std::unique_ptr<Scene> _scene;
std::unique_ptr<Scene> _next_scene; // enqueued next scene
MM::Engine::FunctionDataHandle _f_update_handle;
MM::Engine::FunctionDataHandle _f_update_changer_handle;
using clock = std::chrono::high_resolution_clock;
long long int _accumulator = 0;
std::chrono::time_point<clock> _last_time;
public:
const float f_delta;
float delta_factor = 1.f;
public:
explicit SimpleSceneService(const float update_delta = 1.f/60.f) : f_delta(update_delta) {}
const char* name(void) override { return "SimpleSceneService"; }
bool enable(Engine& engine) override;
void disable(Engine& engine) override;
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
private:
void sceneFixedUpdate(Engine& engine);
void changeSceneFixedUpdate(Engine& engine);
@ -29,6 +42,8 @@ namespace MM::Services {
// be carefull of that one
void changeSceneNow(std::unique_ptr<Scene>&& new_scene) override;
void resetTime(void);
};
} // MM::Services

View File

@ -5,14 +5,14 @@
#include <entt/entity/registry.hpp>
TEST(simple_scene, add_en_dis) {
MM::Engine e;
MM::Engine engine;
e.addService<MM::Services::SimpleSceneService>();
engine.addService<MM::Services::SimpleSceneService>();
ASSERT_TRUE(e.enableService<MM::Services::SimpleSceneService>());
ASSERT_TRUE(engine.enableService<MM::Services::SimpleSceneService>());
{
auto* sss = e.tryService<MM::Services::SimpleSceneService>();
auto* sss = engine.tryService<MM::Services::SimpleSceneService>();
ASSERT_NE(sss, nullptr);
auto& s = sss->getScene();
@ -20,19 +20,19 @@ TEST(simple_scene, add_en_dis) {
ASSERT_TRUE(s.valid(e));
}
e.disableService<MM::Services::SimpleSceneService>();
engine.disableService<MM::Services::SimpleSceneService>();
}
TEST(simple_scene, add_en_dis_provide) {
MM::Engine e;
MM::Engine engine;
e.addService<MM::Services::SimpleSceneService>();
e.provide<MM::Services::SceneServiceInterface, MM::Services::SimpleSceneService>();
engine.addService<MM::Services::SimpleSceneService>();
engine.provide<MM::Services::SceneServiceInterface, MM::Services::SimpleSceneService>();
ASSERT_TRUE(e.enableService<MM::Services::SimpleSceneService>());
ASSERT_TRUE(engine.enableService<MM::Services::SimpleSceneService>());
{
auto* ssi = e.tryService<MM::Services::SceneServiceInterface>();
auto* ssi = engine.tryService<MM::Services::SceneServiceInterface>();
ASSERT_NE(ssi, nullptr);
auto& s = ssi->getScene();
@ -40,7 +40,7 @@ TEST(simple_scene, add_en_dis_provide) {
ASSERT_TRUE(s.valid(e));
}
e.disableService<MM::Services::SimpleSceneService>();
engine.disableService<MM::Services::SimpleSceneService>();
}
TEST(simple_scene, change_scene) {
@ -60,7 +60,9 @@ TEST(simple_scene, change_scene) {
ASSERT_TRUE(sss->getScene().valid(e));
engine.fixedUpdate();
//engine.fixedUpdate();
engine.getUpdateStrategy().addDefered([](MM::Engine& eng) {eng.stop();});
engine.run();
ASSERT_FALSE(sss->getScene().valid(e));
}

View File

@ -1,4 +1,7 @@
#include "simple_sdl_renderer.hpp"
#include "mm/update_strategies/update_strategy.hpp"
#include <entt/core/hashed_string.hpp>
#include <mm/logger.hpp>
#define LOG_CRIT(...) __LOG_CRIT( "SimpleSDLRenderer", __VA_ARGS__)
@ -8,7 +11,6 @@
#define LOG_DEBUG(...) __LOG_DEBUG("SimpleSDLRenderer", __VA_ARGS__)
#define LOG_TRACE(...) __LOG_TRACE("SimpleSDLRenderer", __VA_ARGS__)
namespace MM::Services {
SimpleSDLRendererService::SimpleSDLRendererService(void) {
@ -42,18 +44,12 @@ bool SimpleSDLRendererService::enable(Engine& engine) {
return false;
}
_render_handle = engine.addUpdate([this](Engine& e){ this->render(e); });
targets["display"].reset(renderer, 800, 600);
return true;
}
void SimpleSDLRendererService::disable(Engine& engine) {
if (!_render_handle.expired()) {
engine.removeUpdate(_render_handle);
}
void SimpleSDLRendererService::disable(Engine&) {
processors.clear();
targets.clear();
@ -61,6 +57,17 @@ void SimpleSDLRendererService::disable(Engine& engine) {
SDL_DestroyRenderer(renderer);
}
std::vector<UpdateStrategies::UpdateCreationInfo> SimpleSDLRendererService::registerUpdates(void) {
return {
{
"SimpleSDLRendererService::render"_hs,
"SimpleSDLRendererService::render",
[this](Engine& e){ this->render(e); },
UpdateStrategies::update_phase_t::POST
}
};
}
void SimpleSDLRendererService::render(Engine& engine) {
targets["display"].clear(renderer, {40, 40, 40, 255});

View File

@ -21,8 +21,6 @@ namespace MM::Services {
std::vector<std::function<void(SimpleSDLRendererService&, Engine&)>> processors;
private:
Engine::FunctionDataHandle _render_handle;
public:
bool enable(Engine& engine) override;
@ -30,6 +28,9 @@ namespace MM::Services {
const char* name(void) override { return "SimpleSDLServiceSystem"; };
std::vector<UpdateStrategies::UpdateCreationInfo> registerUpdates(void) override;
private:
void render(Engine& engine);
};

View File

@ -11,7 +11,7 @@
TEST(player_velocity, basic_run) {
float delta = 1/60.f;
MM::Engine engine(delta);
MM::Engine engine;
engine.addService<MM::Services::SDLService>();
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
@ -19,7 +19,7 @@ TEST(player_velocity, basic_run) {
engine.addService<MM::Services::InputService>();
ASSERT_TRUE(engine.enableService<MM::Services::InputService>());
engine.addService<MM::Services::SimpleSceneService>();
engine.addService<MM::Services::SimpleSceneService>(delta);
ASSERT_TRUE(engine.enableService<MM::Services::SimpleSceneService>());
bool provide_ret = engine.provide<MM::Services::SceneServiceInterface, MM::Services::SimpleSceneService>();
@ -37,7 +37,8 @@ TEST(player_velocity, basic_run) {
//v.velocity = { 1.f, 1.f };
//v.rotation = 0.f;
engine.fixedUpdate();
engine.getUpdateStrategy().addDefered([](auto& e) { e.stop(); });
engine.run();
//ASSERT_EQ(t.position.x, 1.f * delta);
// TODO: TEST

View File

@ -1,23 +1,13 @@
#include <gtest/gtest.h>
#include <mm/engine.hpp>
#include <mm/services/simple_scene.hpp>
#include <mm/engine_fwd.hpp>
#include <entt/entity/registry.hpp>
#include <mm/systems/simple_velocity_system2d.hpp>
TEST(simple_velocity_2d, basic_run) {
float delta = 1/60.f;
MM::Engine engine(delta);
engine.addService<MM::Services::SimpleSceneService>();
ASSERT_TRUE(engine.enableService<MM::Services::SimpleSceneService>());
bool provide_ret = engine.provide<MM::Services::SceneServiceInterface, MM::Services::SimpleSceneService>();
ASSERT_TRUE(provide_ret);
auto& scene = engine.tryService<MM::Services::SceneServiceInterface>()->getScene();
MM::Scene scene;
// setup v system
MM::AddSystemToScene(scene, MM::Systems::SimpleVelocity);
@ -31,8 +21,10 @@ TEST(simple_velocity_2d, basic_run) {
v.velocity = { 1.f, 1.f };
v.rotation = 0.f;
engine.fixedUpdate();
::MM::EachSystemInScene(scene, [&](::MM::Scene& s, ::MM::System& fn) {
fn(s, 1.f/60.f);
});
ASSERT_EQ(t.position.x, 1.f * delta);
ASSERT_EQ(t.position.x, 1.f * 1.f/60.f);
}