Kale
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Kale::Scene Class Reference

#include <Scene.hpp>

+ Inheritance diagram for Kale::Scene:

Public Member Functions

 Scene ()
 
 Scene (const std::string &filename)
 
const std::list< std::shared_ptr< Node > > & getNodes () const
 
Color getBgColor () const
 
const CameragetCamera () const
 
Vector2f getViewport () const
 
Rect getSceneBounds () const
 
- Public Member Functions inherited from Kale::EventHandler
virtual void onWindowLostFocus ()
 
virtual void onWindowGainedFocus ()
 
virtual void onControllerConnect (unsigned int controller)
 
virtual void onControllerDisconnect (unsigned int controller)
 
virtual void onControllerButtonPress (unsigned int controller, ControllerButton button)
 
virtual void onControllerButtonRelease (unsigned int controller, ControllerButton button)
 
virtual void onControllerHandle (unsigned int controller, ControllerAxis handle, float position)
 
virtual void onControllerJoystick (unsigned int controller, ControllerAxis joystick, Vector2f position)
 
virtual void onKeyPress (Key key)
 
virtual void onKeyRelease (Key key)
 
virtual void onMouseMove (Vector2f pos)
 
virtual void onMouseScroll (float scroll)
 
virtual void onLeftClick ()
 
virtual void onMiddleClick ()
 
virtual void onRightClick ()
 
virtual void onLeftClickRelease ()
 
virtual void onMiddleClickRelease ()
 
virtual void onRightClickRelease ()
 
virtual void onTouchBegin (unsigned int touch, Vector2f pos)
 
virtual void onTouchMove (unsigned int touch, Vector2f pos)
 
virtual void onTouchEnd (unsigned int touch)
 

Static Public Member Functions

static void addNodeSaveStateConstructor (const std::string &key, std::function< std::shared_ptr< Node >(JSON)> constructor)
 

Protected Member Functions

template<typename T >
void addNode (std::shared_ptr< T > &node)
 
void removeNode (std::shared_ptr< Node > &node)
 
template<typename T >
void removeNode (std::shared_ptr< T > &node)
 
virtual void onPresent ()
 
virtual void onSceneChange ()
 
virtual void onUpdate (size_t threadNum, float deltaTime)
 
virtual void onPreUpdate (size_t threadNum, float deltaTime)
 
void onWindowResize (Vector2ui oldSize, Vector2ui newSize) override
 

Protected Attributes

Vector4f bgColor = 0xFFFFFF
 
Camera camera
 
Vector2f viewport
 
Rect sceneBounds
 

Private Member Functions

void render (float deltaTime) const
 
void update (size_t threadNum, float deltaTime)
 
void updateNodeStructures ()
 

Private Attributes

std::list< std::shared_ptr< Node > > nodes
 
std::vector< std::list< std::shared_ptr< Node > > > updateNodes
 
std::vector< std::list< std::shared_ptr< Node > > > preUpdateNodes
 
std::vector< std::pair< float, float > > threadedNodePerformanceTimes
 
std::queue< std::shared_ptr< Node > > nodesToAdd
 
std::queue< std::shared_ptr< Node > > nodesToRemove
 
std::mutex nodeQueueUpdateMutex
 
std::mutex nodePreUpdateMutex
 
std::condition_variable nodePreUpdateCondVar
 
size_t nodesPreUpdated
 
size_t generation
 
Transform worldToScreen
 

Static Private Attributes

static std::unordered_map< std::string, std::function< std::shared_ptr< Node >(JSON)> > nodeMap
 

Friends

class Application
 
class Node
 

Detailed Description

The main scene class for the game This class handles rendering and nodes management

Definition at line 45 of file Scene.hpp.

Constructor & Destructor Documentation

◆ Scene() [1/2]

Scene::Scene ( )

Constructs a new scene

Definition at line 41 of file Scene.cpp.

◆ Scene() [2/2]

Scene::Scene ( const std::string & filename)

Constructs a new scene from a scene save file

Parameters
filenameThe filename of the scene JSON file

Definition at line 60 of file Scene.cpp.

Member Function Documentation

◆ addNode()

template<typename T >
void Kale::Scene::addNode ( std::shared_ptr< T > & node)
inlineprotected

