gosu_android 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,144 @@
1
+ require 'gosu'
2
+
3
+ module ZOrder
4
+ Background, Stars, Player, UI = *0..3
5
+ end
6
+
7
+ class Player
8
+ attr_reader :score
9
+
10
+ def initialize(window)
11
+ @image = Gosu::Image.new(window, Ruboto::R::drawable::star_fighter, false)
12
+ @beep = Gosu::Sample.new(window, Ruboto::R::raw::beep)
13
+ @x = @y = @vel_x = @vel_y = @angle = 0.0
14
+ @score = 0
15
+ end
16
+
17
+ def warp(x, y)
18
+ @x, @y = x, y
19
+ end
20
+
21
+ def turn_left
22
+ @angle -= 4.5
23
+ end
24
+
25
+ def turn_right
26
+ @angle += 4.5
27
+ end
28
+
29
+ def accelerate
30
+ @vel_x += Gosu::offset_x(@angle, 0.5)
31
+ @vel_y += Gosu::offset_y(@angle, 0.5)
32
+ end
33
+
34
+ def move
35
+ @x += @vel_x
36
+ @y += @vel_y
37
+ @x %= 640
38
+ @y %= 480
39
+
40
+ @vel_x *= 0.95
41
+ @vel_y *= 0.95
42
+ end
43
+
44
+ def draw
45
+ @image.draw_rot(@x, @y, ZOrder::Player, @angle)
46
+ end
47
+
48
+ def collect_stars(stars)
49
+ stars.reject! do |star|
50
+ if Gosu::distance(@x, @y, star.x, star.y) < 35 then
51
+ @score += 10
52
+ @beep.play
53
+ true
54
+ else
55
+ false
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ class Star
62
+ attr_reader :x, :y
63
+
64
+ def initialize(animation)
65
+ @animation = animation
66
+ @color = Gosu::Color.new(0xff000000)
67
+ @color.red = rand(256 - 40) + 40
68
+ @color.green = rand(256 - 40) + 40
69
+ @color.blue = rand(256 - 40) + 40
70
+ @x = rand * 640
71
+ @y = rand * 480
72
+ end
73
+
74
+ def draw
75
+ img = @animation[Gosu::milliseconds / 100 % @animation.size]
76
+ img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
77
+ ZOrder::Stars, 1, 1, @color, :add)
78
+ end
79
+ end
80
+
81
+ class GameWindow < Gosu::Window
82
+ def initialize
83
+ super(640, 480, false)
84
+ self.caption = "Gosu Tutorial Game"
85
+
86
+ @background_image = Gosu::Image.new(self, Ruboto::R::drawable::space, true)
87
+
88
+ @player = Player.new(self)
89
+ @player.warp(320, 240)
90
+
91
+ @star_anim = Gosu::Image::load_tiles(self, Ruboto::R::drawable::star, 25, 25, false)
92
+ @stars = Array.new
93
+
94
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
95
+ end
96
+
97
+ def update
98
+ if button_down? Gosu::KbA then
99
+ @player.turn_left
100
+ end
101
+ if button_down? Gosu::KbD then
102
+ @player.turn_right
103
+ end
104
+ if button_down? Gosu::KbW then
105
+ @player.accelerate
106
+ end
107
+ @player.move
108
+ @player.collect_stars(@stars)
109
+
110
+ if rand(100) < 4 and @stars.size < 25 then
111
+ @stars.push(Star.new(@star_anim))
112
+ end
113
+ end
114
+
115
+ def draw
116
+ @background_image.draw(0, 0, ZOrder::Background)
117
+ @player.draw
118
+ @stars.each { |star| star.draw }
119
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
120
+ end
121
+
122
+ def button_down(id)
123
+ if id == Gosu::KbEscape then
124
+ close
125
+ end
126
+ end
127
+ end
128
+
129
+ class TutorialActivity
130
+ def on_create(bundle)
131
+ super(bundle)
132
+ Gosu::AndroidInitializer.instance.start(self)
133
+ rescue Exception => e
134
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
135
+ end
136
+
137
+ def on_ready
138
+ window = GameWindow.new
139
+ window.show
140
+ window.show_soft_keyboard
141
+ rescue Exception => e
142
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
143
+ end
144
+ end
@@ -0,0 +1,166 @@
1
+ require 'gosu'
2
+
3
+ module Resources
4
+ if defined? Ruboto
5
+ Resources::STAR_FIGHTER = Ruboto::R::drawable::star_fighter
6
+ Resources::BEEP = Ruboto::R::raw::beep
7
+ Resources::SPACE = Ruboto::R::drawable::space
8
+ Resources::STAR = Ruboto::R::drawable::star
9
+ else
10
+ Resources::STAR_FIGHTER = "media/Starfighter.bmp"
11
+ Resources::BEEP = "media/Beep.wav"
12
+ Resources::SPACE = "media/Space.png"
13
+ Resources::STAR = "media/Star.png"
14
+ end
15
+ end
16
+
17
+ module ZOrder
18
+ Background, Stars, Player, UI = *0..3
19
+ end
20
+
21
+ class Player
22
+ attr_reader :score
23
+
24
+ def initialize(window)
25
+ @image = Gosu::Image.new(window, Resources::STAR_FIGHTER, false)
26
+ @beep = Gosu::Sample.new(window, Resources::BEEP)
27
+ @x = @y = @vel_x = @vel_y = @angle = 0.0
28
+ @score = 0
29
+ end
30
+
31
+ def warp(x, y)
32
+ @x, @y = x, y
33
+ end
34
+
35
+ def turn_left
36
+ @angle -= 4.5
37
+ end
38
+
39
+ def turn_right
40
+ @angle += 4.5
41
+ end
42
+
43
+ def accelerate
44
+ @vel_x += Gosu::offset_x(@angle, 0.5)
45
+ @vel_y += Gosu::offset_y(@angle, 0.5)
46
+ end
47
+
48
+ def move
49
+ @x += @vel_x
50
+ @y += @vel_y
51
+ @x %= 640
52
+ @y %= 480
53
+
54
+ @vel_x *= 0.95
55
+ @vel_y *= 0.95
56
+ end
57
+
58
+ def draw
59
+ @image.draw_rot(@x, @y, ZOrder::Player, @angle)
60
+ end
61
+
62
+ def collect_stars(stars)
63
+ stars.reject! do |star|
64
+ if Gosu::distance(@x, @y, star.x, star.y) < 35 then
65
+ @score += 10
66
+ @beep.play
67
+ true
68
+ else
69
+ false
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ class Star
76
+ attr_reader :x, :y
77
+
78
+ def initialize(animation)
79
+ @animation = animation
80
+ @color = Gosu::Color.new(0xff000000)
81
+ @color.red = rand(256 - 40) + 40
82
+ @color.green = rand(256 - 40) + 40
83
+ @color.blue = rand(256 - 40) + 40
84
+ @x = rand * 640
85
+ @y = rand * 480
86
+ end
87
+
88
+ def draw
89
+ img = @animation[Gosu::milliseconds / 100 % @animation.size]
90
+ img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
91
+ ZOrder::Stars, 1, 1, @color, :add)
92
+ end
93
+ end
94
+
95
+ class GameWindow < Gosu::Window
96
+ def initialize
97
+ super(640, 480, false)
98
+ self.caption = "Gosu Tutorial Game"
99
+
100
+ @background_image = Gosu::Image.new(self, Resources::SPACE, true)
101
+
102
+ @player = Player.new(self)
103
+ @player.warp(320, 240)
104
+
105
+ @star_anim = Gosu::Image::load_tiles(self, Resources::STAR, 25, 25, false)
106
+ @stars = Array.new
107
+
108
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
109
+ end
110
+
111
+ def update
112
+ if button_down? Gosu::KbA then
113
+ @player.turn_left
114
+ end
115
+ if button_down? Gosu::KbD then
116
+ @player.turn_right
117
+ end
118
+ if button_down? Gosu::KbW then
119
+ @player.accelerate
120
+ end
121
+ @player.move
122
+ @player.collect_stars(@stars)
123
+
124
+ if rand(100) < 4 and @stars.size < 25 then
125
+ @stars.push(Star.new(@star_anim))
126
+ end
127
+ end
128
+
129
+ def touch_moved(touch)
130
+ @player.warp(touch.x, touch.y)
131
+ end
132
+
133
+ def draw
134
+ @background_image.draw(0, 0, ZOrder::Background)
135
+ @player.draw
136
+ @stars.each { |star| star.draw }
137
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
138
+ end
139
+
140
+ def button_down(id)
141
+ if id == Gosu::KbEscape then
142
+ close
143
+ end
144
+ end
145
+ end
146
+
147
+ if not defined? Ruboto
148
+ window = GameWindow.new
149
+ window.show
150
+ else
151
+ class TutorialCommonActivity
152
+ def on_create(bundle)
153
+ super(bundle)
154
+ Gosu::AndroidInitializer.instance.start(self)
155
+ rescue Exception => e
156
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
157
+ end
158
+
159
+ def on_ready
160
+ window = GameWindow.new
161
+ window.show
162
+ rescue Exception => e
163
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
164
+ end
165
+ end
166
+ end
@@ -8,8 +8,8 @@ class Player
8
8
  attr_reader :score
