Skip to content

Instantly share code, notes, and snippets.

@owenbutler
Last active March 13, 2026 15:08
Show Gist options
  • Select an option

  • Save owenbutler/439cc26ad81599db293f1141721102fc to your computer and use it in GitHub Desktop.

Select an option

Save owenbutler/439cc26ad81599db293f1141721102fc to your computer and use it in GitHub Desktop.
DragonRuby snippets

Description

Collection of small snippets for potentially useful things in DragonRuby

Intended to be copy/pasted.

r = pixel&0xFF
g = (pixel>>8)&0xFF
b = (pixel>>16)&0xFF
a = (pixel>>24)&0xFF
def time_block(name, &block)
start_time = Time.now
block.call
diff = Time.now - start_time
$outputs.debug << "%s: %0.3f" % [name, diff]
end
$gtk.disable_console
module FromNow
@@callbacks ||= nil
@@last_tick ||= -1
def FromNow.reset
@@last_tick = -1
@@callbacks = nil
end
def FromNow.tick(t=$args.tick_count)
if t < @@last_tick # clear callbacks after $gtk.reset
FromNow.reset
return
end
@@last_tick = t
return unless list = @@callbacks&.delete(t)
i = 0; n = list.size
while i < n
list[i].call
i += 1
end
end
def from_now(&blk)
n = self.round
if n == 0
blk.call
else
@@callbacks ||= Hash.new { |hash,tick| hash[tick] = [] }
@@callbacks[$args.tick_count + n] << blk
end
end
# just so you can do "5.frames.from_now { }"
def frames() = self
end
Numeric.include FromNow
# Adjust to taste, or hook up to your volume slider in game?
$sound_volume = 0.5
$saved_mute_volume = nil
def sound_toggle_mute
if $saved_mute_volume
$sound_volume = $saved_mute_volume
$saved_mute_volume = nil
else
$saved_mute_volume = $sound_volume
$sound_volume = 0.0
end
sound_adjust_playing_sounds $sound_volume
end
def sound_adjust_playing_sounds to_gain
$gtk.args.audio.each do |_, a|
a.gain = to_gain
end
end
def play_sound in_sound
sound = in_sound.copy
adjusted_gain = 1.0
if sound.key?(:gain)
adjusted_gain = sound[:gain]
end
sound[:gain] = adjusted_gain * $sound_volume
$gtk.args.audio[sound[:input]] = sound
end
# Example usage:
# Start a backgrounbd looping sound
# play_sound({ input: "sounds/ship-ambience.ogg", looping: true })
#
# Play a sound effect that is already gained down
# play_sound({ input: "sounds/loud-boom.wav", gain: 0.8 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment