Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Gosu::Window closes itself
- - By ssmfd Date 2018-03-31 19:59
Hi, everyone! It's my first post here, so I'm sorry if I make some mistake. Also, english isn't my first language, so... yeah.

As the post subject says, I have a problem with Gosu::Window. I just started a new game a few days ago, and I've been able to evade most obstacles, but not this one.

The problem is this: after an apparently random amount of seconds, the windows closes itself. Interestingly, the program doesn't finish. The 'show' loop continues, as I have to CTRL + C in the console to actually end the program, which ends in "Interrupt".

Is this a known issue? Maybe I just hit a corner case. I've tried several Ruby-oriented game frameworks before and, honestly, I like Gosu the most. I don't want to have to change it.

I use Windows 10, Ruby 2.3.3 and the most recent version of Gosu. Here's the main part of the code; the window (other classes manage different things; if this piece of code isn't enough, I'll post more).

class Main < Gosu::Window
 
  def initialize(scale_factor = 2, res_chosen = false)
 
    super 320*scale_factor, 240*scale_factor
    self.caption = "Game"
   
    @res_chosen = res_chosen
 
    @res_chosen ? @scene = Title.new : @scene = ResChoice.new
   
  end
   
  def update
    @scene.update
    State.reset_all_keys
   
    if !$scale_factor.nil? && !@res_chosen
      close
    end
  end
   
  def draw
    @scene.draw
  end
 
  def scene
    @scene
  end
 
  def scene=(new_scene)
    @scene = new_scene
  end
 
  def button_down(id)
    case id
    when Gosu::KB_DOWN
      State.set_key_down(:down)
    when Gosu::KB_UP
      State.set_key_down(:up)
    when Gosu::KB_LEFT
      State.set_key_down(:left)
    when Gosu::KB_RIGHT
      State.set_key_down(:right)
    when Gosu::KB_RETURN
      State.set_key_down(:return)
    when Gosu::KB_LEFT_ALT
      State.set_key_down(:lalt)
    else
      super
    end
  end
 
  def button_up(id)
    case id
    when Gosu::KB_DOWN
      State.set_key_up(:down)
    when Gosu::KB_UP
      State.set_key_up(:up)
    when Gosu::KB_LEFT
      State.set_key_up(:left)
    when Gosu::KB_RIGHT
      State.set_key_up(:right)
    when Gosu::KB_RETURN
      State.set_key_up(:return)
    when Gosu::KB_LEFT_ALT
      State.set_key_up(:lalt)
    else
      super
    end
  end
 
end

Main.new.show if __FILE__ == $0
$main = Main.new($scale_factor, true)
$main.show if __FILE__ == $0


Sorry if I missed something. I've been using Ruby for years but I still a pretty noob programmer, haha. Thanks in advance!
Parent - By kyonides Date 2018-03-31 22:19
I have noticed two things. The first thing would be the two loops you create at the end of the main script. You only need call one of them! The latter would be the strange condition you set in the update method that should close the game window. Why don't you try solving the issue by removing it?
Parent - By jlnr (dev) Date 2018-04-01 11:00
The two Window.show calls at the end of the file are a little odd but shouldn't be the issue, because the second window will only be created and shown after the first window's show method returns (because it was closed).

Code looks good, but I don't know what Title and ResChoice do. I would suggest isolating the issue by commenting out parts of the source code to see at which point the problem goes away.

Gosu also swallows some exceptions and re-raises them later. If there's a bug in this part of Gosu, it can happen that exceptions just disappear altogether (this has happened before) :/
Parent - - By ssmfd Date 2018-04-01 15:14
Thank you both! I know it may seem weird, but that's because I'm trying to create a way to make some kind of GUI for the player to change the screen size and toggle fullscreen.

I've been playing around, trying to figure out what might be wrong, and I've concluded the 'double window' HAS to do with the problem. If I stay long enough on the first spawned window, even if I smash the keys around to see if I can trigger something, nothing happens. However, the window closes ONLY if the one opened is the second. Sometimes even without me having to do anything; just wait.

So, being that I don't have the abilities to fix that just yet, and that I don't feel this is an easy problem to solve, I decided to eliminate that part of the code altogether. I'm considering include a little external program to manage the settings. Like those old 'setup.exe' from some games that helped you with the game settings, as I believe it's impossible to do from Gosu itself for the moment (unless I use Gosu itself as the external program... I may try that).

Just for information: Title is the class that manages the title screen, and ResChoice manages the screen size selection menu, which goes before the title.

Anyway, sorry for the inconvenience. I hope the next time I post something is the finished game, haha. I'm sure you'll like it!
Parent - - By lol_o2 Date 2018-04-01 15:21
FYI, some recent Gosu version fixed window size handling, so you can easily switch fullscreen mode, and probably resolution, in-game with no need for some additional windows.
Parent - - By ssmfd Date 2018-04-01 15:32
That sounds interesting, at least to try. How do I do that? I mean, as far as I know, to set the window size and fullscreen mode you HAVE to do it from the initialize method in Window.
Parent - - By lol_o2 Date 2018-04-01 21:59
https://www.libgosu.org/cgi-bin/mwf/topic_show.pl?tid=1316

You can do

window.width =
window.height =
window.fullscreen =


and it works while the game is running.
Parent - By ssmfd Date 2018-04-02 10:44
All this time I thought they were read-only...

Well, thank you a lot!
Up Topic Gosu / Gosu Exchange / Gosu::Window closes itself

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill