1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shapes] example - easings rectangle array |
4 | * |
5 | * NOTE: This example requires 'easings.h' library, provided on raylib/src. Just copy |
6 | * the library to same directory as example or make sure it's available on include path. |
7 | * |
8 | * This example has been created using raylib 2.0 (www.raylib.com) |
9 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
10 | * |
11 | * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) |
12 | * |
13 | ********************************************************************************************/ |
14 | |
15 | #include "raylib.h" |
16 | |
17 | #include "easings.h" // Required for easing functions |
18 | |
19 | #define RECS_WIDTH 50 |
20 | #define RECS_HEIGHT 50 |
21 | |
22 | #define MAX_RECS_X 800/RECS_WIDTH |
23 | #define MAX_RECS_Y 450/RECS_HEIGHT |
24 | |
25 | #define PLAY_TIME_IN_FRAMES 240 // At 60 fps = 4 seconds |
26 | |
27 | int main(void) |
28 | { |
29 | // Initialization |
30 | //-------------------------------------------------------------------------------------- |
31 | const int screenWidth = 800; |
32 | const int screenHeight = 450; |
33 | |
34 | InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings rectangle array" ); |
35 | |
36 | Rectangle recs[MAX_RECS_X*MAX_RECS_Y] = { 0 }; |
37 | |
38 | for (int y = 0; y < MAX_RECS_Y; y++) |
39 | { |
40 | for (int x = 0; x < MAX_RECS_X; x++) |
41 | { |
42 | recs[y*MAX_RECS_X + x].x = RECS_WIDTH/2 + RECS_WIDTH*x; |
43 | recs[y*MAX_RECS_X + x].y = RECS_HEIGHT/2 + RECS_HEIGHT*y; |
44 | recs[y*MAX_RECS_X + x].width = RECS_WIDTH; |
45 | recs[y*MAX_RECS_X + x].height = RECS_HEIGHT; |
46 | } |
47 | } |
48 | |
49 | float rotation = 0.0f; |
50 | int framesCounter = 0; |
51 | int state = 0; // Rectangles animation state: 0-Playing, 1-Finished |
52 | |
53 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
54 | //-------------------------------------------------------------------------------------- |
55 | |
56 | // Main game loop |
57 | while (!WindowShouldClose()) // Detect window close button or ESC key |
58 | { |
59 | // Update |
60 | //---------------------------------------------------------------------------------- |
61 | if (state == 0) |
62 | { |
63 | framesCounter++; |
64 | |
65 | for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++) |
66 | { |
67 | recs[i].height = EaseCircOut(framesCounter, RECS_HEIGHT, -RECS_HEIGHT, PLAY_TIME_IN_FRAMES); |
68 | recs[i].width = EaseCircOut(framesCounter, RECS_WIDTH, -RECS_WIDTH, PLAY_TIME_IN_FRAMES); |
69 | |
70 | if (recs[i].height < 0) recs[i].height = 0; |
71 | if (recs[i].width < 0) recs[i].width = 0; |
72 | |
73 | if ((recs[i].height == 0) && (recs[i].width == 0)) state = 1; // Finish playing |
74 | |
75 | rotation = EaseLinearIn(framesCounter, 0.0f, 360.0f, PLAY_TIME_IN_FRAMES); |
76 | } |
77 | } |
78 | else if ((state == 1) && IsKeyPressed(KEY_SPACE)) |
79 | { |
80 | // When animation has finished, press space to restart |
81 | framesCounter = 0; |
82 | |
83 | for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++) |
84 | { |
85 | recs[i].height = RECS_HEIGHT; |
86 | recs[i].width = RECS_WIDTH; |
87 | } |
88 | |
89 | state = 0; |
90 | } |
91 | //---------------------------------------------------------------------------------- |
92 | |
93 | // Draw |
94 | //---------------------------------------------------------------------------------- |
95 | BeginDrawing(); |
96 | |
97 | ClearBackground(RAYWHITE); |
98 | |
99 | if (state == 0) |
100 | { |
101 | for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++) |
102 | { |
103 | DrawRectanglePro(recs[i], (Vector2){ recs[i].width/2, recs[i].height/2 }, rotation, RED); |
104 | } |
105 | } |
106 | else if (state == 1) DrawText("PRESS [SPACE] TO PLAY AGAIN!" , 240, 200, 20, GRAY); |
107 | |
108 | EndDrawing(); |
109 | //---------------------------------------------------------------------------------- |
110 | } |
111 | |
112 | // De-Initialization |
113 | //-------------------------------------------------------------------------------------- |
114 | CloseWindow(); // Close window and OpenGL context |
115 | //-------------------------------------------------------------------------------------- |
116 | |
117 | return 0; |
118 | } |