Adds a node to the scene to render/update

Parameters
nodeThe node to add

Definition at line 168 of file Scene.hpp.

◆ addNodeSaveStateConstructor()

static void Kale::Scene::addNodeSaveStateConstructor ( const std::string & key,
std::function< std::shared_ptr< Node >(JSON)> constructor )
inlinestatic

Adds a node save state constructor to the map of nodes used for scene loading.

Parameters
keyThe key used to identify this type of node
constructorThe constructor used to create nodes

Definition at line 274 of file Scene.hpp.

◆ getBgColor()

Color Scene::getBgColor ( ) const

Gets the background color of the scene

Returns
The background color

Definition at line 305 of file Scene.cpp.

◆ getCamera()

const Camera & Scene::getCamera ( ) const

Gets the camera used to render this scene

Returns
The camera

Definition at line 313 of file Scene.cpp.

◆ getNodes()

const std::list< std::shared_ptr< Node > > & Scene::getNodes ( ) const

Gets the ndoes within the scene

Returns
The nodes

Definition at line 297 of file Scene.cpp.

◆ getSceneBounds()

Rect Scene::getSceneBounds ( ) const

Due to the engine being scaled from 1080p, when dealing with wide or tall windows the screen space may start from a negative number or an unusually large number. This function returns the scene bounds (the x coordinates of the left and right points on the scene display)

Returns
The window bounds in world coordinates

Definition at line 330 of file Scene.cpp.

◆ getViewport()

Vector2f Scene::getViewport ( ) const

Gets the current viewport of the scene

Returns
The viewport

Definition at line 321 of file Scene.cpp.

◆ onPresent()

void Scene::onPresent ( )
protectedvirtual

Called when the current scene is presented

Definition at line 282 of file Scene.cpp.

◆ onPreUpdate()

void Scene::onPreUpdate ( size_t threadNum,
float deltaTime )
protectedvirtual

Called before all nodes are pre updated. Do NOT write code directly in here, this function is called once on each thread every frame. If you must run code every frame outside of a node, then make sure you're only running your required code on one thread by using an if statement on threadNum.

Parameters
threadNumThe thread this function is being called on
deltaTimeThe microseconds since the last update

Definition at line 275 of file Scene.cpp.

◆ onSceneChange()

void Scene::onSceneChange ( )
protectedvirtual

Called when the scene is about to be changed

Definition at line 289 of file Scene.cpp.

◆ onUpdate()

void Scene::onUpdate ( size_t threadNum,
float deltaTime )
protectedvirtual

Called before all nodes are updated. Do NOT write code directly in here, this function is called once on each thread every frame. If you must run code every frame outside of a node, then make sure you're only running your required code on one thread by using an if statement on threadNum.

Parameters
threadNumThe thread this function is being called on
deltaTimeThe microseconds since the last update

Definition at line 264 of file Scene.cpp.

◆ onWindowResize()

void Scene::onWindowResize ( Vector2ui oldSize,
Vector2ui newSize )
overrideprotectedvirtual

Called when the event is fired

Reimplemented from Kale::EventHandler.

Definition at line 103 of file Scene.cpp.

◆ removeNode() [1/2]

void Kale::Scene::removeNode ( std::shared_ptr< Node > & node)
protected

Removes a node from the scene

Parameters
nodeThe node to remove

◆ removeNode() [2/2]

template<typename T >
void Kale::Scene::removeNode ( std::shared_ptr< T > & node)
inlineprotected

Removes a node from the scene

Parameters
nodeThe node to remove

Definition at line 184 of file Scene.hpp.

◆ render()

void Scene::render ( float deltaTime) const
private

Renders the current scene

Parameters
deltaTimeThe time the last frame has taken to update and render

Definition at line 203 of file Scene.cpp.

◆ update()

void Scene::update ( size_t threadNum,
float deltaTime )
private

Updates the current scene

Parameters
threadNumthe index of this thread, ranged 0 - numUpdateThreads
deltaTimeThe time the last frame has taken to update and render

Definition at line 228 of file Scene.cpp.

◆ updateNodeStructures()

void Scene::updateNodeStructures ( )
private

