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.
@@ -103,5 +103,17 @@ module Gosu
103
103
  return [false]
104
104
  end
105
105
 
106
+ def free (left, top)
107
+ @pimpl.blocks.delete_if do |block|
108
+ if block.left == left and block.top == top
109
+ @pimpl.max_w = @pimpl.max_w - 1
110
+ @pimpl.max_h = @pimpl.max_h - 1
111
+ true
112
+ else
113
+ false
114
+ end
115
+ end
116
+ end
117
+
106
118
  end
107
119
  end
@@ -31,7 +31,7 @@ module Gosu
31
31
 
32
32
 
33
33
  def clear_queue
34
- @ops = []
34
+ @ops.clear
35
35
  @op_pool.clearPool
36
36
  end
37
37
 
@@ -42,7 +42,7 @@ module Gosu
42
42
  #Draws text so the top left corner of the text is at (x; y).
43
43
  #param text Formatted text without line-breaks.
44
44
  def draw(text, x, y, z, factor_x = 1, factor_y = 1, c = Color::WHITE,
45
- mode = AM_DEFAULT)
45
+ mode = :default)
46
46
 
47
47
  offset = 0
48
48
  text.each_char do |char|
@@ -7,8 +7,7 @@ module Gosu
7
7
  #to the old color's channels.
8
8
 
9
9
  #amMultiply -> The color's channels will be multiplied with each other.
10
- AM_DEFAULT, AM_ADD, AM_MULTIPLY = *(0..2)
11
- AM_ADDITIVE = AM_ADD
10
+ AM_MODES = { :default => 0, :add => 1, :additive => 1, :multiply => 2 }
12
11
 
13
12
  FF_BOLD = 1
14
13
  FF_ITALIC = 2
@@ -69,6 +69,7 @@ module Gosu
69
69
  else
70
70
  @data = window.create_image(source, src_x, src_y, src_width, src_height, BF_SMOOTH)
71
71
  end
72
+ ObjectSpace.define_finalizer(self, Proc.new{@data.finalize})
72
73
  end
73
74
 
74
75
  public
@@ -80,14 +81,14 @@ module Gosu
80
81
  @data.height
81
82
  end
82
83
 
83
- def draw(x, y, z, factor_x = 1, factor_y = 1, c = Color::WHITE, mode = AM_DEFAULT)
84
+ def draw(x, y, z, factor_x = 1, factor_y = 1, c = Color::WHITE, mode = :default)
84
85
  x2 = x + width*factor_x
85
- y2 = y + height*factor_y
86
- @data.draw(x, y, c, x2, y, c, x, y2, c, x2, y2, c, z, mode)
86
+ y2 = y + height*factor_y
87
+ @data.draw(x, y, c, x2, y, c, x, y2, c, x2, y2, c, z, AM_MODES[mode])
87
88
  end
88
89
 
89
90
  def draw_rot(x, y, z, angle, center_x = 0.5, center_y = 0.5, factor_x = 1.0,
90
- factor_y = 1.0, c = Color::WHITE, mode = AM_DEFAULT)
91
+ factor_y = 1.0, c = Color::WHITE, mode = :default)
91
92
 
92
93
  size_y = width * factor_x
93
94
  size_y = height * factor_y
@@ -113,7 +114,7 @@ module Gosu
113
114
  y + dist_to_left_y + dist_to_bottom_y, c,
114
115
  x + dist_to_right_x + dist_to_bottom_x,
115
116
  y + dist_to_right_y + dist_to_bottom_y,
116
- c, z, mode)
117
+ c, z, AM_MODES[mode])
117
118
  end
118
119
 
119
120
  def self.load_tiles(window, bmp, tile_width, tile_height, tileable)
@@ -4,12 +4,11 @@ require 'gosu_android/graphics/common'
4
4
  module Gosu
5
5
  class TexChunk < ImageData
6
6
  def initialize(graphics, queues, texture, x, y, w, h, padding)
7
- #Define object destructor
8
- ObjectSpace.define_finalizer(self,
9
- self.class.method(:finalize).to_proc)
10
7
  @graphics = graphics
11
8
  @queues = queues
12
9
  @texture = texture
10
+ #Define object destructor
11
+ ObjectSpace.define_finalizer(self, Proc.new{@texture.finalize})
13
12
  @x = x
14
13
  @y = y
15
14
  @w = w
@@ -24,7 +23,7 @@ module Gosu
24
23
  @info.bottom = (@y.to_f + @h) / @texture.size
25
24
  end
26
25
 
27
- def TexChunk.finalize(id)
26
+ def finalize
28
27
  @texture.free(@x - @padding, @y - @padding)
