Skip to content

Commit

Permalink
Add textures srcrec dstrec example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 24, 2023
1 parent 8080cf4 commit 27faed5
Show file tree
Hide file tree
Showing 4 changed files with 76 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 @@ -95,4 +95,5 @@ Examples using raylib textures functionality, including image/textures loading/g
| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|----------------------------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 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) |
| 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/resources/scarfy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/textures/textures_srcrec_dstrec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions examples/textures/textures_srcrec_dstrec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# ******************************************************************************************
#
# raylib [textures] example - Texture source and destination rectangles
#
# Example originally created with raylib 1.3, last time updated with raylib 1.3
#
# 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) 2015-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] examples - texture source and destination rectangles')

# NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
scarfy = Raylib.load_texture(File.join(__dir__, 'resources/scarfy.png')) # Texture loading

frame_width = scarfy.width / 6
frame_height = scarfy.height

# Source rectangle (part of the texture to use for drawing)
source_rec = Raylib::Rectangle.create(0.0, 0.0, frame_width.to_f, frame_height.to_f)

# Destination rectangle (screen rectangle where drawing part of texture)
dest_rec = Raylib::Rectangle.create(SCREEN_WIDTH / 2.0, SCREEN_HEIGHT / 2.0, frame_width * 2.0, frame_height * 2.0)

# Origin of the texture (rotation/scale point), it's relative to destination rectangle size
origin = Raylib::Vector2.create(frame_width.to_f, frame_height.to_f)

rotation = 0

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

# Main game loop
until Raylib.window_should_close # Detect window close button or ESC key
# Update
# ----------------------------------------------------------------------------------
rotation += 1
# ----------------------------------------------------------------------------------

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

# NOTE: Using draw_texture_pro() we can easily rotate and scale the part of the texture we draw
# source_rec defines the part of the texture we use for drawing
# dest_rec defines the rectangle where our texture part will fit (scaling it to fit)
# origin defines the point of the texture used as reference for rotation and scaling
# rotation defines the texture rotation (using origin as rotation point)
Raylib.draw_texture_pro(scarfy, source_rec, dest_rec, origin, rotation.to_f, Raylib::WHITE)
Raylib.draw_line(dest_rec.x.to_i, 0, dest_rec.x.to_i, SCREEN_HEIGHT, Raylib::GRAY)
Raylib.draw_line(0, dest_rec.y.to_i, SCREEN_WIDTH, dest_rec.y.to_i, Raylib::GRAY)
Raylib.draw_text('(c) Scarfy sprite by Eiden Marsal', SCREEN_WIDTH - 200, SCREEN_HEIGHT - 20, 10, Raylib::GRAY)
Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.unload_texture(scarfy) # Texture unloading
Raylib.close_window # Close window and OpenGL context
# --------------------------------------------------------------------------------------

0 comments on commit 27faed5

Please sign in to comment.