Updates the data structures holding nodes based off of the queues, MUST be called on the main thread after the completion of all updates in the frame.

Definition at line 117 of file Scene.cpp.

Friends And Related Symbol Documentation

◆ Application

Application
friend

Definition at line 135 of file Scene.hpp.

◆ Node

friend class Node
friend

Definition at line 136 of file Scene.hpp.

Member Data Documentation

◆ bgColor

Vector4f Kale::Scene::bgColor = 0xFFFFFF
protected

The background color of the scene to use for clearing the screen

Definition at line 143 of file Scene.hpp.

◆ camera

Camera Kale::Scene::camera
protected

The scene's camera for viewing

Definition at line 148 of file Scene.hpp.

◆ generation

size_t Kale::Scene::generation
private

The count of frames used for thread synchronization

Definition at line 108 of file Scene.hpp.

◆ nodeMap

std::unordered_map<std::string, std::function<std::shared_ptr<Node>(JSON)> > Kale::Scene::nodeMap
inlinestaticprivate

A map of strings to a constructor taking a JSON to initialize a node. All nodes capable of save states must be present in this map prior to the first scene's presentation. The best way to do this is via a node setup function, which can be added from application's onBegin to Application.

Definition at line 53 of file Scene.hpp.

◆ nodePreUpdateCondVar

std::condition_variable Kale::Scene::nodePreUpdateCondVar
private

Condition variable to check if threads have completed pre updating

Definition at line 98 of file Scene.hpp.

◆ nodePreUpdateMutex

std::mutex Kale::Scene::nodePreUpdateMutex
private

Mutex used for syncrhonizing pre updates

Definition at line 93 of file Scene.hpp.

◆ nodeQueueUpdateMutex

std::mutex Kale::Scene::nodeQueueUpdateMutex
private

The mutex used for adding/removing nodes safety

Definition at line 88 of file Scene.hpp.

◆ nodes

std::list<std::shared_ptr<Node> > Kale::Scene::nodes
private

A list of all the nodes to be presented in the current scene

Definition at line 58 of file Scene.hpp.

◆ nodesPreUpdated

size_t Kale::Scene::nodesPreUpdated
private

The count of nodes which have pre updated

Definition at line 103 of file Scene.hpp.

◆ nodesToAdd

std::queue<std::shared_ptr<Node> > Kale::Scene::nodesToAdd
private

A queue of nodes to add

Definition at line 78 of file Scene.hpp.

◆ nodesToRemove

std::queue<std::shared_ptr<Node> > Kale::Scene::nodesToRemove
private

A queue of nodes to remove

Definition at line 83 of file Scene.hpp.

◆ preUpdateNodes

std::vector<std::list<std::shared_ptr<Node> > > Kale::Scene::preUpdateNodes
private

Stores the nodes to be pre updated by their pre update thread

Definition at line 68 of file Scene.hpp.

◆ sceneBounds

Rect Kale::Scene::sceneBounds
protected

Due to the engine being scaled from 1080p, when dealing with wide or tall windows the screen space may start from a negative number or an unusually large number. This variable holds the scene bounds, (the x position of the left & right points on the window)

Definition at line 162 of file Scene.hpp.

◆ threadedNodePerformanceTimes

std::vector<std::pair<float, float> > Kale::Scene::threadedNodePerformanceTimes
private

Holds the sum of the update times and pre update times of each set per thread

Definition at line 73 of file Scene.hpp.

◆ updateNodes

std::vector<std::list<std::shared_ptr<Node> > > Kale::Scene::updateNodes
private

Stores the nodes to be updated by their update thread

Definition at line 63 of file Scene.hpp.

◆ viewport

Vector2f Kale::Scene::viewport
protected

The viewport of the world (the width and height of the screen in the world's coordinates) This always has a height of 1080p and the width is dependant on the screen's aspect ratio. It is recommended to code all nodes based off of the 1080p height and let the engine do the scaling

Definition at line 156 of file Scene.hpp.

◆ worldToScreen

Transform Kale::Scene::worldToScreen
private

The world to screen transformation matrix. Used internally for rendering and converting vertices from world points to screen points.

Definition at line 114 of file Scene.hpp.


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