Utility Functions

Discover some useful functions

Utility Functions

The Engines has a set of Constants and Utility functions you can access by including the Utility header (see below).

#include <Nero/utility/Utility.h>

Among those functions you have :

  • Some functions to check if a modifier key (Ctrl, Alt, Shift) is pressed
  • Some functions to get the current date and time
  • Some functions to convert between SFML and Box2D items like colors, vectors etc.
  • Some math and geometry functions (compute a distance, check if a polygon is convex or not etc.)
  • Some string functions (split a string, convert to string etc.)
  • Some  functions to convert a coordinate from Game Screen to Game World.

List of Functions

Here is a list of the constants and functions inside the Utility header.

//Some useful constants
const sf::Time  TIME_PER_FRAME              = sf::seconds(1.f/60.f);
const sf::Time  AUTO_SAVE_TIME              = sf::seconds(30.f);
const float SCALE                           = 50.f;
const float GRAVITY                         = 9.8f;
const int RAND_LIMIT	                    = 32767;
const int SPRITE_LAYER_MAX                  = 1000;
const float PI                              = 3.141592653589793238462643383f;
const int MAX_VALUE                         = 2147483647;
const sf::Color STATIC_MESH_COLOR           = b2_to_sf(b2Color(0.5f, 0.9f, 0.5f)); // sf::Color(0, 255, 0);
const sf::Color DYNAMIC_MESH_COLOR          = b2_to_sf(b2Color(0.9f, 0.7f, 0.7f)); //sf::Color(219, 112, 147);
const sf::Color KINEMATIC_MESH_COLOR        = b2_to_sf(b2Color(0.5f, 0.5f, 0.9f)); //sf::Color(0, 0, 255);
const sf::Color SELECTED_MESH_COLOR         = sf::Color(255, 140, 0);
const sf::Color INVALIDE_MESH_COLOR         = sf::Color(255, 0, 0);
const sf::String NERO_FOLDER                = "NERO";
//Check if a Modifier key is pressed, You have all combinations
bool                CTRL();
bool                SHIFT();
bool                ALT();
bool                CTRL_SHIFT();
bool                CTRL_ALT();
bool                SHIFT_ALT();
bool                CTRL_SHIFT_ALT();
std::string         getdate(std::string delim = "/");
std::string         getTime(std::string delim = ":");
sf::Vector2f        b2_to_sf(b2Vec2 vect, float scale = 1.f);
sf::Vector2f        b2_to_sf(float x, float y, float scale = 1.f);
b2Color             sf_to_b2(const sf::Color& color);
b2Vec2              sf_to_b2(const sf::Vector2f& vect, float scale = 1.f);
b2Vec2              sf_to_b2(float x, float y, float scale = 1.f);
sf::Color           transparent_color(sf::Color color, int transparency = 255);
template 
std::string         toString(T const& value);
std::string         toString(sf::Keyboard::Key key);    //enumerator to string
std::vector &splitString(const std::string &s, char delim, std::vector &elems);
std::vector splitString(const std::string &s, char delim);
std::string         removeFileExtension(const std::string& filename);
std::string         replaceFileExtension(const std::string& filename, const std::string& extension);
float               toDegree(float radian);
float               toRadian(float degree);
int                 randomInt(int exclusiveMax);
float               randomFloat();                        // Random number in range [-1,1]
float               randomFloat(float lo, float hi);      // Random floating point number in range [lo, hi]
float               vectLength(sf::Vector2f vect);
float               vectLength(b2Vec2 vect);
sf::Vector2f        unitVector(sf::Vector2f vect);
float               distance(sf::Vector2f vect1, sf::Vector2f vect2);
float               distance(b2Vec2 vect1, b2Vec2 vect2);
float               distance(sf::Vector2f line_vect1, sf::Vector2f line_vect2, sf::Vector2f vect3);
float               dot_product(const sf::Vector2f& vect1, const sf::Vector2f& vect2);
std::string         loadText(std::string filename);
void                centerOrigin(sf::Sprite& sprite);
void                centerOrigin(sf::Text& text);
bool                isConvexPolygon(std::vector polygon);
bool                isConvex(std::vector polygon);
std::vector   concave_to_convex(PointTab vectorTab);
float                   det(float x1, float y1, float x2, float y2, float x3, float y3);
sf::Vector2f*           hitRay(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
bool                    isOnSegment(float px, float py, float x1, float y1, float x2, float y2);
bool                    isOnLine(float px, float py, float x1, float y1, float x2, float y2);
void                    err();
bool                    pointsMatch(float x1, float y1, float x2, float y2);
int                     validate_polygon(const PointTab &verticesVec);
sf::Vector2f*           hitSegment(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
sf::Vector2f        getPolygonCenter(PointTab vertices);
sf::Vector2f        getLineCenter(const sf::Vector2f& a, const sf::Vector2f& b);
sf::Vector2f        rotateVertex(sf::Vector2f c, float angle, sf::Vector2f p);
sf::IntRect         getViewport(const sfg::Canvas::Ptr& renderWindow);
sf::Vector2f        canvas_to_world(const sf::Vector2f& point, const sfg::Canvas::Ptr& renderWindow);
sf::Vector2f        world_to_canvas(const sf::Vector2f& point, const sfg::Canvas::Ptr& renderWindow);
struct Point
{
    float x;
    float y;
};