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