openrgss 0.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/openrgss.rb ADDED
@@ -0,0 +1,19 @@
1
+ require_relative 'openrgss/rgss'
2
+
3
+ require_relative 'openrgss/bitmap'
4
+ require_relative 'openrgss/color'
5
+ require_relative 'openrgss/font'
6
+ require_relative 'openrgss/plane'
7
+ require_relative 'openrgss/rect'
8
+ require_relative 'openrgss/sprite'
9
+ require_relative 'openrgss/table'
10
+ require_relative 'openrgss/tilemap'
11
+ require_relative 'openrgss/tone'
12
+ require_relative 'openrgss/viewport'
13
+ require_relative 'openrgss/window'
14
+ require_relative 'openrgss/rgsserror'
15
+ require_relative 'openrgss/rgssreset'
16
+
17
+ require_relative 'openrgss/audio'
18
+ require_relative 'openrgss/graphics'
19
+ require_relative 'openrgss/input'
@@ -0,0 +1,109 @@
1
+ # The module that carries out music and sound processing.
2
+ module Audio
3
+ class <<self
4
+
5
+ # Prepares MIDI playback by DirectMusic.
6
+ #
7
+ # A method of the processing at startup in RGSS2 for enabling execution at any time.
8
+ #
9
+ # MIDI playback is possible without calling this method, but in Windows Vista or later, a delay of 1 to 2 seconds will result at playback.
10
+
11
+ def setup_mdi
12
+
13
+ end
14
+
15
+ # Starts BGM playback. Specifies the file name, volume, pitch, and playback starting position in that order.
16
+ #
17
+ # The playback starting position (RGSS3) is only valid for ogg or wav files.
18
+ #
19
+ # Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
20
+
21
+ def bgm_play(filename, volume=100, pitch=100, pos=0)
22
+ SDL::Mixer.play_music SDL::Mixer::Music.load(RGSS.get_file(filename)), -1
23
+ end
24
+
25
+ # Stops BGM playback.
26
+
27
+ def bgm_stop
28
+ SDL::Mixer.halt_music
29
+ end
30
+
31
+ # Starts BGM fadeout. time is the length of the fadeout in milliseconds.
32
+
33
+ def bgm_fade(time)
34
+ SDL::Mixer.fade_out_music(time)
35
+ end
36
+
37
+ # Gets the playback position of the BGM. Only valid for ogg or wav files. Returns 0 when not valid.
38
+
39
+ def bgm_pos
40
+
41
+ end
42
+
43
+ # Starts BGS playback. Specifies the file name, volume, pitch, and playback starting position in that order.
44
+ #
45
+ # The playback starting position (RGSS3) is only valid for ogg or wav files.
46
+ #
47
+ # Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
48
+
49
+ def bgs_play(filename, volume=100, pitch=100, pos=0)
50
+ @bgs_channel = SDL::Mixer.play_channel(-1, SDL::Mixer::Wave.load(RGSS.get_file(filename)), -1)
51
+ end
52
+
53
+ # Stops BGS playback.
54
+
55
+ def bgs_stop
56
+ SDL::Mixer.halt(@bgs_channel) if @bgs_channel
57
+ end
58
+
59
+ # Starts BGS fadeout. time is the length of the fadeout in milliseconds.
60
+
61
+ def bgs_fade(time)
62
+ SDL::Mixer.fade_out(@bgs_channel, time) if @bgs_channel
63
+ end
64
+
65
+ # Gets the playback position of the BGS. Only valid for ogg or wav files. Returns 0 when not valid.
66
+
67
+ def bgs_pos
68
+
69
+ end
70
+
71
+ # Starts ME playback. Sets the file name, volume, and pitch in turn.
72
+ #
73
+ # Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
74
+ #
75
+ # When ME is playing, the BGM will temporarily stop. The timing of when the BGM restarts is slightly different from RGSS1.
76
+
77
+ def me_play(filename, volume=100, pitch=100)
78
+ @me_channel = SDL::Mixer.play_channel(-1, SDL::Mixer::Wave.load(RGSS.get_file(filename)), 0)
79
+ end
80
+
81
+ # Stops ME playback.
82
+
83
+ def me_stop
84
+ SDL::Mixer.halt(@me_channel) if @me_channel
85
+ end
86
+
87
+ # Starts ME fadeout. time is the length of the fadeout in milliseconds.
88
+
89
+ def me_fade(time)
90
+ SDL::Mixer.fade_out(@me_channel, time) if @me_channel
91
+ end
92
+
93
+ # Starts SE playback. Sets the file name, volume, and pitch in turn.
94
+ #
95
+ # Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
96
+ #
97
+ # When attempting to play the same SE more than once in a very short period, they will automatically be filtered to prevent choppy playback
98
+
99
+ def se_play(filename, volume=100, pitch=100)
100
+ @se_channel = SDL::Mixer.play_channel(-1, SDL::Mixer::Wave.load(RGSS.get_file(filename)), 0)
101
+ end
102
+
103
+ # Stops all SE playback.
104
+
105
+ def se_stop
106
+ SDL::Mixer.halt(@se_channel) if @me_channel
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,255 @@
1
+ # The bitmap class. Bitmaps represent images.
2
+ #
3
+ # Sprites (Sprite) and other objects must be used to display bitmaps onscreen.
4
+
5
+ class Bitmap
6
+ attr_accessor :font, :entity, :text
7
+
8
+ # :call-seq:
9
+ # Bitmap.new(filename)
10
+ # Bitmap.new(width, height)
11
+ #
12
+ # Loads the graphic file specified in filename or size and creates a bitmap object.
13
+ #
14
+ # Also automatically searches files included in RGSS-RTP and encrypted archives. File extensions may be omitted.
15
+
16
+ def initialize(width, height=nil)
17
+ @entity = if width.is_a? String
18
+ filename = width
19
+ filepath = RGSS.get_file(filename)
20
+ Log.debug('load bitmap') { filepath }
21
+ SDL::Surface.load(filepath).display_format_alpha
22
+ else
23
+ big_endian = ([1].pack("N") == [1].pack("L"))
24
+ if big_endian
25
+ rmask = 0xff000000
26
+ gmask = 0x00ff0000
27
+ bmask = 0x0000ff00
28
+ amask = 0x000000ff
29
+ else
30
+ rmask = 0x000000ff
31
+ gmask = 0x0000ff00
32
+ bmask = 0x00ff0000
33
+ amask = 0xff000000
34
+ end
35
+ SDL::Surface.new(SDL::SWSURFACE|SDL::SRCALPHA, width, height, 32, rmask, gmask, bmask, amask)
36
+ end
37
+ @font = Font.new
38
+ # @text = [] ~
39
+ end
40
+
41
+ # Frees the bitmap. If the bitmap has already been freed, does nothing.
42
+
43
+ def dispose
44
+ @entity.destroy
45
+ end
46
+
47
+ # Returns true if the bitmap has been freed.
48
+
49
+ def disposed?
50
+ @entity.destroyed?
51
+ end
52
+
53
+ # Gets the bitmap width.
54
+
55
+ def width
56
+ @entity.w
57
+ end
58
+
59
+ # Gets the bitmap height.
60
+
61
+ def height
62
+ @entity.h
63
+ end
64
+
65
+ # Gets the bitmap rectangle (Rect).
66
+
67
+ def rect
68
+ Rect.new(0, 0, width, height)
69
+ end
70
+
71
+ def clone
72
+ b =Bitmap.new(width, height)
73
+ b.entity = @entity.copyRect(0, 0, width, height)
74
+ b.font =Font.clone
75
+ return b
76
+ end
77
+
78
+ # Performs a block transfer from the src_bitmap box src_rect (Rect) to the specified bitmap coordinates (x, y).
79
+ #
80
+ # opacity can be set from 0 to 255.
81
+
82
+ def blt(x, y, src_bitmap, src_rect, opacity=255)
83
+ src_bitmap.entity.set_alpha(SDL::RLEACCEL, opacity)
84
+ SDL::Surface.blit(src_bitmap.entity, src_rect.x, src_rect.y, src_rect.width, src_rect.height, @entity, x, y)
85
+ src_bitmap.entity.set_alpha(SDL::SRCALPHA|SDL::RLEACCEL, 255)
86
+ end
87
+
88
+ # Performs a block transfer from the src_bitmap box src_rect (Rect) to the specified bitmap box dest_rect (Rect).
89
+ #
90
+ # opacity can be set from 0 to 255.
91
+
92
+ def stretch_blt(dest_rect, src_bitmap, src_rect, opacity=255)
93
+ src_bitmap.entity.set_alpha(SDL::RLEACCEL, opacity)
94
+ SDL::Surface.transform_blit(src_bitmap.entity, @entity, 0, src_rect.width.to_f / dest_rect.width, src_rect.height.to_f / dest_rect.height, src_rect.x, src_rect.y, dest_rect.x, dest_rect.y, SDL::Surface::TRANSFORM_AA)
95
+ src_bitmap.entity.set_alpha(SDL::SRCALPHA|SDL::RLEACCEL, 255)
96
+ end
97
+
98
+ # :call-seq:
99
+ # fill_rect(x, y, width, height, color)
100
+ # fill_rect(rect, color)
101
+ #
102
+ # Fills the bitmap box (x, y, width, height) or rect (Rect) with color (Color).
103
+
104
+ def fill_rect(x, y, width=nil, height=nil, color=nil)
105
+ if x.is_a? Rect
106
+ rect = x
107
+ color = y
108
+ x = rect.x
109
+ y = rect.y
110
+ width = rect.width
111
+ height = rect.height
112
+ end
113
+ @entity.fill_rect(x, y, width, height, @entity.map_rgba(color.red, color.green, color.blue, color.alpha))
114
+ end
115
+
116
+ # :call-seq:
117
+ # gradient_fill_rect(x, y, width, height, color1, color2[, vertical])
118
+ # gradient_fill_rect(rect, color1, color2[, vertical])
119
+ #
120
+ # Fills in this bitmap box (x, y, width, height) or rect (Rect) with a gradient from color1 (Color) to color2 (Color).
121
+ #
122
+ # Set vertical to true to create a vertical gradient. Horizontal gradient is the default.
123
+
124
+ def gradient_fill_rect(x, y, width, height=false, color1=nil, color2=nil, vertical=false)
125
+ if x.is_a? Rect
126
+ rect = x
127
+ color1 = y
128
+ color2 = width
129
+ vertical = height
130
+ x = rect.x
131
+ y = rect.y
132
+ width = rect.width
133
+ height = rect.height
134
+ end
135
+ if vertical
136
+ height.times do |i|
137
+ @entity.fill_rect(x, y+i, width, 1, @entity.map_rgba(
138
+ color1.red + (color2.red - color1.red) * i / height,
139
+ color1.green + (color2.green - color1.green) * i / height,
140
+ color1.blue + (color2.blue - color1.blue) * i / height,
141
+ color1.alpha + (color2.alpha - color1.alpha) * i / height
142
+ ))
143
+ end
144
+ else
145
+ width.times do |i|
146
+ @entity.fill_rect(x+i, y, 1, height, @entity.map_rgba(
147
+ color1.red + (color2.red - color1.red) * i / width,
148
+ color1.green + (color2.green - color1.green) * i / width,
149
+ color1.blue + (color2.blue - color1.blue) * i / width,
150
+ color1.alpha + (color2.alpha - color1.alpha) * i / width
151
+ ))
152
+ end
153
+ end
154
+ end
155
+
156
+ # Clears the entire bitmap.
157
+
158
+ def clear
159
+ @entity.fill_rect(0, 0, width, height, 0x00000000)
160
+ end
161
+
162
+ # Clears this bitmap box or (x, y, width, height) or rect (Rect).
163
+
164
+ def clear_rect(x, y=nil, width=nil, height=nil)
165
+ if x.is_a? Rect
166
+ rect = x
167
+ x = rect.x
168
+ y = rect.y
169
+ width = rect.width
170
+ height = rect.height
171
+ end
172
+ @entity.fill_rect(x, y, width, height, 0x00000000)
173
+ end
174
+
175
+ # Gets the color (Color) at the specified pixel (x, y).
176
+
177
+ def get_pixel(x, y)
178
+
179
+ color = @entity.format.getRGBA(@entity.get_pixel(x, y))
180
+ return Color.new(color[0], color[1], color[2], color[3])
181
+ #Color.new((color & @entity.Rmask) >> @entity.Rshift, (color & @entity.Gmask) >> @entity.Gshift, (color & @entity.Bmask) >> @entity.Bshift, (color & @entity.Amask) >> @entity.Ashift)
182
+ end
183
+
184
+ # Sets the specified pixel (x, y) to color (Color).
185
+
186
+ def set_pixel(x, y, color)
187
+ @entity.put_pixel(x, y, @entity.map_rgba(color.red, color.green, color.blue, color.alpha))
188
+ end
189
+
190
+ # Changes the bitmap's hue within 360 degrees of displacement.
191
+ #
192
+ # This process is time-consuming. Furthermore, due to conversion errors, repeated hue changes may result in color loss.
193
+
194
+ def hue_change(hue)
195
+
196
+ end
197
+
198
+ # Applies a blur effect to the bitmap. This process is time consuming.
199
+
200
+ def blur
201
+
202
+ end
203
+
204
+ # Applies a radial blur to the bitmap. angle is used to specify an angle from 0 to 360. The larger the number, the greater the roundness.
205
+ #
206
+ # division is the division number (from 2 to 100). The larger the number, the smoother it will be. This process is very time consuming.
207
+
208
+ def radial_blur(angle, division)
209
+
210
+ end
211
+
212
+ # Draws the string str in the bitmap box (x, y, width, height) or rect (Rect).
213
+ #
214
+ # If str is not a character string object, it will be converted to a character string using the to_s method before processing is performed.
215
+ #
216
+ # If the text length exceeds the box's width, the text width will automatically be reduced by up to 60 percent.
217
+ #
218
+ # Horizontal text is left-aligned by default. Set align to 1 to center the text and to 2 to right-align it. Vertical text is always centered.
219
+ #
220
+ # As this process is time-consuming, redrawing the text with every frame is not recommended.
221
+
222
+ def draw_text(x, y, width=0, height=nil, str=nil, align=0 )
223
+ if x.is_a? Rect
224
+ rect = x
225
+ str = y
226
+ align = width
227
+
228
+ x = rect.x
229
+ y = rect.y
230
+ width = rect.width
231
+ height = rect.height
232
+ end
233
+
234
+ str = str.to_s
235
+ if align == 2
236
+ x += width - @font.entity.text_size(str)[0]
237
+ elsif align == 1
238
+ x += (width - @font.entity.text_size(str)[0]) / 2
239
+ end
240
+
241
+ # @text << [str, x, y, @font.color.red, @font.color.green, @font.color.blue] See you ~
242
+ tmp = @font.entity.render_blended_utf8(str, @font.color.red, @font.color.green, @font.color.blue)
243
+ tmp.set_alpha(SDL::RLEACCEL ,0)
244
+ @entity.put tmp,x,y
245
+ #SDL::Surface.transformBlit tmp,@entity,0,1,1,0,0,x, y,SDL::Surface::TRANSFORM_AA|SDL::Surface::TRANSFORM_SAFE|SDL::Surface::TRANSFORM_SAFE
246
+ end
247
+
248
+ # Gets the box (Rect) used when drawing the string str with the draw_text method. Does not include the outline portion (RGSS3) and the angled portions of italicized text.
249
+ #
250
+ # If str is not a character string object, it will be converted to a character string using the to_s method before processing is performed.
251
+
252
+ def text_size(str)
253
+ Rect.new 0, 0, *@font.entity.text_size(str)
254
+ end
255
+ end
@@ -0,0 +1,106 @@
1
+ # The RGBA color class. Each component is handled with a floating-point value (Float).
2
+ class Color
3
+
4
+ # :call-seq:
5
+ # Color.new(red, green, blue[, alpha])
6
+ # Color.new
7
+ #
8
+ # Creates a Color object. If alpha is omitted, it is assumed to be 255.
9
+ #
10
+ # The default values when no arguments are specified are (0, 0, 0, 0).
11
+
12
+ def initialize(red=0, green=0, blue=0, alpha = 255)
13
+ @red = red
14
+ @blue = blue
15
+ @green = green
16
+ @alpha = alpha
17
+ end
18
+
19
+ # :call-seq:
20
+ # set(red, green, blue[, alpha])
21
+ # set(color)
22
+ #
23
+ # Sets all components at once.
24
+ #
25
+ # The second format copies all the components from a separate Color object.
26
+
27
+ def set(red, blue=0, green=0, alpha = 255)
28
+ if red.is_a? Color
29
+ color = red
30
+ @red = color.red
31
+ @blue = color.blue
32
+ @green = color.green
33
+ @alpha = color.alpha
34
+ else
35
+ @red = red
36
+ @blue = blue
37
+ @green = green
38
+ @alpha = alpha
39
+ end
40
+ return self
41
+ end
42
+
43
+ # The red value (0-255). Out-of-range values are automatically corrected.
44
+ attr_accessor :red
45
+
46
+ # The green value (0-255). Out-of-range values are automatically corrected.
47
+ attr_accessor :blue
48
+
49
+ # The blue value (0-255). Out-of-range values are automatically corrected.
50
+ attr_accessor :green
51
+
52
+ # The alpha value (0-255). Out-of-range values are automatically corrected.
53
+ attr_accessor :alpha
54
+
55
+ def to_s() # :nodoc:
56
+ "(#{@red}, #{@blue}, #{@green}, #{@alpha})"
57
+ end
58
+
59
+ def _dump(depth = 0) # :nodoc:
60
+ [@red, @green, @blue, @alpha].pack('D*')
61
+ end
62
+
63
+ def self._load(string) # :nodoc:
64
+ self.new(*string.unpack('D*')) #fix by zh99998
65
+ end
66
+
67
+ def red=(val) # :nodoc:
68
+ @red = [[0, val].max, 255].min
69
+ end
70
+
71
+ def blue=(val) # :nodoc:
72
+ @blue = [[0, val].max, 255].min
73
+ end
74
+
75
+ def green=(val) # :nodoc:
76
+ @green = [[0, val].max, 255].min
77
+ end
78
+
79
+ def alpha=(val) # :nodoc:
80
+ @alpha = [[0, val].max, 255].min
81
+ end
82
+
83
+ def ==(other) # :nodoc:
84
+ raise TypeError.new("can't convert #{other.class} into Color") unless self.class == other.class
85
+ return @red == other.red &&
86
+ @green == other.green &&
87
+ @blue == other.blue &&
88
+ @alpha == other.alpha
89
+ end
90
+
91
+ def ===(other) # :nodoc:
92
+ raise TypeError.new("can't convert #{other.class} into Color") unless self.class == other.class
93
+ return @red == other.red &&
94
+ @green == other.green &&
95
+ @blue == other.blue &&
96
+ @alpha == other.alpha
97
+ end
98
+
99
+ def egl?(other) # :nodoc:
100
+ raise TypeError.new("can't convert #{other.class} into Color") unless self.class == other.class
101
+ return @red == other.red &&
102
+ @green == other.green &&
103
+ @blue == other.blue &&
104
+ @alpha == other.alpha
105
+ end
106
+ end