Skip to content

Commit

Permalink
Merge pull request #295 from RobLoach/strings
Browse files Browse the repository at this point in the history
Overload some of the std::string to const char* functions
  • Loading branch information
RobLoach authored Feb 26, 2024
2 parents 1b550e0 + 61a3cee commit f790a20
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
20 changes: 20 additions & 0 deletions include/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,35 @@ class Color : public ::Color {
::DrawLineStrip(points, numPoints, *this);
}

void DrawText(const char* text, int posX = 0, int posY = 0, int fontSize = 10.0f) const {
::DrawText(text, posX, posY, fontSize, *this);
}

void DrawText(const std::string& text, int posX = 0, int posY = 0, int fontSize = 10.0f) const {
::DrawText(text.c_str(), posX, posY, fontSize, *this);
}

void DrawText(const ::Font& font, const char* text, ::Vector2 position,
float fontSize, float spacing) const {
::DrawTextEx(font, text, position, fontSize, spacing, *this);
}

void DrawText(const ::Font& font, const std::string& text, ::Vector2 position,
float fontSize, float spacing) const {
::DrawTextEx(font, text.c_str(), position, fontSize, spacing, *this);
}

void DrawText(
const ::Font& font,
const char* text,
::Vector2 position,
::Vector2 origin,
float rotation,
float fontSize,
float spacing) const {
::DrawTextPro(font, text, position, origin, rotation, fontSize, spacing, *this);
}

void DrawText(
const ::Font& font,
const std::string& text,
Expand Down
47 changes: 47 additions & 0 deletions include/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ class Font : public ::Font {
return ::IsFontReady(*this);
}

/**
* Draw text using font and additional parameters.
*/
void DrawText(const char* text, ::Vector2 position, float fontSize,
float spacing, ::Color tint = WHITE) const {
::DrawTextEx(*this, text, position, fontSize, spacing, tint);
}

/**
* Draw text using font and additional parameters.
*/
Expand All @@ -218,6 +226,16 @@ class Font : public ::Font {
::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
}

/**
* Draw text using font and additional parameters.
*/
void DrawText(const char* text, int posX, int posY, float fontSize,
float spacing, ::Color tint = WHITE) const {
::DrawTextEx(*this, text,
{ static_cast<float>(posX), static_cast<float>(posY) },
fontSize, spacing, tint);
}

/**
* Draw text using font and additional parameters.
*/
Expand All @@ -228,6 +246,20 @@ class Font : public ::Font {
fontSize, spacing, tint);
}

void DrawText(
const char* text,
::Vector2 position,
::Vector2 origin,
float rotation,
float fontSize,
float spacing,
::Color tint = WHITE) const {
::DrawTextPro(*this, text,
position, origin,
rotation, fontSize,
spacing, tint);
}

void DrawText(
const std::string& text,
::Vector2 position,
Expand Down Expand Up @@ -265,6 +297,13 @@ class Font : public ::Font {
spacing, tint);
}

/**
* Measure string size for Font
*/
Vector2 MeasureText(const char* text, float fontSize, float spacing) const {
return ::MeasureTextEx(*this, text, fontSize, spacing);
}

/**
* Measure string size for Font
*/
Expand All @@ -279,6 +318,14 @@ class Font : public ::Font {
return ::GetGlyphIndex(*this, character);
}

/**
* Create an image from text (custom sprite font)
*/
::Image ImageText(const char* text, float fontSize,
float spacing, ::Color tint) const {
return ::ImageTextEx(*this, text, fontSize, spacing, tint);
}

