From 047c9d2052237120825a6664cb6ad2ddef33d499 Mon Sep 17 00:00:00 2001 From: themushroompirates <59015901+themushroompirates@users.noreply.github.com> Date: Wed, 15 Oct 2025 19:33:53 +0200 Subject: [PATCH] Added text alignment example (#5254) Co-authored-by: Ray --- examples/Makefile | 3 +- examples/Makefile.Web | 3 +- examples/README.md | 3 +- examples/examples_list.txt | 1 + examples/text/text_words_alignment.c | 130 +++++++++++++++++++++++++ examples/text/text_words_alignment.png | Bin 0 -> 15745 bytes 6 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 examples/text/text_words_alignment.c create mode 100644 examples/text/text_words_alignment.png diff --git a/examples/Makefile b/examples/Makefile index 8b5faf634..69d100fdb 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -612,7 +612,8 @@ TEXT = \ text/text_sprite_fonts \ text/text_unicode_emojis \ text/text_unicode_ranges \ - text/text_writing_anim + text/text_writing_anim \ + text/text_words_alignment MODELS = \ models/models_animation_gpu_skinning \ diff --git a/examples/Makefile.Web b/examples/Makefile.Web index 510725b6e..933ea3499 100644 --- a/examples/Makefile.Web +++ b/examples/Makefile.Web @@ -612,7 +612,8 @@ TEXT = \ text/text_sprite_fonts \ text/text_unicode_emojis \ text/text_unicode_ranges \ - text/text_writing_anim + text/text_writing_anim \ + text/text_words_alignment MODELS = \ models/models_animation_gpu_skinning \ diff --git a/examples/README.md b/examples/README.md index 3468ff34a..9757cea60 100644 --- a/examples/README.md +++ b/examples/README.md @@ -133,7 +133,7 @@ Examples using raylib textures functionality, including image/textures loading/g | [textures_image_rotate](textures/textures_image_rotate.c) | textures_image_rotate | ⭐⭐☆☆ | 1.0 | 1.0 | [Ramon Santamaria](https://github.com/raysan5) | | [textures_textured_curve](textures/textures_textured_curve.c) | textures_textured_curve | ⭐⭐⭐☆ | 4.5 | 4.5 | [Jeffery Myers](https://github.com/JeffM2501) | -### category: text [14] +### category: text [15] Examples using raylib text functionality, including sprite fonts loading/generation and text drawing, provided by raylib [text](../src/rtext.c) module. @@ -153,6 +153,7 @@ Examples using raylib text functionality, including sprite fonts loading/generat | [text_3d_drawing](text/text_3d_drawing.c) | text_3d_drawing | ⭐⭐⭐⭐️ | 3.5 | 4.0 | [Vlad Adrian](https://github.com/demizdor) | | [text_codepoints_loading](text/text_codepoints_loading.c) | text_codepoints_loading | ⭐⭐⭐☆ | 4.2 | 4.2 | [Ramon Santamaria](https://github.com/raysan5) | | [text_inline_styling](text/text_inline_styling.c) | text_inline_styling | ⭐⭐⭐☆ | 5.6-dev | 5.6-dev | [Wagner Barongello](https://github.com/SultansOfCode) | +| [text_words_alignment](text/text_words_alignment.c) | text_words_alignment | ⭐⭐⭐☆ | 5.6-dev | 5.6-dev | [JP Mortiboys](https://github.com/themushroompirates) | ### category: models [25] diff --git a/examples/examples_list.txt b/examples/examples_list.txt index ed904d6f1..9770975fb 100644 --- a/examples/examples_list.txt +++ b/examples/examples_list.txt @@ -114,6 +114,7 @@ text;text_unicode_ranges;★★★★;5.5;5.6;2025;2025;"Vadim Gunko";@GuvaCode text;text_3d_drawing;★★★★;3.5;4.0;2021;2025;"Vlad Adrian";@demizdor text;text_codepoints_loading;★★★☆;4.2;4.2;2022;2025;"Ramon Santamaria";@raysan5 text;text_inline_styling;★★★☆;5.6-dev;5.6-dev;2025;2025;"Wagner Barongello";@SultansOfCode +text;text_words_alignment;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"JP Mortiboys";@themushroompirates models;models_animation_playing;★★☆☆;2.5;3.5;2019;2025;"Culacant";@culacant models;models_billboard_rendering;★★★☆;1.3;3.5;2015;2025;"Ramon Santamaria";@raysan5 models;models_box_collisions;★☆☆☆;1.3;3.5;2015;2025;"Ramon Santamaria";@raysan5 diff --git a/examples/text/text_words_alignment.c b/examples/text/text_words_alignment.c new file mode 100644 index 000000000..0f012ab75 --- /dev/null +++ b/examples/text/text_words_alignment.c @@ -0,0 +1,130 @@ +/******************************************************************************************* +* +* raylib [text] example - text alignment +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.0, last time updated with raylib 5.5 +* +* Example contributed by JP Mortiboys (@themushroompirates) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2025 JP Mortiboys (@themushroompirates) +* +********************************************************************************************/ + +#include "raylib.h" + +#include "raymath.h" // Required for: Lerp() + +typedef enum TextAlignment { + TEXT_ALIGN_LEFT = 0, + TEXT_ALIGN_TOP = 0, + TEXT_ALIGN_CENTRE = 1, + TEXT_ALIGN_MIDDLE = 1, + TEXT_ALIGN_RIGHT = 2, + TEXT_ALIGN_BOTTOM = 2 +} TextAlignment; + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [text] example - text alignment"); + + // Define the rectangle we will draw the text in + Rectangle textContainerRect = (Rectangle){ screenWidth/2-screenWidth/4, screenHeight/2-screenHeight/3, screenWidth/2, screenHeight*2/3 }; + + // Some text to display the current alignment + const char *textAlignNameH[] = { "Left", "Centre", "Right" }; + const char *textAlignNameV[] = { "Top", "Middle", "Bottom" }; + + // Define the text we're going to draw in the rectangle + int wordIndex = 0; + int wordCount = 0; + char **words = TextSplit("raylib is a simple and easy-to-use library to enjoy videogames programming", ' ', &wordCount); + + // Initialize the font size we're going to use + int fontSize = 40; + + // And of course the font... + Font font = GetFontDefault(); + + // Intialize the alignment variables + TextAlignment hAlign = TEXT_ALIGN_CENTRE; + TextAlignment vAlign = TEXT_ALIGN_MIDDLE; + + 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 + //---------------------------------------------------------------------------------- + + if (IsKeyPressed(KEY_LEFT)) { + hAlign = hAlign - 1; + if (hAlign < 0) hAlign = 0; + } + if (IsKeyPressed(KEY_RIGHT)) { + hAlign = hAlign + 1; + if (hAlign > 2) hAlign = 2; + } + if (IsKeyPressed(KEY_UP)) { + vAlign = vAlign - 1; + if (vAlign < 0) vAlign = 0; + } + if (IsKeyPressed(KEY_DOWN)) { + vAlign = vAlign + 1; + if (vAlign > 2) vAlign = 2; + } + + // One word per second + wordIndex = (int)GetTime() % wordCount; + + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(DARKBLUE); + + DrawText("Use Arrow Keys to change the text alignment", 20, 20, 20, LIGHTGRAY); + DrawText(TextFormat("Alignment: Horizontal = %s, Vertical = %s", textAlignNameH[hAlign], textAlignNameV[vAlign]), 20, 40, 20, LIGHTGRAY); + + DrawRectangleRec(textContainerRect, BLUE); + + // Get the size of the text to draw + Vector2 textSize = MeasureTextEx(font, words[wordIndex], fontSize, fontSize*.1f); + + // Calculate the top-left text position based on the rectangle and alignment + Vector2 textPos = (Vector2) { + textContainerRect.x + Lerp(0.0f, textContainerRect.width - textSize.x, ((float)hAlign) * 0.5f), + textContainerRect.y + Lerp(0.0f, textContainerRect.height - textSize.y, ((float)vAlign) * 0.5f) + }; + + // Draw the text + DrawTextEx(font, words[wordIndex], textPos, fontSize, fontSize*.1f, RAYWHITE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/text/text_words_alignment.png b/examples/text/text_words_alignment.png new file mode 100644 index 0000000000000000000000000000000000000000..ae0892381437200ccf3fd6e74005250d658e2282 GIT binary patch literal 15745 zcmeHOdsI?+9tUNROq3p$$_SEfR!+t!RvS`^idci1S$PZDuFpBx= z_?Ee8{Avzr>I2M@$_phO^mJpB5VR=|JQji20XJuGMFgUS-c$!8k!6;~0EzgYN1>X!qrm&% zEnd1r5trD;32K$v*|7T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