Skip to content

Commit

Permalink
Add the textures background scrolling example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 24, 2023
1 parent 1d1cab6 commit 42ae856
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ Examples using raylib shapes drawing functionality.
| 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) |
| 46 | [shapes_top_down_lights](shapes/shapes_top_down_lights.rb) | <img src="shapes/shapes_top_down_lights.png" alt="shapes_top_down_lights" width="80"> | ⭐️⭐️⭐️⭐️ | **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<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|----------------------------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions examples/textures/textures_background_scrolling.rb
Original file line number Diff line number Diff line change
@@ -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
# --------------------------------------------------------------------------------------

0 comments on commit 42ae856

Please sign in to comment.