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.

165 line
6.0 KiB

11 年之前
11 年之前
11 年之前
11 年之前
11 年之前
11 年之前
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - raylib logo animation
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 4.0
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo animation");
  24. int logoPositionX = screenWidth/2 - 128;
  25. int logoPositionY = screenHeight/2 - 128;
  26. int framesCounter = 0;
  27. int lettersCount = 0;
  28. int topSideRecWidth = 16;
  29. int leftSideRecHeight = 16;
  30. int bottomSideRecWidth = 16;
  31. int rightSideRecHeight = 16;
  32. int state = 0; // Tracking animation states (State Machine)
  33. float alpha = 1.0f; // Useful for fading
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  35. //--------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. if (state == 0) // State 0: Small box blinking
  42. {
  43. framesCounter++;
  44. if (framesCounter == 120)
  45. {
  46. state = 1;
  47. framesCounter = 0; // Reset counter... will be used later...
  48. }
  49. }
  50. else if (state == 1) // State 1: Top and left bars growing
  51. {
  52. topSideRecWidth += 4;
  53. leftSideRecHeight += 4;
  54. if (topSideRecWidth == 256) state = 2;
  55. }
  56. else if (state == 2) // State 2: Bottom and right bars growing
  57. {
  58. bottomSideRecWidth += 4;
  59. rightSideRecHeight += 4;
  60. if (bottomSideRecWidth == 256) state = 3;
  61. }
  62. else if (state == 3) // State 3: Letters appearing (one by one)
  63. {
  64. framesCounter++;
  65. if (framesCounter/12) // Every 12 frames, one more letter!
  66. {
  67. lettersCount++;
  68. framesCounter = 0;
  69. }
  70. if (lettersCount >= 10) // When all letters have appeared, just fade out everything
  71. {
  72. alpha -= 0.02f;
  73. if (alpha <= 0.0f)
  74. {
  75. alpha = 0.0f;
  76. state = 4;
  77. }
  78. }
  79. }
  80. else if (state == 4) // State 4: Reset and Replay
  81. {
  82. if (IsKeyPressed(KEY_R))
  83. {
  84. framesCounter = 0;
  85. lettersCount = 0;
  86. topSideRecWidth = 16;
  87. leftSideRecHeight = 16;
  88. bottomSideRecWidth = 16;
  89. rightSideRecHeight = 16;
  90. alpha = 1.0f;
  91. state = 0; // Return to State 0
  92. }
  93. }
  94. //----------------------------------------------------------------------------------
  95. // Draw
  96. //----------------------------------------------------------------------------------
  97. BeginDrawing();
  98. ClearBackground(RAYWHITE);
  99. if (state == 0)
  100. {
  101. if ((framesCounter/15)%2) DrawRectangle(logoPositionX, logoPositionY, 16, 16, BLACK);
  102. }
  103. else if (state == 1)
  104. {
  105. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
  106. DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
  107. }
  108. else if (state == 2)
  109. {
  110. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
  111. DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
  112. DrawRectangle(logoPositionX + 240, logoPositionY, 16, rightSideRecHeight, BLACK);
  113. DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, BLACK);
  114. }
  115. else if (state == 3)
  116. {
  117. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, Fade(BLACK, alpha));
  118. DrawRectangle(logoPositionX, logoPositionY + 16, 16, leftSideRecHeight - 32, Fade(BLACK, alpha));
  119. DrawRectangle(logoPositionX + 240, logoPositionY + 16, 16, rightSideRecHeight - 32, Fade(BLACK, alpha));
  120. DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, Fade(BLACK, alpha));
  121. DrawRectangle(GetScreenWidth()/2 - 112, GetScreenHeight()/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
  122. DrawText(TextSubtext("raylib", 0, lettersCount), GetScreenWidth()/2 - 44, GetScreenHeight()/2 + 48, 50, Fade(BLACK, alpha));
  123. }
  124. else if (state == 4)
  125. {
  126. DrawText("[R] REPLAY", 340, 200, 20, GRAY);
  127. }
  128. EndDrawing();
  129. //----------------------------------------------------------------------------------
  130. }
  131. // De-Initialization
  132. //--------------------------------------------------------------------------------------
  133. CloseWindow(); // Close window and OpenGL context
  134. //--------------------------------------------------------------------------------------
  135. return 0;
  136. }