25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - Font selector
  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. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [text] example - font selector");
  19. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  20. SpriteFont fonts[8]; // SpriteFont array
  21. fonts[0] = LoadSpriteFont("resources/fonts/alagard.rbmf"); // SpriteFont loading
  22. fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.rbmf"); // SpriteFont loading
  23. fonts[2] = LoadSpriteFont("resources/fonts/mecha.rbmf"); // SpriteFont loading
  24. fonts[3] = LoadSpriteFont("resources/fonts/setback.rbmf"); // SpriteFont loading
  25. fonts[4] = LoadSpriteFont("resources/fonts/romulus.rbmf"); // SpriteFont loading
  26. fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.rbmf"); // SpriteFont loading
  27. fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.rbmf"); // SpriteFont loading
  28. fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.rbmf"); // SpriteFont loading
  29. int currentFont = 0; // Selected font
  30. Color colors[8] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
  31. const char fontNames[8][20] = { "[0] Alagard", "[1] PixelPlay", "[2] MECHA", "[3] Setback",
  32. "[4] Romulus", "[5] PixAntiqua", "[6] Alpha Beta", "[7] Jupiter Crash" };
  33. const char text[50] = "THIS is THE FONT you SELECTED!"; // Main text
  34. Vector2 textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].baseSize*3, 1);
  35. Vector2 mousePoint;
  36. Color btnNextOutColor = DARKBLUE; // Button color (outside line)
  37. Color btnNextInColor = SKYBLUE; // Button color (inside)
  38. int framesCounter = 0; // Useful to count frames button is 'active' = clicked
  39. int positionY = 180; // Text selector and button Y position
  40. Rectangle btnNextRec = { 673, positionY, 109, 44 }; // Button rectangle (useful for collision)
  41. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  42. //--------------------------------------------------------------------------------------
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. //----------------------------------------------------------------------------------
  48. // Keyboard-based font selection (easy)
  49. if (IsKeyPressed(KEY_RIGHT))
  50. {
  51. if (currentFont < 7) currentFont++;
  52. }
  53. if (IsKeyPressed(KEY_LEFT))
  54. {
  55. if (currentFont > 0) currentFont--;
  56. }
  57. if (IsKeyPressed('0')) currentFont = 0;
  58. else if (IsKeyPressed('1')) currentFont = 1;
  59. else if (IsKeyPressed('2')) currentFont = 2;
  60. else if (IsKeyPressed('3')) currentFont = 3;
  61. else if (IsKeyPressed('4')) currentFont = 4;
  62. else if (IsKeyPressed('5')) currentFont = 5;
  63. else if (IsKeyPressed('6')) currentFont = 6;
  64. else if (IsKeyPressed('7')) currentFont = 7;
  65. // Mouse-based font selection (NEXT button logic)
  66. mousePoint = GetMousePosition();
  67. if (CheckCollisionPointRec(mousePoint, btnNextRec))
  68. {
  69. // Mouse hover button logic
  70. if (framesCounter == 0)
  71. {
  72. btnNextOutColor = DARKPURPLE;
  73. btnNextInColor = PURPLE;
  74. }
  75. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  76. {
  77. framesCounter = 20; // Frames button is 'active'
  78. btnNextOutColor = MAROON;
  79. btnNextInColor = RED;
  80. }
  81. }
  82. else
  83. {
  84. // Mouse not hover button
  85. btnNextOutColor = DARKBLUE;
  86. btnNextInColor = SKYBLUE;
  87. }
  88. if (framesCounter > 0) framesCounter--;
  89. if (framesCounter == 1) // We change font on frame 1
  90. {
  91. currentFont++;
  92. if (currentFont > 7) currentFont = 0;
  93. }
  94. // Text measurement for better positioning on screen
  95. textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].baseSize*3, 1);
  96. //----------------------------------------------------------------------------------
  97. // Draw
  98. //----------------------------------------------------------------------------------
  99. BeginDrawing();
  100. ClearBackground(RAYWHITE);
  101. DrawText("font selector - use arroys, button or numbers", 160, 80, 20, DARKGRAY);
  102. DrawLine(120, 120, 680, 120, DARKGRAY);
  103. DrawRectangle(18, positionY, 644, 44, DARKGRAY);
  104. DrawRectangle(20, positionY + 2, 640, 40, LIGHTGRAY);
  105. DrawText(fontNames[currentFont], 30, positionY + 13, 20, BLACK);
  106. DrawText("< >", 610, positionY + 8, 30, BLACK);
  107. DrawRectangleRec(btnNextRec, btnNextOutColor);
  108. DrawRectangle(675, positionY + 2, 105, 40, btnNextInColor);
  109. DrawText("NEXT", 700, positionY + 13, 20, btnNextOutColor);
  110. DrawTextEx(fonts[currentFont], text, (Vector2){ screenWidth/2 - textSize.x/2,
  111. 260 + (70 - textSize.y)/2 }, fonts[currentFont].baseSize*3,
  112. 1, colors[currentFont]);
  113. EndDrawing();
  114. //----------------------------------------------------------------------------------
  115. }
  116. // De-Initialization
  117. //--------------------------------------------------------------------------------------
  118. for (int i = 0; i < 8; i++) UnloadSpriteFont(fonts[i]); // SpriteFont(s) unloading
  119. CloseWindow(); // Close window and OpenGL context
  120. //--------------------------------------------------------------------------------------
  121. return 0;
  122. }