Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Multiplatform Tutorial

neochuky edited this page Apr 24, 2013 · 1 revision

#Multiplatform Tutorial

The main goal of Gosu Android is to be easy to get your game in Gosu working easily on Gosu Android. So with tutorial_common_activity.rb we provide a simple example on how to make a game that works on across Android, Windows, OsX and Linux with same code.

module Resources
  if defined? Ruboto
    Resources::STAR_FIGHTER = Ruboto::R::drawable::star_fighter
    Resources::BEEP = Ruboto::R::raw::beep
    Resources::SPACE = Ruboto::R::drawable::space
    Resources::STAR = Ruboto::R::drawable::star
  else
    Resources::STAR_FIGHTER = "media/Starfighter.bmp"
    Resources::BEEP = "media/Beep.wav"
    Resources::SPACE = "media/Space.png"
    Resources::STAR = "media/Star.png"
  end
end

The first thing is to define the adequate routes to the media files according to the current platform. So for example when loading an image we do @image = Gosu::Image.new(window, Resources::STAR_FIGHTER, false)

For the PC we got the input control for the keyboard:

  def update
    if button_down? Gosu::KbA then
      @player.turn_left
    end
    if button_down? Gosu::KbD then
      @player.turn_right
    end
    if button_down? Gosu::KbW then
      @player.accelerate
    end
    @player.move
    @player.collect_stars(@stars)
    
    if rand(100) < 4 and @stars.size < 25 then
      @stars.push(Star.new(@star_anim))
    end
  end

While for Android we got the touch input:

  def touch_moved(touch)
    @player.warp(touch.x, touch.y)
  end 

We know that the touch method will never be called on PC.

Finally we define how the window is created for each platform:

if not defined? Ruboto
  window = GameWindow.new
  window.show
else
  class TutorialCommonActivity
    def on_create(bundle)
      super(bundle)
      Gosu::AndroidInitializer.instance.start(self)
      rescue Exception => e
        puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
    end
    
    def on_ready
      window = GameWindow.new
      window.show
      rescue Exception => e
        puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
    end
  end
end
Clone this wiki locally