Skip to content

Commit f295c16

Browse files
authored
Merge pull request #1 from RobLoach/add_text_class
Add text_font_filters example
2 parents 4c063ab + 5f63d73 commit f295c16

File tree

6 files changed

+173
-78
lines changed

6 files changed

+173
-78
lines changed

clib.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"include/RenderTexture.hpp",
5252
"include/Shader.hpp",
5353
"include/Sound.hpp",
54+
"include/Text.hpp",
5455
"include/Texture.hpp",
5556
"include/Vector2.hpp",
5657
"include/Vector3.hpp",

examples/text/resources/KAISG.ttf

78 KB
Binary file not shown.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [text] example - Font filters
4+
*
5+
* After font loading, font texture atlas filter could be configured for a softer
6+
* display of the font when scaling it to different sizes, that way, it's not required
7+
* to generate multiple fonts at multiple sizes (as long as the scaling is not very different)
8+
*
9+
* This example has been created using raylib 1.3.0 (www.raylib.com)
10+
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
11+
*
12+
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
13+
*
14+
********************************************************************************************/
15+
16+
#include "raylib-cpp.hpp"
17+
18+
int main(void)
19+
{
20+
// Initialization
21+
//--------------------------------------------------------------------------------------
22+
const int screenWidth = 800;
23+
const int screenHeight = 450;
24+
25+
raylib::Window window(screenWidth, screenHeight, "raylib [text] example - font filters");
26+
27+
// TTF Font loading with custom generation parameters
28+
raylib::Font font("resources/KAISG.ttf", 96);
29+
30+
// Generate mipmap levels to use trilinear filtering
31+
// NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR
32+
GenTextureMipmaps(&font.texture);
33+
34+
raylib::Text msg("Loaded Font", font.GetBaseSize(), BLACK, font);
35+
36+
Vector2 fontPosition = { 40.0f, screenHeight/2.0f - 80.0f };
37+
Vector2 textSize = { 0.0f, 0.0f };
38+
39+
// Setup texture scaling filter
40+
SetTextureFilter(font.texture, TEXTURE_FILTER_POINT);
41+
int currentFontFilter = 0; // TEXTURE_FILTER_POINT
42+
43+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
44+
//--------------------------------------------------------------------------------------
45+
46+
// Main game loop
47+
while (!WindowShouldClose()) // Detect window close button or ESC key
48+
{
49+
// Update
50+
//----------------------------------------------------------------------------------
51+
msg.fontSize += GetMouseWheelMove() * 4.0f;
52+
53+
// Choose font texture filter method
54+
if (IsKeyPressed(KEY_ONE))
55+
{
56+
SetTextureFilter(font.texture, TEXTURE_FILTER_POINT);
57+
currentFontFilter = 0;
58+
}
59+
else if (IsKeyPressed(KEY_TWO))
60+
{
61+
SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);
62+
currentFontFilter = 1;
63+
}
64+
else if (IsKeyPressed(KEY_THREE))
65+
{
66+
// NOTE: Trilinear filter won't be noticed on 2D drawing
67+
SetTextureFilter(font.texture, TEXTURE_FILTER_TRILINEAR);
68+
currentFontFilter = 2;
69+
}
70+
71+
textSize = msg.MeasureEx();
72+
73+
if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10;
74+
else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10;
75+
76+
// Load a dropped TTF file dynamically (at current fontSize)
77+
for (const auto& file : raylib::GetDroppedFiles()) {
78+
if (raylib::IsFileExtension(file, ".ttf")) {
79+
msg.font = font = raylib::Font(file, font.GetBaseSize());
80+
}
81+
}
82+
//----------------------------------------------------------------------------------
83+
84+
// Draw
85+
//----------------------------------------------------------------------------------
86+
BeginDrawing();
87+
88+
ClearBackground(RAYWHITE);
89+
90+
DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY);
91+
DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY);
92+
DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY);
93+
DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY);
94+
95+
msg.Draw(fontPosition);
96+
97+
// TODO: It seems texSize measurement is not accurate due to chars offsets...
98+
//DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED);
99+
100+
DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY);
101+
DrawText(TextFormat("Font size: %02.02f", msg.GetFontSize()), 20, screenHeight - 50, 10, DARKGRAY);
102+
DrawText(TextFormat("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY);
103+
DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY);
104+
105+
if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK);
106+
else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK);
107+
else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK);
108+
109+
EndDrawing();
110+
//----------------------------------------------------------------------------------
111+
}
112+
113+
return 0;
114+
}

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ install(FILES
3535
RenderTexture.hpp
3636
Shader.hpp
3737
Sound.hpp
38+
Text.hpp
3839
Texture.hpp
3940
Vector2.hpp
4041
Vector3.hpp

include/Font.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Font : public ::Font {
5252
*
5353
* @see ::LoadFontEx
5454
*/
55-
Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
55+
Font(const std::string& fileName, int fontSize, int* fontChars = 0, int charCount = 0) {
5656
if (!Load(fileName, fontSize, fontChars, charCount)) {
5757
throw RaylibException("Failed to load font from font with extras");
5858
}
@@ -116,6 +116,7 @@ class Font : public ::Font {
116116
GETTERSETTER(::GlyphInfo*, Glyphs, glyphs)
117117

118118
Font& operator=(const ::Font& font) {
119+
Unload();
119120
set(font);
120121
return *this;
121122
}

include/Text.hpp

Lines changed: 55 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,104 +15,84 @@ class Text {
1515
public:
1616
/**
1717
* Initializes a new Text object.
18+
*
1819
* @param text Text to initialize.
19-
* @return void.
20+
* @param fontSize The size of the text.
21+
* @param color The color of the font.
22+
* @param font Font to initialize.
23+
* @param spacing The spacing of the text.
2024
*/
21-
Text(const std::string& text = "")
22-
{
23-
SetText(text);
25+
Text(const std::string& text = "", float fontSize = 10, const ::Color& color = WHITE, const ::Font& font = ::GetFontDefault(), float spacing = 0) : text(text), fontSize(fontSize), color(color), font(font), spacing(spacing) {
26+
// Nothing.
2427
}
25-
28+
2629
/**
27-
* Initializes a new Text object.
30+
* Initializes a new Text object with a custom font.
31+
*
32+
* @param font Font to initialize.
2833
* @param text Text to initialize.
29-
* @param posX X position of the text.
30-
* @param posY Y position of the text.
31-
* @return void.
34+
* @param fontSize The size of the text.
35+
* @param spacing The spacing of the text.
36+
* @param color The color of the font.
3237
*/
33-
Text(const std::string& text, const float posX, const float posY)
34-
{
35-
_text = text;
36-
_position.x = posX;
37-
_position.y = posY;
38+
Text(const ::Font& font, const std::string& text = "", float fontSize = 10, float spacing = 0, const ::Color& color = WHITE) : font(font), text(text), fontSize(fontSize), spacing(spacing), color(color) {
39+
// Nothing.
3840
}
3941

42+
GETTERSETTER(std::string, Text, text)
43+
GETTERSETTER(float, FontSize, fontSize)
44+
GETTERSETTER(::Font, Font, font)
45+
GETTERSETTER(::Color, Color, color)
46+
GETTERSETTER(float, Spacing, spacing)
47+
4048
/**
41-
* Initializes a new Text object.
42-
* @param text Text to initialize.
43-
* @param posX X position of the text.
44-
* @param posY Y position of the text.
45-
* @return void.
49+
* Draw text with values in class.
4650
*/
47-
Text(const std::string& text, const ::Vector2& position)
48-
{
49-
_text = text;
50-
_position = position;
51+
inline void Draw(const ::Vector2& position) {
52+
::DrawTextEx(font, text.c_str(), position, fontSize, spacing, color);
5153
}
5254

5355
/**
54-
* Initializes a new Text object.
55-
* @param font Font to initialize.
56-
* @param text Text to initialize.
57-
* @param posX X position of the text.
58-
* @param posY Y position of the text.
59-
* @return void.
56+
* Draw text with values in class.
6057
*/
61-
Text(const ::Font& font, const std::string& text, const ::Vector2& position, const float fontSize, const float spacing, const ::Color& color)
62-
{
63-
_font = font;
64-
_text = text;
65-
_position = position;
66-
_fontSize = fontSize;
67-
_spacing = spacing;
68-
_color = color;
58+
inline void Draw(int posX, int posY) {
59+
::DrawTextEx(font, text.c_str(), {static_cast<float>(posX), static_cast<float>(posY)}, fontSize, spacing, color);
6960
}
70-
7161

72-
~Text() {
62+
/**
63+
* Draw text using Font and pro parameters (rotation).
64+
*
65+
* @see DrawTextPro()
66+
*/
67+
inline void Draw(const ::Vector2& position, float rotation, const Vector2& origin = {0, 0}) {
68+
::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, color);
7369
}
7470

7571
/**
76-
* @brief Draw text with values in class.
77-
*
72+
* Measure string width for default font
7873
*/
79-
void Draw() {
80-
::DrawTextEx(_font, _text.c_str(), _position, _fontSize, _spacing, _color);
74+
inline int Measure() {
75+
return ::MeasureText(text.c_str(), static_cast<int>(fontSize));
8176
}
8277

8378
/**
84-
* @brief Draw text with values in class.
85-
*
79+
* Measure string size for Font
8680
*/
87-
void Draw(const ::Vector2& position) {
88-
::DrawTextEx(_font, _text.c_str(), position, _fontSize, _spacing, _color);
81+
inline Vector2 MeasureEx() {
82+
return ::MeasureTextEx(font, text.c_str(), fontSize, spacing);
8983
}
90-
91-
std::string GetText() const { return _text; }
92-
void SetText(const std::string& text) { _text = text; }
93-
94-
float GetFontSize() const { return _fontSize; }
95-
void SetFontSize(float fontSize) { _fontSize = fontSize; }
96-
97-
//Font GetFont() const { return _font; }
98-
void SetFont(const ::Font& font) { _font = font; }
99-
100-
Color GetColor() const { return _color; }
101-
void SetColor(const ::Color& color) { _color = color; }
102-
103-
float GetPosX() const { return _position.GetX(); }
104-
void SetPosX(float posX) { _position.SetX(posX); }
105-
106-
float GetPosY() const { return _position.GetY(); }
107-
void SetPosY(float posY) { _position.SetY(posY); }
10884

109-
Vector2 GetPosition() const { return _position; }
110-
void SetPosition(const ::Vector2& position) { _position = position; }
85+
Text& operator=(const Text& other) {
86+
if (this == &other) {
87+
return *this;
88+
}
11189

112-
float GetSpacing() const { return _spacing; }
113-
void SetSpacing(float spacing) { _spacing = spacing; }
90+
text = other.text;
91+
fontSize = other.fontSize;
92+
color = other.color;
93+
font = other.font;
94+
spacing = other.spacing;
11495

115-
Text& operator=(const Text& text) {
11696
return *this;
11797
}
11898

@@ -152,13 +132,11 @@ class Text {
152132
::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, color);
153133
}
154134

155-
private:
156-
std::string _text = "";
157-
float _fontSize = 10;
158-
Font _font = ::GetFontDefault();
159-
Color _color = WHITE;
160-
Vector2 _position = {0.0, 0.0};
161-
float _spacing = 0;
135+
std::string text;
136+
float fontSize;
137+
::Color color;
138+
::Font font;
139+
float spacing;
162140
};
163141
} // namespace raylib
164142

0 commit comments

Comments
 (0)