Skip to content

Commit

Permalink
Add remaining shapes easing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 6b4d109 commit 3ea6740
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 7 deletions.
16 changes: 9 additions & 7 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ Examples using raylib text functionality, including sprite fonts loading/generat

Examples using raylib shapes drawing functionality.

| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|----------------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 31 | [shapes_basic_shapes](shapes/shapes_basic_shapes.rb) | <img src="shapes/shapes_basic_shapes.png" alt="shapes_basic_shapes" width="80"> | ⭐️☆☆☆ | 1.0 | **4.0** | [Ray](https://github.com/raysan5) |
| 32 | [shapes_bouncing_ball](shapes/shapes_bouncing_ball.rb) | <img src="shapes/shapes_bouncing_ball.png" alt="shapes_bouncing_ball" width="80"> | ⭐️☆☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 33 | [shapes_colors_palette](shapes/shapes_colors_palette.rb) | <img src="shapes/shapes_colors_palette.png" alt="shapes_colors_palette" width="80"> | ⭐️⭐️☆☆ | 1.0 | 2.5 | [Ray](https://github.com/raysan5) |
| 38 | [shapes_collision_area](shapes/shapes_collision_area.rb) | <img src="shapes/shapes_collision_area.png" alt="shapes_collision_area" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 40 | [shapes_easings_ball_anim](shapes/shapes_easings_ball_anim.rb) | <img src="shapes/shapes_easings_ball_anim.png" alt="shapes_easings_ball_anim" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|----------------------------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 31 | [shapes_basic_shapes](shapes/shapes_basic_shapes.rb) | <img src="shapes/shapes_basic_shapes.png" alt="shapes_basic_shapes" width="80"> | ⭐️☆☆☆ | 1.0 | **4.0** | [Ray](https://github.com/raysan5) |
| 32 | [shapes_bouncing_ball](shapes/shapes_bouncing_ball.rb) | <img src="shapes/shapes_bouncing_ball.png" alt="shapes_bouncing_ball" width="80"> | ⭐️☆☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 33 | [shapes_colors_palette](shapes/shapes_colors_palette.rb) | <img src="shapes/shapes_colors_palette.png" alt="shapes_colors_palette" width="80"> | ⭐️⭐️☆☆ | 1.0 | 2.5 | [Ray](https://github.com/raysan5) |
| 38 | [shapes_collision_area](shapes/shapes_collision_area.rb) | <img src="shapes/shapes_collision_area.png" alt="shapes_collision_area" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 40 | [shapes_easings_ball_anim](shapes/shapes_easings_ball_anim.rb) | <img src="shapes/shapes_easings_ball_anim.png" alt="shapes_easings_ball_anim" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 41 | [shapes_easings_box_anim](shapes/shapes_easings_box_anim.rb) | <img src="shapes/shapes_easings_box_anim.png" alt="shapes_easings_box_anim" width="80"> | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
| 42 | [shapes_easings_rectangle_array](shapes/shapes_easings_rectangle_array.rb) | <img src="shapes/shapes_easings_rectangle_array.png" alt="shapes_easings_rectangle_array" width="80"> | ⭐️⭐️⭐️☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) |
Binary file added examples/shapes/shapes_easings_box_anim.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 116 additions & 0 deletions examples/shapes/shapes_easings_box_anim.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# ******************************************************************************************
#
# raylib [shapes] example - easings box anim
#
# Example originally created with raylib 2.5, last time updated with raylib 2.5
#
# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.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) 2014-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

require_relative 'reasings' # Required for easing functions

# Initialization
# --------------------------------------------------------------------------------------
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 450

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [shapes] example - easings box anim')

# Box variables to be animated with easings
rec = Raylib::Rectangle.create(Raylib.get_screen_width / 2.0, -100, 100, 100)
rotation = 0.0
alpha = 1.0

state = 0
frames_counter = 0

Raylib.set_target_fps(60) # Set our game to run at 60 frames-per-second
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close # Detect window close button or ESC key
# Update
# ----------------------------------------------------------------------------------
case state
when 0 # Move box down to center of screen
frames_counter += 1

# NOTE: Remember that 3rd parameter of easing function refers to
# desired value variation, do not confuse it with expected final value!
rec.y = Reasings.ease_elastic_out(frames_counter.to_f, -100, (Raylib.get_screen_height / 2.0) + 100, 120)

if frames_counter >= 120
frames_counter = 0
state = 1
end
when 1 # Scale box to an horizontal bar
frames_counter += 1
rec.height = Reasings.ease_bounce_out(frames_counter.to_f, 100, -90, 120)
rec.width = Reasings.ease_bounce_out(frames_counter.to_f, 100, Raylib.get_screen_width.to_f, 120)

if frames_counter >= 120
frames_counter = 0
state = 2
end
when 2 # Rotate horizontal bar rectangle
frames_counter += 1
rotation = Reasings.ease_quad_out(frames_counter.to_f, 0.0, 270.0, 240)

if frames_counter >= 240
frames_counter = 0
state = 3
end
when 3 # Increase bar size to fill all screen
frames_counter += 1
rec.height = Reasings.ease_circ_out(frames_counter.to_f, 10, Raylib.get_screen_width.to_f, 120)

if frames_counter >= 120
frames_counter = 0
state = 4
end
when 4 # Fade out animation
frames_counter += 1
alpha = Reasings.ease_sine_out(frames_counter.to_f, 1.0, -1.0, 160)

if frames_counter >= 160
frames_counter = 0
state = 5
end
end

# Reset animation at any moment
if Raylib.is_key_pressed(Raylib::KEY_SPACE)
rec = Raylib::Rectangle.create(Raylib.get_screen_width / 2.0, -100, 100, 100)
rotation = 0.0
alpha = 1.0
state = 0
frames_counter = 0
end
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)

Raylib.draw_rectangle_pro(rec, Raylib::Vector2.create(rec.width / 2, rec.height / 2), rotation, Raylib.fade(Raylib::BLACK, alpha))

Raylib.draw_text('PRESS [SPACE] TO RESET BOX ANIMATION!', 10, Raylib.get_screen_height - 25, 20, Raylib::LIGHTGRAY)

Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.close_window # Close window and OpenGL context
# --------------------------------------------------------------------------------------
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 110 additions & 0 deletions examples/shapes/shapes_easings_rectangle_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# ******************************************************************************************
#
# raylib [shapes] example - easings rectangle array
#
# NOTE: This example requires 'easings.h' library, provided on raylib/src. Just copy
# the library to same directory as example or make sure it's available on include path.
#
# Example originally created with raylib 2.0, last time updated with raylib 2.5
#
# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.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) 2014-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

require_relative 'reasings' # Required for easing functions

RECS_WIDTH = 50
RECS_HEIGHT = 50
MAX_RECS_X = 800 / RECS_WIDTH
MAX_RECS_Y = 450 / RECS_HEIGHT
PLAY_TIME_IN_FRAMES = 240 # At 60 fps = 4 seconds

# Initialization
# --------------------------------------------------------------------------------------
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 450

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [shapes] example - easings rectangle array")

recs = Array.new(MAX_RECS_X * MAX_RECS_Y) { Raylib::Rectangle.new }

(MAX_RECS_Y).times do |y|
(MAX_RECS_X).times do |x|
recs[y * MAX_RECS_X + x].x = RECS_WIDTH / 2.0 + RECS_WIDTH * x
recs[y * MAX_RECS_X + x].y = RECS_HEIGHT / 2.0 + RECS_HEIGHT * y
recs[y * MAX_RECS_X + x].width = RECS_WIDTH
recs[y * MAX_RECS_X + x].height = RECS_HEIGHT
end
end

rotation = 0.0
frames_counter = 0
state = 0 # Rectangles animation state: 0-Playing, 1-Finished

Raylib.set_target_fps(60) # Set our game to run at 60 frames-per-second
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close # Detect window close button or ESC key
# Update
# ----------------------------------------------------------------------------------
if state.zero?
frames_counter += 1
(MAX_RECS_X * MAX_RECS_Y).times do |i|
recs[i].height = Reasings.ease_circ_out(frames_counter.to_f, RECS_HEIGHT, -RECS_HEIGHT, PLAY_TIME_IN_FRAMES)
recs[i].width = Reasings.ease_circ_out(frames_counter.to_f, RECS_WIDTH, -RECS_WIDTH, PLAY_TIME_IN_FRAMES)

recs[i].height = 0 if recs[i].height.negative?
recs[i].width = 0 if recs[i].width.negative?

state = 1 if recs[i].height.zero? && recs[i].width.zero? # Finish playing

rotation = Reasings.ease_linear_in(frames_counter.to_f, 0.0, 360.0, PLAY_TIME_IN_FRAMES)
end
elsif state == 1 && Raylib.is_key_pressed(Raylib::KEY_SPACE)
# When animation has finished, press space to restart
frames_counter = 0

(MAX_RECS_X * MAX_RECS_Y).times do |i|
recs[i].height = RECS_HEIGHT
recs[i].width = RECS_WIDTH
end

state = 0
end
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)

if state.zero?
(MAX_RECS_X * MAX_RECS_Y).times do |i|
Raylib.draw_rectangle_pro(
recs[i],
Raylib::Vector2.create(recs[i].width / 2, recs[i].height / 2),
rotation,
Raylib::RED
)
end
elsif state == 1
Raylib.draw_text("PRESS [SPACE] TO PLAY AGAIN!", 240, 200, 20, Raylib::GRAY)
end

Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.close_window # Close window and OpenGL context
# --------------------------------------------------------------------------------------

0 comments on commit 3ea6740

Please sign in to comment.