gosu 0.8.6-x86-mingw32 → 0.8.7-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/Gosu/Audio.hpp +171 -171
  3. data/Gosu/Bitmap.hpp +96 -96
  4. data/Gosu/Color.hpp +204 -204
  5. data/Gosu/Directories.hpp +36 -36
  6. data/Gosu/Font.hpp +83 -83
  7. data/Gosu/Gosu.hpp +34 -34
  8. data/Gosu/Graphics.hpp +115 -115
  9. data/Gosu/GraphicsBase.hpp +110 -110
  10. data/Gosu/IO.hpp +269 -269
  11. data/Gosu/Image.hpp +122 -122
  12. data/Gosu/ImageData.hpp +61 -61
  13. data/Gosu/Input.hpp +149 -149
  14. data/Gosu/Inspection.hpp +14 -14
  15. data/Gosu/Math.hpp +135 -135
  16. data/Gosu/Platform.hpp +93 -93
  17. data/Gosu/Sockets.hpp +156 -156
  18. data/Gosu/TR1.hpp +56 -56
  19. data/Gosu/Text.hpp +71 -71
  20. data/Gosu/TextInput.hpp +70 -70
  21. data/Gosu/Utility.hpp +28 -28
  22. data/Gosu/Version.hpp +19 -19
  23. data/Gosu/Window.hpp +145 -145
  24. data/examples/ChipmunkIntegration.rb +275 -275
  25. data/examples/CptnRuby.rb +223 -223
  26. data/examples/GosuZen.rb +68 -68
  27. data/examples/MoreChipmunkAndRMagick.rb +155 -155
  28. data/examples/OpenGLIntegration.rb +225 -225
  29. data/examples/RMagickIntegration.rb +417 -417
  30. data/examples/TextInput.rb +154 -154
  31. data/examples/Tutorial.rb +130 -130
  32. data/examples/media/Beep.wav +0 -0
  33. data/examples/media/CptnRuby Map.txt b/data/examples/media/CptnRuby → Map.txt +0 -0
  34. data/examples/media/Explosion.wav +0 -0
  35. data/examples/media/Landscape.svg +9 -9
  36. data/examples/media/Space.png +0 -0
  37. data/examples/media/Star.png +0 -0
  38. data/examples/media/Starfighter.bmp +0 -0
  39. data/lib/1.8/gosu.so +0 -0
  40. data/lib/1.9/gosu.so +0 -0
  41. data/lib/2.0/gosu.so +0 -0
  42. data/lib/2.1/gosu.so +0 -0
  43. data/lib/FreeImage.dll +0 -0
  44. data/lib/OpenAL32.dll +0 -0
  45. data/lib/gosu.rb +19 -16
  46. data/lib/gosu/patches.rb +81 -81
  47. data/lib/gosu/preview.rb +143 -139
  48. data/lib/gosu/run.rb +11 -11
  49. data/lib/gosu/swig_patches.rb +60 -60
  50. data/lib/gosu/zen.rb +89 -89
  51. metadata +5 -5
