diff options
| author | raysan5 <raysan5@gmail.com> | 2016-08-06 11:32:35 +0200 |
|---|---|---|
| committer | raysan5 <raysan5@gmail.com> | 2016-08-06 11:32:35 +0200 |
| commit | 00c7e54d3c593dbddb036f2185e614e7e4b22a1f (patch) | |
| tree | 3ef6a46031712ff9551c2711adcc9fa3045a618b /examples/textures_particles_trail_blending.lua | |
| parent | 5f1b4e94745303ab9df87421cdd9ffb9448fee01 (diff) | |
| download | raylib-00c7e54d3c593dbddb036f2185e614e7e4b22a1f.tar.gz raylib-00c7e54d3c593dbddb036f2185e614e7e4b22a1f.zip | |
Add raylib lua examples
Diffstat (limited to 'examples/textures_particles_trail_blending.lua')
| -rw-r--r-- | examples/textures_particles_trail_blending.lua | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/examples/textures_particles_trail_blending.lua b/examples/textures_particles_trail_blending.lua new file mode 100644 index 00000000..d5ba7841 --- /dev/null +++ b/examples/textures_particles_trail_blending.lua @@ -0,0 +1,122 @@ +------------------------------------------------------------------------------------------- +-- +-- raylib example - particles trail blending +-- +-- This example has been created using raylib 1.6 (www.raylib.com) +-- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +-- +-- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5) +-- +------------------------------------------------------------------------------------------- + +MAX_PARTICLES = 200 + +-- Particle structure with basic data +struct.Particle { + position, + color, + alpha, + size, + rotation, + active -- NOTE: Use it to activate/deactive particle +} + +-- Initialization +------------------------------------------------------------------------------------------- +local screenWidth = 800 +local screenHeight = 450 + +InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles trail blending") + +-- Particles pool, reuse them! +local mouseTail = {} + +-- Initialize particles +for i = 1, MAX_PARTICLES do + mouseTail[i].position = Vector2(0, 0) + mouseTail[i].color = Color(GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255) + mouseTail[i].alpha = 1.0 + mouseTail[i].size = GetRandomValue(1, 30)/20.0 + mouseTail[i].rotation = GetRandomValue(0, 360) + mouseTail[i].active = false +end + +local gravity = 3.0 + +local smoke = LoadTexture("resources/smoke.png") + +local blending = BLEND.ALPHA + +SetTargetFPS(60) +------------------------------------------------------------------------------------------- + +-- Main game loop +while not WindowShouldClose() do -- 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 = 1, MAX_PARTICLES do + if (not mouseTail[i].active) then + mouseTail[i].active = true + mouseTail[i].alpha = 1.0 + mouseTail[i].position = GetMousePosition() + i = MAX_PARTICLES + end + end + + for i = 1, MAX_PARTICLES do + if (mouseTail[i].active) then + mouseTail[i].position.y = mouseTail[i].position.y + gravity + mouseTail[i].alpha = mouseTail[i].alpha - 0.01 + + if (mouseTail[i].alpha <= 0.0) then mouseTail[i].active = false end + + mouseTail[i].rotation = mouseTail[i].rotation + 5.0 + end + end + + if (IsKeyPressed(KEY.SPACE)) then + if (blending == BLEND.ALPHA) then blending = BLEND_ADDITIVE + else blending = BLEND.ALPHA end + end + --------------------------------------------------------------------------------------- + + -- Draw + --------------------------------------------------------------------------------------- + BeginDrawing() + + ClearBackground(DARKGRAY) + + BeginBlendMode(blending) + + -- Draw active particles + for i = 1, MAX_PARTICLES do + if (mouseTail[i].active) then + DrawTexturePro(smoke, Rectangle(0, 0, smoke.width, smoke.height), + Rectangle(mouseTail[i].position.x, mouseTail[i].position.y, + smoke.width*mouseTail[i].size, smoke.height*mouseTail[i].size), + Vector2(smoke.width*mouseTail[i].size/2, smoke.height*mouseTail[i].size/2), + mouseTail[i].rotation, Fade(mouseTail[i].color, mouseTail[i].alpha)) end + end + + 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 + + EndDrawing() + --------------------------------------------------------------------------------------- +end + +-- De-Initialization +------------------------------------------------------------------------------------------- +UnloadTexture(smoke) + +CloseWindow() -- Close window and OpenGL context +-------------------------------------------------------------------------------------------
\ No newline at end of file |
