-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_particles_blending.bas
146 lines (123 loc) · 5.32 KB
/
example_particles_blending.bas
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
'/*******************************************************************************************
'*
'* raylib example - particles blending
'*
'* Example originally created with raylib 1.7, last time updated with raylib 3.5
'*
'* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
'* BSD-like license that allows static linking with closed source software
'*
'* Copyright (c) 2017-2023 Ramon Santamaria (@raysan5)
'*
'********************************************************************************************/
'$INCLUDE:'include/raylib.bi'
CONST MAX_PARTICLES = 200
'// Particle structure with basic data
TYPE Particle
position AS Vector2
colour AS RColor
alpha AS SINGLE
size AS SINGLE
rotation AS SINGLE
active AS _BYTE 'NOTE: Use it to activate/deactive particle
END TYPE
'//------------------------------------------------------------------------------------
'// Program main entry point
'//------------------------------------------------------------------------------------
' // Initialization
' //--------------------------------------------------------------------------------------
CONST screenWidth = 800
CONST screenHeight = 450
InitWindow screenWidth, screenHeight, "raylib [models] example - particles blending"
' // Particles pool, reuse them!
DIM AS Particle mouseTail(0 TO MAX_PARTICLES)
' // Initialize particles
DIM AS Vector2 vZero: vZero.x = 0: vZero.y = 0
DIM AS LONG i
FOR i = 0 TO MAX_PARTICLES - 1
mouseTail(i).position = vZero
mouseTail(i).colour.r = GetRandomValue(0, 255): mouseTail(i).colour.g = GetRandomValue(0, 255): mouseTail(i).colour.b = GetRandomValue(0, 255)
mouseTail(i).alpha = 1.0
mouseTail(i).size = GetRandomValue(1, 30) / 20.0!
mouseTail(i).rotation = GetRandomValue(0, 360)
mouseTail(i).active = FALSE
NEXT
DIM AS SINGLE gravity: gravity = 3.0!
DIM AS Texture smoke: LoadTexture "assets/image/spark_flame.png", smoke
DIM AS LONG blending: blending = BLEND_ALPHA
DIM AS Rectangle sourceRect, destRect
DIM AS Vector2 origin
DIM AS LONG c
SetTargetFPS 60
' //--------------------------------------------------------------------------------------
' // Main game loop
WHILE NOT 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 TO MAX_PARTICLES - 1
IF NOT mouseTail(i).active THEN
mouseTail(i).active = TRUE
mouseTail(i).alpha = 1.0!
GetMousePosition mouseTail(i).position
i = MAX_PARTICLES
END IF
NEXT
FOR i = 0 TO MAX_PARTICLES - 1
IF mouseTail(i).active THEN
mouseTail(i).position.y = mouseTail(i).position.y + (gravity / 2)
mouseTail(i).alpha = mouseTail(i).alpha - 0.005!
IF mouseTail(i).alpha <= 0.0! THEN mouseTail(i).active = FALSE
mouseTail(i).rotation = mouseTail(i).rotation + 2.0!
END IF
NEXT
IF IsKeyPressed(KEY_SPACE) THEN
IF blending = BLEND_ALPHA THEN
blending = BLEND_ADDITIVE
ELSE
blending = BLEND_ALPHA
END IF
END IF
' //----------------------------------------------------------------------------------
' // Draw
' //----------------------------------------------------------------------------------
BeginDrawing
ClearBackground DARKGRAY
BeginBlendMode blending
' // Draw active particles
FOR i = 0 TO MAX_PARTICLES - 1
IF mouseTail(i).active THEN
sourceRect.x = 0
sourceRect.y = 0
sourceRect.Rwidth = smoke.Rwidth
sourceRect.Rheight = smoke.Rheight
destRect.x = mouseTail(i).position.x
destRect.y = mouseTail(i).position.y
destRect.Rwidth = smoke.Rwidth * mouseTail(i).size
destRect.Rheight = smoke.Rheight * mouseTail(i).size
origin.x = smoke.Rwidth * mouseTail(i).size / 2.0!
origin.y = smoke.Rheight * mouseTail(i).size / 2.0!
c = _RGB32(mouseTail(i).colour.r, mouseTail(i).colour.g, mouseTail(i).colour.b)
DrawTexturePro smoke, sourceRect, destRect, origin, mouseTail(i).rotation, Fade(c, mouseTail(i).alpha)
END IF
NEXT
EndBlendMode
DrawText "PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, BLACK
IF blending = BLEND_ALPHA THEN
DrawText "ALPHA BLENDING", 290, screenHeight - 40, 20, BLACK
ELSE
DrawText "ADDITIVE BLENDING", 280, screenHeight - 40, 20, RAYWHITE
END IF
EndDrawing
' //----------------------------------------------------------------------------------
WEND
' // De-Initialization
' //--------------------------------------------------------------------------------------
UnloadTexture smoke
CloseWindow
' //--------------------------------------------------------------------------------------
SYSTEM
'$INCLUDE:'include/raylib.bas'