font_selector.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Font selector (adapted for HTML5 platform)
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #if defined(PLATFORM_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. //----------------------------------------------------------------------------------
  16. // Global Variables Definition
  17. //----------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. SpriteFont fonts[8]; // SpriteFont array
  21. int currentFont = 0; // Selected font
  22. Color *colors;
  23. const char (*fontNames)[20];
  24. const char text[50] = "THIS is THE FONT you SELECTED!";
  25. Vector2 textSize;
  26. Vector2 mousePoint;
  27. Color btnNextOutColor; // Button color (outside line)
  28. Color btnNextInColor; // Button color (inside)
  29. int framesCounter = 0; // Useful to count frames button is 'active' = clicked
  30. int positionY = 180; // Text selector and button Y position
  31. Rectangle btnNextRec;
  32. //----------------------------------------------------------------------------------
  33. // Module Functions Declaration
  34. //----------------------------------------------------------------------------------
  35. void UpdateDrawFrame(void); // Update and Draw one frame
  36. //----------------------------------------------------------------------------------
  37. // Main Enry Point
  38. //----------------------------------------------------------------------------------
  39. int main()
  40. {
  41. // Initialization
  42. //--------------------------------------------------------------------------------------
  43. InitWindow(screenWidth, screenHeight, "raylib [text] example - font selector");
  44. btnNextOutColor = DARKBLUE;
  45. btnNextInColor = SKYBLUE;
  46. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  47. fonts[0] = LoadSpriteFont("resources/fonts/alagard.rbmf"); // SpriteFont loading
  48. fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.rbmf"); // SpriteFont loading
  49. fonts[2] = LoadSpriteFont("resources/fonts/mecha.rbmf"); // SpriteFont loading
  50. fonts[3] = LoadSpriteFont("resources/fonts/setback.rbmf"); // SpriteFont loading
  51. fonts[4] = LoadSpriteFont("resources/fonts/romulus.rbmf"); // SpriteFont loading
  52. fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.rbmf"); // SpriteFont loading
  53. fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.rbmf"); // SpriteFont loading
  54. fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.rbmf"); // SpriteFont loading
  55. Color tempColors[8] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
  56. colors = tempColors;
  57. const char tempFontNames[8][20] = { "[0] Alagard", "[1] PixelPlay", "[2] MECHA", "[3] Setback",
  58. "[4] Romulus", "[5] PixAntiqua", "[6] Alpha Beta", "[7] Jupiter Crash" };;
  59. fontNames = tempFontNames;
  60. textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].baseSize*3, 1);
  61. btnNextRec = (Rectangle){ 673, positionY, 109, 44 }; // Button rectangle (useful for collision)
  62. #if defined(PLATFORM_WEB)
  63. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  64. #else
  65. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  66. //--------------------------------------------------------------------------------------
  67. // Main game loop
  68. while (!WindowShouldClose()) // Detect window close button or ESC key
  69. {
  70. UpdateDrawFrame();
  71. }
  72. #endif
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. for (int i = 0; i < 8; i++) UnloadSpriteFont(fonts[i]); // SpriteFont(s) unloading
  76. CloseWindow(); // Close window and OpenGL context
  77. //--------------------------------------------------------------------------------------
  78. return 0;
  79. }
  80. //----------------------------------------------------------------------------------
  81. // Module Functions Definition
  82. //----------------------------------------------------------------------------------
  83. void UpdateDrawFrame(void)
  84. {
  85. // Update
  86. //----------------------------------------------------------------------------------
  87. // Keyboard-based font selection (easy)
  88. if (IsKeyPressed(KEY_RIGHT))
  89. {
  90. if (currentFont < 7) currentFont++;
  91. }
  92. if (IsKeyPressed(KEY_LEFT))
  93. {
  94. if (currentFont > 0) currentFont--;
  95. }
  96. if (IsKeyPressed('0')) currentFont = 0;
  97. else if (IsKeyPressed('1')) currentFont = 1;
  98. else if (IsKeyPressed('2')) currentFont = 2;
  99. else if (IsKeyPressed('3')) currentFont = 3;
  100. else if (IsKeyPressed('4')) currentFont = 4;
  101. else if (IsKeyPressed('5')) currentFont = 5;
  102. else if (IsKeyPressed('6')) currentFont = 6;
  103. else if (IsKeyPressed('7')) currentFont = 7;
  104. // Mouse-based font selection (NEXT button logic)
  105. mousePoint = GetMousePosition();
  106. if (CheckCollisionPointRec(mousePoint, btnNextRec))
  107. {
  108. // Mouse hover button logic
  109. if (framesCounter == 0)
  110. {
  111. btnNextOutColor = DARKPURPLE;
  112. btnNextInColor = PURPLE;
  113. }
  114. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  115. {
  116. framesCounter = 20; // Frames button is 'active'
  117. btnNextOutColor = MAROON;
  118. btnNextInColor = RED;
  119. }
  120. }
  121. else
  122. {
  123. // Mouse not hover button
  124. btnNextOutColor = DARKBLUE;
  125. btnNextInColor = SKYBLUE;
  126. }
  127. if (framesCounter > 0) framesCounter--;
  128. if (framesCounter == 1) // We change font on frame 1
  129. {
  130. currentFont++;
  131. if (currentFont > 7) currentFont = 0;
  132. }
  133. // Text measurement for better positioning on screen
  134. textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].baseSize*3, 1);
  135. //----------------------------------------------------------------------------------
  136. // Draw
  137. //----------------------------------------------------------------------------------
  138. BeginDrawing();
  139. ClearBackground(RAYWHITE);
  140. DrawText("font selector - use arroys, button or numbers", 160, 80, 20, DARKGRAY);
  141. DrawLine(120, 120, 680, 120, DARKGRAY);
  142. DrawRectangle(18, positionY, 644, 44, DARKGRAY);
  143. DrawRectangle(20, positionY + 2, 640, 40, LIGHTGRAY);
  144. DrawText(fontNames[currentFont], 30, positionY + 13, 20, BLACK);
  145. DrawText("< >", 610, positionY + 8, 30, BLACK);
  146. DrawRectangleRec(btnNextRec, btnNextOutColor);
  147. DrawRectangle(675, positionY + 2, 105, 40, btnNextInColor);
  148. DrawText("NEXT", 700, positionY + 13, 20, btnNextOutColor);
  149. DrawTextEx(fonts[currentFont], text, (Vector2){ screenWidth/2 - textSize.x/2,
  150. 260 + (70 - textSize.y)/2 }, fonts[currentFont].baseSize*3,
  151. 1, colors[currentFont]);
  152. EndDrawing();
  153. //----------------------------------------------------------------------------------
  154. }