Skip to content

Commit

Permalink
Add textures image generation example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 24, 2023
1 parent a833202 commit 9ea29fc
Show file tree
Hide file tree
Showing 3 changed files with 93 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 @@ -97,4 +97,5 @@ Examples using raylib textures functionality, including image/textures loading/g
| 47 | [textures_logo_raylib](textures/textures_logo_raylib.rb) | <img src="textures/textures_logo_raylib.png" alt="textures_logo_raylib" width="80"> | ⭐️☆☆☆ | 1.0 | 1.0 | [Ray](https://github.com/raysan5) |
| 48 | [textures_srcrec_dstrec](textures/textures_srcrec_dstrec.rb) | <img src="textures/textures_srcrec_dstrec.png" alt="textures_srcrec_dstrec" width="80"> | ⭐️⭐️⭐️☆ | 1.3 | 1.3 | [Ray](https://github.com/raysan5) |
| 49 | [textures_image_drawing](textures/textures_image_drawing.rb) | <img src="textures/textures_image_drawing.png" alt="textures_image_drawing" width="80"> | ⭐️⭐️☆☆ | 1.4 | 1.4 | [Ray](https://github.com/raysan5) |
| 50 | [textures_image_generation](textures/textures_image_generation.rb) | <img src="textures/textures_image_generation.png" alt="textures_image_generation" width="80"> | ⭐️⭐️☆☆ | 1.8 | 1.8 | [Ray](https://github.com/raysan5) |
| 58 | [textures_background_scrolling](textures/textures_background_scrolling.rb) | <img src="textures/textures_background_scrolling.png" alt="textures_background_scrolling" width="80"> | ⭐️☆☆☆ | 2.0 | 2.5 | [Ray](https://github.com/raysan5) |
Binary file added examples/textures/textures_image_generation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions examples/textures/textures_image_generation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# ******************************************************************************************
#
# raylib [textures] example - Procedural images generation
#
# Example originally created with raylib 1.8, last time updated with raylib 1.8
#
# 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) 2017-2023 Wilhem Barbier (@nounoursheureux) and Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

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

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [textures] example - procedural images generation')

NUM_TEXTURES = 6 # Currently we have 7 generation algorithms

vertical_gradient = Raylib.gen_image_gradient_v(SCREEN_WIDTH, SCREEN_HEIGHT, Raylib::RED, Raylib::BLUE)
horizontal_gradient = Raylib.gen_image_gradient_h(SCREEN_WIDTH, SCREEN_HEIGHT, Raylib::RED, Raylib::BLUE)
radial_gradient = Raylib.gen_image_gradient_radial(SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, Raylib::WHITE, Raylib::BLACK)
checked = Raylib.gen_image_checked(SCREEN_WIDTH, SCREEN_HEIGHT, 32, 32, Raylib::RED, Raylib::BLUE)
white_noise = Raylib.gen_image_white_noise(SCREEN_WIDTH, SCREEN_HEIGHT, 0.5)
cellular = Raylib.gen_image_cellular(SCREEN_WIDTH, SCREEN_HEIGHT, 32)

textures = [
Raylib.load_texture_from_image(vertical_gradient),
Raylib.load_texture_from_image(horizontal_gradient),
Raylib.load_texture_from_image(radial_gradient),
Raylib.load_texture_from_image(checked),
Raylib.load_texture_from_image(white_noise),
Raylib.load_texture_from_image(cellular)
]

# Unload image data (CPU RAM)
[vertical_gradient, horizontal_gradient, radial_gradient, checked, white_noise, cellular].each do |image|
Raylib.unload_image(image)
end

current_texture = 0

Raylib.set_target_fps(60)
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close
# Update
# ----------------------------------------------------------------------------------
if Raylib.is_mouse_button_pressed(Raylib::MOUSE_BUTTON_LEFT) || Raylib.is_key_pressed(Raylib::KEY_RIGHT)
current_texture = (current_texture + 1) % NUM_TEXTURES # Cycle between the textures
end
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)
Raylib.draw_texture(textures[current_texture], 0, 0, Raylib::WHITE)
Raylib.draw_rectangle(30, 400, 325, 30, Raylib.fade(Raylib::SKYBLUE, 0.5))
Raylib.draw_rectangle_lines(30, 400, 325, 30, Raylib.fade(Raylib::WHITE, 0.5))
Raylib.draw_text('MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES', 40, 410, 10, Raylib::WHITE)

case current_texture
when 0; Raylib.draw_text('VERTICAL GRADIENT', 560, 10, 20, Raylib::RAYWHITE)
when 1; Raylib.draw_text('HORIZONTAL GRADIENT', 540, 10, 20, Raylib::RAYWHITE)
when 2; Raylib.draw_text('RADIAL GRADIENT', 580, 10, 20, Raylib::LIGHTGRAY)
when 3; Raylib.draw_text('CHECKED', 680, 10, 20, Raylib::RAYWHITE)
when 4; Raylib.draw_text('WHITE NOISE', 640, 10, 20, Raylib::RED)
when 5; Raylib.draw_text('CELLULAR', 670, 10, 20, Raylib::RAYWHITE)
end

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

# De-Initialization
# --------------------------------------------------------------------------------------

# Unload textures data (GPU VRAM)
textures.each { |texture| Raylib.unload_texture(texture) }

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

0 comments on commit 9ea29fc

Please sign in to comment.