diff --git a/clib.json b/clib.json index d2a5e67e..2e641342 100644 --- a/clib.json +++ b/clib.json @@ -51,6 +51,7 @@ "include/RenderTexture.hpp", "include/Shader.hpp", "include/Sound.hpp", + "include/Text.hpp", "include/Texture.hpp", "include/Vector2.hpp", "include/Vector3.hpp", diff --git a/examples/text/resources/KAISG.ttf b/examples/text/resources/KAISG.ttf new file mode 100644 index 00000000..04478b25 Binary files /dev/null and b/examples/text/resources/KAISG.ttf differ diff --git a/examples/text/text_font_filters.cpp b/examples/text/text_font_filters.cpp new file mode 100644 index 00000000..f7586d9b --- /dev/null +++ b/examples/text/text_font_filters.cpp @@ -0,0 +1,114 @@ +/******************************************************************************************* +* +* raylib [text] example - Font filters +* +* After font loading, font texture atlas filter could be configured for a softer +* display of the font when scaling it to different sizes, that way, it's not required +* to generate multiple fonts at multiple sizes (as long as the scaling is not very different) +* +* This example has been created using raylib 1.3.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib-cpp.hpp" + +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + raylib::Window window(screenWidth, screenHeight, "raylib [text] example - font filters"); + + // TTF Font loading with custom generation parameters + raylib::Font font("resources/KAISG.ttf", 96); + + // Generate mipmap levels to use trilinear filtering + // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR + GenTextureMipmaps(&font.texture); + + raylib::Text msg("Loaded Font", font.GetBaseSize(), BLACK, font); + + Vector2 fontPosition = { 40.0f, screenHeight/2.0f - 80.0f }; + Vector2 textSize = { 0.0f, 0.0f }; + + // Setup texture scaling filter + SetTextureFilter(font.texture, TEXTURE_FILTER_POINT); + int currentFontFilter = 0; // TEXTURE_FILTER_POINT + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + msg.fontSize += GetMouseWheelMove() * 4.0f; + + // Choose font texture filter method + if (IsKeyPressed(KEY_ONE)) + { + SetTextureFilter(font.texture, TEXTURE_FILTER_POINT); + currentFontFilter = 0; + } + else if (IsKeyPressed(KEY_TWO)) + { + SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR); + currentFontFilter = 1; + } + else if (IsKeyPressed(KEY_THREE)) + { + // NOTE: Trilinear filter won't be noticed on 2D drawing + SetTextureFilter(font.texture, TEXTURE_FILTER_TRILINEAR); + currentFontFilter = 2; + } + + textSize = msg.MeasureEx(); + + if (IsKeyDown(KEY_LEFT)) fontPosition.x -= 10; + else if (IsKeyDown(KEY_RIGHT)) fontPosition.x += 10; + + // Load a dropped TTF file dynamically (at current fontSize) + for (const auto& file : raylib::GetDroppedFiles()) { + if (raylib::IsFileExtension(file, ".ttf")) { + msg.font = font = raylib::Font(file, font.GetBaseSize()); + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("Use mouse wheel to change font size", 20, 20, 10, GRAY); + DrawText("Use KEY_RIGHT and KEY_LEFT to move text", 20, 40, 10, GRAY); + DrawText("Use 1, 2, 3 to change texture filter", 20, 60, 10, GRAY); + DrawText("Drop a new TTF font for dynamic loading", 20, 80, 10, DARKGRAY); + + msg.Draw(fontPosition); + + // TODO: It seems texSize measurement is not accurate due to chars offsets... + //DrawRectangleLines(fontPosition.x, fontPosition.y, textSize.x, textSize.y, RED); + + DrawRectangle(0, screenHeight - 80, screenWidth, 80, LIGHTGRAY); + DrawText(TextFormat("Font size: %02.02f", msg.GetFontSize()), 20, screenHeight - 50, 10, DARKGRAY); + DrawText(TextFormat("Text size: [%02.02f, %02.02f]", textSize.x, textSize.y), 20, screenHeight - 30, 10, DARKGRAY); + DrawText("CURRENT TEXTURE FILTER:", 250, 400, 20, GRAY); + + if (currentFontFilter == 0) DrawText("POINT", 570, 400, 20, BLACK); + else if (currentFontFilter == 1) DrawText("BILINEAR", 570, 400, 20, BLACK); + else if (currentFontFilter == 2) DrawText("TRILINEAR", 570, 400, 20, BLACK); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + return 0; +} diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 7677c507..e0cf7675 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -35,6 +35,7 @@ install(FILES RenderTexture.hpp Shader.hpp Sound.hpp + Text.hpp Texture.hpp Vector2.hpp Vector3.hpp diff --git a/include/Font.hpp b/include/Font.hpp index e755a3c8..21170c1e 100644 --- a/include/Font.hpp +++ b/include/Font.hpp @@ -52,7 +52,7 @@ class Font : public ::Font { * * @see ::LoadFontEx */ - Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) { + Font(const std::string& fileName, int fontSize, int* fontChars = 0, int charCount = 0) { if (!Load(fileName, fontSize, fontChars, charCount)) { throw RaylibException("Failed to load font from font with extras"); } @@ -116,6 +116,7 @@ class Font : public ::Font { GETTERSETTER(::GlyphInfo*, Glyphs, glyphs) Font& operator=(const ::Font& font) { + Unload(); set(font); return *this; } diff --git a/include/Text.hpp b/include/Text.hpp index 45832055..0d14e468 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -15,104 +15,84 @@ class Text { public: /** * Initializes a new Text object. + * * @param text Text to initialize. - * @return void. + * @param fontSize The size of the text. + * @param color The color of the font. + * @param font Font to initialize. + * @param spacing The spacing of the text. */ - Text(const std::string& text = "") - { - SetText(text); + 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) { + // Nothing. } - + /** - * Initializes a new Text object. + * Initializes a new Text object with a custom font. + * + * @param font Font to initialize. * @param text Text to initialize. - * @param posX X position of the text. - * @param posY Y position of the text. - * @return void. + * @param fontSize The size of the text. + * @param spacing The spacing of the text. + * @param color The color of the font. */ - Text(const std::string& text, const float posX, const float posY) - { - _text = text; - _position.x = posX; - _position.y = posY; + 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) { + // Nothing. } + GETTERSETTER(std::string, Text, text) + GETTERSETTER(float, FontSize, fontSize) + GETTERSETTER(::Font, Font, font) + GETTERSETTER(::Color, Color, color) + GETTERSETTER(float, Spacing, spacing) + /** - * Initializes a new Text object. - * @param text Text to initialize. - * @param posX X position of the text. - * @param posY Y position of the text. - * @return void. + * Draw text with values in class. */ - Text(const std::string& text, const ::Vector2& position) - { - _text = text; - _position = position; + inline void Draw(const ::Vector2& position) { + ::DrawTextEx(font, text.c_str(), position, fontSize, spacing, color); } /** - * Initializes a new Text object. - * @param font Font to initialize. - * @param text Text to initialize. - * @param posX X position of the text. - * @param posY Y position of the text. - * @return void. + * Draw text with values in class. */ - Text(const ::Font& font, const std::string& text, const ::Vector2& position, const float fontSize, const float spacing, const ::Color& color) - { - _font = font; - _text = text; - _position = position; - _fontSize = fontSize; - _spacing = spacing; - _color = color; + inline void Draw(int posX, int posY) { + ::DrawTextEx(font, text.c_str(), {static_cast(posX), static_cast(posY)}, fontSize, spacing, color); } - - ~Text() { + /** + * Draw text using Font and pro parameters (rotation). + * + * @see DrawTextPro() + */ + inline void Draw(const ::Vector2& position, float rotation, const Vector2& origin = {0, 0}) { + ::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, color); } /** - * @brief Draw text with values in class. - * + * Measure string width for default font */ - void Draw() { - ::DrawTextEx(_font, _text.c_str(), _position, _fontSize, _spacing, _color); + inline int Measure() { + return ::MeasureText(text.c_str(), static_cast(fontSize)); } /** - * @brief Draw text with values in class. - * + * Measure string size for Font */ - void Draw(const ::Vector2& position) { - ::DrawTextEx(_font, _text.c_str(), position, _fontSize, _spacing, _color); + inline Vector2 MeasureEx() { + return ::MeasureTextEx(font, text.c_str(), fontSize, spacing); } - - std::string GetText() const { return _text; } - void SetText(const std::string& text) { _text = text; } - - float GetFontSize() const { return _fontSize; } - void SetFontSize(float fontSize) { _fontSize = fontSize; } - - //Font GetFont() const { return _font; } - void SetFont(const ::Font& font) { _font = font; } - - Color GetColor() const { return _color; } - void SetColor(const ::Color& color) { _color = color; } - - float GetPosX() const { return _position.GetX(); } - void SetPosX(float posX) { _position.SetX(posX); } - - float GetPosY() const { return _position.GetY(); } - void SetPosY(float posY) { _position.SetY(posY); } - Vector2 GetPosition() const { return _position; } - void SetPosition(const ::Vector2& position) { _position = position; } + Text& operator=(const Text& other) { + if (this == &other) { + return *this; + } - float GetSpacing() const { return _spacing; } - void SetSpacing(float spacing) { _spacing = spacing; } + text = other.text; + fontSize = other.fontSize; + color = other.color; + font = other.font; + spacing = other.spacing; - Text& operator=(const Text& text) { return *this; } @@ -152,13 +132,11 @@ class Text { ::DrawTextPro(font, text.c_str(), position, origin, rotation, fontSize, spacing, color); } - private: - std::string _text = ""; - float _fontSize = 10; - Font _font = ::GetFontDefault(); - Color _color = WHITE; - Vector2 _position = {0.0, 0.0}; - float _spacing = 0; + std::string text; + float fontSize; + ::Color color; + ::Font font; + float spacing; }; } // namespace raylib