|
| 1 | +# ****************************************************************************************** |
| 2 | +# |
| 3 | +# raylib [textures] example - Bunnymark |
| 4 | +# |
| 5 | +# Example originally created with raylib 1.6, 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) 2014-2023 Ramon Santamaria (@raysan5) |
| 13 | +# |
| 14 | +# ****************************************************************************************** |
| 15 | + |
| 16 | +require 'bundler/setup' |
| 17 | +require 'raylib' |
| 18 | + |
| 19 | +# Particle structure with basic data |
| 20 | +class Bunny < FFI::Struct |
| 21 | + layout :position, Raylib::Vector2, |
| 22 | + :speed, Raylib::Vector2, |
| 23 | + :color, Raylib::Color |
| 24 | + |
| 25 | + def position = self[:position] |
| 26 | + def position=(new_position) self[:position] = new_position end |
| 27 | + def speed = self[:speed] |
| 28 | + def speed=(new_speed) self[:speed] = new_speed end |
| 29 | + def color = self[:color] |
| 30 | + def color=(new_color) self[:color] = new_color end |
| 31 | + |
| 32 | + def self.create(position, speed, color) |
| 33 | + new.tap do |instance| |
| 34 | + instance.position = position |
| 35 | + instance.speed = speed |
| 36 | + instance.color = color |
| 37 | + end |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +# Initialization |
| 42 | +# -------------------------------------------------------------------------------------- |
| 43 | +SCREEN_WIDTH = 800 |
| 44 | +SCREEN_HEIGHT = 450 |
| 45 | + |
| 46 | +Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [textures] example - bunnymark') |
| 47 | + |
| 48 | +MAX_BUNNIES = 50_000 |
| 49 | +MAX_BATCH_ELEMENTS = 8192 |
| 50 | + |
| 51 | +tex_bunny = Raylib.load_texture(File.join(__dir__, 'resources/wabbit_alpha.png')) |
| 52 | + |
| 53 | +bunnies = [] |
| 54 | +bunnies_count = 0 |
| 55 | + |
| 56 | +Raylib.set_target_fps(60) |
| 57 | +# -------------------------------------------------------------------------------------- |
| 58 | + |
| 59 | +# Main game loop |
| 60 | +until Raylib.window_should_close # Detect window close button or ESC key |
| 61 | + # Update |
| 62 | + # ---------------------------------------------------------------------------------- |
| 63 | + if Raylib.is_mouse_button_down(Raylib::MOUSE_BUTTON_LEFT) |
| 64 | + # Create more bunnies |
| 65 | + 100.times do |
| 66 | + next unless bunnies_count < MAX_BUNNIES |
| 67 | + |
| 68 | + position = Raylib.get_mouse_position |
| 69 | + |
| 70 | + speed = Raylib::Vector2.create( |
| 71 | + Raylib.get_random_value(-250, 250) / 60.0, |
| 72 | + Raylib.get_random_value(-250, 250) / 60.0 |
| 73 | + ) |
| 74 | + |
| 75 | + color = Raylib::Color.create( |
| 76 | + Raylib.get_random_value(50, 240), |
| 77 | + Raylib.get_random_value(80, 240), |
| 78 | + Raylib.get_random_value(100, 240), |
| 79 | + 255 |
| 80 | + ) |
| 81 | + bunnies << Bunny.create(position, speed, color) |
| 82 | + bunnies_count += 1 |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + # Update bunnies |
| 87 | + bunnies.each do |bunny| |
| 88 | + bunny.position.x += bunny.speed.x |
| 89 | + bunny.position.y += bunny.speed.y |
| 90 | + |
| 91 | + if (bunny.position.x + (tex_bunny.width / 2) > Raylib.get_screen_width) || |
| 92 | + (bunny.position.x + (tex_bunny.width / 2)).negative? |
| 93 | + bunny.speed.x *= -1 |
| 94 | + end |
| 95 | + |
| 96 | + if (bunny.position.y + (tex_bunny.height / 2) > Raylib.get_screen_height) || |
| 97 | + (bunny.position.y + (tex_bunny.height / 2) - 40).negative? |
| 98 | + bunny.speed.y *= -1 |
| 99 | + end |
| 100 | + end |
| 101 | + # ---------------------------------------------------------------------------------- |
| 102 | + |
| 103 | + # Draw |
| 104 | + # ---------------------------------------------------------------------------------- |
| 105 | + Raylib.begin_drawing |
| 106 | + Raylib.clear_background(Raylib::RAYWHITE) |
| 107 | + |
| 108 | + bunnies.each do |bunny| |
| 109 | + # NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), |
| 110 | + # a draw call is launched and buffer starts being filled again; |
| 111 | + # before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU... |
| 112 | + # Process of sending data is costly and it could happen that GPU data has not been completely |
| 113 | + # processed for drawing while new data is tried to be sent (updating current in-use buffers) |
| 114 | + # it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies |
| 115 | + Raylib.draw_texture(tex_bunny, bunny.position.x.to_i, bunny.position.y.to_i, bunny.color) |
| 116 | + end |
| 117 | + |
| 118 | + Raylib.draw_rectangle(0, 0, SCREEN_WIDTH, 40, Raylib::BLACK) |
| 119 | + Raylib.draw_text(Raylib.text_format('bunnies: %i', :int, bunnies_count), 120, 10, 20, Raylib::GREEN) |
| 120 | + Raylib.draw_text(Raylib.text_format('batched draw calls: %i', :int, 1 + (bunnies_count / MAX_BATCH_ELEMENTS)), 320, 10, 20, Raylib::MAROON) |
| 121 | + |
| 122 | + Raylib.draw_fps(10, 10) |
| 123 | + Raylib.end_drawing |
| 124 | + # ---------------------------------------------------------------------------------- |
| 125 | +end |
| 126 | + |
| 127 | +# De-Initialization |
| 128 | +# -------------------------------------------------------------------------------------- |
| 129 | +Raylib.unload_texture(tex_bunny) # Unload bunny texture |
| 130 | +Raylib.close_window # Close window and OpenGL context |
| 131 | +# -------------------------------------------------------------------------------------- |
0 commit comments