-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextures_particles_blending.php
181 lines (151 loc) · 5.56 KB
/
textures_particles_blending.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
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\{Color, Rectangle, Vector2};
use function Nawarian\Raylib\{
BeginBlendMode,
BeginDrawing,
ClearBackground,
CloseWindow,
DrawText,
DrawTexturePro,
EndBlendMode,
EndDrawing,
Fade,
GetMousePosition,
GetRandomValue,
InitWindow,
IsKeyPressed,
LoadTexture,
SetTargetFPS,
UnloadTexture,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
const MAX_PARTICLES = 200;
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [textures] example - particles blending');
// Particles pool, reuse them!
$mouseTail = [];
// Initialize particles
foreach (range(0, MAX_PARTICLES) as $i) {
$r = GetRandomValue(0, 255);
$g = GetRandomValue(0, 255);
$b = GetRandomValue(0, 255);
$mouseTail[$i] = new class (
new Vector2(0.0, 0.0),
new Color($r, $g, $b, 255),
1.0,
(float) GetRandomValue(1, 30) / 20.0,
(float) GetRandomValue(0, 360),
false
) {
public Vector2 $position;
public Color $color;
public float $alpha;
public float $size;
public float $rotation;
public bool $active; // NOTE: Use it to activate/deactive particle
public function __construct(
Vector2 $position,
Color $color,
float $alpha,
float $size,
float $rotation,
bool $active
) {
$this->position = $position;
$this->color = $color;
$this->alpha = $alpha;
$this->size = $size;
$this->rotation = $rotation;
$this->active = $active;
}
};
}
$gravity = 3.0;
$smoke = LoadTexture(__DIR__ . '/resources/spark_flame.png');
$blending = Raylib::BLEND_ALPHA;
SetTargetFPS(60);
//-------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
// Activate one particle every frame and Update active particles
// NOTE: Particles initial position should be mouse position when activated
// NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0)
// NOTE: When a particle disappears, active = false and it can be reused.
for ($i = 0; $i < MAX_PARTICLES; $i++) {
if (!$mouseTail[$i]->active) {
$mouseTail[$i]->active = true;
$mouseTail[$i]->alpha = 1.0;
$mouseTail[$i]->position = GetMousePosition();
/**
* @psalm-suppress LoopInvalidation
*/
$i = MAX_PARTICLES;
}
}
for ($i = 0; $i < MAX_PARTICLES; $i++) {
if ($mouseTail[$i]->active) {
$mouseTail[$i]->position->y += $gravity / 2;
$mouseTail[$i]->alpha -= 0.005;
if ($mouseTail[$i]->alpha <= 0.0) {
$mouseTail[$i]->active = false;
}
$mouseTail[$i]->rotation += 2.0;
}
}
if (IsKeyPressed(Raylib::KEY_SPACE)) {
if ($blending === Raylib::BLEND_ALPHA) {
$blending = Raylib::BLEND_ADDITIVE;
} else {
$blending = Raylib::BLEND_ALPHA;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color::darkGray());
BeginBlendMode($blending);
//phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
for ($i = 0; $i < MAX_PARTICLES; $i++) {
if ($mouseTail[$i]->active) {
DrawTexturePro(
$smoke,
new Rectangle(0.0, 0.0, (float) $smoke->width, (float) $smoke->height),
new Rectangle(
$mouseTail[$i]->position->x,
$mouseTail[$i]->position->y,
(float) $smoke->width * $mouseTail[$i]->size,
(float) $smoke->height * $mouseTail[$i]->size
),
new Vector2(
(float) $smoke->width * $mouseTail[$i]->size / 2.0,
(float) $smoke->height * $mouseTail[$i]->size / 2.0
),
$mouseTail[$i]->rotation,
Fade($mouseTail[$i]->color, $mouseTail[$i]->alpha)
);
}
}
EndBlendMode();
DrawText('PRESS SPACE to CHANGE BLENDING MODE', 180, 20, 20, Color::black());
if ($blending == Raylib::BLEND_ALPHA) {
DrawText('ALPHA BLENDING', 290, $screenHeight - 40, 20, Color::black());
} else {
DrawText('ADDITIVE BLENDING', 280, $screenHeight - 40, 20, Color::rayWhite());
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture($smoke);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------