9
9
 
10
10
  def initialize(window)
11
- @image = Gosu::Image.new(window, "/mnt/sdcard/jruby/media/Starfighter.bmp", false)
12
- @beep = Gosu::Sample.new(window, "/mnt/sdcard/jruby/media/Beep.wav")
11
+ @image = Gosu::Image.new(window, Ruboto::R::drawable::star_fighter, false)
12
+ @beep = Gosu::Sample.new(window, Ruboto::R::raw::beep)
13
13
  @x = @y = @vel_x = @vel_y = @angle = 0.0
14
14
  @score = 0
15
15
  end
@@ -51,7 +51,7 @@ class Star
51
51
  def draw
52
52
  img = @animation[Gosu::milliseconds / 100 % @animation.size]
53
53
  img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
54
- ZOrder::Stars, 1, 1, @color, Gosu::AM_ADD)
54
+ ZOrder::Stars, 1, 1, @color, :add)
55
55
  end
56
56
  end
57
57
 
@@ -60,15 +60,15 @@ class GameWindow < Gosu::Window
60
60
  super(640, 480, false)
61
61
  self.caption = "Gosu Tutorial Game"
62
62
 
63
- #@background_image = Gosu::Image.new(self, "media/Space.png", true)
63
+ @background_image = Gosu::Image.new(self, Ruboto::R::drawable::space, true)
64
64
 
