Advertisement
estriole

EST - SOV Video Player Conversion

Dec 13th, 2012
4,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 24.36 KB | None | 0 0
  1. #==============================================================================
  2. #
  3. # [ACE]EST_SOV_Video_Player ++ Conversion
  4. #
  5. # v1.4
  6. #==============================================================================
  7. # Author : Estriole
  8. # (Conversion to ace and improving)
  9. # VX version author: SuperOverlord
  10. #
  11. # also credit Crystal Noel for solution for title that have encoding problem character
  12. # also credit ruin for fixes he made to this script.
  13. #
  14. # History :
  15. # version 1.4 2013.05.16 - apply fixes from ruin. typo error and ability to use
  16. #                          play video using battle event page.
  17. # version 1.3 2013.03.05 - create configuration for people that have encoding character
  18. #                          problem with their title. ex: Pokémon Ace.
  19. #                          set the IGNORE_TITLE to true inside module Sov::Video
  20. # version 1.2 2012.09.27 - made if game at fullscreen automaticly become windowed
  21. # version 1.1 2012.09.25 - some bug fix. now avi video can played at full screen mode(alt enter) with some position error.
  22. #                        - can play other format such as mkv but it will play full screen and when played at full screen
  23. #                          mode (alt enter) the size will be smaller and the game switched.
  24. #                        - basicly this script not support full screen game yet. will try to fix this in next version
  25. # version 1.0 2012.09.23 - finish converting + some change
  26. #
  27. #
  28. #==============================================================================
  29. # Features:
  30. #------------------------------------------------------------------------------
  31. # o Play video's on the map or in battle using a simple script event command.
  32. #
  33. # o Optionally pause or exit video's while they play.
  34. #
  35. # o View the video in the game window or in fullscreen.
  36. #
  37. # o Setup video skills, which show the video before damage effects.
  38. #
  39. # o ACE version also made not only video skill but can use video item too
  40. #
  41. # o ACE version also can play video before title scene
  42. #
  43. #==============================================================================
  44. # Instructions:
  45. #------------------------------------------------------------------------------
  46. # o Place all videos in a folder with the same name as in configuration.
  47. #   This folder is created automatically the first time the game is played if
  48. #   it doesn't already exist.
  49. #
  50. # o Playing Videos when on the map.
  51. #
  52. #   - See script calls below.
  53. #
  54. # o Playing videos in battle.
  55. #
  56. #   - As when on the map the script event command can be used in battle also.
  57. #
  58. #   - As well as this you can setup skills as video skills which display
  59. #     a video before damaging the enemy.
  60. #
  61. #     <now below setup can be also used for item video too>
  62. #     To do this the following tags can be used in the skills notebox:
  63. #     1) <video_name = "filename">
  64. #        ~ name is the name of the video file. If the filename extension is
  65. #          missing the first file matching that name is used.
  66. #          (Quotes are necessary around the filename)
  67. #
  68. #      As well as the first tag 2 others are available. These tags will only
  69. #      take effect if placed on a line below the first tag.
  70. #      If these don't exist they are assumed true.
  71. #
  72. #      2) <video_exitable = n>   ~ Can the video be exited by the exit input?
  73. #      3) <video_pausable = n>   ~ Can the video be paused?
  74. #         ~ n is replaced with either t or f (t : true, f : false)
  75. #
  76. #      For other Video properties (x,y,width,height,fullscreen) the default
  77. #      settings are used. (See script calls below)
  78. #
  79. # o Playing videos before title screen
  80. # ctrl + f this : CONFIGURATION FOR VIDEO BEFORE TITLE
  81. # and change the
  82. # @video_title = nil
  83. # to
  84. # @video_title = "yourvideonamewithoutextensionhere"
  85. #
  86. #==============================================================================
  87. # Script Calls:
  88. #------------------------------------------------------------------------------
  89. # Commands (From the script call command on page three of event commands)
  90. #------------------------------------------------------------------------------
  91. # o To change default values for video properties.
  92. #
  93. #  1) Video.default_x = n
  94. #  2) Video.default_y = n
  95. #  3) Video.default_width  = n
  96. #  4) Video.default_height = n
  97. #  5) Video.fullscreen = bool #disabled now since could make error
  98. #  
  99. #  In all 5 commands above:
  100. #  ~ n is an integer value
  101. #  ~ bool is either true or false
  102. #
  103. # o To play videos
  104. #
  105. #   play_video(filename,exitable,pausable)
  106. #   ~ filename : name of video file             (Must be in quotes)
  107. #   ~ exitable : Can the video be exited?       (When left out = true)
  108. #   ~ pausable : Can the video be paused?       (When left out = true)
  109. #  
  110. #   For all other values the default's are used.
  111. #==============================================================================
  112. # Compatibility:
  113. #------------------------------------------------------------------------------
  114. # o Skill videos will depend on the battle system but sould work.
  115. #==============================================================================
  116. # Credit:
  117. #------------------------------------------------------------------------------
  118. # o Credit goes to Trebor and Berka whose scripts helped be figure out the
  119. #   mci_send_stringA function.
  120. #==============================================================================
  121.  
  122. module SOV
  123.   module Video
  124.   #--------------------------------------------------------------------------
  125.   # Configuration
  126.   #--------------------------------------------------------------------------
  127.     # Name of folder for videos to be held in.
  128.     DIR_NAME = "Videos"
  129.     # Exit video input
  130.     EXIT_INPUT  = Input::B
  131.     # Pause video input
  132.     PAUSE_INPUT = Input::C
  133.    
  134.     IGNORE_TITLE = true
  135.     #set to true if your title contain strange character such as é or other.    
  136.   #--------------------------------------------------------------------------
  137.   # End Configuration
  138.   #--------------------------------------------------------------------------
  139.   end
  140. end
  141.  
  142. module SceneManager
  143. ################ CONFIGURATION FOR VIDEO BEFORE TITLE #######################  
  144.   @video_title = nil  # "example" # FILENAME OF THE VIDEO IN VIDEO FOLDER IN QUOTES
  145.   #IF YOU DON'T WANT TO USE THIS FEATURE JUST CHANGE TO NIL
  146. ################ END CONFIGURATION ##########################################  
  147.   def self.run
  148.     DataManager.init
  149.     Audio.setup_midi if use_midi?
  150.     if @video_title != nil
  151.     video = Cache.video(@video_title)
  152.     video.exitable = true # True/False
  153.     video.pausable = true # True/False
  154.     Video.play(video)
  155.     end
  156.     @scene = first_scene_class.new
  157.     @scene.main while @scene
  158.   end
  159. end
  160.  
  161. class << Graphics
  162.   def Graphics.fullscreen? # Property
  163.     screen_size = Win32API.new('user32', 'GetSystemMetrics', 'I', 'I')
  164.     screen_width = screen_size.call(0);   screen_height = screen_size.call(1)    
  165.     detect_fullscreen = false
  166.     detect_fullscreen = true if screen_width == 640 and screen_height == 480
  167.     return detect_fullscreen
  168.   end
  169.   def Graphics.toggle_fullscreen # Main function
  170.     keybd = Win32API.new 'user32.dll', 'keybd_event', ['i', 'i', 'l', 'l'], 'v'
  171.     keybd.call(0xA4, 0, 0, 0)
  172.     keybd.call(13, 0, 0, 0)
  173.     keybd.call(13, 0, 2, 0)
  174.     keybd.call(0xA4, 0, 2, 0)    
  175.   end
  176. end
  177.  
  178. #==============================================================================
  179. # Import
  180. #------------------------------------------------------------------------------
  181. $imported = {} if $imported == nil
  182. $imported['Videos'] = true
  183. #==============================================================================
  184.  
  185. #==============================================================================
  186. # ** SOV::Video::Commands
  187. #==============================================================================
  188.  
  189. module SOV::Video::Commands
  190.   #--------------------------------------------------------------------------
  191.   # * Play a video
  192.   #  filename : video's filename (with or without extension)
  193.   #  exitable : Can the video be exited
  194.   #  pausable : Can the video be paused
  195.   #--------------------------------------------------------------------------
  196.   def play_video(filename,exitable=true,pausable=true)
  197.     video = Cache.video(filename)
  198.     video.exitable = exitable
  199.     video.pausable = pausable
  200.     $game_map.video = video
  201.   end
  202.   #---------------------------------------------------------------------------
  203.   # Define as module function
  204.   #---------------------------------------------------------------------------
  205.   module_function :play_video
  206. end
  207.  
  208. #==============================================================================
  209. # ** SOV::Video::Regexp
  210. #==============================================================================
  211.  
  212. module SOV::Video::Regexp
  213.   #--------------------------------------------------------------------------
  214.   # * Skill
  215.   #--------------------------------------------------------------------------
  216.   module Skill
  217.     FILENAME   = /<video[_ ]?(?:file)?name = "(.+)">/i
  218.     PAUSABLE   = /<video[_ ]?paus(?:e|able) = (t|f)>/i
  219.     EXITABLE   = /<video[_ ]?exit(?:able)? = (t|f)>/i
  220.   end
  221. end
  222.  
  223. #==============================================================================
  224. # ** SOV::Game
  225. #==============================================================================
  226.  
  227. module SOV::Game
  228.   #--------------------------------------------------------------------------
  229.   # Constants
  230.   #--------------------------------------------------------------------------
  231.   INI = 'Game'
  232.   #--------------------------------------------------------------------------
  233.   # * Get the game windows handle
  234.   #--------------------------------------------------------------------------
  235.   def self.hwnd
  236.     unless defined?(@@hwnd)
  237.       find_window = Win32API.new('user32','FindWindow','pp','i')
  238. #      @@hwnd = find_window.call('RGSS Player',title)  
  239.       gamefullscreen = Graphics.fullscreen?
  240.       @@hwnd = find_window.call('RGSS Player',title)  
  241.     end
  242.     return @@hwnd
  243.   end
  244.   #--------------------------------------------------------------------------
  245.   # * Get game title
  246.   #--------------------------------------------------------------------------
  247.   def self.title
  248.     unless defined?(@@title)
  249.       @@title = read_ini('title')
  250.     end
  251.     return @@title
  252.   end
  253.   #--------------------------------------------------------------------------
  254.   # * Read ini (Returns nil or match)
  255.   #--------------------------------------------------------------------------
  256.   def self.read_ini(variable,filename=INI)
  257.     return nil if variable == 'title' && SOV::Video::IGNORE_TITLE
  258.     reg = /^#{variable}=(.*)$/
  259.     File.foreach(filename+'.ini') { |line| break($1) if line =~ reg }
  260.   end
  261. end
  262.  
  263. #==============================================================================
  264. # ** Cache
  265. #==============================================================================
  266.  
  267. module Cache
  268.   #--------------------------------------------------------------------------
  269.   # Class Variables
  270.   #--------------------------------------------------------------------------
  271.   @@vcache = {}
  272.   #--------------------------------------------------------------------------
  273.   # Define as class methods
  274.   #--------------------------------------------------------------------------
  275.   class << self
  276.     #------------------------------------------------------------------------
  277.     # Alias List
  278.     #------------------------------------------------------------------------
  279.     alias sov_video_clear clear unless $@
  280.     #------------------------------------------------------------------------
  281.     # * Get a video object
  282.     #  filename : basename of file
  283.     #------------------------------------------------------------------------
  284.     def video(filename)
  285.       # Get full filename if extension is missing
  286.       if File.extname(filename) == ''
  287.         files = Dir["#{SOV::Video::DIR_NAME}/#{filename}.*"]
  288.         filename = File.basename(files[0]) # Set as first matching file
  289.       end
  290.       # Create or get the video object.
  291.       if @@vcache.has_key?(filename)
  292.         @@vcache[filename]
  293.       else
  294.         @@vcache[filename] = Video.new(filename)
  295.       end
  296.     end
  297.     #------------------------------------------------------------------------
  298.     # * Clear
  299.     #------------------------------------------------------------------------
  300.     def clear
  301.       @@vcache.clear
  302.       sov_video_clear
  303.     end
  304.   end
  305. end
  306.  
  307. #==============================================================================
  308. # ** RPG::Skill
  309. #==============================================================================
  310.  
  311. class RPG::UsableItem
  312.   #--------------------------------------------------------------------------
  313.   # * Determine if skill has a video skill
  314.   #--------------------------------------------------------------------------
  315.   def video
  316.     if @video == nil
  317.       @note.each_line { |line|
  318.         if @video == nil
  319.           @video = Cache.video($1) if line =~ SOV::Video::Regexp::Skill::FILENAME
  320.         else
  321.           @video.pausable = ($1 == 't') if line =~ SOV::Video::Regexp::Skill::PAUSABLE
  322.           @video.exitable = ($1 == 't') if line =~ SOV::Video::Regexp::Skill::EXITABLE
  323.         end
  324.       }
  325.       @video = :invalid if @video == nil
  326.     end
  327.     return @video
  328.   end
  329. end
  330.  
  331. #==============================================================================
  332. # ** Video
  333. #------------------------------------------------------------------------------
  334. #  Class handling playing videos.
  335. #==============================================================================
  336.  
  337. class Video
  338.   #--------------------------------------------------------------------------
  339.   # Constants
  340.   #--------------------------------------------------------------------------
  341.   TYPE_AVI  = 'avivideo'
  342.   TYPE_MPEG = 'mpegvideo'
  343.   #--------------------------------------------------------------------------
  344.   # Class Variables
  345.   #--------------------------------------------------------------------------
  346.   @@default_x = 0
  347.   @@default_y = 0
  348.   @@default_width  = Graphics.width
  349.   @@default_height = Graphics.height
  350.   @@fullscreen = false
  351.   #--------------------------------------------------------------------------
  352.   # * Get and Set default_x/y/width/height
  353.   #--------------------------------------------------------------------------
  354.   for d in %w(x y width height)
  355.     # Define setter method
  356.     module_eval(%Q(def self.default_#{d}=(i); @@default_#{d} = i; end))
  357.     # Define getter method
  358.     module_eval(%Q(def self.default_#{d}; @@default_#{d}; end))
  359.   end
  360.   #--------------------------------------------------------------------------
  361.   # * Get fullscreen
  362.   #--------------------------------------------------------------------------
  363.   def self.fullscreen
  364.     @@fullscreen
  365.   end  
  366.   #--------------------------------------------------------------------------
  367.   # * Set fullscreen
  368.   #--------------------------------------------------------------------------
  369.   def self.fullscreen=(val)
  370.     @@fullscreen = val
  371.   end
  372.   #--------------------------------------------------------------------------
  373.   # * Win32API
  374.   #--------------------------------------------------------------------------
  375.   @@mciSendStringA = Win32API.new('winmm','mciSendStringA','pplp','i')
  376.   #--------------------------------------------------------------------------
  377.   # * Video Command
  378.   #  command_string : string following mci_command_string format
  379.   #  buffer : string to retrieve return data
  380.   #  buffer_size : number of characters in buffer
  381.   #  callback_handle : handle of window to callback to. Used if notify is used
  382.   #                    in the command string. (Not supported by game window)
  383.   #--------------------------------------------------------------------------
  384.   def self.send_command(cmnd_string,buffer='',buffer_size=0,callback_handle=0)
  385.     # Returns error code. No error if NULL
  386.     err = @@mciSendStringA.call(cmnd_string,buffer,buffer_size,callback_handle)
  387.     if err != 0
  388.       buffer = ' ' * 255
  389.       Win32API.new('winmm','mciGetErrorString','LPL','V').call(err,buffer,255)
  390.       raise(buffer.squeeze(' ').chomp('\000'))
  391.     end
  392.   end
  393.    
  394.   #--------------------------------------------------------------------------
  395.   # * Play a video
  396.   #--------------------------------------------------------------------------
  397.   def self.play(video)
  398.     # Make path and buffer
  399.     path = "#{SOV::Video::DIR_NAME}/#{video.filename}"
  400.     buffer = ' ' * 255
  401.     # Initialize device and dock window with game window as parent.
  402.     type = " type #{video.type}" if video.type != ''
  403.     send_command("open #{path}#{type} alias VIDEO style child parent #{SOV::Game.hwnd}")
  404.     # Display video in client rect at x,y with width and height.
  405.     x = video.x
  406.     y = video.y
  407.     width  = video.width
  408.     height = video.height
  409.     send_command("put VIDEO window at #{x} #{y} #{width} #{height}")
  410.     # Begin playing video
  411.     screen = @@fullscreen ? 'fullscreen' : 'window'
  412.     gamefullscreen = Graphics.fullscreen?
  413.     case video.type
  414.     when "avivideo"
  415.       if gamefullscreen == true
  416.       #send_command("put VIDEO window at #{x} #{y} 640 480")
  417.       Graphics.toggle_fullscreen
  418.       send_command("play VIDEO window")
  419.       else
  420.       send_command("play VIDEO window")
  421.       end
  422.     when "mpegvideo"
  423.         if gamefullscreen == true
  424.         Graphics.toggle_fullscreen
  425.         send_command("play VIDEO window")
  426.         else
  427.         send_command("play VIDEO fullscreen")
  428.         end
  429.     else
  430.     end
  431.     flag = 0
  432.     # Start Input and status processing loop
  433.     while buffer !~ /^stopped/
  434.       # Idle processing for a frame
  435.       sleep(1.0/Graphics.frame_rate)
  436.       # Get mode string
  437.       send_command('status VIDEO mode',buffer,255)
  438.       Input.update    
  439.       if Input.trigger?(SOV::Video::PAUSE_INPUT) and video.pausable?
  440.         Sound.play_cursor
  441.         if buffer =~ /^paused/                 # If already paused
  442.           send_command("resume VIDEO")         # Resume video
  443.         else                                   # Otherwise
  444.           send_command("pause VIDEO")          # Pause video
  445.         end
  446.       elsif Input.trigger?(SOV::Video::EXIT_INPUT) and video.exitable?
  447.         Sound.play_cancel
  448.         # Terminate loop on exit input
  449.         break
  450.       end
  451.     end
  452.     # Terminate the device
  453.     send_command('close VIDEO')
  454.   end
  455.   #--------------------------------------------------------------------------
  456.   # Public Instance Variables
  457.   #--------------------------------------------------------------------------
  458.   attr_accessor :x
  459.   attr_accessor :y
  460.   attr_accessor :width
  461.   attr_accessor :height
  462.   attr_writer :exitable
  463.   attr_writer :pausable
  464.   attr_reader :filename
  465.   #--------------------------------------------------------------------------
  466.   # * Initialize
  467.   #--------------------------------------------------------------------------
  468.   def initialize(filename)
  469.     unless FileTest.file?("#{SOV::Video::DIR_NAME}/#{filename}")
  470.       raise(Errno::ENOENT,filename)
  471.     end
  472.     @filename = filename
  473.     @x = @@default_x
  474.     @y = @@default_y
  475.     @width  = @@default_width
  476.     @height = @@default_height
  477.     @exitable = true
  478.     @pausable = true
  479.   end
  480.   #--------------------------------------------------------------------------
  481.   # * Get Type
  482.   #--------------------------------------------------------------------------
  483.   def type
  484.     if @type == nil
  485.       case File.extname(@filename)
  486.       when '.avi'; @type = TYPE_AVI
  487.       when '.mpeg'||'.mpg'; @type = TYPE_MPEG
  488.       else
  489.         @type = TYPE_MPEG#''
  490.       end
  491.     end
  492.     @type
  493.   end
  494.   #--------------------------------------------------------------------------
  495.   # * Is the video exitable?
  496.   #--------------------------------------------------------------------------
  497.   def exitable?
  498.     @exitable
  499.   end
  500.   #--------------------------------------------------------------------------
  501.   # * Is the video pausable?
  502.   #--------------------------------------------------------------------------
  503.   def pausable?
  504.     @pausable
  505.   end
  506.   #--------------------------------------------------------------------------
  507.   # Access
  508.   #--------------------------------------------------------------------------
  509.   private_class_method :send_command  
  510. end
  511.  
  512. #==============================================================================
  513. # ** Game_Interpreter
  514. #==============================================================================
  515.  
  516. class Game_Interpreter
  517.   #--------------------------------------------------------------------------
  518.   # Import
  519.   #--------------------------------------------------------------------------
  520.   include(SOV::Video::Commands)
  521. end
  522.  
  523. #==============================================================================
  524. # ** Game_Map
  525. #==============================================================================
  526.  
  527. class Game_Map
  528.   #--------------------------------------------------------------------------
  529.   # Public Instance Variables
  530.   #--------------------------------------------------------------------------
  531.   attr_accessor :video  
  532. end
  533.  
  534. #==============================================================================
  535. # ** Scene_Map
  536. #==============================================================================
  537.  
  538. class Scene_Map
  539.   #--------------------------------------------------------------------------
  540.   # Alias List
  541.   #--------------------------------------------------------------------------
  542.   alias sov_video_update update unless $@
  543.   #--------------------------------------------------------------------------
  544.   # * Play Video
  545.   #--------------------------------------------------------------------------
  546.   def play_video(video)
  547.     # Memorize and stop current bgm and bgs
  548.     bgm = RPG::BGM.last
  549.     bgs = RPG::BGS.last
  550.     RPG::BGM.stop
  551.     RPG::BGS.stop
  552.     # Play video
  553.     Video.play(video)
  554.     # Restart bgm and bgs
  555.     bgm.play
  556.     bgs.play
  557.   end
  558.   #--------------------------------------------------------------------------
  559.   # * Update
  560.   #--------------------------------------------------------------------------
  561.   def update
  562.     if $game_map.video != nil
  563.       play_video($game_map.video)
  564.       $game_map.video = nil
  565.       Input.update
  566.     else
  567.       sov_video_update
  568.     end
  569.   end
  570. end
  571.  
  572. #==============================================================================
  573. # ** Scene_Battle
  574. #==============================================================================
  575.  
  576. class Scene_Battle
  577.   #--------------------------------------------------------------------------
  578.   # * Alias list
  579.   #--------------------------------------------------------------------------
  580.   alias sov_video_update_battle update unless $@
  581.   alias sov_video_use_item use_item unless $@
  582.   #--------------------------------------------------------------------------
  583.   # * Play Video
  584.   #--------------------------------------------------------------------------
  585.   def play_video(video)
  586.     # Memorize and stop current bgm
  587.     bgm = RPG::BGM.last
  588.     RPG::BGM.stop
  589.     # Play video
  590.     Video.play(video)
  591.     # Restart bgm
  592.     bgm.play
  593.   end
  594.   #--------------------------------------------------------------------------
  595.   # * Execute Action Skill
  596.   #--------------------------------------------------------------------------
  597.   def use_item
  598.     skill = @subject.current_action.item    
  599.     if skill.video.is_a?(Video)
  600.       execute_action_video(skill)
  601.       sov_video_use_item
  602.     else
  603.       sov_video_use_item
  604.     end
  605.   end
  606.   #--------------------------------------------------------------------------
  607.   # * Execute Action Video
  608.   #--------------------------------------------------------------------------
  609.   def execute_action_video(skill)
  610.     br = Graphics.brightness
  611.     120.times { |i| Graphics.brightness = 255 - 255/60 * i; Graphics.update }
  612.     # Play video
  613.     play_video(skill.video)
  614.     # Reset brightness
  615.     Graphics.brightness = br
  616.   end
  617.   #ADDED UPDATE FUNCTION FROM SCENE_MAP TO SCENE_BATTLE
  618.   def update
  619.     if $game_map.video != nil
  620.       play_video($game_map.video)
  621.       $game_map.video = nil
  622.       Input.update
  623.     else
  624.       sov_video_update_battle
  625.     end
  626.   end
  627.  
  628. end
  629.  
  630.  
  631.  
  632.  
  633.  
  634. #==============================================================================
  635. # Pre-Main Processing
  636. #==============================================================================
  637.  
  638. unless FileTest.directory?(SOV::Video::DIR_NAME) # If directory doesn't exist.
  639.   Dir.mkdir(SOV::Video::DIR_NAME)                # Make the directory
  640. end
  641.  
  642. =begin
  643. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement