jade-engine  0.0
JadeEngine::Game Class Reference

Public Member Functions

 Game ()
 
bool Initialize (const GameInitParams &initParams, char *argv[])
 
void CleanUp ()
 
void Start ()
 
 Game (const Game &)=delete
 
Gameoperator= (const Game &)=delete
 
void AddScene (const int32_t id, const std::shared_ptr< IScene > &scene)
 
void PlayScene (const int32_t id)
 
void End ()
 
void SetFullscreen (const bool fullscreen)
 
bool RandomBool ()
 
int32_t RandomNumber (int32_t min, int32_t max)
 
float RandomNumber (float min, float max)
 
float RandomNumber01 ()
 
void SetCursor (const std::string &name)
 
SpriteCreateSolidColorSprite (const uint32_t width, const uint32_t height, const SDL_Color &color, int32_t z, ObjectLayer layer)
 
int32_t GetWidth () const
 
int32_t GetHeight () const
 
int32_t GetHalfWidth () const
 
int32_t GetHalfHeight () const
 
Vector2D_i32 GetHalfSize () const
 
Vector2D_i32 GetMiddlePoint () const
 
const SpriteGetHoveredSprite () const
 
SDL_Renderer * GetRenderer ()
 
bool IsFullscreen () const
 
void SetDisplayMode (const int32_t index)
 
const int32_t GetCurrentDisplayMode () const
 
const std::vector< DisplayModeInfo > & GetDisplayModes () const
 
template<typename Class , typename CreationStruct >
std::add_pointer_t< Class > Create (const CreationStruct &params)
 
void StartBatchCreate ()
 
void EndBatchCreate ()
 
const SpriteSheetDescription * GetSpriteSheetDescription (const std::string &name)
 
void SetKeybinding (const int32_t settingsId, const int32_t newValue)
 
const std::unordered_map< int32_t, KeyBindingDescription > & GetKeyBindings () const
 
int32_t GeyKeyFromKeybind (const int32_t keybindSettingId)
 
int32_t GetMajorVersion () const
 
int32_t GetMinorVersion () const
 
const std::string & GetHashVersion () const
 
int32_t GetCopyrightYear () const
 
const std::string & GetAuthor () const
 
void SetVersion (const int32_t major, const int32_t minor, const std::string &hash)
 
void SetCopyrightYear (const int32_t year)
 
void SetAuthor (const std::string &author)
 
uint32_t GetNativeTextureFormats () const
 
TTF_Font * FindFont (const std::string &fontName, const uint32_t size) const
 
std::shared_ptr< TextureFindTexture (const std::string &textureName) const
 
std::shared_ptr< TextureCopyTexture (const std::shared_ptr< Texture > &textureDesc, const TextureSampling sampling)
 
void DestroyCopyTexture (SDL_Texture *texture)
 

Constructor & Destructor Documentation

◆ Game() [1/2]

JadeEngine::Game::Game ( )

Default ctor for Game class.

Warning
GGame global static variable was already constructed for you, hence you shouldn't construct new instances of this class.

◆ Game() [2/2]

JadeEngine::Game::Game ( const Game )
delete

Copying not-allowed, use GGame global variable as the single Game instance.

Member Function Documentation

◆ AddScene()

void JadeEngine::Game::AddScene ( const int32_t  id,
const std::shared_ptr< IScene > &  scene 
)

Add a scene instance to the Game map of scenes.

Adding scene should generally be done after successful Initialize() and before Start() call.

Parameters
idIdentification of the scene that will serve as a key in the map of scenes. For built-in scene use Scene enum in EngineConstants.h. For new scenes it is recommended to create new enum starting with kScene_JadeEngineScenesEnd to guarantee uniqueness.
sceneScene instance. Game class will make a copy of this shared_ptr so it's not necessary to hold onto it and can be constructed in place.
See also
Game::PlayScene, IScene
#include "EngineConstants.h"
#include "Game.h"
#include "GameInitParams.h"
#include "PoweredByJadeEngineScene.h"
// Assuming we have filled GameInitParams variable kGameInitParams, see GameInitParams for a template
auto& game = GGame;
if (game.Initialize(kGameInitParams, argv))
{
game.AddScene(kScene_PoweredByJadeEngine, std::make_shared<PoweredByJadeEngineScene>());
game.PlayScene(kScene_PoweredByJadeEngine);
game.Start();
}

◆ CleanUp()

void JadeEngine::Game::CleanUp ( )

Destroy all loaded assets, close any outstanding handles, "quit" internal SDL2 systems.

Should be last operation on GGame before exiting the program.

