JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Job Class Referenceabstract

Background job which stays part of the main loop independent of the active jngl::Scene. More...

#include <jngl/job.hpp>

Public Member Functions

virtual void step ()=0
 Advance the game logic.
 
virtual void draw () const =0
 Draw the game state.
 
virtual void onFileDrop (const std::filesystem::path &)
 Called when a file has been dropped onto the window.
 
virtual void onControllersChanged ()
 Whenever the return value of jngl::getConnectedControllers would change, this method gets called.
 
 Job ()=default
 Does nothing.
 
 Job (const Job &)=default
 Copy constructor.
 
Joboperator= (const Job &)=default
 Copy assignment.
 
 Job (Job &&)=default
 Move constructor.
 
Joboperator= (Job &&)=default
 Move assignment.
 
virtual ~Job ()
 Does nothing.
 

Detailed Description

Background job which stays part of the main loop independent of the active jngl::Scene.

This can be used for an achievement system for example. Override this class and use jngl::addJob to register it.

Jobs (meaning instances of this class that were added using jngl::addJob) are always stepped before the active jngl::Scene and drawn after it. The draw order is reversed, meaning the first Job you added will always be drawn last. Example:

#include <jngl.hpp>
#include <jngl/init.hpp>
class MyFirstJob : public jngl::Job {
void step() override { /* ... */ }
void draw() const override { /* ... */}
};
class MySecondJob : public jngl::Job { /* ... */ };
class MyScene : public jngl::Scene {
void step() override { /* ... */ }
void draw() const override { /* ... */ }
};
// ...
params.start = []() {
jngl::addJob<MyFirstJob>();
jngl::addJob<MySecondJob>();
jngl::setScene<MyScene>();
};
return params;
}
Background job which stays part of the main loop independent of the active jngl::Scene.
Definition job.hpp:62
Active state of the game, e.g. a menu or the game itself.
Definition work.hpp:13
Include this file only once, as it defines the main function.
jngl::AppParameters jnglInit()
Implement this function and set AppParameters::start.
Includes all JNGL headers except for init.hpp.
Parameters used to initialize the main window.
std::function< std::shared_ptr< jngl::Work >()> start
A factory function which creates the first jngl::Work.

In this example, the order of function calls in each frame will be:

  1. MyFirstJob::step()
  2. MySecondJob::step()
  3. MyScene::step()
  4. MyScene::draw()
  5. MySecondJob::draw()
  6. MyFirstJob::draw()
Note
Steps 4-6 are not guaranteed to be called the same number of times as steps 1-3, depending on the frame rate and the steps per second.

Definition at line 62 of file job.hpp.

Inheritance diagram for Job:
[legend]

Member Function Documentation

◆ step()

virtual void step ( )
pure virtual

Advance the game logic.

This function gets called 60 times per second or whatever has been set by setStepsPerSecond().

Note
JNGL's main loop calls all Jobs' step() functions before the active Scene's step()

Implemented in Fade, and WorkFactory.

◆ draw()

virtual void draw ( ) const
pure virtual

Draw the game state.

As it isn't garuanteed that this function is called as often as Job::step, you shouldn't change any game state in it.

Note
JNGL's main loop calls all Jobs' draw() functions after the active Scene's draw()

Implemented in Fade, and WorkFactory.


The documentation for this class was generated from the following file: