gosu_android 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/LICENSE +19 -0
  2. data/README.md +35 -0
  3. data/bin/gosu_android +11 -0
  4. data/examples/arkanoid.rb +105 -0
  5. data/examples/pong_activity.rb +99 -0
  6. data/examples/test-game.rb +114 -0
  7. data/lib/gosu.java.jar +0 -0
  8. data/lib/gosu_android.rb +1 -0
  9. data/lib/gosu_android/audio/audio.rb +159 -0
  10. data/lib/gosu_android/commands/base.rb +200 -0
  11. data/lib/gosu_android/description.rb +5 -0
  12. data/lib/gosu_android/graphics/bitmap.rb +12 -0
  13. data/lib/gosu_android/graphics/bitmapUtils.rb +51 -0
  14. data/lib/gosu_android/graphics/blockAllocator.rb +107 -0
  15. data/lib/gosu_android/graphics/color.rb +27 -0
  16. data/lib/gosu_android/graphics/common.rb +21 -0
  17. data/lib/gosu_android/graphics/drawOp.rb +6 -0
  18. data/lib/gosu_android/graphics/drawOpQueue.rb +39 -0
  19. data/lib/gosu_android/graphics/font.rb +61 -0
  20. data/lib/gosu_android/graphics/graphics.rb +227 -0
  21. data/lib/gosu_android/graphics/graphicsBase.rb +27 -0
  22. data/lib/gosu_android/graphics/image.rb +151 -0
  23. data/lib/gosu_android/graphics/imageData.rb +23 -0
  24. data/lib/gosu_android/graphics/largeImageData.rb +116 -0
  25. data/lib/gosu_android/graphics/renderState.rb +5 -0
  26. data/lib/gosu_android/graphics/texChunk.rb +68 -0
  27. data/lib/gosu_android/graphics/texture.rb +86 -0
  28. data/lib/gosu_android/input/buttons.rb +128 -0
  29. data/lib/gosu_android/input/input.rb +120 -0
  30. data/lib/gosu_android/main-window.rb +314 -0
  31. data/lib/gosu_android/math.rb +21 -0
  32. data/lib/gosu_android/physics/physicsManager.rb +57 -0
  33. data/lib/gosu_android/physics/physicsObject.rb +113 -0
  34. data/lib/gosu_android/requires.rb +40 -0
  35. data/lib/gosu_android/timing.rb +8 -0
  36. data/lib/gosu_android/version.rb +3 -0
  37. data/res/drawable-nodpi/ball.png +0 -0
  38. data/res/drawable-nodpi/bar.png +0 -0
  39. data/res/drawable-nodpi/bar_hor.png +0 -0
  40. data/res/drawable-nodpi/character_atlas8.png +0 -0
  41. data/res/drawable-nodpi/ship.png +0 -0
  42. data/res/drawable-nodpi/space.png +0 -0
  43. data/res/drawable-nodpi/star.png +0 -0
  44. data/res/raw/beep.wav +0 -0
  45. data/res/raw/chriss_onac_tempo_red.mp3 +0 -0
  46. metadata +127 -0
