From 0d1e2cc1f3b63c908567b0aee0c53f760d7b0288 Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Thu, 25 Jan 2024 17:04:50 +0100 Subject: [PATCH] Add WorkFactory which allows to delay creation of the next Work --- src/jngl.hpp | 3 ++- src/jngl/WorkFactory.cpp | 27 +++++++++++++++++++++++++++ src/jngl/WorkFactory.hpp | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/jngl/WorkFactory.cpp create mode 100644 src/jngl/WorkFactory.hpp diff --git a/src/jngl.hpp b/src/jngl.hpp index 5de7a4cde..f0901eee2 100644 --- a/src/jngl.hpp +++ b/src/jngl.hpp @@ -1,4 +1,4 @@ -// Copyright 2012-2023 Jan Niklas Hasse +// Copyright 2012-2024 Jan Niklas Hasse // For conditions of distribution and use, see copyright notice in LICENSE.txt /// Includes all JNGL headers except for init.hpp /// @file @@ -21,6 +21,7 @@ #include "jngl/Vertex.hpp" #include "jngl/Video.hpp" #include "jngl/Widget.hpp" +#include "jngl/WorkFactory.hpp" #include "jngl/debug.hpp" #include "jngl/effects.hpp" #include "jngl/font.hpp" diff --git a/src/jngl/WorkFactory.cpp b/src/jngl/WorkFactory.cpp new file mode 100644 index 000000000..2fbe893bc --- /dev/null +++ b/src/jngl/WorkFactory.cpp @@ -0,0 +1,27 @@ +// Copyright 2024 Jan Niklas Hasse +// For conditions of distribution and use, see copyright notice in LICENSE.txt +#include "WorkFactory.hpp" +#include + +namespace jngl { + +WorkFactory::WorkFactory(std::function()> factory) +: factory(std::move(factory)) { +} + +void WorkFactory::step() { + assert(factory); // we should only be stepped once + factory = nullptr; + work->step(); +} + +void WorkFactory::draw() const { + work->draw(); +} + +void WorkFactory::onLoad() { + work = factory(); + jngl::setWork(work); +} + +} // namespace jngl diff --git a/src/jngl/WorkFactory.hpp b/src/jngl/WorkFactory.hpp new file mode 100644 index 000000000..74d9aafd3 --- /dev/null +++ b/src/jngl/WorkFactory.hpp @@ -0,0 +1,23 @@ +// Copyright 2024 Jan Niklas Hasse +// For conditions of distribution and use, see copyright notice in LICENSE.txt +/// @file +#pragma once + +#include "work.hpp" + +namespace jngl { + +class WorkFactory : public jngl::Work { +public: + explicit WorkFactory(std::function()>); + +private: + void step() override; + void draw() const override; + void onLoad() override; + + std::shared_ptr work; + std::function()> factory; +}; + +} // namespace jngl