65
65
  @player = Player.new(self)
66
66
  @player.warp(320, 240)
67
67
 
68
- @star_anim = Gosu::Image::load_tiles(self, "/mnt/sdcard/jruby/media/Star.png", 25, 25, false)
68
+ @star_anim = Gosu::Image::load_tiles(self, Ruboto::R::drawable::star, 25, 25, false)
69
69
  @stars = Array.new
70
70
 
71
- #@font = Gosu::Font.new(self, Gosu::default_font_name, 20)
71
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
72
72
  end
73
73
 
74
74
  def update
@@ -84,10 +84,10 @@ class GameWindow < Gosu::Window
84
84
  end
85
85
 
86
86
  def draw
87
- #@background_image.draw(0, 0, ZOrder::Background)
87
+ @background_image.draw(0, 0, ZOrder::Background)
88
88
  @player.draw
89
89
  @stars.each { |star| star.draw }
90
- #@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
90
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
91
91
  end
92
92
 
93
93
  def button_down(id)
@@ -97,7 +97,7 @@ class GameWindow < Gosu::Window
97
97
  end
98
98
  end
99
99
 
100
- class TestGameActivity
100
+ class TutorialTouchActivity
101
101
  def on_create(bundle)
102
102
  super(bundle)
103
103
  Gosu::AndroidInitializer.instance.start(self)
@@ -111,4 +111,4 @@ class TestGameActivity
111
111
  rescue Exception => e
112
112
  puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
113
113
  end
114
- end
114
+ end
@@ -0,0 +1,34 @@
1
+ require 'gosu'
2
+
3
+ class GameWindow < Gosu::Window
4
+ def initialize
5
+ super 600, 480, false, 30
6
+ self.caption = "Gosu Use Keyboard"
7
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
8
+ self.show_soft_keyboard
9
+ end
10
+
11
+ def button_down(id)
12
+ @key = id
13
+ end
14
+
15
+ def draw
16
+ @font.draw("Press a key to see its code: #{@key}", 10, 10, 3, 1.0, 1.0, 0xffffff00)
17
+ end
18
+ end
19
+
20
+ class UseKeyboardActivity
21
+ def on_create(bundle)
22
+ super(bundle)
23
+ Gosu::AndroidInitializer.instance.start(self)
24
+ rescue Exception => e
25
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
26
+ end
27
+
28
+ def on_ready
29
+ window = GameWindow.new
30
+ window.show
31
+ rescue Exception => e
32
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
33
+ end
34
+ end
Binary file
@@ -8,15 +8,45 @@ module Gosu
8
8
  end