@@ -0,0 +1,314 @@
1
+ require 'gosu_android/requires'
2
+
3
+ require 'gosu_android/graphics/graphics'
4
+ require 'gosu_android/graphics/graphicsBase'
5
+ require 'gosu_android/graphics/font'
6
+ require 'gosu_android/input/input'
7
+ require 'gosu_android/audio/audio'
8
+ require 'gosu_android/physics/physicsManager'
9
+ require 'gosu_android/timing'
10
+
11
+ require 'singleton'
12
+ require 'ruboto/util/toast'
13
+
14
+ module Gosu
15
+
16
+ class AndroidInitializer
17
+ include Singleton
18
+ attr_reader :graphics, :surface_view
19
+ attr_reader :activity
20
+
21
+ def start activity
22
+ activity.toast 'Still loading please wait'
23
+ @activity = activity
24
+ @surface_view = GosuSurfaceView.new(@activity)
25
+ @graphics = Graphics.new(self)
26
+ @surface_view.renderer = @graphics
27
+ @activity.content_view = @surface_view
28
+ activity.toast 'Still loading please wait'
29
+ end
30
+
31
+ def on_ready
32
+ @activity.on_ready
33
+ end
34
+ end
35
+
36
+ class GosuSurfaceView < android.opengl.GLSurfaceView
37
+
38
+ def atributes(window, input)
39
+ @window = window
40
+ @input = input
41
+ end
42
+
43
+ def onTouchEvent(event)
44
+ super
45
+ @input.feed_touch_event(event)
46
+ return true
47
+ end
48
+
49
+ def onKeyDown(keyCode, event)
50
+ super
51
+ @input.feed_key_event(keyCode, event)
52
+ end
53
+
54
+ def onKeyUp(keyCode, event)
55
+ super
56
+ @input.feed_key_event(keyCode, event)
57
+ end
58
+
59
+ def onWindowFocusChanged(has_focus)
60
+ super
61
+ if(@window)
62
+ @window.focus_changed(has_focus, self.get_width, self.get_height)
63
+ end
64
+ end
65
+ end
66
+
67
+
68
+ class Window
69
+ attr_accessor :caption
70
+ attr_accessor :mouse_x
71
+ attr_accessor :mouse_y
72
+ attr_accessor :text_input
73
+ attr_accessor :media_player
74
+ attr_reader :width, :height
75
+ attr_reader :fullscreen
76
+ attr_reader :update_interval
77
+ attr_reader :physics_manager
78
+ attr_reader :fonts_manager
79
+ attr_reader :activity
80
+
81
+ # update_interval:: Interval in milliseconds between two calls
82
+ # to the update member function. The default means the game will run
83
+ # at 60 FPS, which is ideal on standard 60 Hz TFT screens.
84
+ def initialize(width, height, fullscreen, update_interval=16.666666)
85
+ android_initializer = AndroidInitializer.instance
86
+ @fullscreen = fullscreen
87
+ @showing = false
88
+ @activity = android_initializer.activity
89
+ @display = @activity.getWindowManager.getDefaultDisplay
90
+ @width = width
91
+ @height= height
92
+ @update_interval = update_interval/1000.0
93
+ #@surface_view = GosuSurfaceView.new(@activity)
94
+ @surface_view = android_initializer.surface_view
95
+ @input = Input.new(@display, self)
96
+ @surface_view.atributes(self, @input)
97
+ #@graphics = Graphics.new(@width, @height, @fullscreen, self)
98
+ @graphics = android_initializer.graphics
99
+ @graphics.initialize_window(@width, @height, @fullscreen, self)
100
+ #@surface_view.renderer = @graphics
101
+ @surface_view.set_render_mode(JavaImports::GLSurfaceView::RENDERMODE_WHEN_DIRTY)
102
+ @physics_manager = PhysicsManager.new self
103
+ @fonts_manager = FontsManager.new self
104
+ @media_player = nil
105
+ end
106
+
107
+ # Enters a modal loop where the Window is visible on screen and receives calls to draw, update etc.
108
+ def show
109
+ @showing = true
110
+ if @fullscreen
111
+ #Use full java path for Window, since there is a Gosu::Window
112
+ @activity.request_window_feature(JavaImports::Window::FEATURE_NO_TITLE)
113
+ @activity.get_window.set_flags(JavaImports::WindowManager::LayoutParams::FLAG_FULLSCREEN,
114
+ JavaImports::WindowManager::LayoutParams::FLAG_FULLSCREEN)
115
+ #@activity.content_view = @surface_view
116
+ @window = @activity.getWindow
117
+ #Replace position and size with gosu metrics
118
+ else
119
+ @window = @activity.getWindow
120
+ #Only the thread that created the view can change it, so setLayout
121
+ #and setTitle cannot be executed here
122
+ p = Proc.new do
123
+ @window.setLayout(@width, @height)
124
+ @activity.setTitle @caption
125
+ end
126
+ @activity.runOnUiThread(p)
127
+ end
128
+ @screen_width = @surface_view.get_width
129
+ @screen_height = @surface_view.get_height
130
+ end
131
+
132
+ def do_show
133
+ @start_time = Time.now
134
+ do_tick
135
+ #TODO gosu dark side
136
+ @end_time = Time.now
137
+ if (@start_time <= @end_time and (@end_time - @start_time) < @update_interval)
138
+ sleep(@update_interval - (@end_time - @start_time))
139
+ end
140
+ end
141
+
142
+ # Tells the window to end the current show loop as soon as possible.
143
+ def close
144
+ @showing = false
145
+ end
146
+
147
+ # Called every update_interval milliseconds while the window is being
148
+ # shown. Your application's main game logic goes here.
149
+ def update; end
150
+
151
+ # Called after every update and when the OS wants the window to
152
+ # repaint itself. Your application's rendering code goes here.
153
+ def draw; end
154
+
155
+ # Can be overriden to give the game a chance to say no to being redrawn.
156
+ # This is not a definitive answer. The operating system can still cause
157
+ # redraws for one reason or another.
158
+ #
159
+ # By default, the window is redrawn all the time (i.e. Window#needs_redraw?
160
+ # always returns true.)
161
+ def needs_redraw?; end
162
+
163
+ # Can be overriden to show the system cursor when necessary, e.g. in level
164
+ # editors or other situations where introducing a custom cursor is not
165
+ # desired.
166
+ def needs_cursor?; end
167
+
168
+ # Called before update when the user pressed a button while the
169
+ # window had the focus.
170
+ def button_down(id); end
171
+ # Same as buttonDown. Called then the user released a button.
172
+ def button_up(id); end
173
+
174
+ # Returns true if a button is currently pressed. Updated every tick.
175
+ def button_down?(id); end
176
+
177
+ # Called when the user started a touch on the screen
178
+ def touch_began(touch); end
179
+ # Called when the user continue a touch on the screen
180
+ def touch_moved(touch); end
181
+ # Called when the user finished a touch on the screen
182
+ def touch_ended(touch); end
183
+
184
+ # Called when and object collides with another object
185
+ def object_collided(x, y, object); end
186
+
187
+ # This object should be subject to the physics manager
188
+ def apply_physics(object)
189
+ @physics_manager.register_new_object(object)
190
+ end
191
+
192
+ # Stop applying physics to the given object
193
+ def stop_physics(object)
194
+ @physics_manager.delete_object(object)
195
+ end
196
+
197
+ # Draws a line from one point to another (last pixel exclusive).
198
+ # Note: OpenGL lines are not reliable at all and may have a missing pixel at the start
199
+ # or end point. Please only use this for debugging purposes. Otherwise, use a quad or
200
+ # image to simulate lines, or contribute a better draw_line to Gosu.
201
+ def draw_line(x1, y1, c1, x2, y2, c2, z=0, mode=AM_DEFAULT)
202
+ @graphics.draw_line(x1, y1, c1, x2, y2, c2, z, mode)
203
+ end
204
+
205
+ def draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z=0, mode=AM_DEFAULT)
206
+ @graphics.draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, mode)
207
+ end
208
+
209
+ # Draws a rectangle (two triangles) with given corners and corresponding
210
+ # colors.
211
+ # The points can be in clockwise order, or in a Z shape.
212
+ def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z=0, mode=AM_DEFAULT)
213
+ @graphics.draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode)
214
+ end
215
+
216
+ # Flushes all drawing operations to OpenGL so that Z-ordering can start anew. This
217
+ # is useful when drawing several parts of code on top of each other that use conflicting
218
+ # z positions.
219
+ def flush; end
220
+
221
+ # For custom OpenGL calls. Executes the given block in a clean OpenGL environment.
222
+ # Use the ruby-opengl gem to access OpenGL function (if you manage to get it to work).
223
+ # IF no z position is given, it will execute the given block immediately, otherwise,
224
+ # the code will be scheduled to be called between Gosu drawing operations.
225
+ #
226
+ # Note: You cannot call Gosu rendering functions within this block, and you can only
227
+ # call the gl function in the call tree of Window#draw.
228
+ #
229
+ # See examples/OpenGLIntegration.rb for an example.
230
+ def gl(z=nil, &custom_gl_code); end
231
+
232
+ # Limits the drawing area to a given rectangle while evaluating the code inside of the block.
233
+ def clip_to(x, y, w, h, &rendering_code); end
234
+
235
+ # Returns a Gosu::Image that containes everything rendered within the given block. It can be
236
+ # used to optimize rendering of many static images, e.g. the map. There are still several
237
+ # restrictions that you will be informed about via exceptions.
238
+ #
239
+ # The returned Gosu::Image will have the width and height you pass as arguments, regardless
240
+ # of how the area you draw on. It is important to pass accurate values if you plan on using
241
+ # Gosu::Image#draw_as_quad or Gosu::Image#draw_rot with the result later.
242
+ #
243
+ # @return [Gosu::Image]
244
+ def record(width, height, &rendering_code); end
245
+
246
+ # Rotates everything drawn in the block around (around_x, around_y).
247
+ def rotate(angle, around_x=0, around_y=0, &rendering_code); end
248
+
249
+ # Scales everything drawn in the block by a factor.
250
+ def scale(factor_x, factor_y=factor_x, &rendering_code); end
251
+
252
+ # Scales everything drawn in the block by a factor for each dimension.
253
+ def scale(factor_x, factor_y, around_x, around_y, &rendering_code); end
254
+
255
+ # Moves everything drawn in the block by an offset in each dimension.
256
+ def translate(x, y, &rendering_code); end
257
+
258
+ # Applies a free-form matrix rotation to everything drawn in the block.
259
+ def transform(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, &rendering_code); end
260
+
261
+ # Returns the character a button usually produces, or nil. To implement real text-input
262
+ # facilities, look at the TextInput class instead.
263
+ def self.button_id_to_char(id); end
264
+
265
+ # Returns the button that has to be pressed to produce the given character, or nil.
266
+ def self.char_to_button_id(char); end
267
+
268
+ # @deprecated Use Window#mouse_x= and Window#mouse_y= instead.
269
+ def set_mouse_position(x, y); end
270
+
271
+ def caption= value
272
+ @caption = value
273
+ if @showing and not @fullscreen
274
+ p = Proc.new do
275
+ @activity.setTitle @caption
276
+ end
277
+ @activity.runOnUiThread(p)
278
+ end
279
+ end
280
+
281
+ def do_tick
282
+ @input.update
283
+ self.update
284
+ @physics_manager.update
285
+ @graphics.begin(Color::BLACK)
286
+ self.draw
287
+ @graphics.end
288
+ @surface_view.request_render
289
+ end
290
+
291
+ def focus_changed has_focus, width, height
292
+ @screen_width = width
293
+ @screen_height = height
294
+ if @showing and @media_player != nil
295
+ if has_focus
296
+ @media_player.start
297
+ else
298
+ @media_player.pause
299
+ end
300
+ end
301
+ end
302
+
303
+ def show_soft_keyboard
304
+ context = @activity.getApplicationContext
305
+ imm = context.getSystemService(Context::INPUT_METHOD_SERVICE)
306
+ imm.toggleSoftInput(JavaImports::InputMethodManager::SHOW_FORCED,0)
307
+ end
308
+
309
+ def create_image(source, src_x, src_y, src_width, src_height, tileable)
310
+ @graphics.create_image(source, src_x, src_y, src_width, src_height, tileable)
311
+ end
312
+
313
+ end
314
+ end
@@ -0,0 +1,21 @@
1
+ module Gosu
2
+
3
+ def self.offset_x(angle, radius)
4
+ Math::sin(angle / 180 * Math::PI) * radius
5
+ end
6
+
7
+ def self.offset_y(angle, radius)
8
+ Math::cos(angle / 180 * Math::PI) * radius
9
+ end
10
+
11
+ #Returns the square of the distance between two points.
12
+ def self.distance_sqr(x1, y1, x2, y2)
13
+ (x1 - x2)**2 + (y1 - y2)**2
14
+ end
15
+
16
+ #Returns the distance between two points.
17
+ def self.distance(x1, y1, x2, y2)
18
+ Math::sqrt((x1 - x2)**2 + (y1 - y2)**2)
19
+ end
20
+
21
+ end
@@ -0,0 +1,57 @@
1
+ require 'gosu_android/physics/physicsObject'
2
+
3
+ module Gosu
4
+
5
+ class PhysicsManager
6
+ attr_accessor :gravity_x, :gravity_y
7
+ def initialize(window)
8
+ @window = window
9
+ @dt = @window.update_interval
10
+ @squares = []
11
+ @planes = []
12
+ @gravity_x = 0
13
+ @gravity_y = 98 #10 pixels are 1 meter
14
+ end
15
+
16
+ def register_new_object object
17
+ if object.class == Square
18
+ @squares.push object unless @squares.include? object
19
+ elsif object.class == Plane
20
+ @planes.push object unless @planes.include? object
21
+ end
22
+ end
23
+
24
+ def delete_object object
25
+ if object.class == Square
26
+ @squares.delete object
27
+ elsif object.class == Plane
28
+ @planes.delete object
29
+ end
30
+ end
31
+
32
+ def update
33
+ #Gravity
34
+ @squares.each do |square|
35
+ if square.mass_inverted > 0
36
+ square.velocity[0] += @dt*@gravity_x
37
+ square.velocity[1] += @dt*@gravity_y
38
+ end
39
+ end
40
+
41
+ #Collision detection
42
+ @squares.each do |square|
43
+ @planes.each do |plane|
44
+ square.generate_contact plane
45
+ end
46
+ end
47
+
48
+ #Integrate
49
+ @squares.each do |square|
50
+ if square.mass_inverted > 0
51
+ square.integrate
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,113 @@
1
+ require 'gosu_android/graphics/image'
2
+
3
+ module Gosu
4
+
5
+ def self.dot_product(l1, l2)
6
+ sum = 0
7
+ for i in 0...l1.size
8
+ sum += l1[i] * l2[i]
9
+ end
10
+ sum
11
+ end
12
+
13
+ def self.cross_product(l1,l2)
14
+ res = []
15
+ res.push( l1[1]*l2[2] - l1[2]*l2[1] );
16
+ res.push( l1[2]*l2[0] - l1[0]*l2[2] );
17
+ res.push( l1[0]*l2[1] - l1[1]*l2[0] );
18
+ d = res.max
19
+ if(d == 0)
20
+ d = res.min
21
+ res[0] = res[0]/-d
22
+ res[1] = res[1]/-d
23
+ else
24
+ res[0] = res[0]/d
25
+ res[1] = res[1]/d
26
+ end
27
+ res
28
+ end
29
+
30
+ class Square
31
+ attr_accessor :velocity
32
+ attr_reader :position, :center
33
+ attr_reader :mass_inverted, :restitution
34
+ def initialize(window, file_name, x, y, z, size, mass_inverted,
35
+ velocity_x = 0, velocity_y = 0, restitution = 1, tileable = false)
36
+ @window = window
37
+ @position = [x,y]
38
+ @size = size / 2
39
+ @center = [@position[0] + @size, @position[1] + @size]
40
+ @z = z
41
+ @restitution = restitution
42
+ @velocity = [velocity_x, velocity_y]
43
+ @mass_inverted = mass_inverted
44
+ @image = Gosu::Image.new(@window, file_name , tileable)
45
+ @dt = @window.update_interval
46
+ end
47
+
48
+ def integrate
49
+ @position[0] += @velocity[0]*@dt
50
+ @position[1] += @velocity[1]*@dt
51
+ @center = [@position[0] + @size, @position[1] + @size]
52
+ end
53
+
54
+ def generate_contact other_object
55
+ if other_object.class == Square
56
+ elsif other_object.class == Plane
57
+ if @center[0] - @size < other_object.top_limit[0] and other_object.bottom_limit[0] < @center[0] + @size and
58
+ @center[1] - @size < other_object.bottom_limit[1] and other_object.top_limit[1] < @center[1] + @size
59
+ #Calculate new velocity, after the hit
60
+ if other_object.type == :vertical
61
+ @velocity[0] -= (1 + @restitution) * @velocity[0]
62
+ else
63
+ @velocity[1] -= (1 + @restitution) * @velocity[1]
64
+ end
65
+ #Call window event
66
+ @window.object_collided( @position[0], @position[1], other_object )
67
+ end
68
+ end
69
+ end
70
+
71
+ def draw
72
+ @image.draw(@position[0], @position[1], @z)
73
+ end
74
+
75
+ end
76
+
77
+ # Bottom ----- Top
78
+ #
79
+ # Top
80
+ # |
81
+ # |
82
+ # |
83
+ # Bottom
84
+ class Plane
85
+ attr_accessor :bottom_limit, :top_limit
86
+ attr_reader :type
87
+ def initialize(window, file_name, p0, p1, z)
88
+ @window = window
89
+ @image = Gosu::Image.new(@window, file_name)
90
+ @z = z
91
+ @top_limit = Array.new p0
92
+ @bottom_limit = Array.new p1
93
+
94
+ if(@bottom_limit[0] > @top_limit[0] or @bottom_limit[1] < @top_limit[1])
95
+ @top_limit, @bottom_limit = @bottom_limit, @top_limit
96
+ end
97
+
98
+ if @bottom_limit[0] == @top_limit[0]
99
+ @type = :vertical
100
+ elsif @bottom_limit[1] == @top_limit[1]
101
+ @type = :horizontal
102
+ else
103
+ raise "Planes can only be horizontal or vertical"
104
+ end
105
+
106
+ end
107
+
108
+ def draw(x,y,z = @z)
109
+ @image.draw(x,y,z)
110
+ end
111
+
112
+ end
113
+ end