29
28
  end
30
29
 
@@ -6,9 +6,6 @@ module Gosu
6
6
  class Texture
7
7
 
8
8
  def initialize(size, gl)
9
- #Set finalize
10
- ObjectSpace.define_finalizer(self,
11
- self.class.method(:finalize).to_proc)
12
9
  @size = size
13
10
  @allocator = BlockAllocator.new(@size, @size)
14
11
  @num = 0
@@ -47,8 +44,13 @@ module Gosu
47
44
  @gl.glTexParameterf(JavaImports::GL10::GL_TEXTURE_2D, JavaImports::GL10::GL_TEXTURE_MAG_FILTER, JavaImports::GL10::GL_LINEAR)
48
45
  end
49
46
 
50
- def Texture.finalize(id)
51
- @gl.glDeleteTextures(1, @name)
47
+ def finalize
48
+ tbb = (JavaImports::ByteBuffer.allocateDirect(4))
49
+ tbb.order(JavaImports::ByteOrder.nativeOrder)
50
+ texture_buffer = tbb.asIntBuffer
51
+ texture_buffer.put(@name)
52
+ texture_buffer.position(0)
53
+ @gl.glDeleteTextures(1, texture_buffer)
52
54
  end
53
55
 
54
56
  def tex_name
@@ -41,16 +41,38 @@ module Gosu
41
41
  @display = display
42
42
  @window = window
43
43
  @touch_event_list = []
44
- @key_event_list = []
44
+ @key_event_list_up = []
45
+ @key_event_list_down = []
45
46
  @id = 0
47
+ @ingnore_buttons = [JavaImports::KeyEvent::KEYCODE_VOLUME_DOWN,
48
+ JavaImports::KeyEvent::KEYCODE_VOLUME_MUTE,
49
+ JavaImports::KeyEvent::KEYCODE_VOLUME_UP,
50
+ JavaImports::KeyEvent::KEYCODE_BACK,
51
+ JavaImports::KeyEvent::KEYCODE_HOME,
52
+ JavaImports::KeyEvent::KEYCODE_MENU,
53
+ JavaImports::KeyEvent::KEYCODE_POWER,
54
+ JavaImports::KeyEvent::KEYCODE_APP_SWITCH,
55
+ JavaImports::KeyEvent::KEYCODE_UNKNOWN]
46
56
  end
47
57
 
48
58
  def feed_touch_event(event)
49
59
  @touch_event_list.push event
50
60
  end
51
61
 
52
- def feed_key_event(keyCode, event)
53
- @key_event_list.push [keyCode, event]
62
+ def feed_key_event_down(keyCode)
63
+ if @ingnore_buttons.include? keyCode
64
+ return false
65
+ end
66
+ @key_event_list_down.push keyCode
67
+ return true
68
+ end
69
+
70
+ def feed_key_event_up(keyCode)
71
+ if @ingnore_buttons.include? keyCode
72
+ return false
73
+ end
74
+ @key_event_list_up.push keyCode
75
+ return true
54
76
  end
55
77
 
56
78
  # Returns the character a button usually produces, or 0.
@@ -62,7 +84,9 @@ module Gosu
62
84
 
63
85
  # Returns true if a button is currently pressed.
64
86
  # Updated every tick.
65
- def down(btn); end
87
+ def button_down?(id)
88
+ return @key_event_list_down.include? id
89
+ end
66
90
 
67
91
  # Returns the horizontal position of the mouse relative to the top
68
92
  # left corner of the window given to Input's constructor.
@@ -103,8 +127,22 @@ module Gosu
103
127
  @window.touch_ended(touch)
104
128
  end
105
129
  end
130
+ end
131
+
132
+ @key_event_list_down.each do |key_event|
133
+ @window.button_down key_event
106
134
  end
107
- @touch_event_list = []
135
+
136
+ @key_event_list_up.each do |key_event|
137
+ @window.button_up key_event
138
+ end
139
+
140
+ end
141
+
142
+ def clear
143
+ @touch_event_list.clear
144
+ @key_event_list_down.clear
145
+ @key_event_list_up.clear
108
146
  end
109
147
 
110
148
  # Assignable events that are called by update. You can bind these to your own functions.
@@ -5,7 +5,6 @@ require 'gosu_android/graphics/graphicsBase'
5
5
  require 'gosu_android/graphics/font'
6
6
  require 'gosu_android/input/input'
7
7
  require 'gosu_android/audio/audio'
8
- require 'gosu_android/physics/physicsManager'
9
8
  require 'gosu_android/timing'