#include "Game.h"
#include "GameInitParams.h"
int32_t main(int32_t argc, char* argv[])
{
auto& game = GGame;
// Initialize the game and start it, see Game::Initialize
game.CleanUp();
return 0;
}

◆ CopyTexture()

std::shared_ptr< Texture > JadeEngine::Game::CopyTexture ( const std::shared_ptr< Texture > &  textureDesc,
const TextureSampling  sampling 
)

Create a new deep copy of Texture. Note that it is not possible to look-up this new texture later hence the return value should be captured.

Used for tinting and setting transparency of sprites as an unique instance is necessary otherwise such operations would change all sprites using this texture.

Parameters
textureDescExisting texture that can be obtained with Game::FindTexture.
samplingWanted texture sampling for the new texture.
Returns
A new texture, deep copy of the passed texture.
See also
FindTexture, TextureSampling
Warning
Potentially expensive operation including render targets.
Instead of working with textures it is recommend to use existing Sprite-based classes. Should be seldom used.

◆ Create()

template<typename Class , typename CreationStruct >
std::add_pointer_t<Class> JadeEngine::Game::Create ( const CreationStruct &  params)
inline

Create a new game object.

The object must inherit from IGameObject interface.

The object will belong to the current scene unless kObjectLayer_Persistent_UI is specified in which case it will belong to special persistent scene.

When creating large amount of objects consider doing so between GGame.StartBatchCreate() and GGame.EndBatchCreate() block.

Parameters
paramsCreation structure. For the actual type and its description see the class's constructor or header.
Returns
Pointer to the newly created game object. Game instance owns the game object but it might not possible to look it up later. Storing the pointer is advised.
See also
IGameObject, Text, TextParams, FTC, FTCParams, Button, ButtonParams, Checkbox, CheckboxParams, Dropdown, DropdownParams, Slider, SliderParams, Sprite, SpriteParams, BoxSprite, BoxSpriteParams
// Assuming we have filled `TextParams textParams` variable
Text* text = GGame.Create<Text>(textParams);
text->SetPosition(50, 50);

◆ End()

void JadeEngine::Game::End ( )
inline

Request the game loop to finish and stop updating. Typically used when exiting the game.

The game loop will exit at the beginning of the next tick. All updates and rendering will stop. However nothing gets cleaned up for that use Game::CleanUp.

See also
Game::Start, Game::CleanUp
// Assuming we have created a Button _quitButton
if (_quitButton->Released())
{
GGame.End();
}

◆ FindFont()

TTF_Font * JadeEngine::Game::FindFont ( const std::string &  fontName,
const uint32_t  size 
) const

Find underlying SDL2 TTF font instance given a font name and a size.

Parameters
fontNameThe font identification string as defined in GameInitParamsFontEntry when initializing the game. For fonts included in Jade Engine see EngineDefaultInitParams.h : kDefaultFonts.
sizeThe font size as listed in GameInitParams::fontSizes when initializing the game. For font sizes for fonts included in Jade Engine see EngineDefaultInitParams.h : kDefaultFontSizes.
Returns
Found underlying SDL2 TTF_Font opaque pointer or nullptr if not found.
Precondition
GGame was Initialize() with the wanted font or in is part of default assets.
See also
GameInitParamsFontEntry, GameInitParams, Text
Warning
Only useful when creating a complex custom text-based game objects and should be seldom used. For usage see existing text objects such as Text.

◆ FindTexture()

std::shared_ptr< Texture > JadeEngine::Game::FindTexture ( const std::string &  textureName) const

Find Texture instance given a texture name.

Parameters
textureNameThe texture identification string as defined in GameInitParamsTextureEntry when initializing the game.
Returns
Found Texture instance or nullptr if not found.
Precondition
GGame was Initialize() with the wanted texture.
See also
GameInitParamsFontEntry, GameInitParams, Sprite
Warning
Only useful when creating a complex custom texture-based game objects and should be seldom used. For usage see Sprite.

◆ Initialize()

bool JadeEngine::Game::Initialize ( const GameInitParams initParams,
char *  argv[] 
)

Initialize a new game.

Parameters
initParamsParameters required to initialize the game.
argvArgument vector as passed to the main function.
Warning
Should be a first operation on GGame in the program life cycle.
Returns
Whether the initialization was successful. Reasons for failing are generally non-recoverable internal SDL2 libraries failing. Debugging recommended.
See also
GameInitParams
#include "Game.h"
#include "GameInitParams.h"
using namespace JadeEngine;
int32_t main(int32_t argc, char* argv[])
{
// Assuming we have filled GameInitParams variable kGameInitParams, see GameInitParams for a template
auto& game = GGame;
if (game.Initialize(kGameInitParams, argv))
{
// Add scenes, play one, Start(), etc.
}
game.CleanUp();
return 0;
}

