Game class#

These files (Game.hpp and .cpp) contain the main Game class and its AssetManager.

Typedefs

using TexTup = std::tuple<SDL_Texture*, int, int>#

This tuple is the return type of AssetManager::load_texture. It consists of the SDL_Texture pointer, the texture’s width and its height, respectively.

using vec = Eigen::Vector2f
using Animation = Game::AssetManager::Animation#
class Game#
#include <Game.hpp>

Game class definition, used for running the game and handling events. Most functions are defined in Game.cpp.

Public Functions

Game()#
~Game()#
int init(const char *title, int x, int y, int width, int height, bool fullscreen)#

Initialization of SDL, the game screen and initial renderings.

void handle_events()#

Handling inputs and events.

void update()#

Go through our game objects and update them.

void render()#

Render objects in the screen.

void clean()#

Free memory.

inline bool running()#

Public Static Attributes

static SDL_Renderer *renderer = nullptr#

Renderer object responsible for rendering objects.

Definition of Game object’s static variables:

To tell if the game is still running. It is called in the main function at each tick, to determine whether to finish execution or not.

static SDL_Event event#

Event object used in event handling.

static std::vector<Collider*> collider_vector#

Vector of collider objects for checking collision.

static bool tracking_player = false#

Whether the camera should follow the player or not.

static vec camera_position = vec(0.0, 0.0)#

Where the camera is positioned (top-left of the screen).

static std::shared_ptr<Entity> cursor = manager.addEntity("CURSOR")#
static AssetManager assets = Game::AssetManager()#

The game’s AssetManager instance, which handles textures and fonts.

Private Functions

void update_camera_ref_entity()#

For dealing with the camera reference entity if it is destroyed.

Private Members

bool is_running#

Whether the game is still running or it can be closed.

int update_counter = 0#

A tick counter (I believe it is deprecated TODO: REMOVE?)

SDL_Window *window#

The SDL window where the game is displayed.

vec previous_camera_position = vec(0.0, 0.0)#

Previous camera position (for updating).

std::weak_ptr<Entity> camera_ref_entity#

The entity we are following with the camera.

vec previous_ref_entity_position = vec(0.0, 0.0)#

Previous position of the reference entity (for updating).

struct AssetManager#
#include <Game.hpp>

Definition of the AssetManager class, which manages textures and fonts. Function definitions are made in AssetManager.cpp. This class had to be defined in this file because it depends on the Game class definition, which also depends on AssetManager. Creating an AssetManager.hpp header would cause a circular import problem.

Public Functions

inline AssetManager()#
~AssetManager()#

Destroys all texture and font pointers. Defined in AssetManager.cpp.

void add_texture(std::string id, std::string path)#

Load a texture from a path and save it under the ID “id” in the maps.

void add_texture(std::string id, std::string path, std::map<std::string, Animation> sprite_animation_map)

Load a texture from a path and save it under the ID “id” in the maps. Then, save its animations in the animation map.

SDL_Texture *get_texture(std::string id)#

Get a texture from the texture_map.

TexTup get_tuple(std::string id)#

Get complete information from a texture (SDL_Texture* object, width and height).

void destroy_texture(std::string id)#

Destroy a texture based on its ID. This can be called when a given texture is no longer used. This will also remove the map entry (necessary to avoid issues in the destructor).

void add_font(std::string id, std::string path, int font_size)#

Add a new font to the font_map, under the ID “id”.

TTF_Font *get_font(std::string id)#

Get a font object from its ID.

void destroy_font(std::string id)#

Close a font based on its ID. This can be called when a given font is no longer used. This will also remove the map entry (necessary to avoid issues in the destructor).

Public Members

std::map<std::string, SDL_Texture*> texture_map#

A map from strings (an ID) to textures objects.

std::map<std::string, bool> is_animated_map#

A map from strings (an ID) to a bool indicating if the texture has animations.

std::map<std::string, std::map<std::string, Animation>> animation_map#

A map from strings (an ID) to a map of animation objects (animation name -> Animation object).

std::map<std::string, int> width_map#

A map from strings (an ID) to the associated texture’s width.

std::map<std::string, int> height_map#

A map from strings (an ID) to the associated texture’s height.

std::map<std::string, TTF_Font*> font_map#

A map from strings (an ID) to font objects.

struct Animation#
#include <Game.hpp>

Useful struct for structuring Animation information.

Public Functions

inline Animation(int f, int t, int w, int h, int i)#

Constructor

Parameters:
  • f – frames member.

  • t – animation_period member (ms/frame).

  • w – sprite_width member (pixels).

  • h – sprite_height member (pixels).

  • i – the index of the animation.

Public Members

int frames#

Number of frames in the animation.

int animation_period#

Period of each frame in miliseconds.

int sprite_width#
int sprite_height#
int index#

The animation index (for when the Entity has multiple animations).

int src_y#

The height (in pixels) at which we start “reading” the animation from the texture source file. This member is set up by the AssetManager::add_texture member when an animation is added