text_words_alignment.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - words alignment
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by JP Mortiboys (@themushroompirates) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 JP Mortiboys (@themushroompirates)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h" // Required for: Lerp()
  19. #include <stdio.h>
  20. typedef enum TextAlignment {
  21. TEXT_ALIGN_LEFT = 0,
  22. TEXT_ALIGN_TOP = 0,
  23. TEXT_ALIGN_CENTRE = 1,
  24. TEXT_ALIGN_MIDDLE = 1,
  25. TEXT_ALIGN_RIGHT = 2,
  26. TEXT_ALIGN_BOTTOM = 2
  27. } TextAlignment;
  28. //------------------------------------------------------------------------------------
  29. // Program main entry point
  30. //------------------------------------------------------------------------------------
  31. int main(void)
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. const int screenWidth = 800;
  36. const int screenHeight = 450;
  37. InitWindow(screenWidth, screenHeight, "raylib [text] example - words alignment");
  38. // Define the rectangle we will draw the text in
  39. Rectangle textContainerRect = (Rectangle){ screenWidth/2-screenWidth/4, screenHeight/2-screenHeight/3, screenWidth/2, screenHeight*2/3 };
  40. // Some text to display the current alignment
  41. const char *textAlignNameH[] = { "Left", "Centre", "Right" };
  42. const char *textAlignNameV[] = { "Top", "Middle", "Bottom" };
  43. // Define the text we're going to draw in the rectangle
  44. int wordIndex = 0;
  45. int wordCount = 0;
  46. char **words = TextSplit("raylib is a simple and easy-to-use library to enjoy videogames programming", ' ', &wordCount);
  47. // Initialize the font size we're going to use
  48. int fontSize = 40;
  49. // And of course the font...
  50. Font font = GetFontDefault();
  51. // Initialize the alignment variables
  52. TextAlignment hAlign = TEXT_ALIGN_CENTRE;
  53. TextAlignment vAlign = TEXT_ALIGN_MIDDLE;
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. if (IsKeyPressed(KEY_LEFT))
  62. {
  63. if (hAlign > 0) hAlign = hAlign - 1;
  64. }
  65. if (IsKeyPressed(KEY_RIGHT))
  66. {
  67. hAlign = hAlign + 1;
  68. if (hAlign > 2) hAlign = 2;
  69. }
  70. if (IsKeyPressed(KEY_UP))
  71. {
  72. if (vAlign > 0) vAlign = vAlign - 1;
  73. }
  74. if (IsKeyPressed(KEY_DOWN))
  75. {
  76. vAlign = vAlign + 1;
  77. if (vAlign > 2) vAlign = 2;
  78. }
  79. // One word per second
  80. if (wordCount > 0) wordIndex = (int)GetTime()%wordCount;
  81. else wordIndex = 0;
  82. //----------------------------------------------------------------------------------
  83. // Draw
  84. //----------------------------------------------------------------------------------
  85. BeginDrawing();
  86. ClearBackground(DARKBLUE);
  87. DrawText("Use Arrow Keys to change the text alignment", 20, 20, 20, LIGHTGRAY);
  88. DrawText(TextFormat("Alignment: Horizontal = %s, Vertical = %s", textAlignNameH[hAlign], textAlignNameV[vAlign]), 20, 40, 20, LIGHTGRAY);
  89. DrawRectangleRec(textContainerRect, BLUE);
  90. // Get the size of the text to draw
  91. Vector2 textSize = MeasureTextEx(font, words[wordIndex], fontSize, fontSize*.1f);
  92. // Calculate the top-left text position based on the rectangle and alignment
  93. Vector2 textPos = (Vector2){
  94. textContainerRect.x + Lerp(0.0f, textContainerRect.width - textSize.x, ((float)hAlign)*0.5f),
  95. textContainerRect.y + Lerp(0.0f, textContainerRect.height - textSize.y, ((float)vAlign)*0.5f)
  96. };
  97. // Draw the text
  98. DrawTextEx(font, words[wordIndex], textPos, fontSize, fontSize*.1f, RAYWHITE);
  99. EndDrawing();
  100. //----------------------------------------------------------------------------------
  101. }
  102. // De-Initialization
  103. //--------------------------------------------------------------------------------------
  104. CloseWindow(); // Close window and OpenGL context
  105. //--------------------------------------------------------------------------------------
  106. return 0;
  107. }