@@ -1,155 +1,155 @@
1
- # Based on the C Demo3 demonstration distributed with Chipmunk.
2
- # Also with some help from the ChipmunkIntegration.rb program.
3
- #
4
- # License: Same as for Gosu (MIT)
5
- # Created on 21/10/2007, 00:05:19 by Robert Sheehan
6
-
7
- require 'rubygems'
8
- require 'gosu'
9
- require 'chipmunk'
10
- require 'RMagick'
11
-
12
- # Convenience method for converting from radians to a Vec2 vector.
13
- class Numeric
14
- def radians_to_vec2
15
- CP::Vec2.new(Math::cos(self), Math::sin(self))
16
- end
17
- end
18
-
19
- # Layering of sprites
20
- module ZOrder
21
- Background, Box = *0..1
22
- end
23
-
24
- SCREEN_WIDTH = 640
25
- SCREEN_HEIGHT = 480
26
- TICK = 1.0/60.0
27
- NUM_POLYGONS = 80
28
- NUM_SIDES = 4
29
- EDGE_SIZE = 15
30
-
31
- # Everything appears in the Gosu::Window.
32
- class DemoWindow < Gosu::Window
33
-
34
- def initialize
35
- super(SCREEN_WIDTH, SCREEN_HEIGHT, false)
36
- self.caption = "A Chipmunk-RMagick-Gosu Demonstration"
37
- @space = CP::Space.new
38
- @space.iterations = 5
39
- @space.gravity = CP::Vec2.new(0, 100)
40
- # you can replace the background with any image with this line
41
- # background = Magick::ImageList.new( "media/Space.png")
42
- fill = Magick::TextureFill.new(Magick::ImageList.new("granite:"))
43
- background = Magick::Image.new(SCREEN_WIDTH, SCREEN_HEIGHT, fill)
44
- setup_triangles(background)
45
- @background_image = Gosu::Image.new(self, background, true) # turn the image into a Gosu one
46
- @boxes = create_boxes(NUM_POLYGONS)
47
- end
48
-
49
- # Create all of the static triangles.
50
- # Adds them to the space and the background image.
51
- def setup_triangles(background)
52
- gc = Magick::Draw.new
53
- gc.stroke_width(2)
54
- gc.stroke('red')
55
- gc.fill('blue')
56
- # all the triangles are part of the same body
57
- body = CP::Body.new(Float::MAX, Float::MAX)
58
- base = 15
59
- height = 10
60
- shape_vertices = [CP::Vec2.new(-base, base), CP::Vec2.new(base, base), CP::Vec2.new(0, -height)]
61
- # make shapes and images
62
- 9.times do |i|
63
- 6.times do |j|
64
- stagger = (j % 2) * 40
65
- x = i * 80 + stagger
66
- y = j * 70 + 80
67
- shape = CP::Shape::Poly.new(body, shape_vertices, CP::Vec2.new(x, y))
68
- shape.e = 1
69
- shape.u = 1
70
- @space.add_static_shape(shape)
71
- gc.polygon(x - base + 1, y + base - 1, x + base - 1, y + base - 1, x, y - height + 1)
72
- end
73
- end
74
- # do the drawing
75
- gc.draw(background)
76
- end
77
-
78
- # Produces the vertices of a regular polygon.
79
- def polygon_vertices(sides, size)
80
- vertices = []
81
- sides.times do |i|
82
- angle = -2 * Math::PI * i / sides
83
- vertices << angle.radians_to_vec2() * size
84
- end
85
- return vertices
86
- end
87
-
88
- # Produces the image of a polygon.
89
- def polygon_image(vertices)
90
- box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color = 'transparent' }
91
- gc = Magick::Draw.new
92
- gc.stroke('red')
93
- gc.fill('plum')
94
- draw_vertices = vertices.map { |v| [v.x + EDGE_SIZE, v.y + EDGE_SIZE] }.flatten
95
- gc.polygon(*draw_vertices)
96
- gc.draw(box_image)
97
- return Gosu::Image.new(self, box_image, false)
98
- end
99
-
100
- # Produces the polygon objects and adds them to the space.
101
- def create_boxes(num)
102
- box_vertices = polygon_vertices(NUM_SIDES, EDGE_SIZE)
103
- box_image = polygon_image(box_vertices)
104
- boxes = []
105
- num.times do
106
- body = CP::Body.new(1, CP::moment_for_poly(1.0, box_vertices, CP::Vec2.new(0, 0))) # mass, moment of inertia
107
- body.p = CP::Vec2.new(rand(SCREEN_WIDTH), rand(40) - 50)
108
- shape = CP::Shape::Poly.new(body, box_vertices, CP::Vec2.new(0, 0))
109
- shape.e = 0.0
110
- shape.u = 0.4
111
- boxes << Box.new(box_image, body)
112
- @space.add_body(body)
113
- @space.add_shape(shape)
114
- end
115
- return boxes
116
- end
117
-
118
- # All the simulation is done here.
119
- def update
120
- @space.step(TICK)
121
- @boxes.each { |box| box.check_off_screen }
122
- end
123
-
124
- # All the updating of the screen is done here.
125
- def draw
126
- @background_image.draw(0, 0, ZOrder::Background)
127
- @boxes.each { |box| box.draw }
128
- end
129
-
130
- end
131
-
132
- # The falling boxes class.
133
- # Nothing more than a body and an image.
134
- class Box
135
-
136
- def initialize(image, body)
137
- @image = image
138
- @body = body
139
- end
140
-
141
- # If it goes offscreen we put it back to the top.
142
- def check_off_screen
143
- pos = @body.p
144
- if pos.y > SCREEN_HEIGHT + EDGE_SIZE or pos.x > SCREEN_WIDTH + EDGE_SIZE or pos.x < -EDGE_SIZE
145
- @body.p = CP::Vec2.new(rand * SCREEN_WIDTH, 0)
146
- end
147
- end
148
-
149
- def draw
150
- @image.draw_rot(@body.p.x, @body.p.y, ZOrder::Box, @body.a.radians_to_gosu)
151
- end
152
- end
153
-
154
- window = DemoWindow.new
155
- window.show
1
+ # Based on the C Demo3 demonstration distributed with Chipmunk.
2
+ # Also with some help from the ChipmunkIntegration.rb program.
3
+ #
4
+ # License: Same as for Gosu (MIT)
5
+ # Created on 21/10/2007, 00:05:19 by Robert Sheehan
6
+
7
+ require 'rubygems'
8
+ require 'gosu'
9
+ require 'chipmunk'
10
+ require 'RMagick'
11
+
12
+ # Convenience method for converting from radians to a Vec2 vector.
13
+ class Numeric
14
+ def radians_to_vec2
15
+ CP::Vec2.new(Math::cos(self), Math::sin(self))
16
+ end
17
+ end
18
+
19
+ # Layering of sprites
20
+ module ZOrder
21
+ Background, Box = *0..1
22
+ end
23
+
24
+ SCREEN_WIDTH = 640
25
+ SCREEN_HEIGHT = 480
26
+ TICK = 1.0/60.0
27
+ NUM_POLYGONS = 80
28
+ NUM_SIDES = 4
29
+ EDGE_SIZE = 15
30
+
31
+ # Everything appears in the Gosu::Window.
32
+ class DemoWindow < Gosu::Window
33
+
34
+ def initialize
35
+ super(SCREEN_WIDTH, SCREEN_HEIGHT, false)
36
+ self.caption = "A Chipmunk-RMagick-Gosu Demonstration"
37
+ @space = CP::Space.new
38
+ @space.iterations = 5
39
+ @space.gravity = CP::Vec2.new(0, 100)
40
+ # you can replace the background with any image with this line
41
+ # background = Magick::ImageList.new( "media/Space.png")
42
+ fill = Magick::TextureFill.new(Magick::ImageList.new("granite:"))
43
+ background = Magick::Image.new(SCREEN_WIDTH, SCREEN_HEIGHT, fill)
44
+ setup_triangles(background)
45
+ @background_image = Gosu::Image.new(self, background, true) # turn the image into a Gosu one
46
+ @boxes = create_boxes(NUM_POLYGONS)
47
+ end
48
+
49
+ # Create all of the static triangles.
50
+ # Adds them to the space and the background image.
51
+ def setup_triangles(background)
52
+ gc = Magick::Draw.new
53
+ gc.stroke_width(2)
54
+ gc.stroke('red')
55
+ gc.fill('blue')
56
+ # all the triangles are part of the same body
57
+ body = CP::Body.new(Float::MAX, Float::MAX)
58
+ base = 15
59
+ height = 10
60
+ shape_vertices = [CP::Vec2.new(-base, base), CP::Vec2.new(base, base), CP::Vec2.new(0, -height)]
61
+ # make shapes and images
62
+ 9.times do |i|
63
+ 6.times do |j|
64
+ stagger = (j % 2) * 40
65
+ x = i * 80 + stagger
66
+ y = j * 70 + 80
67
+ shape = CP::Shape::Poly.new(body, shape_vertices, CP::Vec2.new(x, y))
68
+ shape.e = 1
69
+ shape.u = 1
70
+ @space.add_static_shape(shape)
71
+ gc.polygon(x - base + 1, y + base - 1, x + base - 1, y + base - 1, x, y - height + 1)
72
+ end
73
+ end
74
+ # do the drawing
75
+ gc.draw(background)
76
+ end
77
+
78
+ # Produces the vertices of a regular polygon.
79
+ def polygon_vertices(sides, size)
80
+ vertices = []
81
+ sides.times do |i|
82
+ angle = -2 * Math::PI * i / sides
83
+ vertices << angle.radians_to_vec2() * size
84
+ end
85
+ return vertices
86
+ end
87
+
88
+ # Produces the image of a polygon.
89
+ def polygon_image(vertices)
90
+ box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color = 'transparent' }
91
+ gc = Magick::Draw.new
92
+ gc.stroke('red')
93
+ gc.fill('plum')
94
+ draw_vertices = vertices.map { |v| [v.x + EDGE_SIZE, v.y + EDGE_SIZE] }.flatten
95
+ gc.polygon(*draw_vertices)
96
+ gc.draw(box_image)
97
+ return Gosu::Image.new(self, box_image, false)
98
+ end
99
+
100
+ # Produces the polygon objects and adds them to the space.
101
+ def create_boxes(num)
102
+ box_vertices = polygon_vertices(NUM_SIDES, EDGE_SIZE)
103
+ box_image = polygon_image(box_vertices)
104
+ boxes = []
105
+ num.times do
106
+ body = CP::Body.new(1, CP::moment_for_poly(1.0, box_vertices, CP::Vec2.new(0, 0))) # mass, moment of inertia
107
+ body.p = CP::Vec2.new(rand(SCREEN_WIDTH), rand(40) - 50)
108
+ shape = CP::Shape::Poly.new(body, box_vertices, CP::Vec2.new(0, 0))
109
+ shape.e = 0.0
110
+ shape.u = 0.4
111
+ boxes << Box.new(box_image, body)
112
+ @space.add_body(body)
113
+ @space.add_shape(shape)
114
+ end
115
+ return boxes
116
+ end
117
+
118
+ # All the simulation is done here.
119
+ def update
120
+ @space.step(TICK)
121
+ @boxes.each { |box| box.check_off_screen }
122
+ end
123
+
124
+ # All the updating of the screen is done here.
125
+ def draw
126
+ @background_image.draw(0, 0, ZOrder::Background)
127
+ @boxes.each { |box| box.draw }
128
+ end
129
+
130
+ end
131
+
132
+ # The falling boxes class.
133
+ # Nothing more than a body and an image.
134
+ class Box
135
+
136
+ def initialize(image, body)
137
+ @image = image
138
+ @body = body
139
+ end
140
+
141
+ # If it goes offscreen we put it back to the top.
142
+ def check_off_screen
143
+ pos = @body.p
144
+ if pos.y > SCREEN_HEIGHT + EDGE_SIZE or pos.x > SCREEN_WIDTH + EDGE_SIZE or pos.x < -EDGE_SIZE
145
+ @body.p = CP::Vec2.new(rand * SCREEN_WIDTH, 0)
146
+ end
147
+ end
148
+
149
+ def draw
150
+ @image.draw_rot(@body.p.x, @body.p.y, ZOrder::Box, @body.a.radians_to_gosu)
151
+ end
152
+ end
153
+
154
+ window = DemoWindow.new
155
+ window.show
@@ -1,226 +1,226 @@
1
- # The tutorial game over a landscape rendered with OpenGL.
2
- # Basically shows how arbitrary OpenGL calls can be put into
3
- # the block given to Window#gl, and that Gosu Images can be
4
- # used as textures using the gl_tex_info call.
5
-
6
- require 'rubygems'
7
- require 'gosu'
8
- require 'gl'
9
- require 'glu'
10
-
11
- include Gl
12
- include Glu
13
-
14
- module ZOrder
15
- Stars, Player, UI = *0..3
16
- end
17
-
18
- # The only really new class here.
19
- # Draws a scrolling, repeating texture with a randomized height map.
20
- class GLBackground
21
- # Height map size
22
- POINTS_X = 7
23
- POINTS_Y = 7
24
- # Scrolling speed
25
- SCROLLS_PER_STEP = 50
26
-
27
- def initialize(window)
28
- @image = Gosu::Image.new(window, "media/Earth.png", true)
29
- @scrolls = 0
30
- @height_map = Array.new(POINTS_Y) { Array.new(POINTS_X) { rand } }
31
- end
32
-
33
- def scroll
34
- @scrolls += 1
35
- if @scrolls == SCROLLS_PER_STEP then
36
- @scrolls = 0
37
- @height_map.shift
38
- @height_map.push Array.new(POINTS_X) { rand }
39
- end
40
- end
41
-
42
- def exec_gl
43
- # Get the name of the OpenGL texture the Image resides on, and the
44
- # u/v coordinates of the rect it occupies.
45
- # gl_tex_info can return nil if the image was too large to fit onto
46
- # a single OpenGL texture and was internally split up.
47
- info = @image.gl_tex_info
48
- return unless info
49
-
50
- # Pretty straightforward OpenGL code.
51
-
52
- glDepthFunc(GL_GEQUAL)
53
- glEnable(GL_DEPTH_TEST)
54
- glEnable(GL_BLEND)
55
-
56
- glMatrixMode(GL_PROJECTION)
57
- glLoadIdentity
58
- glFrustum(-0.10, 0.10, -0.075, 0.075, 1, 100)
59
-
60
- glMatrixMode(GL_MODELVIEW)
61
- glLoadIdentity
62
- glTranslate(0, 0, -4)
63
-
64
- glEnable(GL_TEXTURE_2D)
65
- glBindTexture(GL_TEXTURE_2D, info.tex_name)
66
-
67
- offs_y = 1.0 * @scrolls / SCROLLS_PER_STEP
68
-
69
- 0.upto(POINTS_Y - 2) do |y|
70
- 0.upto(POINTS_X - 2) do |x|
71
- glBegin(GL_TRIANGLE_STRIP)
72
- z = @height_map[y][x]
73
- glColor4d(1, 1, 1, z)
74
- glTexCoord2d(info.left, info.top)
75
- glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)
76
-
77
- z = @height_map[y+1][x]
78
- glColor4d(1, 1, 1, z)
79
- glTexCoord2d(info.left, info.bottom)
80
- glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
81
-
82
- z = @height_map[y][x + 1]
83
- glColor4d(1, 1, 1, z)
84
- glTexCoord2d(info.right, info.top)
85
- glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)
86
-
87
- z = @height_map[y+1][x + 1]
88
- glColor4d(1, 1, 1, z)
89
- glTexCoord2d(info.right, info.bottom)
90
- glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
91
- glEnd
92
- end
93
- end
94
- end
95
- end
96
-
97
- # Roughly adapted from the tutorial game. Always faces north.
98
- class Player
99
- Speed = 7
100
-
101
- attr_reader :score
102
-
103
- def initialize(window, x, y)
104
- @image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
105
- @beep = Gosu::Sample.new(window, "media/Beep.wav")
106
- @x, @y = x, y
107
- @score = 0
108
- end
109
-
110
- def move_left
111
- @x = [@x - Speed, 0].max
112
- end
113
-
114
- def move_right
115
- @x = [@x + Speed, 800].min
116
- end
117
-
118
- def accelerate
119
- @y = [@y - Speed, 50].max
120
- end
121
-
122
- def brake
123
- @y = [@y + Speed, 600].min
124
- end
125
-
126
- def draw
127
- @image.draw(@x - @image.width / 2, @y - @image.height / 2, ZOrder::Player)
128
- end
129
-
130
- def collect_stars(stars)
131
- stars.reject! do |star|
132
- if Gosu::distance(@x, @y, star.x, star.y) < 35 then
133
- @score += 10
134
- @beep.play
135
- true
136
- else
137
- false
138
- end
139
- end
140
- end
141
- end
142
-
143
- # Also taken from the tutorial, but drawn with draw_rot and an increasing angle
144
- # for extra rotation coolness!
145
- class Star
146
- attr_reader :x, :y
147
-
148
- def initialize(animation)
149
- @animation = animation
150
- @color = Gosu::Color.new(0xff000000)
151
- @color.red = rand(255 - 40) + 40
152
- @color.green = rand(255 - 40) + 40
153
- @color.blue = rand(255 - 40) + 40
154
- @x = rand * 800
155
- @y = 0
156
- end
157
-
158
- def draw
159
- img = @animation[Gosu::milliseconds / 100 % @animation.size];
160
- img.draw_rot(@x, @y, ZOrder::Stars, @y, 0.5, 0.5, 1, 1, @color, :add)
161
- end
162
-
163
- def update
164
- # Move towards bottom of screen
165
- @y += 3
166
- # Return false when out of screen (gets deleted then)
167
- @y < 650
168
- end
169
- end
170
-
171
- class GameWindow < Gosu::Window
172
- def initialize
173
- super(800, 600, false)
174
- self.caption = "Gosu & OpenGL Integration Demo"
175
-
176
- @gl_background = GLBackground.new(self)
177
-
178
- @player = Player.new(self, 400, 500)
179
-
180
- @star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
181
- @stars = Array.new
182
-
183
- @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
184
- end
185
-
186
- def update
187
- @player.move_left if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft
188
- @player.move_right if button_down? Gosu::KbRight or button_down? Gosu::GpRight
189
- @player.accelerate if button_down? Gosu::KbUp or button_down? Gosu::GpUp
190
- @player.brake if button_down? Gosu::KbDown or button_down? Gosu::GpDown
191
-
192
- @player.collect_stars(@stars)
193
-
194
- @stars.reject! { |star| !star.update }
195
-
196
- @gl_background.scroll
197
-
198
- @stars.push(Star.new(@star_anim)) if rand(20) == 0
199
- end
200
-
201
- def draw
202
- # gl will execute the given block in a clean OpenGL environment, then reset
203
- # everything so Gosu's rendering can take place again.
204
-
205
- gl do
206
- glClearColor(0.0, 0.2, 0.5, 1.0)
207
- glClearDepth(0)
208
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
209
-
210
- @gl_background.exec_gl
211
- end
212
-
213
- @player.draw
214
- @stars.each { |star| star.draw }
215
- @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
216
- end
217
-
218
- def button_down(id)
219
- if id == Gosu::KbEscape
220
- close
221
- end
222
- end
223
- end
224
-
225
- window = GameWindow.new
1
+ # The tutorial game over a landscape rendered with OpenGL.
2
+ # Basically shows how arbitrary OpenGL calls can be put into
3
+ # the block given to Window#gl, and that Gosu Images can be
4
+ # used as textures using the gl_tex_info call.
5
+
6
+ require 'rubygems'
7
+ require 'gosu'
8
+ require 'gl'
9
+ require 'glu'
10
+
11
+ include Gl
12
+ include Glu
13
+
14
+ module ZOrder
15
+ Stars, Player, UI = *0..3
16
+ end
17
+
18
+ # The only really new class here.
19
+ # Draws a scrolling, repeating texture with a randomized height map.
20
+ class GLBackground
21
+ # Height map size
22
+ POINTS_X = 7
23
+ POINTS_Y = 7
24
+ # Scrolling speed
25
+ SCROLLS_PER_STEP = 50
26
+
27
+ def initialize(window)
28
+ @image = Gosu::Image.new(window, "media/Earth.png", true)
29
+ @scrolls = 0
30
+ @height_map = Array.new(POINTS_Y) { Array.new(POINTS_X) { rand } }
31
+ end
32
+
33
+ def scroll
34
+ @scrolls += 1
35
+ if @scrolls == SCROLLS_PER_STEP then
36
+ @scrolls = 0
37
+ @height_map.shift
38
+ @height_map.push Array.new(POINTS_X) { rand }
39
+ end
40
+ end
41
+
42
+ def exec_gl
43
+ # Get the name of the OpenGL texture the Image resides on, and the
44
+ # u/v coordinates of the rect it occupies.
45
+ # gl_tex_info can return nil if the image was too large to fit onto
46
+ # a single OpenGL texture and was internally split up.
47
+ info = @image.gl_tex_info
48
+ return unless info
49
+
50
+ # Pretty straightforward OpenGL code.
51
+
52
+ glDepthFunc(GL_GEQUAL)
53
+ glEnable(GL_DEPTH_TEST)
54
+ glEnable(GL_BLEND)
55
+
56
+ glMatrixMode(GL_PROJECTION)
57
+ glLoadIdentity
58
+ glFrustum(-0.10, 0.10, -0.075, 0.075, 1, 100)
59
+
60
+ glMatrixMode(GL_MODELVIEW)
61
+ glLoadIdentity
62
+ glTranslate(0, 0, -4)
63
+
64
+ glEnable(GL_TEXTURE_2D)
65
+ glBindTexture(GL_TEXTURE_2D, info.tex_name)
66
+
67
+ offs_y = 1.0 * @scrolls / SCROLLS_PER_STEP
68
+
69
+ 0.upto(POINTS_Y - 2) do |y|
70
+ 0.upto(POINTS_X - 2) do |x|
71
+ glBegin(GL_TRIANGLE_STRIP)
72
+ z = @height_map[y][x]
73
+ glColor4d(1, 1, 1, z)
74
+ glTexCoord2d(info.left, info.top)
75
+ glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)
76
+
77
+ z = @height_map[y+1][x]
78
+ glColor4d(1, 1, 1, z)
79
+ glTexCoord2d(info.left, info.bottom)
80
+ glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
81
+
82
+ z = @height_map[y][x + 1]
83
+ glColor4d(1, 1, 1, z)
84
+ glTexCoord2d(info.right, info.top)
85
+ glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)
86
+
87
+ z = @height_map[y+1][x + 1]
88
+ glColor4d(1, 1, 1, z)
89
+ glTexCoord2d(info.right, info.bottom)
90
+ glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
91
+ glEnd
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ # Roughly adapted from the tutorial game. Always faces north.
98
+ class Player
99
+ Speed = 7
100
+
101
+ attr_reader :score
102
+
103
+ def initialize(window, x, y)
104
+ @image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
105
+ @beep = Gosu::Sample.new(window, "media/Beep.wav")
106
+ @x, @y = x, y
107
+ @score = 0
108
+ end
109
+
110
+ def move_left
111
+ @x = [@x - Speed, 0].max
112
+ end
113
+
114
+ def move_right
115
+ @x = [@x + Speed, 800].min
116
+ end
117
+
118
+ def accelerate
119
+ @y = [@y - Speed, 50].max
120
+ end
121
+
122
+ def brake
123
+ @y = [@y + Speed, 600].min
124
+ end
125
+
126
+ def draw
127
+ @image.draw(@x - @image.width / 2, @y - @image.height / 2, ZOrder::Player)
128
+ end
129
+
130
+ def collect_stars(stars)
131
+ stars.reject! do |star|
132
+ if Gosu::distance(@x, @y, star.x, star.y) < 35 then
133
+ @score += 10
134
+ @beep.play
135
+ true
136
+ else
137
+ false
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ # Also taken from the tutorial, but drawn with draw_rot and an increasing angle
144
+ # for extra rotation coolness!
145
+ class Star
146
+ attr_reader :x, :y
147
+
148
+ def initialize(animation)
149
+ @animation = animation
150
+ @color = Gosu::Color.new(0xff000000)
151
+ @color.red = rand(255 - 40) + 40
152
+ @color.green = rand(255 - 40) + 40
153
+ @color.blue = rand(255 - 40) + 40
154
+ @x = rand * 800
155
+ @y = 0
156
+ end
157
+
158
+ def draw
159
+ img = @animation[Gosu::milliseconds / 100 % @animation.size];
160
+ img.draw_rot(@x, @y, ZOrder::Stars, @y, 0.5, 0.5, 1, 1, @color, :add)
161
+ end
162
+
163
+ def update
164
+ # Move towards bottom of screen
165
+ @y += 3
166
+ # Return false when out of screen (gets deleted then)
167
+ @y < 650
168
+ end
169
+ end
170
+
171
+ class GameWindow < Gosu::Window
172
+ def initialize
173
+ super(800, 600, false)
174
+ self.caption = "Gosu & OpenGL Integration Demo"
175
+
176
+ @gl_background = GLBackground.new(self)
177
+
178
+ @player = Player.new(self, 400, 500)
179
+
180
+ @star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
181
+ @stars = Array.new
182
+
183
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
184
+ end
185
+
186
+ def update
187
+ @player.move_left if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft
188
+ @player.move_right if button_down? Gosu::KbRight or button_down? Gosu::GpRight
189
+ @player.accelerate if button_down? Gosu::KbUp or button_down? Gosu::GpUp
190
+ @player.brake if button_down? Gosu::KbDown or button_down? Gosu::GpDown
191
+
192
+ @player.collect_stars(@stars)
193
+
194
+ @stars.reject! { |star| !star.update }
195
+
196
+ @gl_background.scroll
197
+
198
+ @stars.push(Star.new(@star_anim)) if rand(20) == 0
199
+ end
200
+
201
+ def draw
202
+ # gl will execute the given block in a clean OpenGL environment, then reset
203
+ # everything so Gosu's rendering can take place again.
204
+
205
+ gl do
206
+ glClearColor(0.0, 0.2, 0.5, 1.0)
207
+ glClearDepth(0)
208
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
209
+
210
+ @gl_background.exec_gl
211
+ end
212
+
213
+ @player.draw
214
+ @stars.each { |star| star.draw }
215
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
216
+ end
217
+
218
+ def button_down(id)
219
+ if id == Gosu::KbEscape
220
+ close
221
+ end
222
+ end
223
+ end
224
+
225
+ window = GameWindow.new
226
226
  window.show