10
9
 
11
10
  require 'singleton'
@@ -47,21 +46,12 @@ module Gosu
47
46
  return true
48
47
  end
49
48
 
50
- def onKeyDown(keyCode, event)
51
- super
52
- @input.feed_key_event(keyCode, event)
53
- end
54
-
55
- def onKeyUp(keyCode, event)
56
- super
57
- @input.feed_key_event(keyCode, event)
58
- end
59
-
60
49
  def onWindowFocusChanged(has_focus)
61
50
  super
62
51
  if(@window)
63
52
  @window.focus_changed(has_focus, self.get_width, self.get_height)
64
53
  end
54
+ return true
65
55
  end
66
56
  end
67
57
 
@@ -74,8 +64,8 @@ module Gosu
74
64
  attr_accessor :media_player
75
65
  attr_reader :width, :height
76
66
  attr_reader :fullscreen
67
+ attr_reader :internal_update_interval
77
68
  attr_reader :update_interval
78
- attr_reader :physics_manager
79
69
  attr_reader :fonts_manager
80
70
  attr_reader :activity
81
71
 
@@ -90,7 +80,8 @@ module Gosu
90
80
  @display = @activity.getWindowManager.getDefaultDisplay
91
81
  @width = width
92
82
  @height= height
93
- @update_interval = update_interval/1000.0
83
+ @internal_update_interval = update_interval/1000.0
84
+ @update_interval = update_interval
94
85
  #@surface_view = GosuSurfaceView.new(@activity)
95
86
  @surface_view = android_initializer.surface_view
96
87
  @input = Input.new(@display, self)
@@ -100,11 +91,60 @@ module Gosu
100
91
  @graphics.initialize_window(@width, @height, @fullscreen, self)
101
92
  #@surface_view.renderer = @graphics
102
93
  @surface_view.set_render_mode(JavaImports::GLSurfaceView::RENDERMODE_WHEN_DIRTY)
103
- @physics_manager = PhysicsManager.new self
104
94
  @fonts_manager = FontsManager.new self
105
95
  @media_player = nil
96
+ add_key_event_listener
97
+ @activity.input = @input
98
+ @showing_keyboard = false
106
99
  end
107
100
 
101
+ #TODO This is monkey patching, there has to be a better way to do it
102
+ #This method adds listeners to the activity that called gosu
103
+ #becouse the key events can only be cought in the activity
104
+
105
+ #TODO At least check if the methods were already defined,
106
+ #if so called them after/before this methods
107
+ def add_key_event_listener
108
+ # Get the class of the object.
109
+ @activity.class.class_eval do
110
+
111
+ attr_accessor :input
112
+
113
+ def on_destroy
114
+ super
115
+ #Release audio resources
116
+ Song.release_resources
117
+ end
118
+
119
+ def onKeyDown(keyCode, event)
120
+ if @input.feed_key_event_down(keyCode)
121
+ return true
122
+ else
123
+ return super keyCode, event
124
+ end
125
+ end
126
+
127
+ def onKeyUp(keyCode, event)
128
+ if @input.feed_key_event_up(keyCode)
129
+ return true
130
+ else
131
+ return super keyCode, event
132
+ end
133
+ end
134
+
135
+ #TODO It does never get called, it does not matter how long
136
+ #the press was
137
+ def onKeyLongPress(keyCode, event)
138
+ if @input.feed_key_event_up(keyCode)
139
+ return true
140
+ else
141
+ return super keyCode, event
142
+ end
143
+ end
144
+
145
+ end
146
+ end
147
+
108
148
  # Enters a modal loop where the Window is visible on screen and receives calls to draw, update etc.
109
149
  def show
110
150
  @showing = true
@@ -135,14 +175,17 @@ module Gosu
135
175
  do_tick
136
176
  #TODO gosu dark side
137
177
  @end_time = Time.now
138
- if (@start_time <= @end_time and (@end_time - @start_time) < @update_interval)
139
- sleep(@update_interval - (@end_time - @start_time))
178
+ if (@start_time <= @end_time and (@end_time - @start_time) < @internal_update_interval)
179
+ sleep(@internal_update_interval - (@end_time - @start_time))
140
180
  end
141
181
  end
142
182
 
143
183
  # Tells the window to end the current show loop as soon as possible.
144
- def close
184
+ def close
185
+ #Self trigger window lost focus, since we are going to home
186
+ focus_changed false, @screen_width, @screen_height
145
187
  @showing = false
188
+ @activity.moveTaskToBack(true)
146
189
  end
147
190
 
148
191
  # Called every update_interval milliseconds while the window is being
