親,你想用用RMXP做出像一些大型遊戲那樣插入AVI視訊麼,那麼下面就是福利了

joeage發表於2012-12-02

# Note:
#
#   1.遊戲必須使用遊戲樣板工程附帶的RGSS102J.dll

#   2.在下方 PROJECT_NAME = 後面填寫你的遊戲工程名.
#
#   3.在遊戲中,呼叫指令碼事件播放你的視訊檔案,如果一行寫不下可以在逗號後換行.
#
#   $MP.play(movie_name, movie_length,
#            skip, fullscr,
#            x, y, width, height)
#
# 引數說明:
#
#     movie_name   : 視訊檔名(*.avi),必須
#     movie_length : 電影時間,單位是秒,必須
#             skip : 是否可以按A鍵跳過,true/false,預設是true
#          fullscr : 是否強制為全螢幕播放,true/false,預設是false
#              x,y : 視訊播放的左上角座標,預設是0,0
#     width,height : 視訊的寬度,可以任意.預設是640,480
#
# 例如:
#        $MP.play("logo.avi",13,false,true)
#==============================================================================

# ------------------------------------------------------------------------
# 高精度計時器 by FantasyDR
# ------------------------------------------------------------------------
# E-mail: fnd@163.net
# ------------------------------------------------------------------------
# 2005.10.18
# ------------------------------------------------------------------------
# 該類已經被定義為全域性變數 $sys_timer
# 如果只需要精確到毫秒,請設定初始化引數為true
# decimal屬性設定返回時間值的小數位數。
# ------------------------------------------------------------------------
# 下面是一些有用的方法列表,呼叫時寫:$sys_timer.方法名
# 例如 $sys_timer.clear()
# ------------------------------------------------------------------------
# clear() :計時器清零
#   now() :獲取當前經過的時間,單位毫秒
# now_s() :獲取當前經過的時間,單位秒
# ------------------------------------------------------------------------

class SystemTimer

 attr_accessor:decimal  #小數位數設定,預設為3
 
 def initialize(use_GetTime=false)
   # 初始化,根據系統選擇不同精度計時器
   @qpFrequency = Win32API.new("kernel32","QueryPerformanceFrequency",'p','L')
   @qpCounter = Win32API.new("kernel32","QueryPerformanceCounter",'p','L')
   @tGetTime = Win32API.new("winmm","timeGetTime",'','L')
  
   @decimal=3
   @perf_cnt=" " * 8
   @time_start=" " * 8
   @time_now=" " * 8
  
   result = @qpFrequency.call(@perf_cnt)
  
   if use_GetTime
     result = 0
   end
  
   if result!=0
     @perf_flag=true
   else
     @perf_flag=false
     @perf_cnt=[1000,0].pack('LL')
   end
  
   #設定時間比例因數
   @time_scale=@perf_cnt.unpack('LL')
   @time_scale[0] /= 1000.0
   @time_scale[1] /= 1000.0
  
   #起始時間清零
   self.clear()
 end
 
 #-=====================-#
 # 計時器清零
 #-=====================-#
 def clear()
   if @perf_flag
     @qpCounter.call(@time_start)
   else
     @time_start=[@tGetTime.call(),0].pack('LL')
   end
 end
 
 #-==============================-#
 # 獲取當前經過的時間,單位毫秒
 #-==============================-#
 def now()
   now_time = 0.0e1
   now_time += self.timer() - self.start()
   now_time /= self.scale()
   return self.debug(now_time)
 end
 
 #-==============================-#
 # 獲取當前經過的時間,單位秒
 #-==============================-#
 def now_s()
   now_time = 0.0e1
   now_time += self.timer() - self.start()
   now_time /= (self.scale()*1000)  
   return self.debug(now_time)
 end
 
 #-==============================-#
 # 幀錯...
 #-==============================-#
 def debug(now_time)
   if @decimal>0
    now_time = (now_time * (10**@decimal)).floor/(10.0**@decimal)
   else
    now_time = now_time.floor
   end
   return now_time
  
   #以下用於debug模式
   if now_time < 0
     p "Timer Wrong!! Clear...",now_time,\
       @perf_flag,@qpCounter,@tGetTime,
       @time_now.unpack('LL')[0],@time_now.unpack('LL')[1],
       @time_start.unpack('LL')[0],@time_start.unpack('LL')[1]
     self.clear()
     return 0.0
   else
     return now_time
   end
 end
 
 #-=====================-#
 # 獲取時間比例因數
 #-=====================-#
 def scale()
   return @time_scale[0]+\
          @time_scale[1]*0xffffffff
 end
 
 #-=====================-#
 # 獲取起始滴答數
 #-=====================-#
 def start()
   return @time_start.unpack('LL')[0]+\
          @time_start.unpack('LL')[1]*0xffffffff
 end
 
 #-=====================-#
 # 獲取當前的嘀噠數
 #-=====================-#
 def timer()
   if @perf_flag
     @qpCounter.call(@time_now)
   else
     @time_now=[@tGetTime.call(),0].pack('LL')
   end
   return @time_now.unpack('LL')[0]+\
          @time_now.unpack('LL')[1]*0xffffffff
 end
