-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshapes_logo_raylib_anim.php
173 lines (143 loc) · 5.7 KB
/
shapes_logo_raylib_anim.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\Color;
use function Nawarian\Raylib\{
BeginDrawing,
ClearBackground,
CloseWindow,
DrawRectangle,
DrawText,
EndDrawing,
Fade,
InitWindow,
IsKeyPressed,
SetTargetFPS,
TextSubtext,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [shapes] example - raylib logo animation');
$logoPositionX = $screenWidth / 2 - 128;
$logoPositionY = $screenHeight / 2 - 128;
$framesCounter = 0;
$lettersCount = 0;
$topSideRecWidth = 16;
$leftSideRecHeight = 16;
$bottomSideRecWidth = 16;
$rightSideRecHeight = 16;
$state = 0; // Tracking animation states (State Machine)
$alpha = 1.0; // Useful for fading
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
if ($state == 0) { // State 0: Small box blinking
$framesCounter++;
if ($framesCounter === 120) {
$state = 1;
$framesCounter = 0; // Reset counter... will be used later...
}
} elseif ($state === 1) { // State 1: Top and left bars growing
$topSideRecWidth += 4;
$leftSideRecHeight += 4;
if ($topSideRecWidth === 256) {
$state = 2;
}
} elseif ($state === 2) { // State 2: Bottom and right bars growing
$bottomSideRecWidth += 4;
$rightSideRecHeight += 4;
if ($bottomSideRecWidth === 256) {
$state = 3;
}
} elseif ($state === 3) { // State 3: Letters appearing (one by one)
$framesCounter++;
if ($framesCounter / 12) { // Every 12 frames, one more letter!
$lettersCount++;
$framesCounter = 0;
}
if ($lettersCount >= 10) { // When all letters have appeared, just fade out everything
$alpha -= 0.02;
if ($alpha <= 0.0) {
$alpha = 0.0;
$state = 4;
}
}
} else { // State 4: Reset and Replay
if (IsKeyPressed(Raylib::KEY_R)) {
$framesCounter = 0;
$lettersCount = 0;
$topSideRecWidth = 16;
$leftSideRecHeight = 16;
$bottomSideRecWidth = 16;
$rightSideRecHeight = 16;
$alpha = 1.0;
$state = 0; // Return to State 0
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
ClearBackground(Color::rayWhite());
if ($state === 0) {
if (($framesCounter / 15) % 2) {
DrawRectangle($logoPositionX, $logoPositionY, 16, 16, Color::black());
}
} elseif ($state === 1) {
DrawRectangle($logoPositionX, $logoPositionY, $topSideRecWidth, 16, Color::black());
DrawRectangle($logoPositionX, $logoPositionY, 16, $leftSideRecHeight, Color::black());
} elseif ($state === 2) {
DrawRectangle($logoPositionX, $logoPositionY, $topSideRecWidth, 16, Color::black());
DrawRectangle($logoPositionX, $logoPositionY, 16, $leftSideRecHeight, Color::black());
DrawRectangle($logoPositionX + 240, $logoPositionY, 16, $rightSideRecHeight, Color::black());
DrawRectangle($logoPositionX, $logoPositionY + 240, $bottomSideRecWidth, 16, Color::black());
} elseif ($state === 3) {
DrawRectangle($logoPositionX, $logoPositionY, $topSideRecWidth, 16, Fade(Color::black(), $alpha));
DrawRectangle(
$logoPositionX,
$logoPositionY + 16,
16,
$leftSideRecHeight - 32,
Fade(Color::black(), $alpha)
);
DrawRectangle(
$logoPositionX + 240,
$logoPositionY + 16,
16,
$rightSideRecHeight - 32,
Fade(Color::black(), $alpha)
);
DrawRectangle($logoPositionX, $logoPositionY + 240, $bottomSideRecWidth, 16, Fade(Color::black(), $alpha));
DrawRectangle(
(int) ($screenWidth / 2 - 112),
(int) ($screenHeight / 2 - 112),
224,
224,
Fade(Color::rayWhite(), $alpha)
);
DrawText(
TextSubtext('raylib', 0, $lettersCount),
(int) ($screenWidth / 2 - 44),
(int) ($screenHeight / 2 + 48),
50,
Fade(Color::black(), $alpha)
);
} else {
DrawText('[R] REPLAY', 340, 200, 20, Color::gray());
}
// phpcs:enable Generic.WhiteSpace.ScopeIndent.IncorrectExact
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------