|
| 1 | +# ****************************************************************************************** |
| 2 | +# |
| 3 | +# raylib [textures] example - sprite button |
| 4 | +# |
| 5 | +# Example originally created with raylib 2.5, last time updated with raylib 2.5 |
| 6 | +# |
| 7 | +# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.5 |
| 8 | +# |
| 9 | +# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 10 | +# BSD-like license that allows static linking with closed source software |
| 11 | +# |
| 12 | +# Copyright (c) 2019-2023 Ramon Santamaria (@raysan5) |
| 13 | +# |
| 14 | +# ****************************************************************************************** |
| 15 | + |
| 16 | +require 'bundler/setup' |
| 17 | +require 'raylib' |
| 18 | + |
| 19 | +NUM_FRAMES = 3 # Number of frames (rectangles) for the button sprite texture |
| 20 | + |
| 21 | +# Initialization |
| 22 | +# -------------------------------------------------------------------------------------- |
| 23 | +SCREEN_WIDTH = 800 |
| 24 | +SCREEN_HEIGHT = 450 |
| 25 | + |
| 26 | +Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [textures] example - sprite button') |
| 27 | + |
| 28 | +Raylib.init_audio_device # Initialize audio device |
| 29 | + |
| 30 | +fx_button = Raylib.load_sound(File.join(__dir__, 'resources/buttonfx.wav')) # Load button sound |
| 31 | +button = Raylib.load_texture(File.join(__dir__, 'resources/button.png')) # Load button texture |
| 32 | + |
| 33 | +# Define frame rectangle for drawing |
| 34 | +frame_height = button.height / NUM_FRAMES.to_f |
| 35 | +source_rec = Raylib::Rectangle.create(0, 0, button.width.to_f, frame_height) |
| 36 | + |
| 37 | +# Define button bounds on screen |
| 38 | +btn_bounds = Raylib::Rectangle.create( |
| 39 | + SCREEN_WIDTH / 2.0 - button.width / 2.0, |
| 40 | + SCREEN_HEIGHT / 2.0 - button.height / NUM_FRAMES / 2.0, |
| 41 | + button.width.to_f, |
| 42 | + frame_height |
| 43 | +) |
| 44 | + |
| 45 | +Raylib.set_target_fps(60) |
| 46 | +# -------------------------------------------------------------------------------------- |
| 47 | + |
| 48 | +# Main game loop |
| 49 | +until Raylib.window_should_close # Detect window close button or ESC key |
| 50 | + # Update |
| 51 | + # ---------------------------------------------------------------------------------- |
| 52 | + mouse_point = Raylib.get_mouse_position |
| 53 | + btn_action = false |
| 54 | + |
| 55 | + # Check button state 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSED |
| 56 | + if Raylib.check_collision_point_rec(mouse_point, btn_bounds) |
| 57 | + if Raylib.is_mouse_button_down(Raylib::MOUSE_BUTTON_LEFT) |
| 58 | + btn_state = 2 |
| 59 | + else |
| 60 | + btn_state = 1 |
| 61 | + end |
| 62 | + |
| 63 | + btn_action = true if Raylib.is_mouse_button_released(Raylib::MOUSE_BUTTON_LEFT) |
| 64 | + else |
| 65 | + btn_state = 0 |
| 66 | + end |
| 67 | + |
| 68 | + Raylib.play_sound(fx_button) if btn_action |
| 69 | + |
| 70 | + # Calculate button frame rectangle to draw depending on button state |
| 71 | + source_rec.y = btn_state * frame_height |
| 72 | + # ---------------------------------------------------------------------------------- |
| 73 | + |
| 74 | + # Draw |
| 75 | + # ---------------------------------------------------------------------------------- |
| 76 | + Raylib.begin_drawing |
| 77 | + Raylib.clear_background(Raylib::RAYWHITE) |
| 78 | + |
| 79 | + Raylib.draw_texture_rec(button, source_rec, Raylib::Vector2.create(btn_bounds.x, btn_bounds.y), Raylib::WHITE) # Draw button frame |
| 80 | + |
| 81 | + Raylib.end_drawing |
| 82 | + # ---------------------------------------------------------------------------------- |
| 83 | +end |
| 84 | + |
| 85 | +# De-Initialization |
| 86 | +# -------------------------------------------------------------------------------------- |
| 87 | +Raylib.unload_texture(button) # Unload button texture |
| 88 | +Raylib.unload_sound(fx_button) # Unload sound |
| 89 | +Raylib.close_audio_device # Close audio device |
| 90 | +Raylib.close_window # Close window and OpenGL context |
| 91 | +# -------------------------------------------------------------------------------------- |
0 commit comments