Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gosu::Window with progress bar
require "gosu"
class WindowWithPreloader < Gosu::Window
def initialize
super 800, 600
# This is the "to do list" for the progress bar.
# In this case, it's a list of all images to load.
@images_to_load = Dir.glob("images/*.png")
# Loaded images will be stored here.
@images = {}
end
def update
# As long as we have work to do, perform one action and then return.
if not @images_to_load.empty?
next_filename = @images_to_load.pop
@images[next_filename] = Gosu::Image.new(next_filename)
return
end
puts "Images loaded, normal game logic goes here"
end
def draw
# As long as we have work to do, display the progress bar and return.
if not @images_to_load.empty?
# Background
Gosu.draw_rect 100, 260, 600, 40, Gosu::Color::WHITE
# This value will be 0.0 first, then grow to 1.0 with every tick
filled_ratio = 1.0 * @images.size / (@images.size + @images_to_load.size)
# Foreground
Gosu.draw_rect 105, 265, filled_ratio * 590, 30, Gosu::Color::RED
return
end
# Normal rendering goes here, @images is fully loaded at this point.
image_name = @images.keys.sample
puts "Images loaded, rendering #{image_name}"
@images[image_name].draw 0, 0, 0
end
end
WindowWithPreloader.new.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.