◆ operator=()

Game& JadeEngine::Game::operator= ( const Game )
delete

Copying not-allowed, use GGame global variable as the single Game instance.

◆ PlayScene()

void JadeEngine::Game::PlayScene ( const int32_t  id)

Play a previously added scene, making it the current scene.

Among other things, the current scene receives Update() callbacks and all game objects created during this scene are rendered.

If the scene is being played for the first time its Start() callback is triggered.

Nothing happens if a scene that was not previously added is played.

Parameters
nameScene identification number as specified in Game::AddScene.
See also
IScene, Game::AddScene
Precondition
The scene was added using Game::AddScene.
#include "EngineConstants.h"
#include "Game.h"
#include "GameInitParams.h"
#include "PoweredByJadeEngineScene.h"
// Assuming we have filled GameInitParams variable kGameInitParams, see GameInitParams for a template
auto& game = GGame;
if (game.Initialize(kGameInitParams, argv))
{
game.AddScene(kScene_PoweredByJadeEngine, std::make_shared<PoweredByJadeEngineScene>());
game.PlayScene(kScene_PoweredByJadeEngine);
game.Start();
}

◆ RandomBool()

bool JadeEngine::Game::RandomBool ( )

Return a random boolean value.

Internally uses std::mt19937 and std::uniform_int_distribution.

◆ RandomNumber() [1/2]

float JadeEngine::Game::RandomNumber ( float  min,
float  max 
)

Returns a random floating value.

Internally uses std::mt19937 and std::uniform_real_distribution.

Parameters
minMinimum value of random number. The potential random value includes this minimum.
maxMaximum value of random number. The potential random value includes this maximum.

◆ RandomNumber() [2/2]

int32_t JadeEngine::Game::RandomNumber ( int32_t  min,
int32_t  max 
)

Returns a random integer value.

Internally uses std::mt19937 and std::uniform_int_distribution.

Parameters
minMinimum value of random number. The potential random value includes this minimum.
maxMaximum value of random number. The potential random value includes this maximum.

◆ RandomNumber01()

float JadeEngine::Game::RandomNumber01 ( )

Returns a random floating value between 0.0f and 1.0f (including both values).

Internally uses std::mt19937 and std::uniform_real_distribution.

◆ SetCursor()

void JadeEngine::Game::SetCursor ( const std::string &  name)

Set a cursor as an active one.

Precondition
The cursor has been added in Game::Initialize.
See also
Game::Initialize, GameInitParams::cursors
Parameters
nameCursor name as specified in GameInitParamsCursorEntry::assetName

◆ SetFullscreen()

void JadeEngine::Game::SetFullscreen ( const bool  fullscreen)

Signal the game to switch to fullscreen or windowed mode.

It is advised to also update Setting::FullScreen settings value if this change is meant to persist across sessions. The actual change happens as the very first thing next game loop tick.

Parameters
fullscreenWhether the game should be in fullscreen mode or not. Nothing happens if it's currently already in the desired mode.
// Assuming we have created a Checkbox _fullScreenCheckbox
if (_fullScreenCheckbox->Changed())
{
GGame.SetFullscreen(_fullScreenCheckbox->Checked());
GPersistence.SetSettingTyped<Setting::FullScreen>(_fullScreenCheckbox->Checked());
}

◆ Start()

void JadeEngine::Game::Start ( )

Enter a blocking game-loop and start the game.

In order to exit the loop call Game::End.

It will also start GTime ticking.

See also
Game::End
Precondition
Game::Initialize was called and was successful.
#include "Game.h"
#include "GameInitParams.h"
// Assuming we have filled GameInitParams variable kGameInitParams, see GameInitParams for a template
auto& game = GGame;
if (game.Initialize(kGameInitParams, argv))
{
// Add scenes, play one
game.Start();
}
game.CleanUp();

The documentation for this class was generated from the following files:
GameInitParams.h
JadeEngine::Game::Create
std::add_pointer_t< Class > Create(const CreationStruct &params)
Definition: Game.h:299
JadeEngine::Game::AddScene
void AddScene(const int32_t id, const std::shared_ptr< IScene > &scene)
Definition: Game.cpp:505
JadeEngine::Game::CleanUp
void CleanUp()
Definition: Game.cpp:538
JadeEngine::Game::SetFullscreen
void SetFullscreen(const bool fullscreen)
Definition: Game.cpp:798
JadeEngine::Game::Start
void Start()
Definition: Game.cpp:763
JadeEngine::Game::End
void End()
Definition: Game.h:198