9
9
  end
10
10
 
11
- class AudioFocusListener
11
+ #TODO No listener gets ever called, also when a song ends
12
+ #logcat returns error:
13
+ # E/MP3Extractor( 95): Unable to resync. Signalling end of stream.
14
+ class AudioFocusListener < JavaImports::Service
15
+ include JavaImports::AudioManager::OnAudioFocusChangeListener
12
16
  def onAudioFocusChange focusChange
13
17
  puts "In focus change #{focusChange}"
18
+ return false
19
+ end
20
+
21
+ def onBind intent
22
+ return nil
14
23
  end
15
24
 
16
25
  def toString
17
26
  self.class.to_s
18
27
  end
19
28
  end
29
+
30
+ class AudioCompletionListener
31
+ include JavaImports::MediaPlayer::OnCompletionListener
32
+ def onCompletion mp
33
+ return true
34
+ end
35
+ end
36
+
37
+ class AudioErrorListener
38
+ include JavaImports::MediaPlayer::OnErrorListener
39
+ def onError(mp, what, extra)
40
+ return true
41
+ end
42
+ end
43
+
44
+ class AudioInfoListener
45
+ include JavaImports::MediaPlayer::OnInfoListener
46
+ def onInfo(mp, what, extra)
47
+ return true
48
+ end
49
+ end
20
50
 
21
51
  #TODO ManageAudioFocus, when app loses, stop song
22
52
  #TODO Raise a warning is the file could not be loaded
@@ -26,8 +56,6 @@ module Gosu
26
56
  #system and loads the sample from a file.
27
57
  def initialize(window, filename)
28
58
  @window = window
29
- #Set finalize
30
- ObjectSpace.define_finalizer(self, self.class.method(:finalize).to_proc)
31
59
  if not defined? @@pool
32
60
  @@pool = JavaImports::SoundPool.new(MAX_SAMPLES, JavaImports::AudioManager::STREAM_MUSIC, 0)
33
61
  end
@@ -37,6 +65,8 @@ module Gosu
37
65
  else
38
66
  @id = @@pool.load(filename, 1)
39
67
  end
68
+ #Set finalize
69
+ ObjectSpace.define_finalizer(self, self.class.finalize(@id))
40
70
  end
41
71
 
42
72
  #Plays the sample without panning.
@@ -62,14 +92,14 @@ module Gosu
62
92
  end
63
93
  end
64
94
 
65
- def Sample.finalize(id)
66
- @@pool.unload(@id)
95
+ def self.finalize(id)
96
+ proc { @@pool.unload(id) }
67
97
  end
68
98
 
69
99
  end
70
100
 
71
101
  #TODO Error on playing several songs, bear in mind mediaplayer states
72
- #FIXME Set listener for when the data finished loading asynchronously
102
+ #TODO Set listener for when the data finished loading asynchronously
73
103
  # add some checks play is reached before that
74
104
  class Song
75
105
  attr_reader :current_song
@@ -81,6 +111,9 @@ module Gosu
81
111
  context = @window.activity.getApplicationContext
82
112
  @@audio_manager = context.getSystemService(Context::AUDIO_SERVICE)
83
113
  focus = @@audio_manager.requestAudioFocus(@@audio_focus_listener, JavaImports::AudioManager::STREAM_MUSIC, JavaImports::AudioManager::AUDIOFOCUS_GAIN)
114
+ @@media_player.setOnCompletionListener AudioCompletionListener.new
115
+ @@media_player.setOnErrorListener AudioErrorListener.new
116
+ @@media_player.setOnInfoListener AudioInfoListener.new
84
117
  else
85
118
  @@media_player.reset
86
119
  focus = @@audio_manager.requestAudioFocus(@@audio_focus_listener, JavaImports::AudioManager::STREAM_MUSIC, JavaImports::AudioManager::AUDIOFOCUS_GAIN)
@@ -153,6 +186,12 @@ module Gosu
153
186
  end
154
187
  @@current_song = 0
155
188
  end
189
+
190
+ def Song.release_resources
191
+ if defined? @@media_player
192
+ @@audio_manager.abandonAudioFocus @@audio_focus_listener
193
+ end
194
+ end
156
195
 
157
196
  end
158
197