Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Alternate Solutions to Multiple Windows?
- - By FireThestrak Date 2014-06-16 22:01
I've been kicking around the forum a bit trying to find a solution for this, but I decided to go ahead and ask straight out.

I'm trying to make a simple launcher using Gosu. Akin to, though not as complicated as, Minecraft.
I've got a fullscreen window in which the game is played in (currently just one level), and I want to add a launcher (600x800, not fullscreen) that has level-select capabilities.

I've seen around the forums that multiple windows would be a pain to implement and is therefore not supported. But is there a way to swap between two screen resolutions and fullscreen?
Parent - - By jahmaican Date 2014-06-17 08:25 Edited 2014-06-17 08:38
AFAIK dynamic resolution change is impossible, but from what I understand, you just need 2 Gosu::Window classes, e.g. Launcher < Gosu::Window and Game < Gosu::Window. First you create and show the Launcher window, and inside you need a method that will call Game.new.show and close (in this order) after some event (e.g. key pressed).

If you need an existing example, check this one.

And frankly, I never tried it, but if you needed multiple windows at once, I don't see how this would be a pain. You'd just need an observer to pass data between them, or even go wild and use global variables (but then you will be judged).
Parent - By jlnr (dev) Date 2014-06-17 20:04
It probably works on some platforms but it's not officially supported (yet). :(
Parent - By FireThestrak Date 2014-06-18 03:13
Ah, I see what you are doing. I got that working, but what I really wanted to do was bring the launcher back after the game window closed so that the player wouldn't need to relaunch the application from the level select screen. But when I do things get... weird. Like the draw method breaks or something. I haven't had the time for a proper debug.
Parent - - By jlnr (dev) Date 2014-06-17 20:07
I don't think that's "officially" possible right now. However I am in the process of moving most Gosu::Window implementations on top of SDL2, and I think that'll make it easier to add support for resolution changing soon. For now the only thing that's guaranteed to work is to have two executables/apps (depending on your OS) and closing the first while launching the second.
Parent - By EdwinOdesseiron Date 2014-06-17 22:15
There is a way to change screen resolution without having 2+ separate Gosu::Window implementations. I've done it in my game, for switching between fullscreen and windowed mode (but it works with resolution as well - checked that). Basically you either create global variables with size ($game_width, $game_height), or add arguments to GameWindow < Gosu::Window initialize. When you create a window, make it global too, so when you first create it, you do it like that:
$window = GameWindow.new(800,600) #w,h
$window.show

# GameWindow's initialize method:
def initialize(width, height)
super(width, height, false)
...
end

Then, let's say in your options menu, you have the option to change the resolution to 1024x768. To make it work, you can simply use this:
$window.close
$window = GameWindow.new(1024,768)
$window.show


And it will restart your window with new size. You may further alter it to open with certain things on, ie. in Die Erlösung I made the window to take one argument - target (a Symbol), and depending on the target, we're sent to either a Intro/Splash, Title screen or Settings screen. So there's plenty of possibilities.
Parent - - By FireThestrak Date 2014-06-18 03:21
Oh, yeah, SDL2 would be sweet.

I quickly tried the following:

require './lib/engine.rb'
require './lib/home.rb'

loop do
  home_window = HomeWindow.new
  home_window.show
  home_window.close
  break if home_window.exit

  game_window = GameWindow.new
  game_window.show
  game_window.close
end


Which after the first iteration starts to freak out. Looks like the draw method is stuck on the same frame (may be an issue with update...) and flopping back and forth between the frame and a black window.
I haven't done any real digging yet... But trying unofficial things leads to unofficial bugs.
Parent - - By jahmaican Date 2014-06-18 07:31
Unofficial bugs are the best bugs! Your attitude is wrong though. Someone tells you something is impossible, you should try and prove him wrong. This is how lots of crazy stuff is done.

I'm sincerely suprised fiddling around with windows can be this nondeterministic. Shame that I don't have much time recently, because I'd love to do some more research on this matter.

Although, I guess the problems would be OS dependant, so if you make it work in your environment and don't mind targeting only the platform you're using, there's a good chance it'll be okay for most users too.
Parent - By jlnr (dev) Date 2014-06-18 21:41
Right, usually this ends up in bug reports where everything works fine on Windows but suddenly the Mac port crashes, etc. :)

If it works on one platform and that's good enough, there's no reason not to rely on it, as far as I'm concerned.
Parent - By FireThestrak Date 2014-06-22 19:54
Believe me, if I had time I'd love to mess around with this, but my project ends in eight days. So if it requires digging, I'm just going to skip it for now.
Parent - By arrow Date 2014-07-12 18:28 Edited 2014-07-12 18:34
I found a mac specific solution that should be portable to other platforms. The idea is that I created two Ruby Gosu applications. One for the launcher and one for the game. In ruby you can call a command in a subshell using the "system" method. In Launcher.app I use the system command to start Game.app and if it opens successfully then I close the launcher window.

You can download the working example here: https://github.com/Aerotune/gosu_mac_game_launcher

I'm using mac and to open an application I can enter "open -a /path/to/game.app" in the terminal. This is system specific and you would need to find out how you would do it on Windows and Linux.

Mac specific example for a Launcher.app's main.rb:

class Window < Gosu::Window
  def initialize
    super 400, 300, false

    self.caption = "Game Launcher!"
    @font = Gosu::Font.new(self, "Arial", 16)
    @message = "Game Launcher! Press Enter to start game!"
  end

  def draw
    x = self.width / 2 - @font.text_width(@message)/2.0
    y = self.height / 2 - 8
    @font.draw @message, x, y, 0
  end
 
  def button_down id
    case id
    when Gosu::KbReturn
      game_path = "/path/to/Game.app"
      if system "open -a #{game_path}"
        # successfully opened the game!
        close
      else
        @message = "Failed to open the game!"
      end
    end
  end
end

Window.new.show



Documentation for Ruby 'system' method
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-system

Documentation for Mac 'open' command
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/open.1.html

Edit: If you have trouble parsing what level to load to the game from the launcher I could help out with how you could do it.
Up Topic Gosu / Gosu Exchange / Alternate Solutions to Multiple Windows?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill