gosu_android 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,200 @@
1
+ require 'rake'
2
+
3
+ module Gosu
4
+ module Commands
5
+ module Base
6
+
7
+ def self.show_help
8
+ puts "Usage: gosu_android [OPTION]"
9
+ puts "Adds or deletes dependencies for Gosu in a Ruboto project."
10
+ puts "If no argument is specified it adds the dependencies."
11
+ puts ""
12
+ puts " -a, --add adds the files to the project"
13
+ puts " -d, --delete deletes the files from the project"
14
+ puts " -h, --help display this help and exit"
15
+ puts ""
16
+ puts "The options -a and -d can not be given together."
17
+ puts ""
18
+ puts "Exit status:"
19
+ puts " 0 if everything went ok,"
20
+ puts " 1 if there was some error."
21
+ end
22
+
23
+ def self.add_files
24
+ #root -> user directory
25
+ root = Dir.pwd
26
+ #gem root -> where the gem is located
27
+ gem_root = File.expand_path(File.dirname(__FILE__))
28
+ gem_root.slice! "/commands"
29
+
30
+ #Get all the files needed for gosu android
31
+ gem_root_s = gem_root + "/*"
32
+ gem_root_ss = gem_root + "/*/*"
33
+
34
+ #List of not usefull files
35
+ lib_files = FileList[ gem_root_s, gem_root_ss ].to_a
36
+ not_copy_files = ["commands", "description", "version"]
37
+
38
+ #Do not copy the ones that are not needed
39
+ not_copy_files.each do |not_copy|
40
+ lib_files.delete_if do |element|
41
+ element.include? not_copy
42
+ end
43
+ end
44
+
45
+ #Copy the files
46
+ lib_files.each do |file|
47
+ src = String.new file
48
+ file.slice!(gem_root)
49
+ dst = root + '/src/gosu_android' + file
50
+ FileUtils.mkdir_p(File.dirname(dst))
51
+ if file.include? ".rb"
52
+ FileUtils.cp(src, dst)
53
+ end
54
+ end
55
+
56
+ #FIXME This is a dirty way to delete de last
57
+ #gosu_android from the gem_root variable
58
+
59
+ #Main file and jar dependencies go in different folders
60
+ #than normal files
61
+ gem_root = gem_root + "/"
62
+ gem_root.slice! "/gosu_android/"
63
+
64
+ src = gem_root + "/gosu_android.rb"
65
+ dst = root + "/src/gosu_android.rb"
66
+ FileUtils.cp(src, dst)
67
+ src = gem_root + "/gosu.java.jar"
68
+ dst = root + "/libs/gosu.java.jar"
69
+ FileUtils.cp(src, dst)
70
+
71
+ #Resources files
72
+ gem_root = gem_root + "_"
73
+ gem_root.slice! "/lib_"
74
+ gem_root_s = gem_root + "/res/*"
75
+ gem_root_ss = gem_root + "/res/*/**"
76
+
77
+ #Get all resources
78
+ lib_files = FileList[ gem_root_s, gem_root_ss ].to_a
79
+
80
+ #Copy the resources
81
+ lib_files.each do |file|
82
+ src = String.new file
83
+ file.slice!(gem_root)
84
+ dst = root + file
85
+ FileUtils.mkdir_p(File.dirname(dst))
86
+ if file.include? "."
87
+ FileUtils.cp(src, dst)
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+ def self.delete_files
94
+ root = Dir.pwd
95
+
96
+ #Deleting whole gosu_android folder
97
+ to_delete = root + "/src/gosu_android"
98
+ begin
99
+ FileUtils.rm_rf to_delete
100
+ rescue => e
101
+ $stderr.puts e.message
102
+ end
103
+
104
+ #Deleting gosu_android.rb file
105
+ to_delete = root + "/src/gosu_android.rb"
106
+ begin
107
+ File.delete to_delete
108
+ rescue => e
109
+ $stderr.puts e.message
110
+ end
111
+
112
+ #Deleting gosu.java.jar file
113
+ to_delete = root + "/libs/gosu.java.jar"
114
+ begin
115
+ File.delete to_delete
116
+ rescue => e
117
+ $stderr.puts e.message
118
+ end
119
+
120
+ #gem root -> where the gem is located
121
+ gem_root = File.expand_path(File.dirname(__FILE__))
122
+ gem_root.slice! "/commands"
123
+
124
+ gem_root = gem_root + "/"
125
+ gem_root.slice! "/gosu_android/"
126
+
127
+ #Resources files
128
+ gem_root = gem_root + "_"
129
+ gem_root.slice! "/lib_"
130
+ gem_root_s = gem_root + "/res/*"
131
+ gem_root_ss = gem_root + "/res/*/**"
132
+
133
+ #Get all resources
134
+ lib_files = FileList[ gem_root_s, gem_root_ss ].to_a
135
+
136
+ #Delete only the previous copied resources
137
+ lib_files.each do |file|
138
+ file.slice!(gem_root)
139
+ to_delete = root + file
140
+ if file.include? "."
141
+ begin
142
+ File.delete to_delete
143
+ rescue => e
144
+ $stderr.puts e.message
145
+ end
146
+ end
147
+ end
148
+
149
+ end
150
+
151
+ def self.main
152
+ add = false
153
+ delete = false
154
+ help = false
155
+ ARGV.each do|a|
156
+ case a
157
+ when "-a"
158
+ add = true
159
+ when "--add"
160
+ add = true
161
+ when "-d"
162
+ delete = true
163
+ when "--delete"
164
+ delete = true
165
+ when "-h"
166
+ help = true
167
+ when "--help"
168
+ help = true
169
+ end
170
+ end
171
+
172
+ if help
173
+ show_help
174
+ exit 0
175
+ end
176
+
177
+ if add and delete
178
+ $stderr.puts "Add and delete can not be perform at the same time\n"
179
+ exit 1
180
+ end
181
+
182
+ if not add and not delete
183
+ add = true
184
+ end
185
+
186
+ if add
187
+ add_files
188
+ exit 0
189
+ end
190
+
191
+ if delete
192
+ delete_files
193
+ exit 0
194
+ end
195
+ end
196
+
197
+ end
198
+ end
199
+ end
200
+
@@ -0,0 +1,5 @@
1
+ module Gosu
2
+ DESCRIPTION = <<EOF
3
+ Gosu-Android is an implementation for Android devices of the multiplatform Gosu library.
4
+ EOF
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'gosu_android/requires'
2
+ require 'gosu_android/graphics/color'
3
+
4
+ module Gosu
5
+ #Custom java files
6
+ java_import 'gosu.java.Bitmap'
7
+
8
+ #TODO define load_image with reader argument
9
+ def self.load_image_file(window, file_name)
10
+ Gosu::Bitmap.new(window.activity.getApplicationContext, file_name)
11
+ end
12
+ end
@@ -0,0 +1,51 @@
1
+ require 'gosu_android/graphics/graphicsBase'
2
+
3
+ module Gosu
4
+ def self.apply_border_flags(dest, source, src_x, src_y, src_width, src_height, border_flags)
5
+ dest.resize(src_width + 2, src_height + 2)
6
+
7
+ #The borders are made "harder" by duplicating the original bitmap's
8
+ #borders.
9
+
10
+ #Top.
11
+ if (border_flags & BF_TILEABLE_TOP)
12
+ dest.insert(source, 1, 0, src_x, src_y, src_width, 1)
13
+ end
14
+ #Bottom.
15
+ if (border_flags & BF_TILEABLE_BOTTOM)
16
+ dest.insert(source, 1, dest.height - 1,
17
+ src_x, src_y + src_height - 1, src_width, 1)
18
+ end
19
+ #Left.
20
+ if (border_flags & BF_TILEABLE_LEFT)
21
+ dest.insert(source, 0, 1, src_x, src_y, 1, src_height)
22
+ end
23
+ #Right.
24
+ if (border_flags & BF_TILEABLE_RIGHT)
25
+ dest.insert(source, dest.width - 1, 1,
26
+ src_x + src_width - 1, src_y, 1, src_height)
27
+ end
28
+ #Top left.
29
+ if ((border_flags & BF_TILEABLE_TOP) and (border_flags & BF_TILEABLE_LEFT))
30
+ dest.set_pixel(0, 0,
31
+ source.get_pixel(src_x, src_y))
32
+ end
33
+ #Top right.
34
+ if ((border_flags & BF_TILEABLE_TOP) and (border_flags & BF_TILEABLE_RIGHT))
35
+ dest.set_pixel(dest.width - 1, 0,
36
+ source.get_pixel(src_x + src_width - 1, src_y))
37
+ end
38
+ #Bottom left.
39
+ if ((border_flags & BF_TILEABLE_BOTTOM) and (border_flags & BF_TILEABLE_LEFT))
40
+ dest.set_pixel(0, dest.height - 1,
41
+ source.get_pixel(src_x, src_y + src_height - 1))
42
+ end
43
+ #Bottom right.
44
+ if ((border_flags & BF_TILEABLE_BOTTOM) and (border_flags & BF_TILEABLE_RIGHT))
45
+ dest.set_pixel(dest.width - 1, dest.height - 1,
46
+ source.get_pixel(src_x + src_width - 1, src_y + src_height - 1))
47
+ end
48
+ #Now put the final image into the prepared borders.
49
+ dest.insert(source, 1, 1, src_x, src_y, src_width, src_height)
50
+ end
51
+ end
@@ -0,0 +1,107 @@
1
+ module Gosu
2
+ class BlockAllocator
3
+ private
4
+ class Impl < Struct.new(:width, :height, :blocks, :first_x, :first_y, :max_w, :max_h)
5
+ alias :old_initialize :initialize
6
+
7
+ def initialize(*args)
8
+ old_initialize *args
9
+ if self[:blocks] == nil
10
+ self[:blocks] = Array.new
11
+ end
12
+ end
13
+
14
+ def mark_block_used(block, a_width, a_height)
15
+ self[:first_x] += a_width
16
+ if (self[:first_x] + a_width) >= self[:width]
17
+ self[:first_x] = 0
18
+ self[:first_y] += a_height
19
+ end
20
+ self[:blocks].push block
21
+ end
22
+
23
+ def is_block_free(block)
24
+ #(The right-th column and the bottom-th row are outside of the block.)
25
+ right = block.left + block.width
26
+ bottom = block.top + block.height
27
+
28
+ #Block isn't valid.
29
+ if (right > self[:width] || bottom > self[:height])
30
+ return false
31
+ end
32
+
33
+ #Test if the block collides with any existing rects.
34
+ self[:blocks].each do |i|
35
+ if (i.left < right and block.left < i.left + i.width and
36
+ i.top < bottom and block.top < i.top + i.height)
37
+ return false
38
+ end
39
+ end
40
+ true
41
+ end
42
+
43
+ end
44
+
45
+ public
46
+ class Block < Struct.new(:left, :top, :width, :height); end
47
+
48
+ def initialize(width, height)
49
+ @pimpl = Impl.new(width, height, nil, 0, 0, width, height)
50
+ end
51
+
52
+ def width
53
+ @pimpl.width
54
+ end
55
+
56
+ def height
57
+ @pimpl.height
58
+ end
59
+
60
+ def alloc(a_width, a_height)
61
+ #The rect wouldn't even fit onto the texture!
62
+ if a_width > width || a_height > height
63
+ return [false]
64
+ end
65
+ #We know there's no space left.
66
+ if a_width > @pimpl.max_w && a_height > @pimpl.max_h
67
+ return [false]
68
+ end
69
+ #Start to look for a place next to the last returned rect. Chances are
70
+ #good we'll find a place there.
71
+ b = Block.new(@pimpl.first_x, @pimpl.first_y, a_width, a_height)
72
+ if @pimpl.is_block_free(b)
73
+ @pimpl.mark_block_used(b, a_width, a_height)
74
+ return [true, b]
75
+ end
76
+
77
+ b.top = 0
78
+ b.left = 0
79
+ #Brute force: Look for a free place on this texture.
80
+ while(b.top <= (height - a_height)) do
81
+ while(b.left <= (width - a_width)) do
82
+ if @pimpl.is_block_free(b)
83
+ #Found a nice place!
84
+
85
+ #Try to make up for the large for()-stepping.
86
+ while (b.top > 0 and @pimpl.is_block_free(Block.new(b.left, b.top - 1, a_width, a_height))) do
87
+ b.top -= 1
88
+ end
89
+ while (b.left > 0 and @pimpl.is_block_free(Block.new(b.left - 1, b.top, a_width, a_height))) do
90
+ b.left -= 1
91
+ end
92
+ @pimpl.mark_block_used(b, a_width, a_height)
93
+ return [true, b]
94
+ end
95
+ b.top += 16
96
+ b.left += 8
97
+ end
98
+ end
99
+
100
+ #So there was no space for the bitmap. Remember this for later.
101
+ @pimpl.max_w = a_width - 1
102
+ @pimpl.max_h = a_height - 1
103
+ return [false]
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,27 @@
1
+ #require 'math.rb'
2
+
3
+ module Gosu
4
+
5
+ class HSV < Struct.new(:h, :s, :v)
6
+ end
7
+
8
+ #Custom java files
9
+ java_import 'gosu.java.Color'
10
+
11
+ Gosu::Color::NONE = Color.new(0x00000000)
12
+ Gosu::Color::BLACK = Color.new(0xff000000)
13
+ Gosu::Color::GRAY = Color.new(0xff808080)
14
+ Gosu::Color::WHITE = Color.new(0xffffffff)
15
+ Gosu::Color::AQUA = Color.new(0xff00ffff)
16
+ Gosu::Color::RED = Color.new(0xffff0000)
17
+ Gosu::Color::GREEN = Color.new(0xff00ff00)
18
+ Gosu::Color::BLUE = Color.new(0xff0000ff)
19
+ Gosu::Color::YELLOW = Color.new(0xffffff00)
20
+ Gosu::Color::FUCHSIA = Color.new(0xffff00ff)
21
+ Gosu::Color::CYAN = Color.new(0xff00ffff)
22
+
23
+ class Color
24
+ GL_FORMAT = 0x1908
25
+ end
26
+
27
+ end
@@ -0,0 +1,21 @@
1
+ module Gosu
2
+ NO_TEXTURE = -1
3
+ NO_CLIPPING = 0xffffffff
4
+ #In various places in Gosu, width==NO_CLIPPING conventionally means
5
+ #that no clipping should happen.
6
+ java_import 'gosu.java.ClipRect'
7
+
8
+ def self.is_p_to_the_left_of_ab(xa, ya, xb, yb, xp, yp)
9
+ ((xb - xa) * (yp - ya) - (xp - xa) * (yb - ya)) > 0
10
+ end
11
+
12
+ def self.reorder_coordinates_if_necessary(x1, y1, x2, y2, x3, y3, c3, x4, y4, c4)
13
+ if (Gosu::is_p_to_the_left_of_ab(x1, y1, x2, y2, x3, y3) ==
14
+ Gosu::is_p_to_the_left_of_ab(x2, y2, x3, y3, x4, y4))
15
+ true
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,6 @@
1
+ module Gosu
2
+ java_import 'gosu.java.Vertex'
3
+ java_import 'gosu.java.DrawOp'
4
+ java_import 'gosu.java.DrawOpPool'
5
+ end
6
+
@@ -0,0 +1,39 @@
1
+ require 'gosu_android/graphics/renderState'
2
+
3
+ module Gosu
4
+ class DrawOpQueue
5
+ attr_reader :op_pool
6
+ def initialize(gl)
7
+ @ops = []
8
+ @gl = gl
9
+ @op_pool = DrawOpPool.new(@gl, 50)
10
+ end
11
+
12
+ def gl= gl
13
+ @gl = gl
14
+ end
15
+
16
+ def schedule_draw_op(op)
17
+ #TODO Should do more stuff, check original code
18
+ @ops.push op
19
+ end
20
+
21
+ def perform_draw_ops_and_code
22
+ #Sort by z
23
+ @ops.sort!
24
+ manager = RenderStateManager.new(@gl)
25
+ @ops.each do |op|
26
+ manager.render_state = op.render_state
27
+ op.perform(nil) if op.vertices_or_block_index >= 0
28
+ end
29
+ @op_pool.clearPool
30
+ end
31
+
32
+
33
+ def clear_queue
34
+ @ops = []
35
+ @op_pool.clearPool
36
+ end
37
+
38
+ end
39
+ end