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,61 @@
1
+ require 'gosu_android/requires'
2
+ require 'gosu_android/graphics/color'
3
+ require 'gosu_android/graphics/graphicsBase'
4
+ require 'gosu_android/graphics/image'
5
+
6
+ module Gosu
7
+
8
+ class FontsManager
9
+
10
+ def initialize(window)
11
+ file = Ruboto::R::drawable::character_atlas8
12
+ font_vector = Gosu::Image::load_tiles(window, file, 13, 25, false)
13
+ symbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-/*\'\"!?[]{}_.,:; "
14
+ @font_symbols = {}
15
+ i = 0
16
+ symbols.each_char do |symbol|
17
+ @font_symbols[symbol] = font_vector[i]
18
+ i += 1
19
+ end
20
+ end
21
+
22
+ def getSymbol(symbol)
23
+ @font_symbols[symbol]
24
+ end
25
+
26
+ end
27
+
28
+ class Font
29
+ attr_reader :name, :flags
30
+ def initialize(window, font_name, font_height, font_flags = :ff_bold)
31
+ @window = window
32
+ @fonts_manager = window.fonts_manager
33
+ @name = font_name
34
+ @height = font_height * 2
35
+ @flags = flags
36
+ end
37
+
38
+ def height
39
+ @height / 2
40
+ end
41
+
42
+ #Draws text so the top left corner of the text is at (x; y).
43
+ #param text Formatted text without line-breaks.
44
+ def draw(text, x, y, z, factor_x = 1, factor_y = 1, c = Color::WHITE,
45
+ mode = AM_DEFAULT)
46
+
47
+ offset = 0
48
+ text.each_char do |char|
49
+ (@fonts_manager.getSymbol char ).draw(x + offset, y, z, factor_x, factor_y, c, mode)
50
+ offset += 10
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ def self.default_font_name
58
+ JavaImports::Typeface::MONOSPACE
59
+ end
60
+
61
+ end
@@ -0,0 +1,227 @@
1
+ require 'gosu_android/requires'
2
+ require 'gosu_android/graphics/graphicsBase'
3
+ require 'gosu_android/graphics/color'
4
+ require 'gosu_android/graphics/drawOp'
5
+ require 'gosu_android/graphics/drawOpQueue'
6
+ require 'gosu_android/graphics/image'
7
+ require 'gosu_android/graphics/largeImageData'
8
+ require 'gosu_android/graphics/bitmapUtils'
9
+ require 'gosu_android/graphics/texture'
10
+ require 'gosu_android/graphics/font'
11
+
12
+ module Gosu
13
+ class Graphics
14
+ attr_reader :width, :height
15
+ attr_reader :fullscreen
16
+ attr_reader :gl
17
+
18
+ MAX_TEXTURE_SIZE = 1024
19
+
20
+ def initialize(android_initializer)
21
+ @android_initializer = android_initializer
22
+ end
23
+
24
+ def initialize_window(physical_width, physical_height, fullscreen, window)
25
+ @window = window
26
+ @virt_width = physical_height
27
+ @virt_height = physical_width
28
+
29
+ @phys_width = physical_width
30
+ @phys_height = physical_height
31
+
32
+ @fullscreen = fullscreen
33
+ #Gl stuff moved to render
34
+
35
+ @queues = DrawOpQueue.new(@gl)
36
+ @textures = []
37
+ end
38
+
39
+ def set_resolution(virtualWidth, virtualHeight); end
40
+
41
+ #Prepares the graphics object for drawing. Nothing must be drawn
42
+ #without calling begin.
43
+ def begin(clear_with_color = Color::BLACK)
44
+ if @gl == nil
45
+ raise "Surface must be created before calling begin"
46
+ end
47
+ @gl.glClearColor(clear_with_color.red / 255.0, clear_with_color.green / 255.0,
48
+ clear_with_color.blue / 255.0, clear_with_color.alpha / 255.0)
49
+ @gl.glClear(JavaImports::GL10::GL_COLOR_BUFFER_BIT | JavaImports::GL10::GL_DEPTH_BUFFER_BIT)
50
+
51
+ true
52
+ end
53
+ #Every call to begin must have a matching call to end.
54
+ def end
55
+ flush
56
+ @gl.glFlush
57
+ end
58
+ #Flushes the Z queue to the screen and starts a new one.
59
+ #Useful for games that are *very* composite in nature (splitscreen).
60
+ def flush
61
+ @queues.perform_draw_ops_and_code
62
+ @queues.clear_queue
63
+ end
64
+
65
+ #Finishes all pending Gosu drawing operations and executes
66
+ #the following OpenGL code in a clean environment.
67
+ def beginGL; end
68
+ #Resets Gosu o its default rendering state.
69
+ def endGL; end
70
+ #(Experimental)
71
+ #Schedules a custom GL functor to be executed at a certain Z level.
72
+ #The functor is called in a clean GL context (as given by beginGL/endGL).
73
+ #Gosu's rendering up to the Z level may not yet have been glFlush()ed.
74
+ #Note: You may not call any Gosu rendering functions from within the
75
+ #functor, and you must schedule it from within Window::draw's call tree.
76
+ def scheduleGL(functor, z); end
77
+
78
+ #Enables clipping to a specified rectangle.
79
+ def begin_clipping(x, y, width, height); end
80
+ #Disables clipping.
81
+ def end_clipping; end
82
+
83
+ #Starts recording a macro. Cannot be nested.
84
+ def begin_recording; end
85
+ #Finishes building the macro and returns it as a drawable object.
86
+ #The width and height affect nothing about the recording process,
87
+ #the resulting macro will simply return these values when you ask
88
+ #it.
89
+ #Most usually, the return value is passed to Image::Image().
90
+ def end_recording(width, height); end
91
+
92
+ #Pushes one transformation onto the transformation stack.
93
+ def push_transform(transform); end
94
+ #Pops one transformation from the transformation stack.
95
+ def pop_transform; end
96
+
97
+ #Draws a line from one po to another (last pixel exclusive).
98
+ #Note: OpenGL lines are not reliable at all and may have a missing pixel at the start
99
+ #or end po. Please only use this for debugging purposes. Otherwise, use a quad or
100
+ #image to simulate lines, or contribute a better drawLine to Gosu.
101
+ def draw_line( x1, y1, c1, x2, y2, c2, z, mode)
102
+ op = @queues.op_pool.newDrawOp
103
+ op.render_state.mode = mode
104
+ op.vertices_or_block_index = 2
105
+ op.vertices[0].set(x1, y1, c1)
106
+ op.vertices[1].set(x2, y2, c2)
107
+ op.z = z
108
+ @queues.schedule_draw_op op
109
+ end
110
+
111
+ def draw_triangle( x1, y1, c1, x2, y2, c2, x3, y3, c3, z, mode)
112
+ op = @queues.op_pool.newDrawOp
113
+ op.render_state.mode = mode
114
+ op.vertices_or_block_index = 3
115
+ op.vertices[0].set(x1, y1, c1)
116
+ op.vertices[1].set(x2, y2, c2)
117
+ op.vertices[2].set(x3, y3, c3)
118
+ op.z = z
119
+ @queues.schedule_draw_op op
120
+ end
121
+
122
+ def draw_quad( x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode)
123
+ op = @queues.op_pool.newDrawOp
124
+ op.render_state.mode = mode
125
+ op.vertices_or_block_index = 4
126
+ op.vertices[0].set(x1, y1, c1)
127
+ op.vertices[1].set(x2, y2, c2)
128
+ op.vertices[2].set(x3, y3, c3)
129
+ op.vertices[3].set(x4, y4, c4)
130
+ op.z = z
131
+ @queues.schedule_draw_op op
132
+ end
133
+
134
+ #TODO If @gl == nil Texture.new will fail, this has to be fixed
135
+ #Turns a portion of a bitmap o something that can be drawn on
136
+ #this graphics object.
137
+ def create_image(src, src_x, src_y, src_width, src_height, border_flags)
138
+ max_size = MAX_TEXTURE_SIZE
139
+ #Special case: If the texture is supposed to have hard borders,
140
+ #is quadratic, has a size that is at least 64 pixels but less than 256
141
+ #pixels and a power of two, create a single texture just for this image.
142
+ if ((border_flags & BF_TILEABLE) == BF_TILEABLE and src_width == src_height and
143
+ (src_width & (src_width - 1)) == 0 and src_width >= 64)
144
+
145
+ texture = Texture.new(src_width, @gl)
146
+ #Use the source bitmap directly if the source area completely covers
147
+ #it.
148
+ if (src_x == 0 and src_width == src.width and src_y == 0 and src_height == src.height)
149
+ data = texture.try_alloc(self, @queues, texture, src, 0)
150
+ else
151
+ trimmed_src = Bitmap.new
152
+ trimmed_src.resize(src_width, src_height)
153
+ trimmed_src.insert(src, 0, 0, src_x, src_y, src_width, src_height)
154
+ data = texture.try_alloc(self, @queues, texture, trimmed_src, 0)
155
+ end
156
+
157
+ if data == nil
158
+ raise "Internal texture block allocation error"
159
+ end
160
+ return data
161
+ end
162
+
163
+ #Too large to fit on a single texture.
164
+ #TODO LargeImageData not implemented yet
165
+ if (src_width > max_size - 2 || src_height > max_size - 2)
166
+ bmp = Bitmap.new(src_width, src_height)
167
+ bmp.insert(src, 0, 0, src_x, src_y, src_width, src_height)
168
+ lidi = LargeImageData.new(self, bmp, max_size - 2, max_size - 2, border_flags)
169
+ return lidi
170
+ end
171
+
172
+ bmp = Bitmap.new
173
+ Gosu::apply_border_flags(bmp, src, src_x, src_y, src_width, src_height, border_flags)
174
+
175
+ #Try to put the bitmap into one of the already allocated textures.
176
+ @textures.each do |tex|
177
+ data = tex.try_alloc(self, @queues, tex, bmp, 1)
178
+ return data if data != nil
179
+ end
180
+
181
+ #All textures are full: Create a new one.
182
+ texture = Texture.new(max_size, @gl)
183
+ @textures.push texture
184
+
185
+ data = texture.try_alloc(self, @queues, texture, bmp, 1)
186
+ if data == nil
187
+ raise "Internal texture block allocation error"
188
+ end
189
+ data
190
+ end
191
+
192
+ def onDrawFrame(gl)
193
+ #gl.glClear(JavaImports::GL10::GL_COLOR_BUFFER_BIT | JavaImports::GL10::GL_DEPTH_BUFFER_BIT)
194
+ @window.do_show
195
+ rescue Exception => e
196
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
197
+ end
198
+
199
+ def onSurfaceChanged(gl, width, height)
200
+ @gl = gl
201
+ @gl.glViewport(0, 0, width, height)
202
+ end
203
+
204
+ def onSurfaceCreated(gl, config)
205
+ @gl = gl
206
+ #@queues.gl = @gl
207
+ @android_initializer.on_ready
208
+ #Options to improve performance
209
+ @gl.glDisable(JavaImports::GL10::GL_DITHER)
210
+ @gl.glHint(JavaImports::GL10::GL_PERSPECTIVE_CORRECTION_HINT, JavaImports::GL10::GL_FASTEST)
211
+
212
+ @gl.glMatrixMode(JavaImports::GL10::GL_PROJECTION)
213
+ @gl.glLoadIdentity
214
+ @gl.glViewport(0, 0, @window.width, @window.height)
215
+
216
+ @gl.glOrthof(0, @window.width, @window.height, 0, -1, 1)
217
+
218
+
219
+ @gl.glMatrixMode(JavaImports::GL10::GL_MODELVIEW)
220
+ @gl.glLoadIdentity
221
+
222
+ @gl.glEnable(JavaImports::GL10::GL_BLEND)
223
+ rescue Exception => e
224
+ puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,27 @@
1
+ module Gosu
2
+ #amDefault -> The color's channels will be interpolated. The alpha channel
3
+ #specifies the opacity of the new color, 255 is full opacity.
4
+
5
+ #amAdd -> The colors' channels will be added. The alpha channel specifies
6
+ #the percentage of the new color's channels that will be added
7
+ #to the old color's channels.
8
+
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
12
+
13
+ FF_BOLD = 1
14
+ FF_ITALIC = 2
15
+ FF_UNDERLINE = 4
16
+ FF_COMBINATIONS = 8
17
+
18
+ TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY = *(0..3)
19
+
20
+ #Flags that affect the tileability of an image
21
+ BF_SMOOTH = 0
22
+ BF_TILEABLE_LEFT = 1
23
+ BF_TILEABLE_TOP = 2
24
+ BF_TILEABLE_RIGHT = 4
25
+ BF_TILEABLE_BOTTOM = 8
26
+ BF_TILEABLE = BF_TILEABLE_LEFT | BF_TILEABLE_TOP | BF_TILEABLE_RIGHT | BF_TILEABLE_BOTTOM
27
+ end
@@ -0,0 +1,151 @@
1
+ require 'gosu_android/graphics/bitmap'
2
+ require 'gosu_android/graphics/color'
3
+ require 'gosu_android/graphics/graphicsBase'
4
+ require 'gosu_android/math'
5
+
6
+ module Gosu
7
+
8
+ class Image
9
+
10
+ def initialize(*args)
11
+ case args.length
12
+ #Argument is ImageData
13
+ when 1
14
+ initialize_1 args[0]
15
+ when 2
16
+ if args[1].class == Bitmap
17
+ initialize_3_bitmap(args[0], args[1])
18
+ else
19
+ initialize_3_file_name(args[0], args[1])
20
+ end
21
+ when 3
22
+ if args[1].class == Bitmap
23
+ initialize_3_bitmap(args[0], args[1], args[2])
24
+ else
25
+ initialize_3_file_name(args[0], args[1], args[2])
26
+ end
27
+ when 6
28
+ if args[1].class == Bitmap
29
+ initialize_7_bitmap(args[0], args[1], args[2], args[3], args[4], args[5])
30
+ else
31
+ initialize_7_file_name(args[0], args[1], args[2], args[3], args[4], args[5])
32
+ end
33
+ when 7
34
+ if args[1].class == Bitmap
35
+ initialize_7_bitmap(args[0], args[1], args[2], args[3], args[4], args[5], args[6])
36
+ else
37
+ initialize_7_file_name(args[0], args[1], args[2], args[3], args[4], args[5], args[6])
38
+ end
39
+ else
40
+ raise ArgumentError
41
+ end
42
+ end
43
+
44
+ #Private initialize methods
45
+ private
46
+ def initialize_1 data
47
+ @data = data
48
+ end
49
+
50
+ def initialize_3_bitmap(window, source, tileable = false)
51
+ initialize_7_bitmap(window, source, 0, 0, source.width, source.height, tileable)
52
+ end
53
+
54
+ def initialize_3_file_name(window, file_name, tileable = false)
55
+ bmp = Gosu::load_image_file(window, file_name)
56
+ initialize_3_bitmap(window, bmp, tileable)
57
+ end
58
+
59
+ def initialize_7_file_name(window, file_name, src_x, src_y, src_width, src_height,
60
+ tileable = false)
61
+ bmp = Gosu::load_image_file(window, file_name)
62
+ initialize_7_bitmap(window, bmp, src_x, src_y, src_width, src_height, tileable)
63
+ end
64
+
65
+ def initialize_7_bitmap(window, source, src_x, src_y, src_width, src_height,
66
+ tileable = false)
67
+ if tileable
68
+ @data = window.create_image(source, src_x, src_y, src_width, src_height, BF_TILEABLE)
69
+ else
70
+ @data = window.create_image(source, src_x, src_y, src_width, src_height, BF_SMOOTH)
71
+ end
72
+ end
73
+
74
+ public
75
+ def width
76
+ @data.width
77
+ end
78
+
79
+ def height
80
+ @data.height
81
+ end
82
+
83
+ def draw(x, y, z, factor_x = 1, factor_y = 1, c = Color::WHITE, mode = AM_DEFAULT)
84
+ 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)
87
+ end
88
+
89
+ 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
+
92
+ size_y = width * factor_x
93
+ size_y = height * factor_y
94
+ offs_x = Gosu::offset_x(angle, 1)
95
+ offs_y = Gosu::offset_y(angle, 1)
96
+
97
+ #Offset to the centers of the original Image's edges when it is rotated
98
+ #by <angle> degrees.
99
+ dist_to_left_x = +offs_y * size_y * center_x
100
+ dist_to_left_y = -offs_x * size_y * center_x
101
+ dist_to_right_x = -offs_y * size_y * (1 - center_x)
102
+ dist_to_right_y = +offs_x * size_y * (1 - center_x)
103
+ dist_to_top_x = +offs_x * size_y * center_y
104
+ dist_to_top_y = +offs_y * size_y * center_y
105
+ dist_to_bottom_x = -offs_x * size_y * (1 - center_y)
106
+ dist_to_bottom_y = -offs_y * size_y * (1 - center_y)
107
+
108
+ @data.draw(x + dist_to_left_x + dist_to_top_x,
109
+ y + dist_to_left_y + dist_to_top_y, c,
110
+ x + dist_to_right_x + dist_to_top_x,
111
+ y + dist_to_right_y + dist_to_top_y, c,
112
+ x + dist_to_left_x + dist_to_bottom_x,
113
+ y + dist_to_left_y + dist_to_bottom_y, c,
114
+ x + dist_to_right_x + dist_to_bottom_x,
115
+ y + dist_to_right_y + dist_to_bottom_y,
116
+ c, z, mode)
117
+ end
118
+
119
+ def self.load_tiles(window, bmp, tile_width, tile_height, tileable)
120
+ images = []
121
+
122
+ #If bmp is a file path
123
+ if bmp.class == String or bmp.class == Fixnum
124
+ bmp = Gosu::load_image_file(window, bmp)
125
+ end
126
+
127
+ if (tile_width > 0)
128
+ tiles_x = bmp.width / tile_width
129
+ else
130
+ tiles_x = -tile_width
131
+ tile_width = bmp.width / tiles_x
132
+ end
133
+
134
+ if (tile_height > 0)
135
+ tiles_y = bmp.height / tile_height
136
+ else
137
+ tiles_y = -tile_height
138
+ tile_height = bmp.height / tiles_y
139
+ end
140
+
141
+ tiles_y.times do |y|
142
+ tiles_x.times do |x|
143
+ images.push Image.new(window, bmp, x * tile_width, y * tile_height, tile_width, tile_height, tileable)
144
+ end
145
+ end
146
+ images
147
+ end
148
+
149
+ end
150
+
151
+ end