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.

179 lines
6.1 KiB

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