Motion Blur

Motion Blur
2012.01.21b
.:Fênix:.


Image


  1. #===============================================================================
  2. # ** Motion Blur
  3. #-------------------------------------------------------------------------------
  4. #   @version 2012.01.21b
  5. #   @author  .:Fênix:. <[email hidden]> & zeus81
  6. #   @brief   Blurs the screen when it moves
  7. #===============================================================================
  8.  
  9. module Motion_Blur
  10.   Enabled          = true        # Enable the script? (true / false)
  11.   Opacity_Variable = 1           # Variable what changes the overlay opacity
  12.   Z_Variable       = 2           # Variable what changes the overlay Z
  13.   Loss_Variable    = 3           # Variable what changes the overlay opacity loss
  14.   Default_Opacity  = 128         # Default overlay opacity
  15.   Default_Z        = 999         # Default overlay Z
  16.   Default_Loss     = 32          # Default overlay opacity loss
  17.   Scenes           = [Scene_Map] # Scenes what supports motion blur
  18. end
  19.  
  20. if (Motion_Blur::Enabled)
  21.  
  22.   module Graphics
  23.    
  24.     RtlMoveMemory          = Win32API.new("kernel32", "RtlMoveMemory",          "pii",       "i")
  25.     BitBlt                 = Win32API.new("gdi32",    "BitBlt",                 "iiiiiiiii", "i")
  26.     CreateCompatibleBitmap = Win32API.new("gdi32",    "CreateCompatibleBitmap", "iii",       "i")
  27.     CreateCompatibleDC     = Win32API.new("gdi32",    "CreateCompatibleDC",     "i",         "i")
  28.     DeleteObject           = Win32API.new("gdi32",    "DeleteObject",           "i",         "i")
  29.     GetDIBits              = Win32API.new("gdi32",    "GetDIBits",              "iiiiipi",   "i")
  30.     SelectObject           = Win32API.new("gdi32",    "SelectObject",           "ii",        "i")
  31.     SetDIBits              = Win32API.new("gdi32",    "SetDIBits",              "iiiiipi",   "i")
  32.     GetForegroundWindow    = Win32API.new("user32",   "GetForegroundWindow",    "",          "i")
  33.     GetSystemMetrics       = Win32API.new("user32",   "GetSystemMetrics",       "i",         "i")
  34.    
  35.     HWnd                   = GetForegroundWindow.call
  36.     DC                     = Win32API.new("user32",   "GetDC",                  "i",         "i").call(HWnd)
  37.    
  38.     def self.snapshot(width = defined?(RPG::Cache) ? 640 : self.width,
  39.         height = defined?(RPG::Cache) ? 480 : self.height)
  40.       bitmap  = Bitmap.new(width, height)
  41.       address = "\0" * 4
  42.       RtlMoveMemory.call(address, bitmap.__id__ * 2 + 16, 4)
  43.       RtlMoveMemory.call(address, address.unpack("L")[0] + 8, 4)
  44.       RtlMoveMemory.call(address, address.unpack("L")[0] + 16, 4)
  45.       address = address.unpack("L")[0]
  46.       info    = [40, width, height, 1, 32, 0, 0, 0, 0, 0, 0].pack("LllSSLLllLL")
  47.       hDC     = CreateCompatibleDC.call(DC)
  48.       hBM     = CreateCompatibleBitmap.call(DC, width, height)
  49.       DeleteObject.call(SelectObject.call(hDC, hBM))
  50.       SetDIBits.call(hDC, hBM, 0, height, address, info, 0)
  51.       BitBlt.call(hDC, 0, 0, width, height, DC, 0, 0, 0xCC0020)
  52.       GetDIBits.call(hDC, hBM, 0, height, address, info, 0)
  53.       DeleteObject.call(hBM)
  54.       DeleteObject.call(hDC)
  55.       return bitmap
  56.     end
  57.    
  58.   end
  59.  
  60.   class Scene_Title
  61.    
  62.     alias :motionBlur :command_new_game
  63.    
  64.     def command_new_game
  65.       motionBlur
  66.       $game_variables[Motion_Blur::Opacity_Variable] =
  67.         Motion_Blur::Default_Opacity
  68.       $game_variables[Motion_Blur::Z_Variable] =
  69.         Motion_Blur::Default_Z
  70.       $game_variables[Motion_Blur::Loss_Variable] =
  71.         Motion_Blur::Default_Loss
  72.     end
  73.    
  74.   end
  75.  
  76.   class Scene_Map
  77.    
  78.     method = "#{defined?(SceneManager) ? 'perform_' : defined?(RPG::Cache) ? '' :
  79.      'update_'}transfer#{defined?(SceneManager) ? '' : '_player'}"
  80.    
  81.     eval("
  82.      
  83.      alias :motionBlur :#{method}
  84.      
  85.      def #{method}
  86.        return if (!defined?(SceneManager) && !defined?(RPG::Cache) &&
  87.          !$game_player.transfer?)
  88.        if (@motionBlur != nil)
  89.          @motionBlur.each {|sprite|
  90.            sprite.bitmap.dispose
  91.            sprite.dispose
  92.          }
  93.          @motionBlur.clear
  94.        end
  95.        motionBlur
  96.      end
  97.      
  98.    ")
  99.    
  100.   end
  101.  
  102.   Motion_Blur::Scenes.each {|scene|
  103.    
  104.     scene.class_eval {
  105.      
  106.       alias :motionBlur_1 :main
  107.       alias :motionBlur_2 :update
  108.      
  109.       def main
  110.         @motionBlur = []
  111.         motionBlur_1
  112.         for sprite in @motionBlur
  113.           sprite.bitmap.dispose
  114.           sprite.dispose
  115.         end
  116.         @motionBlur.clear
  117.         @motionBlur = nil
  118.       end
  119.      
  120.       def update
  121.         loss = $game_variables[Motion_Blur::Loss_Variable]
  122.         if (@motionBlur.size > 0)
  123.           (@motionBlur.size - 1).downto(0) {|index|
  124.             sprite = @motionBlur[index]
  125.             sprite.opacity -= loss
  126.             if (sprite.opacity == 0)
  127.               sprite.bitmap.dispose
  128.               sprite.dispose
  129.               @motionBlur.delete_at(index)
  130.             end
  131.           }
  132.         end
  133.         opacity = $game_variables[Motion_Blur::Opacity_Variable]
  134.         if ((opacity > 0) && (opacity > loss))
  135.           @motionBlur   << Sprite.new
  136.           sprite         = @motionBlur.last
  137.           sprite.bitmap  = Graphics.snapshot
  138.           sprite.z       = $game_variables[Motion_Blur::Z_Variable]
  139.           if (defined?(Cache) && (Graphics::GetForegroundWindow.call == Graphics::HWnd))
  140.             sprite.ox = 48 if (Graphics.width < 640 && Graphics::GetSystemMetrics.call(0) == 640)
  141.             sprite.oy = 32 if (Graphics.height < 480 && Graphics::GetSystemMetrics.call(1) == 480)
  142.           end
  143.           sprite.opacity = opacity
  144.         end
  145.         motionBlur_2
  146.       end
  147.      
  148.     }
  149.    
  150.   }
  151.  
  152. end
Expand to see the code.
Amazing script. I find out it works very well.

I sorry to say this, but I have a little request. Can you modify it a little so I can turn it on / off?
I'm trying it but....It's quite difficult.
@DJ Thanks. Well, when you start a new game, the script sets the variable with ID Opacity_Variable to Default_Opacity (and also the other variables in the module). So, if you want to disable the effect, just set that variable to 0.
.:Fênix:. wrote:
@DJ Thanks. Well, when you start a new game, the script sets the variable with ID Opacity_Variable to Default_Opacity (and also the other variables in the module). So, if you want to disable the effect, just set that variable to 0.

I just managed to make modification on my own yesterday. Also i found out some facts to improve the script. Well, I tried setting default opacity to 0 But i found out that it still make game lags since it won't stop script from generating sprites. Instead, i made a global Boolean variable to activate/deactivate, And when diactivated, I forced script to stop generating and only dispose the sprite. It works fine, and it saves cpu useage when not in use.
Making a snapshot with the method You have in your script (finding window with Win32 api) won't work in full screen, and also a bit laggy. So i modified the script to use external dll module to capture the screen, And i found out it works a lot faster and works both in windowed and fullscreen. I uswd WF-Capture script to make it faster.

Well, hope this helped you.
@DJ Well, everything ran fine here. :huh:
But anyways, I've seen his script before. But my idea is using no extra DLL, but sure you can make a new version using it. 🙂

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76035