@@ -173,7 +216,9 @@ module Gosu
173
216
  def button_up(id); end
174
217
 
175
218
  # Returns true if a button is currently pressed. Updated every tick.
176
- def button_down?(id); end
219
+ def button_down?(id)
220
+ return @input.button_down? id
221
+ end
177
222
 
178
223
  # Called when the user started a touch on the screen
179
224
  def touch_began(touch); end
@@ -185,33 +230,23 @@ module Gosu
185
230
  # Called when and object collides with another object
186
231
  def object_collided(x, y, object); end
187
232
 
188
- # This object should be subject to the physics manager
189
- def apply_physics(object)
190
- @physics_manager.register_new_object(object)
191
- end
192
-
193
- # Stop applying physics to the given object
194
- def stop_physics(object)
195
- @physics_manager.delete_object(object)
196
- end
197
-
198
233
  # Draws a line from one point to another (last pixel exclusive).
199
234
  # Note: OpenGL lines are not reliable at all and may have a missing pixel at the start
200
235
  # or end point. Please only use this for debugging purposes. Otherwise, use a quad or
201
236
  # image to simulate lines, or contribute a better draw_line to Gosu.
202
- def draw_line(x1, y1, c1, x2, y2, c2, z=0, mode=AM_DEFAULT)
203
- @graphics.draw_line(x1, y1, c1, x2, y2, c2, z, mode)
237
+ def draw_line(x1, y1, c1, x2, y2, c2, z=0, mode=:default)
238
+ @graphics.draw_line(x1, y1, c1, x2, y2, c2, z, AM_MODES[mode])
204
239
  end
205
240
 
206
- def draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z=0, mode=AM_DEFAULT)
207
- @graphics.draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, mode)
241
+ def draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z=0, mode=:default)
242
+ @graphics.draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, AM_MODES[mode])
208
243
  end
209
244
 
210
245
  # Draws a rectangle (two triangles) with given corners and corresponding
211
246
  # colors.
212
247
  # The points can be in clockwise order, or in a Z shape.
213
- def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z=0, mode=AM_DEFAULT)
214
- @graphics.draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode)
248
+ def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z=0, mode=:default)
249
+ @graphics.draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, AM_MODES[mode])
215
250
  end
216
251
 
217
252
  # Flushes all drawing operations to OpenGL so that Z-ordering can start anew. This
@@ -282,29 +317,57 @@ module Gosu
282
317
  def do_tick
283
318
  @input.update
284
319
  self.update
285
- @physics_manager.update
320
+ @input.clear
286
321
  @graphics.begin(Color::BLACK)
287
322
  self.draw
288
323
  @graphics.end
289
324
  @surface_view.request_render
290
325
  end
291
326
 
327
+ #TODO On screen rotation the app breaks down
292
328
  def focus_changed has_focus, width, height
293
329
  @screen_width = width
294
330
  @screen_height = height
295
- if @showing and @media_player != nil
296
- if has_focus
297
- @media_player.start
298
- else
299
- @media_player.pause
331
+
332
+ if has_focus
333
+
334
+ if @showing and @media_player != nil
335
+ @media_player.start
336
+ end
337
+
338
+ #TODO Keyboard does not appears again
339
+ if @showing_keyboard
340
+ show_soft_keyboard
300
341
  end
301
- end
342
+
343
+ else
344
+ #Hide keyboard but mark it so it will be shown later
345
+ if @showing_keyboard
346
+ hide_soft_keyboard
347
+ @showing_keyboard = true
348
+ end
349
+
350
+ if @showing and @media_player != nil
351
+ @media_player.pause
352
+ end
353
+
354
+ end
355
+ return true
302
356
  end
303
357
 
304
- def show_soft_keyboard
358
+ #TODO It would be nice that the keyboard was transparent
359
+ def show_soft_keyboard
305
360
  context = @activity.getApplicationContext
306
361
  imm = context.getSystemService(Context::INPUT_METHOD_SERVICE)
307
362
  imm.toggleSoftInput(JavaImports::InputMethodManager::SHOW_FORCED,0)
363
+ @showing_keyboard = true
364
+ end
365
+
366
+ def hide_soft_keyboard
367
+ context = @activity.getApplicationContext
368
+ imm = context.getSystemService(Context::INPUT_METHOD_SERVICE)
369
+ imm.toggleSoftInput(JavaImports::InputMethodManager::HIDE_IMPLICIT_ONLY,0)
370
+ @showing_keyboard = false
308
371
  end
309
372
 
310
373
  def create_image(source, src_x, src_y, src_width, src_height, tileable)