You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.8 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*******************************************************************************************
  2. *
  3. * raylib example 06c - Font selection...
  4. *
  5. * This example has been created using raylib 1.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2013 Ramon Santamaria (Ray San - raysan@raysanweb.com)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 150;
  18. InitWindow(screenWidth, screenHeight, "raylib example 06c - font selection");
  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, GetFontBaseSize(fonts[currentFont])*3, 1);
  35. Vector2 mousePoint;
  36. Rectangle btnNextRec = { 673, 18, 109, 44 }; // Button rectangle (useful for collision)
  37. Color btnNextOutColor = DARKBLUE; // Button color (outside line)
  38. Color btnNextInColor = SKYBLUE; // Button color (inside)
  39. int framesCounter = 0; // Useful to count frames button is 'active' = clicked
  40. SetTargetFPS(60);
  41. //--------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. // Keyboard-based font selection (easy)
  48. if (IsKeyPressed(KEY_RIGHT))
  49. {
  50. if (currentFont < 7) currentFont++;
  51. }
  52. if (IsKeyPressed(KEY_LEFT))
  53. {
  54. if (currentFont > 0) currentFont--;
  55. }
  56. // Mouse-based font selection (NEXT button logic)
  57. mousePoint = GetMousePosition();
  58. if (CheckCollisionPointRec(mousePoint, btnNextRec))
  59. {
  60. // Mouse hover button logic
  61. if (framesCounter == 0)
  62. {
  63. btnNextOutColor = DARKPURPLE;
  64. btnNextInColor = PURPLE;
  65. }
  66. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  67. {
  68. framesCounter = 20; // Frames button is 'active'
  69. btnNextOutColor = MAROON;
  70. btnNextInColor = RED;
  71. }
  72. }
  73. else
  74. {
  75. // Mouse not hover button
  76. btnNextOutColor = DARKBLUE;
  77. btnNextInColor = SKYBLUE;
  78. }
  79. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && (framesCounter > 0)) framesCounter--;
  80. if (framesCounter == 1) // We change font on frame 1
  81. {
  82. currentFont++;
  83. if (currentFont > 7) currentFont = 0;
  84. }
  85. // Text measurement for better positioning on screen
  86. textSize = MeasureTextEx(fonts[currentFont], text, GetFontBaseSize(fonts[currentFont])*3, 1);
  87. //----------------------------------------------------------------------------------
  88. // Draw
  89. //----------------------------------------------------------------------------------
  90. BeginDrawing();
  91. ClearBackground(RAYWHITE);
  92. DrawRectangle(18, 18, 644, 44, DARKGRAY);
  93. DrawRectangle(20, 20, 640, 40, LIGHTGRAY);
  94. DrawText(fontNames[currentFont], 30, 31, 20, BLACK);
  95. DrawText("< >", 610, 26, 30, BLACK);
  96. DrawRectangleRec(btnNextRec, btnNextOutColor);
  97. DrawRectangle(675, 20, 105, 40, btnNextInColor);
  98. DrawText("NEXT", 700, 31, 20, btnNextOutColor);
  99. DrawTextEx(fonts[currentFont], text, (Vector2){ screenWidth/2 - textSize.x/2,
  100. 75 + (70 - textSize.y)/2 }, GetFontBaseSize(fonts[currentFont])*3,
  101. 1, colors[currentFont]);
  102. EndDrawing();
  103. //----------------------------------------------------------------------------------
  104. }
  105. // De-Initialization
  106. //--------------------------------------------------------------------------------------
  107. for (int i = 0; i < 8; i++) UnloadSpriteFont(fonts[i]); // SpriteFont(s) unloading
  108. CloseWindow(); // Close window and OpenGL context
  109. //--------------------------------------------------------------------------------------
  110. return 0;
  111. }