04-09-2013, 11:50 AM
Mostly ported from OpenRGSS hidden classes ;)
SDL Audio implementation
Code:
require 'rubygems'
require 'rubysdl'
#**Part of OpenRGSS
# The module that carries out music and sound processing.
module Audio
class <
# Prepares MIDI playback by DirectMusic.
#
# A method of the processing at startup in RGSS2 for enabling execution at any time.
#
# 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.
def setup_mdi
end
# Starts BGM playback. Specifies the file name, volume, pitch, and playback starting position in that order.
#
# The playback starting position (RGSS3) is only valid for ogg or wav files.
#
# Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
def bgm_play(filename, volume=100, pitch=100, pos=0)
SDL::Mixer.play_music SDL::Mixer::Music.load(RGSS.get_file(filename)), -1 rescue puts($!)
end
# Stops BGM playback.
def bgm_stop
SDL::Mixer.halt_music
end
# Starts BGM fadeout. time is the length of the fadeout in milliseconds.
def bgm_fade(time)
SDL::Mixer.fade_out_music(time)
end
# Gets the playback position of the BGM. Only valid for ogg or wav files. Returns 0 when not valid.
def bgm_pos
end
# Starts BGS playback. Specifies the file name, volume, pitch, and playback starting position in that order.
#
# The playback starting position (RGSS3) is only valid for ogg or wav files.
#
# Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
def bgs_play(filename, volume=100, pitch=100, pos=0)
@bgs_channel = SDL::Mixer.play_channel(-1, SDL::Mixer::Wave.load(RGSS.get_file(filename)), -1) rescue puts($!)
end
# Stops BGS playback.
def bgs_stop
SDL::Mixer.halt(@bgs_channel) if @bgs_channel
end
# Starts BGS fadeout. time is the length of the fadeout in milliseconds.
def bgs_fade(time)
SDL::Mixer.fade_out(@bgs_channel, time) if @bgs_channel
end
# Gets the playback position of the BGS. Only valid for ogg or wav files. Returns 0 when not valid.
def bgs_pos
end
# Starts ME playback. Sets the file name, volume, and pitch in turn.
#
# Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
#
# When ME is playing, the BGM will temporarily stop. The timing of when the BGM restarts is slightly different from RGSS1.
def me_play(filename, volume=100, pitch=100)
@me_channel = SDL::Mixer.play_channel(-1, SDL::Mixer::Wave.load(RGSS.get_file(filename)), 0) rescue puts($!)
end
# Stops ME playback.
def me_stop
SDL::Mixer.halt(@me_channel) if @me_channel
end
# Starts ME fadeout. time is the length of the fadeout in milliseconds.
def me_fade(time)
SDL::Mixer.fade_out(@me_channel, time) if @me_channel
end
# Starts SE playback. Sets the file name, volume, and pitch in turn.
#
# Also automatically searches files included in RGSS-RTP. File extensions may be omitted.
#
# When attempting to play the same SE more than once in a very short period, they will automatically be filtered to prevent choppy playback
def se_play(filename, volume=100, pitch=100)
@se_channel = SDL::Mixer.play_channel(-1, SDL::Mixer::Wave.load(RGSS.get_file(filename)), 0) rescue puts($!)
end
# Stops all SE playback.
def se_stop
SDL::Mixer.halt(@se_channel) if @me_channel
end
end
end
Font implementation
Code:
#**Part of OpenRGSS
# The font class. Font is a property of the Bitmap class.
#
# If there is a "Fonts" folder directly under the game folder, the font files in it can be used even if they are not installed on the system.
#
# You can change the default values set for each component when a new Font object is created.
#
# Font.default_name = ["Myriad", "Verdana"]
# Font.default_size = 22
# Font.default_bold = true
class Font
@@cache = {}
# Creates a Font object.
def initialize(arg_name=@@default_name, arg_size=@@default_size)
@name = arg_name
@size = arg_size
@bold = @@default_bold
@italic= @@default_italic
@color = @@default_color
end
# Returns true if the specified font exists on the system.
def Font.exist?(arg_font_name)
font_key = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts'
reg_open_keyex = Win32API.new('Advapi32', 'RegOpenKeyEx', 'lpllp', 'l')
reg_enum_value = Win32API.new('Advapi32', 'RegEnumValue', 'llppiiii', 'l')
reg_close_key = Win32API.new('Advapi32', 'RegCloseKey', 'l', 'l')
open_key = [0].pack('V')
# get key
reg_open_keyex.call(0x80000002, font_key, 0, 0x20019, open_key)
open_key = (open_key + [0].pack('V')).unpack('V').first
# enumerate
buffer = "\0"*256
buff_size = [255].pack('l')
key_i = 0
font_names= []
while (reg_enum_value.call(open_key, key_i, buffer, buff_size, 0, 0, 0, 0).zero?)
# get name
font_names << buffer[0, buff_size.unpack('l').first].sub(/\s\(.*\)/, '')
# reset
buff_size = [255].pack('l')
# increment
key_i += 1
end
reg_close_key.call(open_key)
# test
return font_names.include?(arg_font_name)
end
# SDL::TTF对象
def entity
result = @@cache[[@name, @size]] ||= SDL::TTF.open('wqy-microhei.ttc', @size)
result.style = (@bold ? SDL::TTF::STYLE_BOLD : 0) | (@italic ? SDL::TTF::STYLE_ITALIC : 0)
result
end
# The font name. Include an array of strings to specify multiple fonts to be used in a desired order.
#
# font.name = ["Myriad", "Verdana"]
# In this example, if the higher priority font Myriad does not exist on the system, the second choice Verdana will be used instead.
#
# The default is ["Verdana", "Arial", "Courier New"].
attr_accessor :name
# The font size. The default is 24.
attr_accessor :size
# The bold flag. The default is FALSE.
attr_accessor :bold
# The italic flag. The default is FALSE.
attr_accessor :italic
# The flag for outline text. The default is TRUE.
attr_accessor :outline
# The flag for shadow text. The default is false (RGSS3). When enabled, a black shadow will be drawn to the bottom right of the character.
attr_accessor :shadow
# The font color (Color). Alpha values may also be used. The default is (255,255,255,255).
#
# Alpha values are also used when drawing outline (RGSS3) and shadow text.
attr_accessor :color
# The outline color (Color). The default is (0,0,0,128).
attr_accessor :out_color
class < [:name, :size, :bold, :italic, :color, :outline, :shadow, :out_color].each { |attribute|
name = 'default_' + attribute.to_s
define_method(name) { class_variable_get('@@'+name) }
define_method(name+'=') { |value| class_variable_set('@@'+name, value) }
}
end
#------------------------------------------------------------------------
# * Standard Einstellungen aus der RGSS102E.dll.
#------------------------------------------------------------------------
@@default_name = "Arial"
@@default_size = 22
@@default_bold = false
@@default_italic= false
@@default_color = Color.new(255, 255, 255, 255)
end
Plane implementation
Code:
#**Part of OpenRGSS
# The Plane class. Planes are special sprites that tile bitmap patterns across the entire screen and are used to display parallax backgrounds and so on.
class Plane
# Refers to the bitmap (Bitmap) used in the plane.
#attr_accessor :bitmap
# Refers to the viewport (Viewport) associated with the plane.
#attr_accessor :viewport
# The plane's visibility. If TRUE, the plane is visible. The default value is TRUE.
#attr_accessor :visible
# The plane's z-coordinate. The larger the value, the closer to the player the plane will be displayed.
#
# If multiple objects share the same z-coordinate, the more recently created object will be displayed closest to the player.
#attr_accessor :z
# The x-coordinate of the plane's starting point. Change this value to scroll the plane.
attr_accessor :ox
# The y-coordinate of the plane's starting point. Change this value to scroll the plane.
attr_accessor :oy
# The plane's x-axis zoom level. 1.0 denotes actual pixel size.
#attr_accessor :zoom_x
# The plane's y-axis zoom level. 1.0 denotes actual pixel size.
#attr_accessor :zoom_y
# The plane's opacity (0-255). Out-of-range values are automatically corrected.
#attr_accessor :opacity
# The plane's blending mode (0: normal, 1: addition, 2: subtraction).
#attr_accessor :blend_type
# The color (Color) to be blended with the plane. Alpha values are used in the blending ratio.
#attr_accessor :color
# The plane's color tone (Tone).
#attr_accessor :tone
# Creates a Plane object. Specifies a viewport (Viewport) when necessary.
def initialize(arg_viewport=nil)
@sprite = Sprite.new(arg_viewport)
@src_bitmap = nil
end
# Frees the plane. If the plane has already been freed, does nothing.
def dispose
@sprite.bitmap.dispose unless @sprite.bitmap.nil? or @sprite.bitmap.disposed?
@sprite.dispose unless @sprite.nil? or @sprite.disposed?
end
# Returns TRUE if the plane has been freed.
def disposed?
@sprite.nil? or @sprite.disposed?
end
def ox=(val)
@sprite.ox= (val % (@src_bitmap.nil? ? 1 : @src_bitmap.width))
end
def oy=(val)
@sprite.oy= (val % (@src_bitmap.nil? ? 1 : @src_bitmap.height))
end
def bitmap
@src_bitmap
end
def bitmap=(arg_bmp)
vp_width = @sprite.viewport.nil? ? \
Graphics.width : @sprite.viewport.rect.width
vp_height = @sprite.viewport.nil? ? \
Graphics.height : @sprite.viewport.rect.height
x_steps = [(vp_width / arg_bmp.width).ceil, 1].max * 2
y_steps = [(vp_height / arg_bmp.height).ceil, 1].max * 2
bmp_width = x_steps * arg_bmp.width
bmp_height = y_steps * arg_bmp.height
@src_bitmap = arg_bmp
@sprite.bitmap.dispose unless @sprite.bitmap.nil? or @sprite.bitmap.disposed?
@sprite.bitmap = Bitmap.new(bmp_width, bmp_height)
x_steps.times { |ix| y_steps.times { |iy|
@sprite.bitmap.blt(ix * arg_bmp.width, iy * arg_bmp.height,
@src_bitmap, @src_bitmap.rect)
} }
end
def method_missing(symbol, *args)
@sprite.method(symbol).call(*args)
end
end
SDL Graphics implementation
Code:
#**Part of OpenRGSS
require 'rubygems'
require 'rubysdl'
module Graphics
FPS_COUNT = 30
@frame_rate = 60
@frame_count = 0
@frame_count_recent = 0
@skip = 0
@ticks = 0
@fps_ticks = 0
@brightness = 255
@width = 640
@height = 480
@graphics_render_target = Bitmap.new(@width, @height) # need rebuild when resize.
@freeze = false # or true?
class < attr_reader :width, :height
attr_accessor :entity
attr_accessor :frame_rate, :frame_count, :brightness
attr_reader :real_fps
def resize_screen(width, height)
@width = width
@height = height
end
def update
RGSS.update
@frame_count += 1
if @skip >= 10 or SDL.get_ticks < @ticks + 1000 / frame_rate
@entity.fill_rect(0, 0, @width, @height, 0x000000)
if (@old_resources!=RGSS.resources) # Maybe here can make a dirty mark
RGSS.resources.sort!
@old_resources=RGSS.resources.clone
end
unless @freezed # redraw only when !freezed
@graphics_render_target.entity.fill_rect(0, 0, @width, @height, @graphics_render_target.entity.map_rgba(0, 0, 0, 255))
@graphics_render_target.entity.set_alpha(SDL::RLEACCEL, 0)
RGSS.resources.each { |resource| resource.draw(@graphics_render_target) }
end
@entity.put @graphics_render_target.entity, 0, 0
@entity.drawRect(0, 0, @width, @height, @entity.map_rgb(0, 0, 0), true, 255-@brightness) # draw gray layout
@entity.update_rect(0, 0, 0, 0)
sleeptime = @ticks + 1000.0 / frame_rate - SDL.get_ticks
sleep sleeptime.to_f / 1000 if sleeptime > 0
@skip = 0
@ticks = SDL.get_ticks
else
@skip += 1
end
@frame_count_recent += 1
if @frame_count_recent >= FPS_COUNT
@frame_count_recent = 0
now = SDL.get_ticks
@real_fps = FPS_COUNT * 1000 / (now - @fps_ticks)
@fps_ticks = now
end
end
def wait(duration)
duration.times { update }
end
def fadeout(duration)
step=255/duration
duration.times { |i| @brightness=255-i*step; update }
@brightness=0
end
def fadein(duration)
step=255/duration
duration.times { |i| @brightness=i*step; update }
@brightness=255
end
def freeze
@freezed=true
end
def transition(duration=10, filename=nil, vague=40)
if (duration==0)
@freezed=false; return;
end
if (filename.nil?) #
imgmap=Array.new(@width) { Array.new(@height) { 255 } }
else #
b =Bitmap.new(filename); pfb = b.entity.format
imgmap=Array.new(@width) { |x| Array.new(@height) { |y| [pfb.get_rgb(b.entity[x, y])[0], 1].max } }
#TODO : free
end
step =255/duration
new_frame = Bitmap.new(@width, @height)
RGSS.resources.sort!
@old_resources=RGSS.resources.clone
new_frame.entity.fill_rect(0, 0, @width, @height, new_frame.entity.map_rgba(0, 0, 0, 255))
new_frame.entity.set_alpha(SDL::SRCALPHA, 255)
RGSS.resources.each { |resource| resource.draw(new_frame) }
# draw frame to bitmap
pf = new_frame.entity.format
new_frame.entity.lock
picmap=Array.new(@width) { |x| Array.new(@height) { |y| pf.getRGBA(new_frame.entity[x, y]) } } # better to use bit calculate
new_frame.entity.unlock
maker = Bitmap.new(@width, @height)
# create pre-render layoyt
maker.entity.fill_rect(0, 0, @width, @height, new_frame.entity.map_rgba(0, 0, 0, 255))
maker.entity.put @graphics_render_target.entity, 0, 0
# transition layout
new_frame.entity.lock
@width.times { |x| @height.times { |y|
if (imgmap[x][y]!=0)
new_frame.entity[x, y]=pf.map_rgba(picmap[x][y][0], picmap[x][y][1], picmap[x][y][2], [255/(duration/(255.0/imgmap[x][y])), 255].min)
#TODO : alpha will be 255 after many render.it's different fron RPG Maker
else
new_frame.entity[x, y]=pf.map_rgba(picmap[x][y][0], picmap[x][y][1], picmap[x][y][2], 255)
end
} }
new_frame.entity.unlock
duration.times { |i|
@entity.fill_rect(0, 0, @width, @height, 0x000000)
maker.entity.put new_frame.entity, 0, 0 # alpha
@entity.put maker.entity, 0, 0
@entity.update_rect(0, 0, 0, 0)
}
# TODO: free
@graphics_render_target.entity.set_alpha(0, 255); @freezed=false; @brightness=255; update
end
def snap_to_bitmap
return @graphics_render_target.clone # free
end
def frame_reset
end
def play_movie(filename)
end
def brightness=(brightness)
@brightness = brightness < 0 ? 0 : brightness > 255 ? 255 : brightness
#gamma = @brightness.to_f / 255
#SDL::Screen.set_gamma(5,1,1)
#seems SDL::Screen.set_gamma and SDL::Screen.set_gamma_rmap doesn't work
end
end
end
SDL Sprite implementation
Code:
#**Part of OpenRGSS
require 'rubygems'
require 'rubysdl'
# The sprite class. Sprites are the basic concept used to display characters and other objects on the game screen.
class Sprite
# Refers to the bitmap (Bitmap) used for the sprite's starting point.
attr_accessor :bitmap
# The box (Rect) taken from a bitmap.
attr_accessor :src_rect
# Refers to the viewport (Viewport) associated with the sprite.
attr_accessor :viewport
# The x-coordinate of the sprite's starting point.
attr_accessor :ox
# The y-coordinate of the sprite's starting point.
attr_accessor :oy
# The sprite's x-axis zoom level. 1.0 denotes actual pixel size.
attr_accessor :zoom_x
# The sprite's y-axis zoom level. 1.0 denotes actual pixel size.
attr_accessor :zoom_y
# The sprite's angle of rotation. Specifies up to 360 degrees of counterclockwise rotation. However, drawing a rotated sprite is time-consuming, so avoid overuse.
attr_accessor :angle
# Defines the amplitude, frequency, speed, and phase of the wave effect. A raster scroll effect is achieved by using a sinusoidal function to draw the sprite with each line's horizontal position slightly different from the last.
#
# wave_amp is the wave amplitude and wave_length is the wave frequency, and each is specified by a number of pixels.
#
# wave_speed specifies the speed of the wave animation. The default is 360, and the larger the value, the faster the effect.
#
# wave_phase specifies the phase of the top line of the sprite using an angle of up to 360 degrees. This is updated each time the update method is called. It is not necessary to use this property unless it is required for two sprites to have their wave effects synchronized.
attr_accessor :wave_amp
attr_accessor :wave_length
attr_accessor :wave_speed
attr_accessor :wave_phase
# A flag denoting the sprite has been flipped horizontally. If TRUE, the sprite will be drawn flipped. The default is false.
attr_accessor :mirror
# The bush depth and opacity of a sprite. This can be used to represent a situation such as the character's legs being hidden by bushes.
#
# For bush_depth, the number of pixels for the bush section is specified. The default value is 0.
#
# For bush_opacity, the opacity of the bush section from 0 to 255 is specified. Out-of-range values will be corrected automatically. The default value is 128.
#
# The bush_opacity value will be multiplied by opacity. For example, if both opacity and bush_opacity are set to 128, it will be handled as a transparency on # top of a transparency, for an actual opacity of 64.
attr_accessor :bush_depth
attr_accessor :bush_opacity
# The sprite's opacity (0-255). Out-of-range values are automatically corrected.
attr_accessor :opacity
# The sprite's blending mode (0: normal, 1: addition, 2: subtraction).
attr_accessor :blend_type
# The color (Color) to be blended with the sprite. Alpha values are used in the blending ratio.
#
# Handled separately from the color blended into a flash effect. However, the color with the higher alpha value when displayed will have the higher priority when blended.
attr_accessor :color
# The sprite's color tone (Tone).
attr_accessor :tone
include RGSS::Drawable
# Creates a new sprite object. Specifies a viewport (Viewport) when necessary.
def initialize(viewport=nil)
@src_rect = Rect.new(0, 0, 0, 0)
@x = 0
@y = 0
@z = 0
@ox = 0
@oy = 0
@zoom_x = 1
@zoom_y = 1
@angle = 0
@src_rect = Rect.new
@color = Color.new
@tone = Tone.new
@opacity = 255
@visible = true
super(viewport)
end
def bitmap=(bitmap)
@src_rect.set(0, 0, bitmap.width, bitmap.height) if bitmap
@bitmap = bitmap
end
# Begins flashing the sprite. duration specifies the number of frames flashing will last.
#
# If color is set to nil, the sprite will disappear while flashing.
def flash(color, duration)
end
# Advances the sprite flash or wave phase. As a general rule, this method is called once per frame.
#
# It is not necessary to call this if a flash or wave is not needed.
def update
end
# Gets the width of the sprite. Equivalent to src_rect.width.
def width
bitmap.width
end
# Gets the height of the sprite. Equivalent to src_rect.height.
def height
bitmap.height
end
def draw(destination=Graphics)
return unless bitmap and opacity > 0
base_x = @x-@ox
base_y = @y-@oy
if viewport
destination.entity.set_clip_rect(viewport.rect.x, viewport.rect.y, viewport.rect.width, viewport.rect.height)
base_x += viewport.rect.x - viewport.ox
base_y += viewport.rect.y - viewport.oy
end
SDL::Surface.blit(@bitmap.entity, @src_rect.x, @src_rect.y, @src_rect.width, @src_rect.height, destination.entity, base_x, base_y)
destination.entity.set_clip_rect(0, 0, destination.width, destination.height) if viewport
end
end
SDL Bitmap implementation
Code:
require 'rubygems'
require 'rubysdl'
#**Part of OpenRGSS
# The bitmap class. Bitmaps represent images.
#
# Sprites (Sprite) and other objects must be used to display bitmaps onscreen.
class Bitmap
attr_accessor :font, :entity, :text
# :call-seq:
# Bitmap.new(filename)
# Bitmap.new(width, height)
#
# Loads the graphic file specified in filename or size and creates a bitmap object.
#
# Also automatically searches files included in RGSS-RTP and encrypted archives. File extensions may be omitted.
def initialize(width, height=nil)
@entity = if width.is_a? String
filename = width
filepath = RGSS.get_file(filename)
Log.debug('load bitmap') { filepath }
SDL::Surface.load(filepath).display_format_alpha
else
big_endian = ([1].pack("N") == [1].pack("L"))
if big_endian
rmask = 0xff000000
gmask = 0x00ff0000
bmask = 0x0000ff00
amask = 0x000000ff
else
rmask = 0x000000ff
gmask = 0x0000ff00
bmask = 0x00ff0000
amask = 0xff000000
end
SDL::Surface.new(SDL::SWSURFACE|SDL::SRCALPHA, width, height, 32, rmask, gmask, bmask, amask)
end
@font = Font.new
# @text = [] ~
end
# Frees the bitmap. If the bitmap has already been freed, does nothing.
def dispose
@entity.destroy
end
# Returns true if the bitmap has been freed.
def disposed?
@entity.destroyed?
end
# Gets the bitmap width.
def width
@entity.w
end
# Gets the bitmap height.
def height
@entity.h
end
# Gets the bitmap rectangle (Rect).
def rect
Rect.new(0, 0, width, height)
end
def clone
b =Bitmap.new(width, height)
b.entity = @entity.copyRect(0, 0, width, height)
b.font =Font.clone
return b
end
# Performs a block transfer from the src_bitmap box src_rect (Rect) to the specified bitmap coordinates (x, y).
#
# opacity can be set from 0 to 255.
def blt(x, y, src_bitmap, src_rect, opacity=255)
src_bitmap.entity.set_alpha(SDL::RLEACCEL, opacity)
SDL::Surface.blit(src_bitmap.entity, src_rect.x, src_rect.y, src_rect.width, src_rect.height, @entity, x, y)
src_bitmap.entity.set_alpha(SDL::SRCALPHA|SDL::RLEACCEL, 255)
end
# Performs a block transfer from the src_bitmap box src_rect (Rect) to the specified bitmap box dest_rect (Rect).
#
# opacity can be set from 0 to 255.
def stretch_blt(dest_rect, src_bitmap, src_rect, opacity=255)
src_bitmap.entity.set_alpha(SDL::RLEACCEL, opacity)
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)
src_bitmap.entity.set_alpha(SDL::SRCALPHA|SDL::RLEACCEL, 255)
end
# :call-seq:
# fill_rect(x, y, width, height, color)
# fill_rect(rect, color)
#
# Fills the bitmap box (x, y, width, height) or rect (Rect) with color (Color).
def fill_rect(x, y, width=nil, height=nil, color=nil)
if x.is_a? Rect
rect = x
color = y
x = rect.x
y = rect.y
width = rect.width
height = rect.height
end
@entity.fill_rect(x, y, width, height, @entity.map_rgba(color.red, color.green, color.blue, color.alpha))
end
# :call-seq:
# gradient_fill_rect(x, y, width, height, color1, color2[, vertical])
# gradient_fill_rect(rect, color1, color2[, vertical])
#
# Fills in this bitmap box (x, y, width, height) or rect (Rect) with a gradient from color1 (Color) to color2 (Color).
#
# Set vertical to true to create a vertical gradient. Horizontal gradient is the default.
def gradient_fill_rect(x, y, width, height=false, color1=nil, color2=nil, vertical=false)
if x.is_a? Rect
rect = x
color1 = y
color2 = width
vertical = height
x = rect.x
y = rect.y
width = rect.width
height = rect.height
end
if vertical
height.times do |i|
@entity.fill_rect(x, y+i, width, 1, @entity.map_rgba(
color1.red + (color2.red - color1.red) * i / height,
color1.green + (color2.green - color1.green) * i / height,
color1.blue + (color2.blue - color1.blue) * i / height,
color1.alpha + (color2.alpha - color1.alpha) * i / height
))
end
else
width.times do |i|
@entity.fill_rect(x+i, y, 1, height, @entity.map_rgba(
color1.red + (color2.red - color1.red) * i / width,
color1.green + (color2.green - color1.green) * i / width,
color1.blue + (color2.blue - color1.blue) * i / width,
color1.alpha + (color2.alpha - color1.alpha) * i / width
))
end
end
end
# Clears the entire bitmap.
def clear
@entity.fill_rect(0, 0, width, height, 0x00000000)
end
# Clears this bitmap box or (x, y, width, height) or rect (Rect).
def clear_rect(x, y=nil, width=nil, height=nil)
if x.is_a? Rect
rect = x
x = rect.x
y = rect.y
width = rect.width
height = rect.height
end
@entity.fill_rect(x, y, width, height, 0x00000000)
end
# Gets the color (Color) at the specified pixel (x, y).
def get_pixel(x, y)
color = @entity.format.getRGBA(@entity.get_pixel(x, y))
return Color.new(color[0], color[1], color[2], color[3])
#Color.new((color & @entity.Rmask) >> @entity.Rshift, (color & @entity.Gmask) >> @entity.Gshift, (color & @entity.Bmask) >> @entity.Bshift, (color & @entity.Amask) >> @entity.Ashift)
end
# Sets the specified pixel (x, y) to color (Color).
def set_pixel(x, y, color)
@entity.put_pixel(x, y, @entity.map_rgba(color.red, color.green, color.blue, color.alpha))
end
# Changes the bitmap's hue within 360 degrees of displacement.
#
# This process is time-consuming. Furthermore, due to conversion errors, repeated hue changes may result in color loss.
def hue_change(hue)
end
# Applies a blur effect to the bitmap. This process is time consuming.
def blur
end
# 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.
#
# division is the division number (from 2 to 100). The larger the number, the smoother it will be. This process is very time consuming.
def radial_blur(angle, division)
end
# Draws the string str in the bitmap box (x, y, width, height) or rect (Rect).
#
# 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.
#
# If the text length exceeds the box's width, the text width will automatically be reduced by up to 60 percent.
#
# 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.
#
# As this process is time-consuming, redrawing the text with every frame is not recommended.
def draw_text(x, y, width=0, height=nil, str=nil, align=0 )
if x.is_a? Rect
rect = x
str = y
align = width
x = rect.x
y = rect.y
width = rect.width
height = rect.height
end
str = str.to_s
if align == 2
x += width - @font.entity.text_size(str)[0]
elsif align == 1
x += (width - @font.entity.text_size(str)[0]) / 2
end
# @text << [str, x, y, @font.color.red, @font.color.green, @font.color.blue] See you ~
tmp = @font.entity.render_blended_utf8(str, @font.color.red, @font.color.green, @font.color.blue)
tmp.set_alpha(SDL::RLEACCEL ,0)
@entity.put tmp,x,y
#SDL::Surface.transformBlit tmp,@entity,0,1,1,0,0,x, y,SDL::Surface::TRANSFORM_AA|SDL::Surface::TRANSFORM_SAFE|SDL::Surface::TRANSFORM_SAFE
end
# 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.
#
# 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.
def text_size(str)
Rect.new 0, 0, *@font.entity.text_size(str)
end
end
Rect implementation
Code:
class Rect
# The x-coordinate of the rectangle's upper left corner.
attr_accessor :x
# The y-coordinate of the rectangle's upper left corner.
attr_accessor :y
# The rectangle's width.
attr_accessor :width
# The rectangle's height.
attr_accessor :height
# :call-seq:
# Rect.new(x, y, width, height)
# Rect.new
#
# Creates a new Rect object.
#
# The default values when no arguments are specified are (0, 0, 0, 0).
def initialize(x=0, y=0, width=0, height=0)
set(x, y, width, height)
end
# :call-seq:
# set(x, y, width, height)
# set(rect)
#
# Sets all parameters at once.
#
# The second format copies all the components from a separate Rect object.
def set(x, y=0, width=0, height=0)
if x.is_a? Rect
rect = x
@x = rect.x
@y = rect.y
@width = rect.width
@height = rect.height
else
@x = x
@y = y
@width = width
@height = height
end
end
def to_s
"(#{x}, #{y}, #{width}, #{height})"
end
# Sets all components to 0.
def empty
set(0, 0, 0, 0)
end
# Vergleichsmethode
def == other
if other.kind_of?(Rect) then
x == other.x && y == other.y && width == other.width && height == other.height
else
raise TypeError.new("Can't convert #{other.class} to Rect")
end
end
# Serialisiert das Objekt
def _dump limit
[x, y, width, height].pack("iiii")
end
# Deserialisiert das Objekt
def self._load str
new(*str.unpack("iiii"))
end
end
Viewport implementation
Code:
#**Part of OpenRGSS
class Viewport
#The box (Rect) defining the viewport.
attr_accessor :rect
# The viewport's visibility. If TRUE, the viewport is visible. The default is TRUE.
attr_accessor :visible
# The viewport's z-coordinate. The larger the value, the closer to the player the plane will be displayed.
#
# If multiple objects share the same z-coordinate, the more recently created object will be displayed closest to the player.
attr_accessor :z
# The x-coordinate of the viewport's starting point. Change this value to shake the screen, etc.
attr_accessor :ox
# The y-coordinate of the viewport's starting point. Change this value to shake the screen, etc.
attr_accessor :oy
# The color (Color) to be blended with the viewport. Alpha values are used in the blending ratio.
#
# Handled separately from the color blended into a flash effect.
attr_accessor :color
# The viewport's color tone (Tone).
attr_accessor :tone
attr_accessor :created_at
# :call-seq:
# Viewport.new(x, y, width, height)
# Viewport.new(rect)
# Viewport.new (RGSS3)
#
# Creates a viewport object.
#
# Same size as the screen if no argument is specified.
def initialize(*args)
@tone = Tone.new
@color = Color.new
@rect = args.empty? ? Rect.new(0, 0, Graphics.width, Graphics.height) : Rect.new(*args)
@created_at = Time.now
@z = 0
@ox = 0
@oy = 0
end
# Frees the viewport. If the viewport has already been freed, does nothing.
#
# This operation will not result in a separate associated object being automatically freed.
def dispose
@disposed = true
end
# Returns TRUE if the viewport has been freed.
def disposed?
@disposed
end
# Begins flashing the viewport. duration specifies the number of frames the flash will last.
#
# If color is set to nil, the viewport will disappear while flashing.
def flash(color, duration)
end
# Refreshes the viewport flash. As a rule, this method is called once per frame.
#
# It is not necessary to call this method if no flash effect is needed.
def update
end
def z=(z)
@z = z
RGSS.resources.select { |resource| resource.viewport == self }.each { |resource| resource.visible = resource.visible }
end
end
RGSS Error implementation
Code:
class RGSSError < StandardError
end
class RGSSReset < Exception
end
Skills: Android, Ruby, PHP, Linux