shapes_logo_raylib_anim.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - raylib logo animation (adapted for HTML5 platform)
  4. *
  5. * This example has been created using raylib 1.4 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2014 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. int logoPositionX;
  21. int logoPositionY;
  22. int framesCounter = 0;
  23. int lettersCount = 0;
  24. int topSideRecWidth = 16;
  25. int leftSideRecHeight = 16;
  26. int bottomSideRecWidth = 16;
  27. int rightSideRecHeight = 16;
  28. int state = 0; // Tracking animation states (State Machine)
  29. float alpha = 1.0f; // Useful for fading
  30. //----------------------------------------------------------------------------------
  31. // Module Functions Declaration
  32. //----------------------------------------------------------------------------------
  33. void UpdateDrawFrame(void); // Update and Draw one frame
  34. //----------------------------------------------------------------------------------
  35. // Main Enry Point
  36. //----------------------------------------------------------------------------------
  37. int main()
  38. {
  39. // Initialization
  40. //--------------------------------------------------------------------------------------
  41. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo animation");
  42. logoPositionX = screenWidth/2 - 128;
  43. logoPositionY = screenHeight/2 - 128;
  44. #if defined(PLATFORM_WEB)
  45. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  46. #else
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. UpdateDrawFrame();
  53. }
  54. #endif
  55. // De-Initialization
  56. //--------------------------------------------------------------------------------------
  57. CloseWindow(); // Close window and OpenGL context
  58. //--------------------------------------------------------------------------------------
  59. return 0;
  60. }
  61. //----------------------------------------------------------------------------------
  62. // Module Functions Definition
  63. //----------------------------------------------------------------------------------
  64. void UpdateDrawFrame(void)
  65. {
  66. // Update
  67. //----------------------------------------------------------------------------------
  68. if (state == 0) // State 0: Small box blinking
  69. {
  70. framesCounter++;
  71. if (framesCounter == 120)
  72. {
  73. state = 1;
  74. framesCounter = 0; // Reset counter... will be used later...
  75. }
  76. }
  77. else if (state == 1) // State 1: Top and left bars growing
  78. {
  79. topSideRecWidth += 4;
  80. leftSideRecHeight += 4;
  81. if (topSideRecWidth == 256) state = 2;
  82. }
  83. else if (state == 2) // State 2: Bottom and right bars growing
  84. {
  85. bottomSideRecWidth += 4;
  86. rightSideRecHeight += 4;
  87. if (bottomSideRecWidth == 256) state = 3;
  88. }
  89. else if (state == 3) // State 3: Letters appearing (one by one)
  90. {
  91. framesCounter++;
  92. if (framesCounter/12) // Every 12 frames, one more letter!
  93. {
  94. lettersCount++;
  95. framesCounter = 0;
  96. }
  97. if (lettersCount >= 10) // When all letters have appeared, just fade out everything
  98. {
  99. alpha -= 0.02f;
  100. if (alpha <= 0.0f)
  101. {
  102. alpha = 0.0f;
  103. state = 4;
  104. }
  105. }
  106. }
  107. else if (state == 4) // State 4: Reset and Replay
  108. {
  109. if (IsKeyPressed('R'))
  110. {
  111. framesCounter = 0;
  112. lettersCount = 0;
  113. topSideRecWidth = 16;
  114. leftSideRecHeight = 16;
  115. bottomSideRecWidth = 16;
  116. rightSideRecHeight = 16;
  117. alpha = 1.0f;
  118. state = 0; // Return to State 0
  119. }
  120. }
  121. //----------------------------------------------------------------------------------
  122. // Draw
  123. //----------------------------------------------------------------------------------
  124. BeginDrawing();
  125. ClearBackground(RAYWHITE);
  126. if (state == 0)
  127. {
  128. if ((framesCounter/15)%2) DrawRectangle(logoPositionX, logoPositionY, 16, 16, BLACK);
  129. }
  130. else if (state == 1)
  131. {
  132. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
  133. DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
  134. }
  135. else if (state == 2)
  136. {
  137. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
  138. DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
  139. DrawRectangle(logoPositionX + 240, logoPositionY, 16, rightSideRecHeight, BLACK);
  140. DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, BLACK);
  141. }
  142. else if (state == 3)
  143. {
  144. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, Fade(BLACK, alpha));
  145. DrawRectangle(logoPositionX, logoPositionY + 16, 16, leftSideRecHeight - 32, Fade(BLACK, alpha));
  146. DrawRectangle(logoPositionX + 240, logoPositionY + 16, 16, rightSideRecHeight - 32, Fade(BLACK, alpha));
  147. DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, Fade(BLACK, alpha));
  148. DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
  149. DrawText(SubText("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
  150. }
  151. else if (state == 4)
  152. {
  153. DrawText("[R] REPLAY", 340, 200, 20, GRAY);
  154. }
  155. EndDrawing();
  156. //----------------------------------------------------------------------------------
  157. }