JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
job.hpp
Go to the documentation of this file.
1// Copyright 2012-2023 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include <functional>
8#include <memory>
9
10namespace jngl {
11
16class Job {
17public:
22 virtual void step() = 0;
23
28 virtual void draw() const = 0;
29
31 Job() = default;
32
34 Job(const Job&) = default;
35
37 Job& operator=(const Job&) = default;
38
40 Job(Job&&) = default;
41
43 Job& operator=(Job&&) = default;
44
46 virtual ~Job();
47};
48
51
53template <class T, class... Args>
54void addJob(Args&&... args) {
55 addJob(std::make_shared<T>(std::forward<Args>(args)...));
56}
57
62
64std::shared_ptr<Job> getJob(const std::function<bool(Job&)>& predicate);
65
67template <class T> std::shared_ptr<T> getJob() {
68 return std::dynamic_pointer_cast<T>(
69 getJob([](Job& job) { return dynamic_cast<T*>(&job) != nullptr; }));
70}
71
72} // namespace jngl
Background job which stays part of the main loop independent of the active jngl::Work.
Definition: job.hpp:16
virtual void draw() const =0
Draw the game state.
Job & operator=(Job &&)=default
Move assignment.
Job & operator=(const Job &)=default
Copy assignment.
virtual void step()=0
Advance the game logic.
virtual ~Job()
Does nothing.
Job()=default
Does nothing.
Job(const Job &)=default
Copy constructor.
Job(Job &&)=default
Move constructor.
JNGL's main namespace.
Definition: Achievement.hpp:10
std::shared_ptr< T > getJob()
Returns the first Job that is a T.
Definition: job.hpp:67
void removeJob(Job *)
Removes the passed Job after all Jobs have been stepped.
void addJob(std::shared_ptr< Job > job)
Add a new Job which will be always be stepped and drawn by App::mainLoop()