-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WorkFactory which allows to delay creation of the next Work
- Loading branch information
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |