diff --git a/examples/README.md b/examples/README.md index 305e3de..3f8e00f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -87,3 +87,11 @@ Examples using raylib shapes drawing functionality. | 41 | [shapes_easings_box_anim](shapes/shapes_easings_box_anim.rb) | shapes_easings_box_anim | ⭐️⭐️☆☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) | | 42 | [shapes_easings_rectangle_array](shapes/shapes_easings_rectangle_array.rb) | shapes_easings_rectangle_array | ⭐️⭐️⭐️☆ | 2.5 | 2.5 | [Ray](https://github.com/raysan5) | | 46 | [shapes_top_down_lights](shapes/shapes_top_down_lights.rb) | shapes_top_down_lights | ⭐️⭐️⭐️⭐️ | **4.2** | **4.2** | [Jeffery Myers](https://github.com/JeffM2501) | + +## Textures + +Examples using raylib textures functionality, including image/textures loading/generation and drawing. + +| ## | example | image | difficulty
level | version
created | last version
updated | original
developer | +|----|----------------------------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------| +| 58 | [textures_background_scrolling](textures/textures_background_scrolling.rb) | textures_background_scrolling | ⭐️☆☆☆ | 2.0 | 2.5 | [Ray](https://github.com/raysan5) | diff --git a/examples/textures/resources/cyberpunk_street_background.png b/examples/textures/resources/cyberpunk_street_background.png new file mode 100644 index 0000000..838d08a Binary files /dev/null and b/examples/textures/resources/cyberpunk_street_background.png differ diff --git a/examples/textures/resources/cyberpunk_street_foreground.png b/examples/textures/resources/cyberpunk_street_foreground.png new file mode 100644 index 0000000..528b4ae Binary files /dev/null and b/examples/textures/resources/cyberpunk_street_foreground.png differ diff --git a/examples/textures/resources/cyberpunk_street_midground.png b/examples/textures/resources/cyberpunk_street_midground.png new file mode 100644 index 0000000..73f24fe Binary files /dev/null and b/examples/textures/resources/cyberpunk_street_midground.png differ diff --git a/examples/textures/textures_background_scrolling.png b/examples/textures/textures_background_scrolling.png new file mode 100644 index 0000000..d21e269 Binary files /dev/null and b/examples/textures/textures_background_scrolling.png differ diff --git a/examples/textures/textures_background_scrolling.rb b/examples/textures/textures_background_scrolling.rb new file mode 100644 index 0000000..b7d746c --- /dev/null +++ b/examples/textures/textures_background_scrolling.rb @@ -0,0 +1,85 @@ +# ****************************************************************************************** +# +# raylib [textures] example - Background scrolling +# +# 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) 2019-2023 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 - background scrolling') + +# NOTE: Be careful, background width must be equal or bigger than screen width +# if not, texture should be draw more than two times for scrolling effect +background = Raylib.load_texture(File.join(__dir__, 'resources/cyberpunk_street_background.png')) +midground = Raylib.load_texture(File.join(__dir__, 'resources/cyberpunk_street_midground.png')) +foreground = Raylib.load_texture(File.join(__dir__, 'resources/cyberpunk_street_foreground.png')) + +scrolling_back = 0.0 +scrolling_mid = 0.0 +scrolling_fore = 0.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 + # ---------------------------------------------------------------------------------- + scrolling_back -= 0.1 + scrolling_mid -= 0.5 + scrolling_fore -= 1.0 + + # NOTE: Texture is scaled twice its size, so it sould be considered on scrolling + scrolling_back = 0 if scrolling_back <= -background.width * 2 + scrolling_mid = 0 if scrolling_mid <= -midground.width * 2 + scrolling_fore = 0 if scrolling_fore <= -foreground.width * 2 + # ---------------------------------------------------------------------------------- + + # Draw + # ---------------------------------------------------------------------------------- + Raylib.begin_drawing + Raylib.clear_background(Raylib.get_color(0x052c46ff)) + + # Draw background image twice + # NOTE: Texture is scaled twice its size + Raylib.draw_texture_ex(background, Raylib::Vector2.create(scrolling_back, 20), 0.0, 2.0, Raylib::WHITE) + Raylib.draw_texture_ex(background, Raylib::Vector2.create(background.width * 2 + scrolling_back, 20), 0.0, 2.0, Raylib::WHITE) + + # Draw midground image twice + Raylib.draw_texture_ex(midground, Raylib::Vector2.create(scrolling_mid, 20), 0.0, 2.0, Raylib::WHITE) + Raylib.draw_texture_ex(midground, Raylib::Vector2.create(midground.width * 2 + scrolling_mid, 20), 0.0, 2.0, Raylib::WHITE) + + # Draw foreground image twice + Raylib.draw_texture_ex(foreground, Raylib::Vector2.create(scrolling_fore, 70), 0.0, 2.0, Raylib::WHITE) + Raylib.draw_texture_ex(foreground, Raylib::Vector2.create(foreground.width * 2 + scrolling_fore, 70), 0.0, 2.0, Raylib::WHITE) + + Raylib.draw_text('BACKGROUND SCROLLING & PARALLAX', 10, 10, 20, Raylib::RED) + Raylib.draw_text('(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)', SCREEN_WIDTH - 330, SCREEN_HEIGHT - 20, 10, Raylib::RAYWHITE) + + Raylib.end_drawing + # ---------------------------------------------------------------------------------- +end + +# De-Initialization +# -------------------------------------------------------------------------------------- +Raylib.unload_texture(background) # Unload background texture +Raylib.unload_texture(midground) # Unload midground texture +Raylib.unload_texture(foreground) # Unload foreground texture + +Raylib.close_window # Close window and OpenGL context +# --------------------------------------------------------------------------------------