- Core
- Platform
- Credits
Everything concerning the User Interface such as the creation/destruction of windows, renderers, text, etc.. is here.
void create_window(SDL_Window*& window, const char* title, int x, int y, int width, int height): creates a window, with titletitle, of sizew*hat (x,y). By default,xandyareSDL_WINDOWPOS_CENTERED, and size is 960*540.void destroy_window(SDL_Window*& window): destroys thewindowand deals with the clean up of memory.
void create_renderer(SDL_Window* window, SDL_Renderer*& renderer): creates a new renderer for the selectedwindow, withSDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATEDon.void render_rect(SDL_Renderer* renderer, SDL_Rect rect, SDL_Color color): renders a rectangle on a specifiedrenderer, with a determined color.void renderer_update(SDL_Renderer* renderer): updated the renderer on each frame.void renderer_clear(SDL_Renderer* renderer): clears the renderer on each frame.void destroy_renderer(SDL_Renderer*& renderer): destroys the givenrendererand deals with the clean up of memory.
-
void init_ttf(): initializes theSDL_ttflibrary. Also, opens a dummy font to check if everything works correctly, and immediately closes it. -
void render_text(SDL_Renderer* renderer, const char* s, TTF_Font *font, int x, int y, int scale, SDL_Color fore): renders textsinto the givenrenderer, using thefontat the position (x,y) with sizescaleand colorfore.-
void generate_text(SDL_Renderer* renderer, const char* s, TTF_Font* font, int x, int y, int scale, SDL_Color fore): parent function, takes in an array of characterss, creates aSDL_Surfacefrom therendererand then creates aSDL_Texturein which a rectangle holds the text. If the array of characterssis longer than a set maximum length of$max_len = k * (W_{width} / scale)$ , where$k$ is the size of a single character,$W_{width}$ is the width of the window, and$scale$ is the size of the text to be rendered, it is split into chunks and then rendered one by one, using an offset.
-
-
void close_ttf(): closes theSDL_ttflibrary and deals with the clean up of memory.
The source directory has every component tied to the game engine itself.
Each time an image is created, we use the resource manager to efficiently load into memory the appropriate texture by checking an unordered map, which key is the file path. If found, it returns the texture, otherwise it's created from scratch and then added to the map.
SDL_Texture* get_texture(SDL_Renderer* renderer, const char* path): returns the texture if found, otherwise creates a new one.- Returns: appropriate
SDL_Texture*
- Returns: appropriate
void remove_texture(): removes a texture from the map.
Game objects is an abstract class that defines objects inside the engine.
GameObject(const char* name, const Vector2D& pos, int w, int h, SDL_Renderer* renderer, SDL_Color color): creates a new GameObject object with a specificnameat(pos.x,pos.y)withw*hdimensions. The object has a default color ofC_WHITEif not provided~GameObject(): deconstructor, deals with the clean up of memory. Will be called automatically when the program closes.
Then, the following methods are accessible:
const char* GameObject::get_name() const: get the GameObject object name.- Returns: a
const char*array.
- Returns: a
void GameObject::set_name(const char* n_n): set the GameObject object name,Vector2D GameObject::get_pos() const: get the GameObject object position.- Returns: a
Vector2Dof the position.
- Returns: a
void GameObject::set_pos(const Vector2D& n_pos): set the GameObject object position using aVector2D.int GameObject::get_width() const: get the GameObject object width.- Returns: a
intof the width.
- Returns: a
int GameObject::get_height() const: get the GameObject object height.- Returns: a
intof the width.
- Returns: a
void GameObject::set_width(int n_w): set the GameObject object width.void GameObject::set_height(int n_h): set the GameObject object height.Image& GameObject::get_image(): get the image alias.- Returns: a
Image&alias.
- Returns: a
const Image& GameObject::get_image() const: get the constant image alias, overrides the previous one if needed.- Returns: a
const Image&alias.
- Returns: a
void GameObject::set_image(const Image& image): set the GameObject object image using the copy constructor.void GameObject::set_image(Image&& image): set the GameObject object image using the move constructor. More efficent.GameObject::render(): renders theimginto the determined renderer (not the one of the GameObject object, but the one used in the creation of the image).
The Rigidbody is an abstract class that is responsible for physics simulation inside the game engine. Every GameObject has a Rigidbody object attached. Since every Rigidbody is defined as a pointer inside the GameObject, to access its methods it needs the operator ->
Rigidbody::Rigidbody(GameObject* n_go, double n_mass = 0.0, double n_drag = 0.0) noexcept: creates a new Rigidbody object with a specificGameObjectwithmassanddragRigidbody::~Rigidbody(): deconstructor, deals with the clean up of memory. Will be called automatically when the program closes.
Then, the following methods are accessible:
-
void Rigidbody::set_mass(double n_m): set the Rigidbody object mass. -
void Rigidbody::set_drag(double n_d): set the Rigidbody object drag. -
const double Rigidbody::get_mass() const: get the Rigidbody object mass.-
Returns: a
const doubleof the mass.
-
Returns: a
-
const double Rigidbody::get_drag() const: get the Rigidbody object drag.-
Returns: a
const doubleof the drag.
-
Returns: a
-
void Rigidbody::set_gravity(double n_g): set the Rigidbody object gravity. By default its 9,87. -
void Rigidbody::fall(): makes the Rigidbody object susceptible to gravity. -
void Rigidbody::add_force(const Vector2D& f): add a force onto the Rigidbody object. Uses$\frac{F_x * \delta t}{m}$
Always constrained to stay within the screen. -
void Rigidbody::accelerate(const Vector2D& a): accelerate the Rigidbody object. Uses$F_x * \delta t$
Always constrained to stay within the screen. -
void Rigidbody::impulse(const Vector2D& i): add an impulse onto the Rigidbody object. Uses$\frac{F_x}{m}$
Always constrained to stay within the screen. -
void Rigidbody::move(const Vector2D& n_pos): move the Rigidbody object to a determinedVector2Dposition using LERP and a step of0.1.
Always constrained to stay within the screen. -
bool constraint(GameObject* n_go, const Vector2D& max, const Vector2D& min): constrain the Rigidbody object into the window of size(max,min).-
Returns:
trueif it's within the constrains, elsefalse
-
Returns:
2D graphics and utilities are here.
Image is the class that defines all images rendered onto the window. Every time a image is created, it uses a resource manager to see if it was already loaded into memory. If it's the first instance, it is added, otherwise it's loaded as-is. Each one can be created as such:
Image(SDL_Renderer *renderer, const char* image_path, const Vector2D& pos int width, int height): instantiates a new Image object onto a givenrender, with the file located inimage_path(all images are stored intocore/imgs, a folder then copied into the build directory) at(pos.x,pos.y). By default,widthandheightare set to the original image size, but if provided, the image is scaled accordingly.Image(const Image& img) noexcept: copy constructor.Image(Image&& img) noexcept: move constructor.Image::~Image(): deconstructor, deals with the clean up of memory. Will be called automatically when the program closes.
Then, the following methods are accessible:
Vector2D Image::get_pos() const: get the Image object position.- Returns: a
Vector2Dof the position.
- Returns: a
void Image::set_pos(const Vector2D& n_pos): set the Image object position using aVector2D.int Image::get_width() const: get the Image object width.- Returns: a
intof the width.
- Returns: a
int Image::get_height() const: get the Image object height.- Returns: a
intof the width.
- Returns: a
void Image::set_width(int n_w): set the Image object width.void Image::set_height(int n_h): set the Image object height.SDL_Color Image::get_color() const: get the Image object color.- Returns: a
SDL_Color
- Returns: a
void Image::set_color(SDL_Color color): changes the image color to the RGBA value ofcolor.
When this texture is rendered, during the copy operation each source color channel is modulated by the appropriate color value according to the following formula:
$src_C = src_C \times (color / 255)$
SDL_Renderer* Image::get_renderer() const: get the Image object renderer.- Returns: a
SDL_Renderer*
- Returns: a
void Image::render(int x_pos, int y_pos): renders the image at a given position(x_pos,y_pos). By default, they're set to the origin point, else to the set position.
Spritesheet is the class that defines spritesheet usage inside the game engine. When a path is specified, the corresponding image is loaded in memory, and partitioned using the x,y,w,h dimensions provided as such:
sprite.x = i * xsprite.y = (i % frames) * ysprite.w = wsprite.h = h
To render the singular frames, just the index is needed (in a incremental order from the
Spritesheet(SDL_Renderer* renderer, const char* n_path, const Vector2D& n_pos, int n_frames, int n_x, int n_y, int n_w, int n_h, SDL_Color n_color) noexcept: creates a new Spritesheet object, using the image atpath, rendering it atpos. The image is subdivided intoframesframes, each having(x,y)position andw*hsize (in the image, not the rendering screen). The color is by default set toC_WHITE~Spritesheet() noexcept: deconstructor, deals with the clean up of memory. Will be called automatically when the program closes.
Then, the following methods are accessible:
Vector2D Spritesheet::get_pos() const: get the Spritesheet object position.- Returns: a
Vector2Dof the position.
- Returns: a
void Spritesheet::set_pos(const Vector2D& n_pos): set the Spritesheet object position using aVector2D.int Spritesheet::get_width() const: get the Spritesheet object width.- Returns: a
intof the width.
- Returns: a
int Spritesheet::get_height() const: get the Spritesheet object height.- Returns: a
intof the width.
- Returns: a
void Spritesheet::set_width(int n_w): set the Spritesheet object width.void Spritesheet::set_height(int n_h): set the Spritesheet object height.SDL_Color Spritesheet::get_color() const: get the Spritesheet object color.- Returns: a
SDL_Color
- Returns: a
void Spritesheet::set_color(SDL_Color color): changes the spritesheet color to the RGBA value ofcolor.
When this texture is rendered, during the copy operation each source color channel is modulated by the appropriate color value according to the following formula:
$src_C = src_C \times (color / 255)$
void Spritesheet::render(int n_frames): renders the image at a given position(pos.x,pos.x)using the indexn_frames
A simple Button UI object, derived from GameObject. Each one can be created as such:
Button(const char* txt, const Vector2D& pos, int width, int height, const Image& image) noexcept: creates a button that will display thetxtat(pos.x,pos.y)with sizewidth*height, using theimageas sprite.~Button() noexcept: deconstructor, deals with the clean up of memory. Will be called automatically when the program closes.
Then, the following methods are accessible:
Vector2D Button::get_pos() const: get the Button object position.- Returns: a
Vector2Dof the position.
- Returns: a
void Button::set_pos(const Vector2D& n_pos): set the Button object position using aVector2Dint Button::get_width() const: get the Button object width.- Returns: a
intof the width.
- Returns: a
int Button::get_height() const: get the Button object height.- Returns: a
intof the height.
- Returns: a
void Button::set_width(int n_w): set the Button object width.void Button::set_height(int n_h): set the Button object height.bool Button::clicked() const: get the Button object state.- Returns:
trueif the button is clicked, elsefalse
- Returns:
void render(TTF_Font* font, int scale = 10, SDL_Color color = C_WHITE) const: render theimageand the text using thefontwith sizescaleand colorcolor.
Vector2D is a class that defines bi-dimensional vectors, and their core functionalities. It uses doubles to represent accurately points on the canvas.
Vector2D(): creates aVector2D(0.0, 0.0)Vector2D(double x, double y): instantiates a newVector2Dobject with(x,y).- Returns:
Vector2D(x, y)
- Returns:
Then, the following data fields are accessible:
double x: X position.double y: Y position.
The following operators:
operator+(const Vector2D&, const Vector2D&): returns the sum of two vectors.- Returns:
Vector2D(x1 + x2, y1 + y2)
- Returns:
operator-(const Vector2D&, const Vector2D&): returns the subtraction of two vectors.- Returns:
Vector2D(x1 + (-x2), y1 + (-y2))
- Returns:
operator*(const Vector2D&, const double): returns the vector multiplied by a scalar.- Returns:
Vector2D(x * double, y * double)
- Returns:
operator==(const Vector2D&, const Vector2D&)- Returns: true if
x1 == x2andy1 == y2else false
- Returns: true if
operator!=(const Vector2D&, const Vector2D&)- Returns: true if
x1 != x2ory1 != y2else false
- Returns: true if
operator<(const Vector2D&, const Vector2D&)- Returns: true if
x1 < x2andy1 < y2else false
- Returns: true if
operator>(const Vector2D&, const Vector2D&)- Returns: true if
x1 > x2andy1 > y2else false
- Returns: true if
operator<=(const Vector2D&, const Vector2D&)- Returns: true if
x1 <= x2andy1 <= y2else false
- Returns: true if
operator>=(const Vector2D&, const Vector2D&)- Returns: true if
x1 >= x2andy1 >= y2else false
- Returns: true if
operator<<(std::ostream&, const Vector2D&)- Returns: a
std::ostream&object to print out.
- Returns: a
And the following methods too:
double Vector2D::magnitude(): returns the magnitude of the vector using the Pythagorean theorem.- Returns:
double
- Returns:
Vector2D Vector2D::to_vect2d(const Vector3D&): transforms a Vector3D object to a Vector2D one.- Returns:
Vector2D
- Returns:
double Vector2D::dot2d(const Vector2D&, const Vector2D&): returns the dot product of two vectors as a scalar.- Returns: scalar as
double
- Returns: scalar as
double Vector2D::angle2d(const Vector2D&, const Vector2D&): returns the angle between two vectors in degrees.- Returns: angle in degrees as
double
- Returns: angle in degrees as
3D graphics and utilities are here.
Vector3D is a class that defines three-dimensional vectors, and their core functionalities. It uses doubles to represent accurately points on the canvas.
Vector3D(): creates aVector3D(0.0, 0.0, 0.0)Vector3D(double x, double y, double z): instantiates a newVector3Dobject with(x,y,z).- Returns:
Vector3D(x, y, z)
- Returns:
Then, the following data fields are accessible:
double x: X position.double y:Y position.double z: Z position.
The following operators:
operator+(const Vector3D&, const Vector3D&): returns the sum of two vectors.- Returns:
Vector3D(x1 + x2, y1 + y2, z1 + z2)
- Returns:
operator-(const Vector3D&, const Vector3D&): returns the subtraction of two vectors.- Returns:
Vector3D(x1 + (-x2), y1 + (-y2), z1 + (-z2))
- Returns:
operator*(const Vector3D&, const double): returns the vector multiplied by a scalar.- Returns:
Vector3D(x * scalar, y * scalar, z * scalar)
- Returns:
operator==(const Vector3D&, const Vector3D&)- Returns: true if
x1 == x2,y1 == y2andz1 == z2else false
- Returns: true if
operator!=(const Vector3D&, const Vector3D&)- Returns: true if
x1 != x2,y1 != y2orz1 != z2else false
- Returns: true if
operator<(const Vector3D&, const Vector3D&)- Returns: true if
x1 < x2,y1 < y2andz1 < z2else false
- Returns: true if
operator>(const Vector3D&, const Vector3D&)- Returns: true if
x1 > x2,y1 > y2andz1 > z2else false
- Returns: true if
operator<=(const Vector3D&, const Vector3D&)- Returns: true if
x1 <= x2,y1 <= y2andz1 <= z2else false
- Returns: true if
operator>=(const Vector3D&, const Vector3D&)- Returns: true if
x1 >= x2,y1 >= y2andz1 >= z2else false
- Returns: true if
operator<<(std::ostream& const Vector3D&)- Returns: a
std::ostream&object to print out.
- Returns: a
And the following methods too:
double Vector3D::magnitude(): returns the magnitude of the vector using the Pythagorean theorem.- Returns:
double
- Returns:
Vector3D Vector3D::to_vect3d(const Vector2D&): transforms a Vector2D object to a Vector3D one.- Returns:
Vector3D
- Returns:
double Vector3D::dot3d(const Vector3D&, const Vector3D&): returns the dot product of two vectors as a scalar.- Returns: scalar as
double
- Returns: scalar as
Vector3D Vector3D::cross3d(const Vector3D&, const Vector3D&): returns the cross product of two vectors as vector.- Returns: cross product as
Vector3D
- Returns: cross product as
double Vector3D::angle3d(const Vector3D&, const Vector3D&): returns the angle between two vectors in degrees.- Returns: angle in degrees as
double
- Returns: angle in degrees as
The functionalities that aren't specific to one field, and multi-purpose, are here. These include:
std::vector<const char*> split(const char* s, size_t max_len): splits an array of characterssinto chunks to fit a set maximum length.- Returns: an
std::vectorofconst char*containing all of the lines split to fit the maximum length.
- Returns: an
void free_chunks(std::vector<const char*>& chunks): sincesplit()usesmalloc(), this function frees the occupied memory.
void cap(Uint64 start, int max, int show = 0, SDL_Renderer* renderer = NULL, TTF_Font* font = NULL): caps the frame-per-second rate at a determinedmax. Inmain(), the variableUint64 startkeeps track of time, then is used to cap the FPS. Ifshow > 0and the rest of the fields are given, a green text displaying the FPS rate is shown in the window.
Uses the ANSI escape code sequence to print the text in various colors.
void sdl_error(const char* s): prints a red underlined error message, followed bySDL_GetError()and stops execution.- Returns:
EXIT_FAILURE
- Returns:
void error(const char* s): prints a red underlined error message and stops execution.- Returns:
EXIT_FAILURE
- Returns:
void warning(const char* s): prints a yellow warning message.void success(const char* s): prints a green message.void message(const char* s): prints a grey message.
Uses SDL_Color to define colors.
Every color is RGBA, with Uint8 C_WHITE.r, Uint8 C_WHITE.g, Uint8 C_WHITE.b and Uint8 C_WHITE.a. Alpha is by default set at 255.
Accessible data fields:
Vector2D position: a vector that holds the mouse X and Y positions updated every frame.Vector2D click: a generic vector that holds the X, Y position in which a mouse button was clicked, updated every frame.Vector2D left_click: a vector that holds the X, Y position in which the left mouse button was clicked, updated every frame.Vector2D right_click: a vector that holds the X, Y position in which the right mouse button was clicked, updated every frame.
Methods:
Vector2D get_position(SDL_MouseMotionEvent& event): gets the mouse X, Y position and stores them in a vector.- Returns: a
Vector2Dwith the mouse's X and Y position.
- Returns: a
Vector2D clicked(SDL_MouseButtonEvent& event): gets the coordinates where a mouse button was pressed, and stores them in a vector.- Returns: a
Vector2Dwith the coordinates.
- Returns: a
Every event that SDL captures is processed here.
void event_handler(SDL_Window* window, bool& gameloop): captures events like mouse or window events to then work with them.
The project and this wiki are updated and maintained by: Unpwnabl