Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Motion Blur

Motion Blur
2012.01.21b
.:Fênix:.


motionblur.png


Code:
#===============================================================================

# ** Motion Blur

#-------------------------------------------------------------------------------

#   @version 2012.01.21b

#   @author  .:Fênix:. <bmotamer@gmail.com> & zeus81

#   @brief   Blurs the screen when it moves

#===============================================================================

 

module Motion_Blur

  Enabled          = true        # Enable the script? (true / false)

  Opacity_Variable = 1           # Variable what changes the overlay opacity

  Z_Variable       = 2           # Variable what changes the overlay Z

  Loss_Variable    = 3           # Variable what changes the overlay opacity loss

  Default_Opacity  = 128         # Default overlay opacity

  Default_Z        = 999         # Default overlay Z

  Default_Loss     = 32          # Default overlay opacity loss

  Scenes           = [Scene_Map] # Scenes what supports motion blur

end

 

if (Motion_Blur::Enabled)

  

  module Graphics

    

    RtlMoveMemory          = Win32API.new("kernel32", "RtlMoveMemory",          "pii",       "i")

    BitBlt                 = Win32API.new("gdi32",    "BitBlt",                 "iiiiiiiii", "i")

    CreateCompatibleBitmap = Win32API.new("gdi32",    "CreateCompatibleBitmap", "iii",       "i")

    CreateCompatibleDC     = Win32API.new("gdi32",    "CreateCompatibleDC",     "i",         "i")

    DeleteObject           = Win32API.new("gdi32",    "DeleteObject",           "i",         "i")

    GetDIBits              = Win32API.new("gdi32",    "GetDIBits",              "iiiiipi",   "i")

    SelectObject           = Win32API.new("gdi32",    "SelectObject",           "ii",        "i")

    SetDIBits              = Win32API.new("gdi32",    "SetDIBits",              "iiiiipi",   "i")

    GetForegroundWindow    = Win32API.new("user32",   "GetForegroundWindow",    "",          "i")

    GetSystemMetrics       = Win32API.new("user32",   "GetSystemMetrics",       "i",         "i")

    

    HWnd                   = GetForegroundWindow.call

    DC                     = Win32API.new("user32",   "GetDC",                  "i",         "i").call(HWnd)

    

    def self.snapshot(width = defined?(RPG::Cache) ? 640 : self.width,

        height = defined?(RPG::Cache) ? 480 : self.height)

      bitmap  = Bitmap.new(width, height)

      address = "\0" * 4

      RtlMoveMemory.call(address, bitmap.__id__ * 2 + 16, 4)

      RtlMoveMemory.call(address, address.unpack("L")[0] + 8, 4)

      RtlMoveMemory.call(address, address.unpack("L")[0] + 16, 4)

      address = address.unpack("L")[0]

      info    = [40, width, height, 1, 32, 0, 0, 0, 0, 0, 0].pack("LllSSLLllLL")

      hDC     = CreateCompatibleDC.call(DC)

      hBM     = CreateCompatibleBitmap.call(DC, width, height)

      DeleteObject.call(SelectObject.call(hDC, hBM))

      SetDIBits.call(hDC, hBM, 0, height, address, info, 0)

      BitBlt.call(hDC, 0, 0, width, height, DC, 0, 0, 0xCC0020)

      GetDIBits.call(hDC, hBM, 0, height, address, info, 0)

      DeleteObject.call(hBM)

      DeleteObject.call(hDC)

      return bitmap

    end

    

  end

  

  class Scene_Title

    

    alias :motionBlur :command_new_game

    

    def command_new_game

      motionBlur

      $game_variables[Motion_Blur::Opacity_Variable] =

        Motion_Blur::Default_Opacity

      $game_variables[Motion_Blur::Z_Variable] =

        Motion_Blur::Default_Z

      $game_variables[Motion_Blur::Loss_Variable] =

        Motion_Blur::Default_Loss

    end

    

  end

  

  class Scene_Map

    

    method = "#{defined?(SceneManager) ? 'perform_' : defined?(RPG::Cache) ? '' :

      'update_'}transfer#{defined?(SceneManager) ? '' : '_player'}"

    

    eval("

      

      alias :motionBlur :#{method}

      

      def #{method}

        return if (!defined?(SceneManager) && !defined?(RPG::Cache) &&

          !$game_player.transfer?)

        if (@motionBlur != nil)

          @motionBlur.each {|sprite|

            sprite.bitmap.dispose

            sprite.dispose

          }

          @motionBlur.clear

        end

        motionBlur

      end

      

    ")

    

  end

  

  Motion_Blur::Scenes.each {|scene|

    

    scene.class_eval {

      

      alias :motionBlur_1 :main

      alias :motionBlur_2 :update

      

      def main

        @motionBlur = []

        motionBlur_1

        for sprite in @motionBlur

          sprite.bitmap.dispose

          sprite.dispose

        end

        @motionBlur.clear

        @motionBlur = nil

      end

      

      def update

        loss = $game_variables[Motion_Blur::Loss_Variable]

        if (@motionBlur.size > 0)

          (@motionBlur.size - 1).downto(0) {|index|

            sprite = @motionBlur[index]

            sprite.opacity -= loss

            if (sprite.opacity == 0)

              sprite.bitmap.dispose

              sprite.dispose

              @motionBlur.delete_at(index)

            end

          }

        end

        opacity = $game_variables[Motion_Blur::Opacity_Variable]

        if ((opacity > 0) && (opacity > loss))

          @motionBlur   << Sprite.new

          sprite         = @motionBlur.last

          sprite.bitmap  = Graphics.snapshot

          sprite.z       = $game_variables[Motion_Blur::Z_Variable]

          if (defined?(Cache) && (Graphics::GetForegroundWindow.call == Graphics::HWnd))

            sprite.ox = 48 if (Graphics.width < 640 && Graphics::GetSystemMetrics.call(0) == 640)

            sprite.oy = 32 if (Graphics.height < 480 && Graphics::GetSystemMetrics.call(1) == 480)

          end

          sprite.opacity = opacity

        end

        motionBlur_2

      end

      

    }

    

  }

  

end
 

DJ

Some guy that did RMXP before
Member

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.
 

DJ

Some guy that did RMXP before
Member

.:Fênix:.":2drf5yny said:
@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. :smile:
 
DJ":23ttui4o said:
.:Fênix:.":23ttui4o said:
@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.
Does anyone know how to get in touch with DJ? Motion Blur is a big part of our game Unraveled: Tale of the Shipbreaker's Daughter, and it's very sad that it doesn't work in Full Screen. DJ hasn't been here since January -- I hope I can get in touch with him somehow. Or does anyone else have the edit with the WF-Capture script?

(Edit: I apologize for necro-posting and I'm very OK with just opening a new topic about this, but I figured in this case it would be better to just grab the old topic so that I don't have to quote everything that was said here. :))
 
I have not seen DJ in a while and do not have this script unfortunately. Or any way to contact DJ. No worries about necroposting :thumbsup:
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top