end

#-------------------------------------#
# 初始化自身成一個全域性變數
#-------------------------------------#
$sys_timer=SystemTimer.new()
#-------------------------------------#

#==============================================================================
# ☆★☆ AVI播放器 ☆★☆
#------------------------------------------------------------------------------
# - FantasyDR
# - 2006.3.8
#------------------------------------------------------------------------------
# MSN: FantasyDR_SJL@hotmail.com
#------------------------------------------------------------------------------
# Note:
#
#   1.遊戲必須使用遊戲樣板工程附帶的RGSS102J.dll

#   2.在下方 PROJECT_NAME = 後面填寫你的遊戲工程名.
#
#   3.在遊戲中,呼叫指令碼事件播放你的視訊檔案,如果一行寫不下可以在逗號後換行.
#
#   $MP.play(movie_name, movie_length,
#            skip, fullscr,
#            x, y, width, height, loop)
#
# 引數說明:
#
#     movie_name   : 視訊檔名(*.avi),必須
#     movie_length : 電影時間,單位是秒,必須
#             skip : 是否可以按A鍵跳過,true/false,預設是true
#          fullscr : 是否強制為全螢幕播放,true/false,預設是false
#              x,y : 視訊播放的左上角座標,預設是0,0
#     width,height : 視訊的寬度,可以任意.預設是640,480
#             loop : 迴圈播放,true/false,預設是true
#
# 例如播放logo.avi,時間13秒,禁止跳過,強制全屏,範圍(是0,0)-(640,480),迴圈播放
#        $MP.play("logo.avi",13,false,true)
#==============================================================================

# ★★★請先這裡填寫遊戲的工程名★★★

PROJECT_NAME = "神劍情天"

#==============================================================================
# ■ Win32API
#------------------------------------------------------------------------------
#  需要用到的API
#==============================================================================

# 切換到全屏延時
SWITCH_DELAY = 0.1

# API使用的一些常數
WS_EX_TOPMOST = 0x8
WS_EX_TOOLWINDOW= 0x80
WS_VISIBLE = 0x10000000
WS_POPUP = 0x80000000
GWL_HINSTANCE = (-6)
WM_CLOSE = 0x10
WS_CHILD = 0x40000000
WS_NONE = 0x16000000
CP_ACP = 0
CP_UTF8 = 65001

# 字元編碼轉換API
$MP_m2w = Win32API.new('kernel32', 'MultiByteToWideChar', '%w(i,l,p,i,p,i)', 'i')
$MP_w2m = Win32API.new('kernel32', 'WideCharToMultiByte', '%w(i,l,p,i,p,i,p,p)', 'i')
 
# 按鍵API
$MP_keybd = Win32API.new('user32', 'keybd_event', '%w(i,i,l,l)', 'v')

# 視訊播放API
$MP_mciSendString = Win32API.new('winmm','mciSendString','%w(p,p,l,l)','V')

# 鎖定視窗
# hWnd,ifEnable
$MP_EnableWindow = Win32API.new('user32','EnableWindow','%w(l,l)','L')

# 啟用視窗
# hWnd
$MP_SetActiveWindow = Win32API.new('user32','SetActiveWindow','%w(l)','L')

# 當前活動視窗
$MP_GetActiveWindow = Win32API.new('user32','GetActiveWindow','%w()','L')

# hWnd,wMsg,wParam,lParam
$MP_PostMessage = Win32API.new('user32','PostMessage','%w(l,l,l,p)','L')

# 獲取當前視窗控制程式碼
$MP_FindWindowEX = Win32API.new('user32','FindWindowEx','%w(l,l,p,p)','L')

# 獲取螢幕座標
$MP_ClientToScreen = Win32API.new("user32", "ClientToScreen", 'ip', 'i')

# 獲取hInt
$MP_GetWindowLong= Win32API.new('user32','GetWindowLong','%w(l,l)','L')

# 獲取類名
# hWnd,lpClassName,maxCount
$MP_GetClassName= Win32API.new('user32','GetClassName','%w(l,p,l)','L')

# 建立窗體
# ExStyle,ClassName,WindowName,
# style,x,y,width,height
# 0,0,hInstance,0
$MP_CreateWindowEX = Win32API.new('user32','CreateWindowEx','%w(l,p,p,l,l,l,l,l,l,l,l,p)','L')

#==============================================================================
# ■ MoviePlayer
#------------------------------------------------------------------------------
#  處理視訊播放畫面的類。
#==============================================================================

