A small namespace rework

This commit is contained in:
Toasterbirb 2022-01-18 13:32:01 +02:00
parent b30de44a05
commit 77db031342
19 changed files with 442 additions and 407 deletions

View File

@ -960,7 +960,7 @@ RECURSIVE = NO
# Note that relative paths are relative to the directory from which doxygen is
# run.
EXCLUDE =
EXCLUDE = ./include/doctest.h
# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
@ -1253,7 +1253,7 @@ HTML_STYLESHEET =
# list). For an example see the documentation.
# This tag requires that the tag GENERATE_HTML is set to YES.
HTML_EXTRA_STYLESHEET =
HTML_EXTRA_STYLESHEET = doxygen-awesome-css/doxygen-awesome.css
# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or
# other source files which should be copied to the HTML output directory. Note

View File

@ -4,12 +4,13 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
namespace Birb2D
namespace Birb
{
struct Audio
{
static bool Init(int flags);
static bool Init(int flags); ///< Initializes the SDL_mixer library
/// Used for short sound effects
class SoundFile
{
public:
@ -25,6 +26,7 @@ namespace Birb2D
std::string filePath;
};
/// Used for longer audio pieces like music. Shouldn't be used for sound effects
class MusicFile
{
public:

View File

@ -5,43 +5,44 @@
#include <SDL2/SDL_ttf.h>
#include "Utils.hpp"
namespace Birb2D
namespace Birb
{
///
/// TextComponent contains details needed to generate a sprite for the Entity in case the Entity is used to display text.
///
struct TextComponent
{
TextComponent();
TextComponent(std::string p_text, TTF_Font* font, SDL_Color* p_color);
std::string text;
TTF_Font* font;
SDL_Color* color;
SDL_Color* color; ///< Surface color of the text
};
/// Entities are objects that contain all of the information required to render stuff
/// Entities can be anything really. For example, it could be text or a picture. This could be extended to animations in the future
struct Entity
{
Entity(); /*!< Creates empty Entity object */
Entity(std::string p_name, Rect p_rect, SDL_Texture* p_texture); /*!< Creates an Entity with a SDL_Texture to render with custom scale */
Entity(std::string p_name, Vector2int pos, TextComponent p_textComponent); /*!< Creates a Text Entity using a TextComponent */
Entity(std::string p_name, Vector2int pos, SDL_Texture* p_texture); /*!< Creates an Entity with a SDL_Texture to render without specifying a scale */
Entity(); ///< Creates empty Entity object
Entity(std::string p_name, Rect p_rect, SDL_Texture* p_texture); ///< Creates an Entity with a SDL_Texture to render with custom scale
Entity(std::string p_name, Vector2int pos, TextComponent p_textComponent); ///< Creates a Text Entity using a TextComponent
Entity(std::string p_name, Vector2int pos, SDL_Texture* p_texture); ///< Creates an Entity with a SDL_Texture to render without specifying a scale
/* Make it possible to update the TextComponent */
void SetText(std::string newText);
void SetFont(TTF_Font* font);
void SetColor(SDL_Color* color);
void SetText(std::string newText); ///< Change the text in TextComponent and reload the sprite
void SetFont(TTF_Font* font); ///< Change the font in TextComponent and reload the sprite
void SetColor(SDL_Color* color); ///< Change the color in TextComponent and reload the sprite
std::string name;
SDL_Texture* sprite;
std::string name; ///< Name of the entity. Used for debugging
SDL_Texture* sprite; ///< Sprite to be rendered
float angle;
Rect rect;
Vector2f localScale;
float angle; ///< Sets the rotation of the entity when rendering it
Rect rect; ///< Sets the position and the dimensions of the entity
Vector2f localScale; ///< Scale modifier for the Entity rendering
TextComponent textComponent; /*!< Having a TextComponent in an Entity enables the rendering of Text */
void LoadSprite();
void ReloadSprite();
void SetBaseEntityValues();
TextComponent textComponent; ///< Having a TextComponent in an Entity enables the rendering of Text
void LoadSprite(); ///< Create a sprite for the Entity using details found in the textComponent variable
void ReloadSprite(); ///< Destroy the old sprite and create a new one. Useful for refreshing text after editing the textComponent variable
void SetBaseEntityValues(); ///< Used to set some default value when they aren't provided during Entity initialization
};
}

View File

@ -3,6 +3,8 @@
#include <iostream>
#include <string.h>
namespace Birb
{
namespace Debug
{
enum Type
@ -12,3 +14,4 @@ namespace Debug
void Log(std::string text, Type type = Type::log);
static std::vector<std::string> lines;
}
}

View File

@ -2,6 +2,8 @@
#include <iostream>
#include <math.h>
namespace Birb
{
struct Vector2f
{
Vector2f()
@ -106,3 +108,4 @@ struct Vector3int
int x, y, z;
};
}

View File

@ -7,7 +7,7 @@
#include "Utils.hpp"
#include "Entity.hpp"
namespace Birb2D
namespace Birb
{
struct Window
{
@ -45,6 +45,7 @@ namespace Birb2D
/* ---------------------- */
};
/// Methods for loading different resources like fonts and textures
struct Resources
{
static SDL_Texture* LoadTexture(std::string p_filePath);
@ -52,6 +53,7 @@ namespace Birb2D
static TTF_Font* LoadFont(std::string p_filePath, int p_fontSize);
};
/// Methods for rendering things
struct Render
{
static void DrawEntity(Entity& entity);

View File

@ -3,8 +3,9 @@
#include <cmath>
#include <string>
namespace Birb2D
namespace Birb
{
/// Class for timing things. Accurate down to milliseconds
class Timer
{
public:
@ -15,12 +16,12 @@ namespace Birb2D
double ElapsedMinutes();
double ElapsedHours();
double CalcSeconds(double mills);
double CalcMinutes(double mills);
double CalcHours(double mills);
double CalcSeconds(double mills); ///< Converts milliseconds into seconds
double CalcMinutes(double mills); ///< Converts milliseconds into minutes
double CalcHours(double mills); ///< Converts milliseconds into hours
std::string DigitalFormat();
std::string SplitDigitalFormat(double previousmills);
std::string DigitalFormat(); ///< Prints the current elapsed time in digital format (00:00:00:000)
std::string SplitDigitalFormat(double previousmills /**< [in] elapsed milliseconds of the previous split */); ///< Used for calculating digital time for a split during timing
bool running = false;
private:

View File

@ -1,16 +1,17 @@
#pragma once
namespace Birb2D
namespace Birb
{
/// Limit the runtime speed and handle delta time
struct TimeStep
{
/* Functions */
void Init();
void Start();
bool Running();
void Step();
void End();
float getTimestepAlpha();
void Init(); ///< Reset the currentTime variable
void Start(); ///< Start a new step. Call this in the beginning of the gameloop
bool Running(); ///< Check if the accumulator is empty yet. While this is true, handle input events etc.
void Step(); ///< Deplete the accumulator. Speed determined by the value of timeStep
void End(); ///< End the timeStep and delay the rendering to stay within the refreshrate
float getTimestepAlpha(); ///< Method to get deltatime
/* Variables */
const float timeStep = 0.01f;

View File

@ -8,6 +8,8 @@
#include <math.h>
namespace Birb
{
struct Rect
{
Rect();
@ -55,15 +57,17 @@ namespace utils
SDL_QueryTexture(texture, NULL, NULL, &x, &y);
}
}
namespace Birb2D
{
struct Texture
{
Texture();
Texture(SDL_Texture* p_sdlTexture, Vector2int p_dimensions);
SDL_Texture* sdlTexture;
Vector2int dimensions;
};
}
//namespace Birb2D
//{
// /// Deprecated way of handling textures. Kept for now, but please don't use this
// struct Texture
// {
// Texture();
// Texture(SDL_Texture* p_sdlTexture, Vector2int p_dimensions);
// SDL_Texture* sdlTexture;
// Vector2int dimensions;
// };
//
//}

View File

@ -3,6 +3,8 @@
#include <SDL2/SDL.h>
namespace Birb
{
namespace Global
{
namespace IsInit
@ -33,3 +35,4 @@ namespace Colors
SDL_Color ChangeColorIntensity(SDL_Color color, int delta);
}
}

View File

@ -2,7 +2,7 @@
#include "Audio.hpp"
#include "Logger.hpp"
namespace Birb2D
namespace Birb
{
//** Initializes the SDL_mixer library. Needs to be called before any other audio features are used
bool Audio::Init(int flags)

View File

@ -2,7 +2,7 @@
#include "Entity.hpp"
#include "Renderwindow.hpp"
namespace Birb2D
namespace Birb
{
TextComponent::TextComponent()
{

View File

@ -3,7 +3,11 @@
#include <fstream>
#include <SDL2/SDL.h>
void Debug::Log(std::string text, Type type)
namespace Birb
{
namespace Debug
{
void Log(std::string text, Type type)
{
std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);
@ -92,3 +96,5 @@ void Debug::Log(std::string text, Type type)
outfile.open("log.txt", std::ios_base::app);
outfile << line;
}
}
}

View File

@ -2,7 +2,7 @@
#include "Values.hpp"
#include "Logger.hpp"
namespace Birb2D
namespace Birb
{
Window::Window(std::string p_title, Vector2int p_window_dimensions, int p_refresh_rate)
:refresh_rate(p_refresh_rate), window_dimensions(p_window_dimensions), win_title(p_title)

View File

@ -14,17 +14,17 @@
TEST_CASE("logging")
{
CHECK_NOTHROW(Debug::Log("Log (this is only a test)"));
CHECK_NOTHROW(Debug::Log("Log (this is only a test)", Debug::log));
CHECK_NOTHROW(Debug::Log("Warning (this is only a test)", Debug::warning));
CHECK_NOTHROW(Debug::Log("Error (this is only a test)", Debug::error));
CHECK_NOTHROW(Birb::Debug::Log("Log (this is only a test)"));
CHECK_NOTHROW(Birb::Debug::Log("Log (this is only a test)", Birb::Debug::log));
CHECK_NOTHROW(Birb::Debug::Log("Warning (this is only a test)", Birb::Debug::warning));
CHECK_NOTHROW(Birb::Debug::Log("Error (this is only a test)", Birb::Debug::error));
}
TEST_CASE("window and rendering functions")
{
Birb2D::Window window("Title", Vector2int(1280, 720), 60);
SDL_Texture* texture = Birb2D::Resources::LoadTexture("/home/toasterbirb/git/birb2d/res/textures/giga_stretch.png");
TTF_Font* font = Birb2D::Resources::LoadFont("/home/toasterbirb/git/birb2d/res/fonts/freefont/FreeMonoBold.ttf", 32);
Birb::Window window("Title", Birb::Vector2int(1280, 720), 60);
SDL_Texture* texture = Birb::Resources::LoadTexture("/home/toasterbirb/git/birb2d/res/textures/giga_stretch.png");
TTF_Font* font = Birb::Resources::LoadFont("/home/toasterbirb/git/birb2d/res/fonts/freefont/FreeMonoBold.ttf", 32);
CHECK(window.win_title == "Title");
CHECK(window.window_dimensions.x == 1280);
@ -45,37 +45,37 @@ TEST_CASE("window and rendering functions")
// ### Math stuff and other utilities ###
TEST_CASE("Default Vector2f")
{
Vector2f vector;
Birb::Vector2f vector;
CHECK(vector.x == 0);
CHECK(vector.y == 0);
}
TEST_CASE("Vector2f with arguments")
{
CHECK(Vector2f(1, 4).x == 1);
CHECK(Vector2f(1, 4).y == 4);
CHECK(Vector2f(1.53f, 5.21f).x == 1.53f);
CHECK(Vector2f(1.53f, 5.21f).y == 5.21f);
CHECK(Birb::Vector2f(1, 4).x == 1);
CHECK(Birb::Vector2f(1, 4).y == 4);
CHECK(Birb::Vector2f(1.53f, 5.21f).x == 1.53f);
CHECK(Birb::Vector2f(1.53f, 5.21f).y == 5.21f);
}
TEST_CASE("Default Vector2int")
{
Vector2int vector;
Birb::Vector2int vector;
CHECK(vector.x == 0);
CHECK(vector.y == 0);
}
TEST_CASE("Vector2int with arguments")
{
CHECK(Vector2int(1, 4).x == 1);
CHECK(Vector2int(1, 4).y == 4);
CHECK(Vector2int(1.53f, 5.21f).x == 2);
CHECK(Vector2int(1.53f, 5.21f).y == 5);
CHECK(Birb::Vector2int(1, 4).x == 1);
CHECK(Birb::Vector2int(1, 4).y == 4);
CHECK(Birb::Vector2int(1.53f, 5.21f).x == 2);
CHECK(Birb::Vector2int(1.53f, 5.21f).y == 5);
}
TEST_CASE("Default Vector3f")
{
Vector3f vector;
Birb::Vector3f vector;
CHECK(vector.x == 0);
CHECK(vector.y == 0);
CHECK(vector.z == 0);
@ -83,17 +83,17 @@ TEST_CASE("Default Vector3f")
TEST_CASE("Vector3f with arguments")
{
CHECK(Vector3f(1, 4, 2).x == 1);
CHECK(Vector3f(1, 4, 2).y == 4);
CHECK(Vector3f(1, 4, 2).z == 2);
CHECK(Vector3f(1.53f, 5.21f, 2.45f).x == 1.53f);
CHECK(Vector3f(1.53f, 5.21f, 2.45f).y == 5.21f);
CHECK(Vector3f(1.53f, 5.21f, 2.45f).z == 2.45f);
CHECK(Birb::Vector3f(1, 4, 2).x == 1);
CHECK(Birb::Vector3f(1, 4, 2).y == 4);
CHECK(Birb::Vector3f(1, 4, 2).z == 2);
CHECK(Birb::Vector3f(1.53f, 5.21f, 2.45f).x == 1.53f);
CHECK(Birb::Vector3f(1.53f, 5.21f, 2.45f).y == 5.21f);
CHECK(Birb::Vector3f(1.53f, 5.21f, 2.45f).z == 2.45f);
}
TEST_CASE("Default Vector3int")
{
Vector3int vector;
Birb::Vector3int vector;
CHECK(vector.x == 0);
CHECK(vector.y == 0);
CHECK(vector.z == 0);
@ -101,17 +101,17 @@ TEST_CASE("Default Vector3int")
TEST_CASE("Vector3int with arguments")
{
CHECK(Vector3int(1, 4, 2).x == 1);
CHECK(Vector3int(1, 4, 2).y == 4);
CHECK(Vector3int(1, 4, 2).z == 2);
CHECK(Vector3int(1.53f, 5.21f, 2.45f).x == 2);
CHECK(Vector3int(1.53f, 5.21f, 2.45f).y == 5);
CHECK(Vector3int(1.53f, 5.21f, 2.45f).z == 2);
CHECK(Birb::Vector3int(1, 4, 2).x == 1);
CHECK(Birb::Vector3int(1, 4, 2).y == 4);
CHECK(Birb::Vector3int(1, 4, 2).z == 2);
CHECK(Birb::Vector3int(1.53f, 5.21f, 2.45f).x == 2);
CHECK(Birb::Vector3int(1.53f, 5.21f, 2.45f).y == 5);
CHECK(Birb::Vector3int(1.53f, 5.21f, 2.45f).z == 2);
}
TEST_CASE("Default Rect")
{
Rect defaultRect;
Birb::Rect defaultRect;
CHECK(defaultRect.x == 0);
CHECK(defaultRect.y == 0);
CHECK(defaultRect.w == 0);
@ -121,7 +121,7 @@ TEST_CASE("Default Rect")
TEST_CASE("Rect with arguments")
{
Rect customRect(10.54, 20, 30.234, 40.6668);
Birb::Rect customRect(10.54, 20, 30.234, 40.6668);
CHECK(customRect.x == 10.54f);
CHECK(customRect.y == 20);
CHECK(customRect.w == 30.234f);
@ -130,8 +130,8 @@ TEST_CASE("Rect with arguments")
TEST_CASE("Rect with rounded values (integer)")
{
Rect customRect(10.54, 20, 30.234, 40.6668);
Rect roundedRect = customRect.getInt();
Birb::Rect customRect(10.54, 20, 30.234, 40.6668);
Birb::Rect roundedRect = customRect.getInt();
CHECK(roundedRect.x == 11);
CHECK(roundedRect.y == 20);

View File

@ -1,6 +1,6 @@
#include "Timer.hpp"
namespace Birb2D
namespace Birb
{
void Timer::Start()
{

View File

@ -3,7 +3,7 @@
#include "Utils.hpp"
#include "Values.hpp"
namespace Birb2D
namespace Birb
{
void TimeStep::Init()
{

View File

@ -1,5 +1,7 @@
#include "Utils.hpp"
namespace Birb
{
Rect::Rect()
:x(0.0f), y(0.0f), w(0.0f), h(0.0f)
{}
@ -28,13 +30,14 @@ SDL_Rect Rect::getSDLRect()
sdlrect.y = y;
return sdlrect;
}
Birb2D::Texture::Texture()
{
sdlTexture = NULL;
dimensions = Vector2int(-1, -1);
}
Birb2D::Texture::Texture(SDL_Texture* p_sdlTexture, Vector2int p_dimensions)
:sdlTexture(p_sdlTexture), dimensions(p_dimensions)
{}
//Birb2D::Texture::Texture()
//{
// sdlTexture = NULL;
// dimensions = Vector2int(-1, -1);
//}
//
//Birb2D::Texture::Texture(SDL_Texture* p_sdlTexture, Vector2int p_dimensions)
//:sdlTexture(p_sdlTexture), dimensions(p_dimensions)
//{}

View File

@ -1,8 +1,12 @@
#include <math.h>
#include "Values.hpp"
namespace Birb
{
/* Dims or lightens color */
SDL_Color Colors::ChangeColorIntensity(SDL_Color color, int delta)
namespace Colors
{
SDL_Color ChangeColorIntensity(SDL_Color color, int delta)
{
SDL_Color newColor = color;
@ -53,3 +57,5 @@ SDL_Color Colors::ChangeColorIntensity(SDL_Color color, int delta)
return newColor;
}
}
}