#-------------------------------------------------------------------------- # ● 分解gif图片 #-------------------------------------------------------------------------- def analyze_gif @old_dir = Dir.pwd Dir.chdir("GIF") @total_gif = [] for g in Dir["*"] suf = g.split(/\./) if suf[1].is_a?(String) and suf[1].downcase == "gif" @total_gif.push(g) Dir.mkdir(suf[0]) unless Dir["*"].include? suf[0] end end @total_gif.each{|file| self.readdata(file)} Dir.chdir(@old_dir) p '全部分解完毕,点击确定退出' exit end
#-------------------------------------------------------------------------- # ● 读取文件数据:成功返回@gif_infos,失败返回nil #-------------------------------------------------------------------------- def readdata(filename) # 检查是否有临时记忆文件 tmp_filename = File.basename(filename,".*") unless Dir["~TEMP/#{tmp_filename}_infos.fmd"].empty? begin giffr = open("~TEMP/#{tmp_filename}_infos.fmd","rb") tmp_infos = Marshal.load(giffr) giffr.close if Dir["~TEMP/#{tmp_filename}_*.png"].size == tmp_infos.total_frames return tmp_infos end rescue end end # 初始化数据 self.initial_var(filename) # 打开文件 begin @f = open(filename,"rb") # 读取文件头 self.read_gifh # 读取逻辑屏幕描述块 self.read_gifs # 读取下一个字节 @temp = @f.read(1)[0] # 循环读取每个图象描述块 while true if @temp == 0x2c # 读取图象描述块 self.read_gifi end if @temp == 0x21 # 读取图象扩展块 self.read_gife end if @temp == 0x3b break end # 读取下一个字节 @temp = @f.read(1)[0] end # ※ 设置图象帧数 @gif_infos.total_frames = @frame_count # ※ 写入图象分解数据 giffw = open("~TEMP/#{@filename}_infos.fmd","wb") Marshal.dump(@gif_infos,giffw) giffw.close rescue return nil ensure @f.close end return @gif_infos end
#-------------------------------------------------------------------------- # ● 读取文件头 #-------------------------------------------------------------------------- def read_gifh @gifh = @f.read(SIZE_GIFH) if @gifh[0,3] != "GIF" raise "不是GIF文件!" end if @gifh[3,3] != "87a" and @gifh[3,3] != "89a" raise "不支持的版本!" end end
#-------------------------------------------------------------------------- # ● png数据块 #-------------------------------------------------------------------------- def make_png_idat lz_data = [] if @_trans_index != nil for i in 0...@png_data.size lz_data.push 0 for j in @png_data[i] if j == @_trans_index lz_data.push @_pal[j*3],@_pal[j*3+1],@_pal[j*3+2],0 else lz_data.push @_pal[j*3],@_pal[j*3+1],@_pal[j*3+2],255 end end end else for i in 0...@png_data.size lz_data.push 0 lz_data += @png_data[i] end end id_data = Zlib::Deflate.deflate(lz_data.pack("C*"),9) id_size = [id_data.size].pack("N") id_sign = "IDAT" id_crc = [Zlib.crc32(id_sign + id_data)].pack("N") return id_size + id_sign + id_data + id_crc end
#-------------------------------------------------------------------------- # ● 预处理:建立临时文件夹 #-------------------------------------------------------------------------- unless Dir["*"].include?("~TEMP") Dir.mkdir("~TEMP") System.attrib("~TEMP","+h","+s") end
#-------------------------------------------------------------------------- # ● 清空硬缓存 #-------------------------------------------------------------------------- def self.TmpFileclear begin Dir["~TEMP/*"].each{|filename| File.delete filename} rescue end end
#-------------------------------------------------------------------------- # ● 预加载图片 #-------------------------------------------------------------------------- def self.pre_load(filename_arr) filename_arr.each{|fn| GIF.readdata(fn)} end
#-------------------------------------------------------------------------- # ● 获取图片信息 #-------------------------------------------------------------------------- def bitmap @gif_infos end
#-------------------------------------------------------------------------- # ● 设置图片文件名 #-------------------------------------------------------------------------- def bitmap=(filename) @gif_infos = GIF.readdata(filename) if @gif_infos != nil @sp_arr = Array.new basename = File.basename(filename,".*") for i in 0...@gif_infos.total_frames sp = Sprite.new(@viewport) sp.bitmap = Bitmap.new(sprintf("~TEMP/#{basename}_%02d.png",i + 1)) sp.visible = i == 0 sp.x = @gif_infos.frame_data[i].offset_x sp.y = @gif_infos.frame_data[i].offset_y @sp_arr << sp end @update_frame_count = 0 @current_show_frame = 0 @next_frame_counts = (@gif_infos.frame_data[0].delay_time * Graphics.frame_rate / 100) end end
#-------------------------------------------------------------------------- # ● 定义x= #-------------------------------------------------------------------------- def x=(x) if @gif_infos.nil? @x = 0 return end @x = x for i in 0...@gif_infos.total_frames @sp_arr[i].x = @gif_infos.frame_data[i].offset_x + @x end end
#-------------------------------------------------------------------------- # ● 定义y= #-------------------------------------------------------------------------- def y=(y) if @gif_infos.nil? @y = 0 return end @y = y for i in 0...@gif_infos.total_frames @sp_arr[i].y = @gif_infos.frame_data[i].offset_y + @y end end
#-------------------------------------------------------------------------- # ● 定义z= #-------------------------------------------------------------------------- def z=(z) if @gif_infos.nil? @z = 0 return end @z = z for i in 0...@gif_infos.total_frames @sp_arr[i].z = @z end end
#-------------------------------------------------------------------------- # ● 更新精灵 #-------------------------------------------------------------------------- def update if @gif_infos.nil? return end @update_frame_count += 1 if @update_frame_count >= @next_frame_counts @current_show_frame = (@current_show_frame + 1) % @gif_infos.total_frames case @gif_infos.frame_data[@current_show_frame - 1].disposal_method when 0 # 不处理
when 1 # 图象保留在原处
when 2 # 当前图象消失 @sp_arr[@current_show_frame - 1].visible = false when 3 # 尚不明 = =
end @sp_arr[@current_show_frame].visible = true if @current_show_frame == 0 @loop_counts -= 1 if @loop_counts > 0 if @loop_counts == 0 self.dispose return end for i in 0...@sp_arr.size @sp_arr[i].visible = i == @current_show_frame end end @update_frame_count = 0 @next_frame_counts = (@gif_infos.frame_data[@current_show_frame].\ delay_time * Graphics.frame_rate / 100) end end
end
GIF Script Topic And i wonder if anyone know how to Change this part: @sp = GIFSprite.new @sp.bitmap="Graphics/GIF/name_of_gif.gif" @sp.x = 100 @sp.y = 200
So the GIF ll show up at the (walking monster)Events place and not at: @sp.x = 100 @sp.y = 200 ..
Regards,
This post has been edited by de22lano: Aug 9 2011, 05:46 AM
Group: +Gold Member
Posts: 1,525
Type: Scripter
RM Skill: Undisclosed
At the end of the code for the normal GIFSprite, append:
CODE
class Game_Character
attr_accessor :gif_following
alias nr_followingGIF_initialize initialize def initialize(*args) @gif_following = nil # Run the original initialize return nr_followingGIF_initialize(*args) end end
class Spriteset_Map
alias nr_followingGIF_initialize initialize def initialize(*args) @gif_following_ev = {} @last_filename_gif_following_ev = {} gif_handler # Run the original initialize return nr_followingGIF_initialize(*args) end
def gif_handler for ev in $game_map.events.values + [$game_player] # Get the X & Y of the event now # If they changed GIF's if @last_filename_gif_following_ev[ev] != ev.gif_following # Dispose the old one if @gif_following_ev[ev].is_a?(GIFSprite) @gif_following_ev[ev].dispose @gif_following_ev.delete(ev) end # Store the new filename @last_filename_gif_following_ev[ev] = ev.gif_following # Do nothing if the new GIF is blank next if not ev.gif_following.is_a?(String) # Make the new one @gif_following_ev[ev] = GIFSprite.new(@viewport_1) @gif_following_ev[ev].bitmap = ev.gif_following @gif_following_ev[ev].x = ev.screen_x - @gif_following_ev[ev].width / 2 @gif_following_ev[ev].y = ev.screen_y - @gif_following_ev[ev].height # If the GIF is the same (and it exists) elsif ev.gif_following != nil # Update the X & Y (if it's changed) new_x = ev.screen_x - @gif_following_ev[ev].width / 2 new_y = ev.screen_y - @gif_following_ev[ev].height if (@gif_following_ev[ev].x != new_x) or (@gif_following_ev[ev].y != new_y) @gif_following_ev[ev].x = new_x @gif_following_ev[ev].y = new_y end # If the GIF doesn't exist else next end # Update the gif @gif_following_ev[ev].update end end
alias nr_followingGIF_update update unless $@ def update(*args) gif_handler # Run the original update return nr_followingGIF_update(*args) end
alias nr_followingGIF_dispose dispose unless $@ def dispose(*args) # Dispose each GIF @gif_following_ev.each_value { |v| v.dispose } # Run the original dispose return nr_followingGIF_dispose(*args) end end
And then you need to have an event run the script:
Group: Member
Posts: 44
Type: Artist
RM Skill: Skilled
QUOTE (Night_Runner @ Sep 8 2011, 11:08 PM)
At the end of the code for the normal GIFSprite, append:
CODE
class Game_Character
attr_accessor :gif_following
alias nr_followingGIF_initialize initialize def initialize(*args) @gif_following = nil # Run the original initialize return nr_followingGIF_initialize(*args) end end
class Spriteset_Map
alias nr_followingGIF_initialize initialize def initialize(*args) @gif_following_ev = {} @last_filename_gif_following_ev = {} gif_handler # Run the original initialize return nr_followingGIF_initialize(*args) end
def gif_handler for ev in $game_map.events.values + [$game_player] # Get the X & Y of the event now # If they changed GIF's if @last_filename_gif_following_ev[ev] != ev.gif_following # Dispose the old one if @gif_following_ev[ev].is_a?(GIFSprite) @gif_following_ev[ev].dispose @gif_following_ev.delete(ev) end # Store the new filename @last_filename_gif_following_ev[ev] = ev.gif_following # Do nothing if the new GIF is blank next if not ev.gif_following.is_a?(String) # Make the new one @gif_following_ev[ev] = GIFSprite.new(@viewport_1) @gif_following_ev[ev].bitmap = ev.gif_following @gif_following_ev[ev].x = ev.screen_x - @gif_following_ev[ev].width / 2 @gif_following_ev[ev].y = ev.screen_y - @gif_following_ev[ev].height # If the GIF is the same (and it exists) elsif ev.gif_following != nil # Update the X & Y (if it's changed) new_x = ev.screen_x - @gif_following_ev[ev].width / 2 new_y = ev.screen_y - @gif_following_ev[ev].height if (@gif_following_ev[ev].x != new_x) or (@gif_following_ev[ev].y != new_y) @gif_following_ev[ev].x = new_x @gif_following_ev[ev].y = new_y end # If the GIF doesn't exist else next end # Update the gif @gif_following_ev[ev].update end end
alias nr_followingGIF_update update unless $@ def update(*args) gif_handler # Run the original update return nr_followingGIF_update(*args) end
alias nr_followingGIF_dispose dispose unless $@ def dispose(*args) # Dispose each GIF @gif_following_ev.each_value { |v| v.dispose } # Run the original dispose return nr_followingGIF_dispose(*args) end end
And then you need to have an event run the script:
Where 3 is the ID of the event that will have the GIF, and "Graphics/GIF/g6.gif" is the name of the GIF to show on them.
If you want to take the GIF off, instead of "Graphics/GIF/g6.gif" just use nil
Woww nice dude, thankssz whas waiting for this so long, appreciated.. I think its time to put on a demo soon.. ive been working on the projec for a long time now nd its been awhile that i didnt post anything!!