class MoviePlayer
 #--------------------------------------------------------------------------
 # ● 初始化
 #     project_name : 工程名稱
 #--------------------------------------------------------------------------
 def initialize(project_name = PROJECT_NAME)
   @sys_timer=SystemTimer.new()
   buffer = "\0\0" * project_name.size
   @project_name = "\0" * project_name.size
  
   $MP_m2w.call(CP_UTF8, 0, project_name, -1, buffer, project_name.size)
   $MP_w2m.call(CP_ACP,0,buffer,-1,@project_name,project_name.size,0,0)
  
   @hWnd = $MP_FindWindowEX.call(0,0,nil,@project_name)
   @hInt = $MP_GetWindowLong.call(@hWnd,GWL_HINSTANCE)
   @class_name = " " * 256
   $MP_GetClassName.call(@hWnd,@class_name,256)
 end
 #--------------------------------------------------------------------------
 # ● 是否已經全螢幕
 #--------------------------------------------------------------------------
 def is_full?
   # 播放起始座標
   point = [0, 0].pack('ll')
   if $MP_ClientToScreen.call(@hWnd, point) == 0
     return false
   end
   x, y = point.unpack('ll')
   if x == 0 and y == 0
     return true
   else
     return false
   end
 end
 #--------------------------------------------------------------------------
 # ● 切換全屏
 #--------------------------------------------------------------------------
 def switch_full
   $MP_keybd.call (0xA4, 0, 0, 0)
   $MP_keybd.call (13, 0, 0, 0)
   $MP_keybd.call (13, 0, 2, 0)
   $MP_keybd.call (0xA4, 0, 2, 0)
   Graphics.update
   sleep(SWITCH_DELAY)
   Graphics.update
 end
 #--------------------------------------------------------------------------
 # ● 播放電影
 #     movie_name : 視訊檔名(*.avi)
 #     movie_length : 電影時間,單位是秒
 #     skip : 是否可以按鍵跳過
 #     fullscr : 是否強制為全螢幕播放
 #     x,y,width,height: 播放的位置以及寬度
 #     loop : 迴圈播放
 #--------------------------------------------------------------------------
 def play(movie_name,movie_length,
          skip = true,fullscr = false,
          x = 0,y = 0,width = 640,height = 480,loop = true)
   # 資料不合法則退出
   return true if movie_name == nil or movie_length <= 0
   # 檔案不存在
   #.........................................................................
   #movie_name = Dir.getwd()+"\\"+movie_name
   #.........................................................................
   return true unless FileTest.exist?(movie_name)

   # 視窗寬度
   width -= (x + width)- 640 if (x + width) > 640
   height -= (y + height)- 480 if (y + height) > 480
  
   if fullscr and !is_full?
     self.switch_full
   end
  
   fullscr = self.is_full?
  
   # 播放起始座標
   point = [x, y].pack('ll')
   if $MP_ClientToScreen.call(@hWnd, point) == 0
     return true
   end
   x, y = point.unpack('ll')
   return true  if (x + width) < 0 or (y+height) < 0
  
   if fullscr
     wnd = $MP_CreateWindowEX.call(WS_EX_TOPMOST,@class_name,@project_name,
                                   WS_VISIBLE | WS_POPUP,x,y,width,height,
                                   0,0,@hInt,0)
   else
     wnd = $MP_CreateWindowEX.call(WS_EX_TOOLWINDOW,@class_name,@project_name,
                                   WS_VISIBLE | WS_POPUP,x,y,width,height,
                                   0,0,@hInt,0)
   end                             
   # 窗體建立失敗
   return true if wnd == 0
  
   # 遮蔽原窗體
   $MP_EnableWindow.call(@hWnd,0)
  
   $MP_mciSendString.call("open \"" + movie_name + "\"" +
                          " alias FILE style 1073741824 parent " +\
                           wnd.to_s,0,0,0)
   if loop
     $MP_mciSendString.call("play FILE repeat window",0,0,0)
   else
     $MP_mciSendString.call("play FILE window",0,0,0)
   end
  
   @sys_timer.clear()
   step = 0.1
   begin
     loop do
       # 如果在視窗模式
       unless fullscr
         # 變成全屏
         if self.is_full?
           break
         else
           Graphics.update
         end
       end
       #sleep(step)
       if skip
         Input.update
         break if Input.trigger?(Input::A)
       end
       if @sys_timer.now_s >= movie_length
         break
       end
       if $MP_GetActiveWindow.call() != wnd
         $MP_SetActiveWindow.call(wnd)
       end
     end
   rescue Hangup
     retry
   end
   # 關閉當前窗體
   $MP_PostMessage.call(wnd,WM_CLOSE,0,0)
   $MP_mciSendString.call("close FILE",0,0,0)
   $MP_EnableWindow.call(@hWnd,1)
   $MP_SetActiveWindow.call(@hWnd)
   return true
 end
end

$MP = MoviePlayer.new

相關文章