Skip to content

Commit

Permalink
Add WorkFactory which allows to delay creation of the next Work
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Jan 25, 2024
1 parent 9a66076 commit 0d1e2cc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/jngl.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2023 Jan Niklas Hasse <[email protected]>
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
/// Includes all JNGL headers except for init.hpp
/// @file
Expand All @@ -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"
Expand Down
27 changes: 27 additions & 0 deletions src/jngl/WorkFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Jan Niklas Hasse <[email protected]>
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#include "WorkFactory.hpp"
#include <cassert>

namespace jngl {

WorkFactory::WorkFactory(std::function<std::shared_ptr<Work>()> 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
23 changes: 23 additions & 0 deletions src/jngl/WorkFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 Jan Niklas Hasse <[email protected]>
// 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<std::shared_ptr<Work>()>);

private:
void step() override;
void draw() const override;
void onLoad() override;

std::shared_ptr<Work> work;
std::function<std::shared_ptr<Work>()> factory;
};

} // namespace jngl

0 comments on commit 0d1e2cc

Please sign in to comment.