Skip to content

Commit

Permalink
Add shapes colors palette example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent f262ad7 commit ac55269
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ Examples using raylib shapes drawing functionality.
|----|----------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 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) |
Binary file added examples/shapes/shapes_colors_palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions examples/shapes/shapes_colors_palette.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# ******************************************************************************************
#
# raylib [shapes] example - Colors palette
#
# Example originally created with raylib 1.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'

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

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [shapes] example - colors palette')

MAX_COLORS_COUNT = 21 # Number of colors available

colors = [
Raylib::DARKGRAY, Raylib::MAROON, Raylib::ORANGE, Raylib::DARKGREEN, Raylib::DARKBLUE, Raylib::DARKPURPLE,
Raylib::DARKBROWN, Raylib::GRAY, Raylib::RED, Raylib::GOLD, Raylib::LIME, Raylib::BLUE, Raylib::VIOLET, Raylib::BROWN,
Raylib::LIGHTGRAY, Raylib::PINK, Raylib::YELLOW, Raylib::GREEN, Raylib::SKYBLUE, Raylib::PURPLE, Raylib::BEIGE
]

COLOR_NAMES = %w[
DARKGRAY MAROON ORANGE DARKGREEN DARKBLUE DARKPURPLE DARKBROWN GRAY RED GOLD LIME
BLUE VIOLET BROWN LIGHTGRAY PINK YELLOW GREEN SKYBLUE PURPLE BEIGE
].freeze

colors_recs = Array.new(MAX_COLORS_COUNT) do |i|
Raylib::Rectangle.create(
20.0 + 100.0 * (i % 7) + 10.0 * (i % 7),
80.0 + 100.0 * (i / 7) + 10.0 * (i / 7),
100.0,
100.0
)
end

color_state = Array.new(MAX_COLORS_COUNT, 0) # Color state: 0-DEFAULT, 1-MOUSE_HOVER

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
# ----------------------------------------------------------------------------------
mouse_point = Raylib.get_mouse_position

MAX_COLORS_COUNT.times do |i|
color_state[i] = Raylib.check_collision_point_rec(mouse_point, colors_recs[i]) ? 1 : 0
end
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)
Raylib.draw_text('raylib colors palette', 28, 42, 20, Raylib::BLACK)
Raylib.draw_text(
'press SPACE to see all colors',
Raylib.get_screen_width - 180,
Raylib.get_screen_height - 40, 10, Raylib::GRAY
)

MAX_COLORS_COUNT.times do |i| # Draw all rectangles
Raylib.draw_rectangle_rec(colors_recs[i], Raylib.fade(colors[i], color_state[i] == 1 ? 0.6 : 1.0))

if Raylib.is_key_down(Raylib::KEY_SPACE) || color_state[i] == 1
Raylib.draw_rectangle(
colors_recs[i].x, colors_recs[i].y + colors_recs[i].height - 26,
colors_recs[i].width, 20, Raylib::BLACK
)
Raylib.draw_rectangle_lines_ex(colors_recs[i], 6, Raylib.fade(Raylib::BLACK, 0.3))
Raylib.draw_text(
COLOR_NAMES[i], colors_recs[i].x + colors_recs[i].width - Raylib.measure_text(COLOR_NAMES[i], 10) - 12,
colors_recs[i].y + colors_recs[i].height - 20, 10, colors[i]
)
end
end

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

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

0 comments on commit ac55269

Please sign in to comment.