/**
* Create an image from text (custom sprite font)
*/
Expand Down
50 changes: 47 additions & 3 deletions include/Functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,41 @@ namespace raylib {
/**
* Draw text (using default font)
*/
[[maybe_unused]] RLCPPAPI inline void DrawText(const std::string& text, int posX, int posY, int fontSize, ::Color color) {
[[maybe_unused]] RLCPPAPI inline void DrawText(const char* text, int posX, int posY, int fontSize, ::Color color) {
::DrawText(text, posX, posY, fontSize, color);
}

/**
* Draw text (using default font)
*/
[[maybe_unused]] RLCPPAPI inline void DrawText(const std::string& text, int posX, int posY, int fontSize, ::Color color) {
::DrawText(text.c_str(), posX, posY, fontSize, color);
}

/**
* Draw text using font and additional parameters
*/
[[maybe_unused]] RLCPPAPI inline void DrawTextEx(const Font& font, const std::string& text, Vector2 position,
[[maybe_unused]] RLCPPAPI inline void DrawTextEx(const Font& font, char* text, Vector2 position,
float fontSize, float spacing, ::Color tint) {
::DrawTextEx(font, text, position, fontSize, spacing, tint);
}

/**
* Draw text using font and additional parameters
*/
[[maybe_unused]] RLCPPAPI inline void DrawTextEx(const Font& font, const std::string& text, Vector2 position,
float fontSize, float spacing, ::Color tint) {
::DrawTextEx(font, text.c_str(), position, fontSize, spacing, tint);
}

/**
* Draw text using Font and pro parameters (rotation)
*/
[[maybe_unused]] RLCPPAPI inline void DrawTextPro(const Font& font, const char* text, Vector2 position,
Vector2 origin, float rotation, float fontSize, float spacing, ::Color tint) {
::DrawTextPro(font, text, position, origin, rotation, fontSize, spacing, tint);
}

/**
* Draw text using Font and pro parameters (rotation)
*/
Expand All @@ -277,17 +300,38 @@ namespace raylib {
/**
* Measure string width for default font
*/
[[maybe_unused]] RLCPPAPI inline int MeasureText(const std::string& text, int fontSize) {
[[maybe_unused]] RLCPPAPI inline int MeasureText(const char* text, int fontSize) {
return ::MeasureText(text, fontSize);
}

/**
* Measure string width for default font
*/
[[maybe_unused]] RLCPPAPI inline int MeasureText(const std::string& text, int fontSize) {
return ::MeasureText(text.c_str(), fontSize);
}

/**
* Check if two text string are equal
*/
[[maybe_unused]] RLCPPAPI inline bool TextIsEqual(const char* text1, const char* text2) {
return ::TextIsEqual(text1, text2);
}

/**
* Check if two text string are equal
*/
[[maybe_unused]] RLCPPAPI inline bool TextIsEqual(const std::string& text1, const std::string& text2) {
return ::TextIsEqual(text1.c_str(), text2.c_str());
}

/**
* Check if two text string are equal
*/
[[maybe_unused]] RLCPPAPI inline unsigned int TextLength(const char* text) {
return ::TextLength(text);
}

/**
* Check if two text string are equal
*/
Expand Down
20 changes: 20 additions & 0 deletions include/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,16 @@ class Image : public ::Image {
::ImageDraw(this, src, srcRec, dstRec, tint);
}

void DrawText(const char* text, ::Vector2 position, int fontSize,
::Color color = {255, 255, 255, 255}) {
::ImageDrawText(this,
text,
static_cast<int>(position.x),
static_cast<int>(position.y),
fontSize,
color);
}

void DrawText(const std::string& text, ::Vector2 position, int fontSize,
::Color color = {255, 255, 255, 255}) {
::ImageDrawText(this,
Expand All @@ -660,11 +670,21 @@ class Image : public ::Image {
::ImageDrawText(this, text.c_str(), x, y, fontSize, color);
}

void DrawText(const char* text, int x, int y, int fontSize,
::Color color = {255, 255, 255, 255}) {
::ImageDrawText(this, text, x, y, fontSize, color);
}

void DrawText(const ::Font& font, const std::string& text, ::Vector2 position,
float fontSize, float spacing, ::Color tint = {255, 255, 255, 255}) {
::ImageDrawTextEx(this, font, text.c_str(), position, fontSize, spacing, tint);
}

void DrawText(const ::Font& font, const char* text, ::Vector2 position,
float fontSize, float spacing, ::Color tint = {255, 255, 255, 255}) {
::ImageDrawTextEx(this, font, text, position, fontSize, spacing, tint);
}

/**
* Load color data from image as a Color array (RGBA - 32bit)
*/
Expand Down

0 comments on commit f790a20

Please sign in to comment.