Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / 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
Parent - - By jlnr (dev) Date 2013-01-16 02:51
I have heard this before. It is a little strange but since it should be easy to reproduce, I have filed a ticket: https://github.com/jlnr/gosu/issues/161

Ruby's shutdown vs. Gosu Audio has caused issues before. At least this time it's not a segfault! :)
Parent - - By zenkalia Date 2013-01-21 02:50
Oh I've been seeing this too.  Would love to see this resolved.  In my project I'm seeing it when trying to call close after playing a second background song.

The way I got around it was to explicitly stop the background music before calling close.  https://github.com/zenkalia/rubystein/commit/fc14dcdc3f3c42ce3570545e301228556c6fe3b9

This project is managing background music using this function ( https://github.com/zenkalia/rubystein/blob/master/wolf3d.rb#L92 ) which might be janky?  I'm still new to Gosu but thought I'd link to this for context.
Parent - By jlnr (dev) Date 2013-01-21 04:41
The method looks good to me.
Parent - By zenkalia Date 2013-01-21 02:52
Hey, I commented further down the thread but in case you don't get notified for that...

The way I got around it was to explicitly stop the background music before calling close.  https://github.com/zenkalia/rubystein/commit/fc14dcdc3f3c42ce3570545e301228556c6fe3b9

<3
Up Topic Gosu / Gosu Exchange / Playing Song prevents full game close - Ruby, Mac OSX 10.8

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill