Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Forum
Search
Topic Playing Song prevents full game close - Ruby, Mac OSX 10.8 By spirulence Date 2013-01-15 23:27
Hello everybody!

I've just started my Ruby + Gosu journey (today, actually) and I've run into this problem. I'm using Ruby 1.9.3 on Mac OSX 10.8.

If I store a few music files in a Hash, and play one of them, and try to close my game, my game window closes, but the game stays open in the terminal until I perform Ctrl-C. Here's some code that produces the problem, along with a commented workaround.

require 'gosu'

class GameWindow < Gosu::Window
  def initialize
    super 1024, 768, false
    @res = Resources.new(self, "resources")
    @state = MainMenuState.new(self, @res)
  end
 
  def button_down(id)
    if id == Gosu::KbEscape
      #uncomment for the workaround
      #@res.music.each do |title, song|
      #  song.stop
      #end
      close
    end
  end
end

class Resources
  attr_accessor :music

  def initialize(window, directory)
    @music = {}
    Dir.glob("#{directory}/music/*.ogg").each do |filename|
      base = File.basename(filename, ".ogg")
      @music[base] = Gosu::Song.new(window, filename)
    end
  end
end

class MainMenuState
  def initialize(window, res)
    @music = res.music["title_screen"]
    @music.play
  end
end

window = GameWindow.new
window.show


The workaround is to simply stop every loaded song and then close the window. It's very easy when you have all your resources loaded in one class.

What's interesting is that this problem goes away if I store the Songs directly in the window class:

require 'gosu'

class GameWindow < Gosu::Window
  def initialize
    super 640, 480, false
    self.caption = "Song test"
    @songs = [Gosu::Song.new(self, "title_screen.ogg"),
          Gosu::Song.new(self, "main.ogg")]
    @songs[0].play
  end
 
  def button_down(id)
    if id == Gosu::KbEscape
      close
    end
  end
end

window = GameWindow.new
window.show


You can grab the ogg files I used from http://kiberion.net/pannewb/archived/omega/

This makes me think that it might have something to do with the order or timing of garbage collection. It's probably not something worth fixing, as that's a hard thing to predict, and the workaround is so easy. Hopefully this can help somebody else who is in the same situation as I was. :)

Thank you for the great work you've done to make gosu! I expect many hours of joy to come from it. :D

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill