ex06b_logo_anim.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*******************************************************************************************
  2. *
  3. * raylib example 06b - raylib Logo Animation
  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 - [email protected])
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. // Useful function for fade-ins and fade-outs
  13. Color Fade(Color col, float alpha)
  14. {
  15. return (Color){col.r, col.g, col.b, col.a*alpha};
  16. }
  17. int main()
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. int screenWidth = 800;
  22. int screenHeight = 450;
  23. int logoPositionX = screenWidth/2 - 128;
  24. int logoPositionY = screenHeight/2 - 128;
  25. int framesCounter = 0;
  26. int lettersCount = 0;
  27. int topSideRecWidth = 16;
  28. int leftSideRecHeight = 16;
  29. int bottomSideRecWidth = 16;
  30. int rightSideRecHeight = 16;
  31. char raylib[8] = " "; // raylib text array, max 8 letters
  32. int state = 0; // Tracking animation states (State Machine)
  33. float alpha = 1.0; // Useful for fading
  34. InitWindow(screenWidth, screenHeight, "raylib example 06b - raylib logo animation");
  35. SetTargetFPS(60);
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. if (state == 0) // State 0: Small box blinking
  43. {
  44. framesCounter++;
  45. if (framesCounter == 120)
  46. {
  47. state = 1;
  48. framesCounter = 0; // Reset counter... will be used later...
  49. }
  50. }
  51. else if (state == 1) // State 1: Top and left bars growing
  52. {
  53. topSideRecWidth += 4;
  54. leftSideRecHeight += 4;
  55. if (topSideRecWidth == 256) state = 2;
  56. }
  57. else if (state == 2) // State 2: Bottom and right bars growing
  58. {
  59. bottomSideRecWidth += 4;
  60. rightSideRecHeight += 4;
  61. if (bottomSideRecWidth == 256) state = 3;
  62. }
  63. else if (state == 3) // State 3: Letters appearing (one by one)
  64. {
  65. framesCounter++;
  66. if (framesCounter/12) // Every 12 frames, one more letter!
  67. {
  68. lettersCount++;
  69. framesCounter = 0;
  70. }
  71. switch (lettersCount)
  72. {
  73. case 1: raylib[0] = 'r'; break;
  74. case 2: raylib[1] = 'a'; break;
  75. case 3: raylib[2] = 'y'; break;
  76. case 4: raylib[3] = 'l'; break;
  77. case 5: raylib[4] = 'i'; break;
  78. case 6: raylib[5] = 'b'; break;
  79. default: break;
  80. }
  81. if (lettersCount >= 10) // When all letters have appeared, just fade out everything
  82. {
  83. alpha -= 0.02;
  84. if (alpha <= 0)
  85. {
  86. alpha = 0;
  87. state = 4;
  88. }
  89. }
  90. }
  91. else if (state == 4) // State 4: Reset and Replay
  92. {
  93. if (IsKeyPressed('R'))
  94. {
  95. framesCounter = 0;
  96. lettersCount = 0;
  97. topSideRecWidth = 16;
  98. leftSideRecHeight = 16;
  99. bottomSideRecWidth = 16;
  100. rightSideRecHeight = 16;
  101. for (int i = 0; i < 8; i++) raylib[i] = ' ';
  102. alpha = 1.0;
  103. state = 0; // Return to State 0
  104. }
  105. }
  106. //----------------------------------------------------------------------------------
  107. // Draw
  108. //----------------------------------------------------------------------------------
  109. BeginDrawing();
  110. ClearBackground(RAYWHITE);
  111. if (state == 0)
  112. {
  113. if ((framesCounter/15)%2) DrawRectangle(logoPositionX, logoPositionY, 16, 16, BLACK);
  114. }
  115. else if (state == 1)
  116. {
  117. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
  118. DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
  119. }
  120. else if (state == 2)
  121. {
  122. DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
  123. DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
  124. DrawRectangle(logoPositionX + 240, logoPositionY, 16, rightSideRecHeight, BLACK);
  125. DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, BLACK);
  126. }
  127. else if (state == 3)
  128. {
  129. DrawRectangle(logoPositionX, logoPositionY, 256, 256, Fade(BLACK, alpha));
  130. DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
  131. DrawText(raylib, 356, 273, 50, Fade(BLACK, alpha));
  132. }
  133. else if (state == 4)
  134. {
  135. DrawText("[R] REPLAY", 340, 200, 20, GRAY);
  136. }
  137. EndDrawing();
  138. //----------------------------------------------------------------------------------
  139. }
  140. // De-Initialization
  141. //--------------------------------------------------------------------------------------
  142. CloseWindow(); // Close window and OpenGL context
  143. //--------------------------------------------------------------------------------------
  144. return 0;
  145. }