bg_utils 0.0.12

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/bg_utils.rb +221 -0
  3. metadata +72 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f434ea68b6ae90960ef9ceee640b6549827b10371b18baf11ec37d277fac7985
4
+ data.tar.gz: d54767202d44528e50cd85f8a6f776d3565d3931b75f91ebd946038d95823718
5
+ SHA512:
6
+ metadata.gz: d53fb52ba9b8031c4ef299c36227e169ad5a6cd93e4ab675e0284d2f06892ef8c7e16e3e77e4b3320455775c920a536720451a9a120e79f6e870221df95323e1
7
+ data.tar.gz: 632bf01a7ce5bab1191b0a6e794383948b3539b145ae9fa7300c059352492c82d90ac4ba9c4a88b836de733b40ebfd2c186ebf27b72870f6e45d35903fa7410c
@@ -0,0 +1,221 @@
1
+ module BgUtils
2
+ class Skybox
3
+ def initialize(filename)
4
+ # filename is supposed to lead to a composed texture like below :
5
+ # X
6
+ # XXXX
7
+ # X
8
+
9
+ texture = Gosu::Image.new(filename, :retro=>true)
10
+ @tile_size = texture.width / 4
11
+ texture = nil # we don't need it anymore
12
+ @textures = {
13
+ :y_pos => Gosu::Image.new(filename, :rect => [@tile_size, 0, @tile_size, @tile_size]),
14
+ :x_neg => Gosu::Image.new(filename, :rect => [0, @tile_size, @tile_size, @tile_size]),
15
+ :z_neg => Gosu::Image.new(filename, :rect => [@tile_size, @tile_size, @tile_size, @tile_size]),
16
+ :x_pos => Gosu::Image.new(filename, :rect => [2 * @tile_size, @tile_size, @tile_size, @tile_size]),
17
+ :z_pos => Gosu::Image.new(filename, :rect => [3 * @tile_size, @tile_size, @tile_size, @tile_size]),
18
+ :y_neg => Gosu::Image.new(filename, :rect => [@tile_size, 2 * @tile_size, @tile_size, @tile_size])
19
+ }
20
+ self.gen_list
21
+ end
22
+
23
+ def switch_texture(face)
24
+ tex = @textures[face].gl_tex_info
25
+ glBindTexture(GL_TEXTURE_2D, tex.tex_name)
26
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
27
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
28
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
29
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
30
+ return tex
31
+ end
32
+
33
+ def gen_list
34
+ @display_list = glGenLists(1)
35
+ glNewList(@display_list, GL_COMPILE)
36
+ glDepthMask(GL_FALSE)
37
+ tex = switch_texture(:z_neg)
38
+ glBegin(GL_QUADS)
39
+ glTexCoord2d(tex.left, tex.top); glVertex3f(-1, 1, -1)
40
+ glTexCoord2d(tex.left, tex.bottom); glVertex3f(-1, -1, -1)
41
+ glTexCoord2d(tex.right, tex.bottom); glVertex3f(1, -1, -1)
42
+ glTexCoord2d(tex.right, tex.top); glVertex3f(1, 1, -1)
43
+ glEnd
44
+ tex = switch_texture(:z_pos)
45
+ glBindTexture(GL_TEXTURE_2D, tex.tex_name)
46
+ glBegin(GL_QUADS)
47
+ glTexCoord2d(tex.left, tex.top); glVertex3f(1, 1, 1)
48
+ glTexCoord2d(tex.left, tex.bottom); glVertex3f(1, -1, 1)
49
+ glTexCoord2d(tex.right, tex.bottom); glVertex3f(-1, -1, 1)
50
+ glTexCoord2d(tex.right, tex.top); glVertex3f(-1, 1, 1)
51
+ glEnd
52
+ tex = switch_texture(:x_neg)
53
+ glBindTexture(GL_TEXTURE_2D, tex.tex_name)
54
+ glBegin(GL_QUADS)
55
+ glTexCoord2d(tex.left, tex.top); glVertex3f(-1, 1, 1)
56
+ glTexCoord2d(tex.left, tex.bottom); glVertex3f(-1, -1, 1)
57
+ glTexCoord2d(tex.right, tex.bottom); glVertex3f(-1, -1, -1)
58
+ glTexCoord2d(tex.right, tex.top); glVertex3f(-1, 1, -1)
59
+ glEnd
60
+ tex = switch_texture(:x_pos)
61
+ glBindTexture(GL_TEXTURE_2D, tex.tex_name)
62
+ glBegin(GL_QUADS)
63
+ glTexCoord2d(tex.left, tex.top); glVertex3f(1, 1, -1)
64
+ glTexCoord2d(tex.left, tex.bottom); glVertex3f(1, -1, -1)
65
+ glTexCoord2d(tex.right, tex.bottom); glVertex3f(1, -1, 1)
66
+ glTexCoord2d(tex.right, tex.top); glVertex3f(1, 1, 1)
67
+ glEnd
68
+ tex = switch_texture(:y_pos)
69
+ glBindTexture(GL_TEXTURE_2D, tex.tex_name)
70
+ glBegin(GL_QUADS)
71
+ glTexCoord2d(tex.left, tex.top); glVertex3f(-1, 1, 1)
72
+ glTexCoord2d(tex.left, tex.bottom); glVertex3f(-1, 1, -1)
73
+ glTexCoord2d(tex.right, tex.bottom); glVertex3f(1, 1, -1)
74
+ glTexCoord2d(tex.right, tex.top); glVertex3f(1, 1, 1)
75
+ glEnd
76
+ tex = switch_texture(:y_neg)
77
+ glBindTexture(GL_TEXTURE_2D, tex.tex_name)
78
+ glBegin(GL_QUADS)
79
+ glTexCoord2d(tex.left, tex.top); glVertex3f(-1, -1, -1)
80
+ glTexCoord2d(tex.left, tex.bottom); glVertex3f(-1, -1, 1)
81
+ glTexCoord2d(tex.right, tex.bottom); glVertex3f(1, -1, 1)
82
+ glTexCoord2d(tex.right, tex.top); glVertex3f(1, -1, -1)
83
+ glEnd
84
+ glDepthMask(GL_TRUE)
85
+ glEndList
86
+ end
87
+
88
+ def draw(x = 0, y = 0, z = 0, rot_pitch = 0, rot_yaw = 0)
89
+ glPushMatrix
90
+ glTranslatef(x, y, z)
91
+ glRotatef(rot_pitch, 1, 0, 0)
92
+ glRotatef(rot_yaw, 0, 1, 0)
93
+ glCallList(@display_list)
94
+ glPopMatrix
95
+
96
+ end
97
+ end
98
+
99
+ class EmptyImageSource
100
+ private
101
+ def initialize(columns, rows, gosu_color)
102
+ @columns, @rows = columns, rows
103
+ @gosu_color = gosu_color
104
+ end
105
+
106
+ def columns; @columns; end
107
+ def rows; @rows; end
108
+
109
+ def to_blob
110
+ color = [@gosu_color.red, @gosu_color.green, @gosu_color.blue, @gosu_color.alpha]
111
+ color.pack("C*") * @columns * @rows
112
+ end
113
+ end
114
+
115
+ public
116
+ def self.get_pixel(gosu_image, x, y)
117
+ if x < 0 or x >= gosu_image.width or y < 0 or y >= gosu_image.height
118
+ return nil
119
+ else
120
+ result = gosu_image.to_blob[(y * gosu_image.width + x) * 4, 4].unpack("C*")
121
+ return Gosu::Color.new(result[3], result[0], result[1], result[2])
122
+ end
123
+ end
124
+
125
+ def self.set_pixel(gosu_image, x, y, color, brush_size = 1)
126
+ one_pixel_image = Gosu::Image.new(EmptyImageSource.new(brush_size.floor, brush_size.floor, color))
127
+ gosu_image.insert(one_pixel_image, x.floor, y.floor)
128
+ end
129
+
130
+ class TiledImage
131
+ attr_reader :width, :height, :scale_x, :scale_y
132
+ def initialize(gosu_image, width, height, scale_x = 1.0, scale_y = 1.0, color = 0xff_ffffff, mode = :default, options = {:retro => true})
133
+ options[:tileable] = true
134
+ if gosu_image.is_a?(Gosu::Image) or gosu_image.is_a?(String)
135
+ @image = (gosu_image.is_a?(Gosu::Image)) ? gosu_image : Gosu::Image.new(gosu_image, options)
136
+ else
137
+ raise("Wrong parameter 0 for 'Image' : \ngiven : #{gosu_image}\nshould be either a Gosu::Image instance or a string that contains path and filename")
138
+ end
139
+
140
+ @width, @height, @scale_x, @scale_y = width, height, scale_x, scale_y
141
+ @color, @mode = color, mode
142
+ end
143
+
144
+ def draw(x, y, z, scale_x = 1, scale_y = 1)
145
+ @drawing_macro ||= create_macro
146
+ @drawing_macro.draw(x, y, z, scale_x, scale_y)
147
+ end
148
+
149
+ def draw_rot(x, y, z, angle, center_x = 0.5, center_y = 0.5, scale_x = 1, scale_y = 1)
150
+ @drawing_macro ||= create_macro
151
+ @drawing_macro.draw_rot(x, y, z, angle, center_x, center_y, scale_x, scale_y)
152
+ end
153
+
154
+ def draw_as_quad(x1, y1, x2, y2, x3, y3, x4, y4, z)
155
+ @drawing_macro ||= create_macro
156
+ c = Gosu::Color::WHITE
157
+ @drawing_macro.draw_as_quad(x1, y1, c, x2, y2, c, x3, y3, c, x4, y4, c, z)
158
+ end
159
+
160
+ private
161
+ def create_macro
162
+ Gosu::record(@width, @height) do
163
+ tile_width = @image.width * @scale_x
164
+ tile_height = @image.height * @scale_y
165
+ c = @color
166
+
167
+ # filling with as many full tiles as possible
168
+ for tile_x in 0...(@width / tile_width).floor
169
+ for tile_y in 0...(@height / tile_height).floor
170
+ @image.draw(tile_x * tile_width, tile_y * tile_height, 0, @scale_x, @scale_y, c, @mode)
171
+ end
172
+ end
173
+
174
+ # filling extra width with part of the tile if needed
175
+ if (extra_width = @width % tile_width) > 0
176
+ subimage_width = (extra_width / @scale_x).floor
177
+ subimage = @image.subimage(0, 0, subimage_width, @image.height)
178
+ for tile_y in 0...(@height / tile_height).floor
179
+ subimage.draw_as_quad(
180
+ @width - extra_width, tile_y * tile_height, c,
181
+ @width, tile_y * tile_height, c,
182
+ @width - extra_width, (tile_y + 1) * tile_height, c,
183
+ @width, (tile_y + 1) * tile_height, c,
184
+ 0, @mode
185
+ )
186
+ end
187
+ end
188
+
189
+ # filling extra height with part of the tile if needed
190
+ if (extra_height = @height % tile_height) > 0
191
+ subimage_height = (extra_height / @scale_y).floor
192
+ subimage = @image.subimage(0, 0, @image.width, subimage_height)
193
+ for tile_x in 0...(@width / tile_width).floor
194
+ subimage.draw_as_quad(
195
+ tile_x * tile_width, @height - extra_height, c,
196
+ (tile_x + 1) * tile_width, @height - extra_height, c,
197
+ tile_x * tile_width, @height, c,
198
+ (tile_x + 1) * tile_width, @height, c,
199
+ 0, @mode
200
+ )
201
+ end
202
+ end
203
+
204
+ # if there were extra pixels in both axis, we have to draw a bottom right corner
205
+ if (extra_width > 0 and extra_height > 0)
206
+ subimage_width = (extra_width / @scale_x).floor
207
+ subimage_height = (extra_height / @scale_y).floor
208
+ subimage = @image.subimage(0, 0, subimage_width, subimage_height)
209
+
210
+ subimage.draw_as_quad(
211
+ @width - extra_width, @height - extra_height, c,
212
+ @width, @height - extra_height, c,
213
+ @width - extra_width, @height, c,
214
+ @width, @height, c,
215
+ 0, @mode
216
+ )
217
+ end
218
+ end
219
+ end
220
+ end
221
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bg_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
+ platform: ruby
6
+ authors:
7
+ - bestguigui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gosu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opengl-bindings
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.6
41
+ description: Adds some functionalities to the Ruby Gosu framework
42
+ email: guillaume.quillet@yahoo.fr
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/bg_utils.rb
48
+ homepage: https://github.com/bestguigui
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.7.6
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A Ruby Gosu gem extension
72
+ test_files: []