JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Work Class Reference

Active state of the game, e.g. a menu or the game itself. More...

#include <jngl/work.hpp>

Public Member Functions

virtual void onBackEvent ()
 Gets called on Android's "back" gesture or when the Esc key is pressed.
 
virtual void onQuitEvent ()
 Gets called when the user closes the main window or quit() has been called.
 
virtual void onControllerBack ()
 Gets called when the "Back" button is pressed on any connected controller (Android only)
 
virtual void onPauseEvent ()
 Gets called when the main window loses focus or the app is put in the background.
 
virtual void onLoad ()
 Gets called when the Work is activated by the main loop.
 
virtual void onUnload ()
 Gets called when the Work is deactivated by the main loop.
 
virtual void onToggleFullscreen ()
 Gets called when the user pressed Alt + Enter
 
virtual bool supportsScreenSize (Vec2 screenSize) const
 Gets asked by JNGL whether this Scene could be shown with screenSize instead of letter-boxing.
 
- Public Member Functions inherited from Job
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.
 
virtual void onScreenSizeChanged ()
 Gets called after the return value of jngl::getScreenSize() has changed, see jngl::Scene::supportsScreenSize()
 
 Job ()=default
 Does nothing.
 
 Job (const Job &)=default
 Copy constructor.
 
Job & operator= (const Job &)=default
 Copy assignment.
 
 Job (Job &&)=default
 Move constructor.
 
Job & operator= (Job &&)=default
 Move assignment.
 
virtual ~Job ()
 Does nothing.
 

Detailed Description

Active state of the game, e.g. a menu or the game itself.

Definition at line 14 of file work.hpp.

Inheritance diagram for Work:
[legend]
Collaboration diagram for Work:
[legend]

Member Function Documentation

◆ onBackEvent()

virtual void onBackEvent ( )
virtual

Gets called on Android's "back" gesture or when the Esc key is pressed.

Calls quit() by default, so exits the game. It's a good idea that you would only use the default behavior in the main menu though. While in-game you normally would switch to a pause menu. In the pause menu onBackEvent() would go to the main menu's Work.

Example:

class MainMenu : public jngl::Work {
// default implementation of onBackEvent which quits the game
};
class PauseMenu : public jngl::Work {
void onBackEvent() override {
}
};
class Game : public jngl::Work {
void onBackEvent() override {
}
};
Active state of the game, e.g. a menu or the game itself.
Definition work.hpp:14
virtual void onBackEvent()
Gets called on Android's "back" gesture or when the Esc key is pressed.
void setScene(std::shared_ptr< Scene >)
Sets the passed Scene to be active in the main loop for the next frame.

◆ onQuitEvent()

virtual void onQuitEvent ( )
virtual

Gets called when the user closes the main window or quit() has been called.

To continue with the main loop, call cancelQuit(). For example if you want to switch to a pause menu instead of exiting the game:

class Game : public jngl::Work {
void onQuitEvent() {
}
};
virtual void onQuitEvent()
Gets called when the user closes the main window or quit() has been called.
void cancelQuit()
Undo quit() or ignore a quit event caused by the user closing the main window.
Note
Ctrl+C (SIGINT) calls forceQuit() with exitcode 130 instead and does not invoke this.

Reimplemented in Fade.

◆ onControllerBack()

virtual void onControllerBack ( )
virtual

Gets called when the "Back" button is pressed on any connected controller (Android only)

As the TV remote is registered as a controller this is important to handle Back on the TV remote. Most likely you won't need to overwrite this though, as it calls onBackEvent() by default and having Android's "Back" button behave the same as "Back" on the TV remote is a good idea.

◆ onPauseEvent()

virtual void onPauseEvent ( )
virtual

Gets called when the main window loses focus or the app is put in the background.

On iOS and Android this will happen when the user switches to another app. As the main loop continues to run you might want to pause the game if necessary.

◆ onLoad()

virtual void onLoad ( )
virtual

Gets called when the Work is activated by the main loop.

Reimplemented in WorkFactory.

◆ onToggleFullscreen()

virtual void onToggleFullscreen ( )
virtual

Gets called when the user pressed Alt + Enter

By default it switches between fullscreen and windowed mode. You can overwrite this if you want to do something else, e.g. if you want to disable toggling fullscreen mode because you need the keyboard shortcut for something else.

Note
Only implemented on desktop platforms (i.e. Linux, Windows and macOS).

◆ supportsScreenSize()

virtual bool supportsScreenSize ( Vec2 screenSize) const
virtual

Gets asked by JNGL whether this Scene could be shown with screenSize instead of letter-boxing.

The canvas keeps the size it had when the window was created, even when the window changes its shape later on, e.g. because the user rotated their phone or resized the window. Instead JNGL scales it to fit into the window, letter-boxing it where the shapes differ. The same happens when AppParameters::minAspectRatio or maxAspectRatio didn't allow the canvas to fill the whole window in the first place.

Before each step in which the window doesn't have that shape, JNGL asks the active Scene whether it can handle the screen size that would fill the whole window instead. If it can't and the window's aspect ratio is outside of AppParameters::minAspectRatio and maxAspectRatio, JNGL asks again for the screen size which is letter-boxed to them, i.e. what the canvas would be if the window had been created with its current size. When this returns true, jngl::getScreenSize() will return screenSize from then on and Job::onScreenSizeChanged() gets called. Once it returns false for both, e.g. because another Scene became active which doesn't override this method, the canvas goes back to its original size.

screenSize is the size of the canvas in actual pixels divided by the scale factor that AppParameters::scaleFactor returns for it. If that hasn't been set, jngl::getScaleFactor() is used, as it doesn't change once the window has been created.

Returns false by default.

class MainMenu : public jngl::Scene {
bool supportsScreenSize(jngl::Vec2 screenSize) const override {
return screenSize.x >= 900; // the buttons need at least that much space
}
void onScreenSizeChanged() override {
quitButton.setPos(jngl::getScreenSize() / 2. - jngl::Vec2(100, 50));
}
// ...
};
virtual void onScreenSizeChanged()
Gets called after the return value of jngl::getScreenSize() has changed, see jngl::Scene::supportsScr...
Two-dimensional vector.
Definition Vec2.hpp:36
double x
x component
Definition Vec2.hpp:45
virtual bool supportsScreenSize(Vec2 screenSize) const
Gets asked by JNGL whether this Scene could be shown with screenSize instead of letter-boxing.
Vec2 getScreenSize()
Returns the size of the useable draw area (excluding letter-boxing) in screen pixels.

Reimplemented in Fade.


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