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.

Keeping an unfocused game window running

Hey all,

I've been trying to get RMXP to pause the background music whenever the game window is not focused in the Windows OS, but I'm having trouble getting it to work.  Since as of now everything pauses (except for the music) with RMXP when its game window is not focused, I figured something in the Graphics module was causing this pause - though after tinkering with it, I'm not so sure anymore.  I edited the Graphics module a bit by making RMXP call my override Graphics update method before the old one so that I could put a check for what the currently active window in Windows is and then check its name against my game window's name - and this works fine.

But the problem is, if I unfocus the game window when its running, the active window check never gets called because the code pauses when the game window isn't focused, despite my editing of the Graphics update method!  The only thing I can think of that would cause this is maybe the Ruby interpreter in RMXP is what is pausing when the game window is not focused.  If so, is there a solution to this problem?  Or is it just a problem with my code?

Here is my modification to the Graphics module:
Code:
module Graphics
  class << self
    alias new_update update
    def update
      hTheActiveWindow = Win32API.new("user32", "GetActiveWindow", [], 'L')
      h = hTheActiveWindow.call
      
      getWindowText = Win32API.new("user32","GetWindowText","IPI","L")
      name = "                         "#big enough buffer to support the game's name
      getWindowText.call(h, name, 15)
      
      if name.slice(0,7) != "My Game"
        p "the focused window is not my game!"
      end
      
      #run original Graphics.update method
      new_update
    end
  end
end
I know my API calls work because I had it set to print a message if the active window was the game, which it ended up spamming while it was active.

And I've been testing this just in my Scene_Title loop, which just has this bit of code:
Code:
    loop do
      Graphics.update
      Input.update
      update

      if $scene != self
        break
      end
    end

So, is there a way to get RMXP to continue running the scripts even while its game window is not focused?  Or is there just a flaw in my code?

I'd appreciate any help.  Thanks!
 
I've been trying that too(but in a completely different way than you did), but since it wasn't very important to me I decided to quit after I failed.

Even tho it would be really nice, I don't think it's possible.
 

Zeriab

Sponsor

The biggest problem with getting this to work is that the Ruby interpreter and the audio playing mechanism runs in separate threads.
I believe it will be easier to pause the music when the game window is unfocused than keep the ruby interpreter running.

You could try making DLL file which starts a thread that monitors the game window. It will then halt the audio processing thread if the window becomes unfocused and start it again when the window becomes focused again.
I don't know how difficult it will be nor how much resources it will consume, but I believe it is a plausible way of preventing the music from playing while the rest of the game doesn't run.
 

Zeriab

Sponsor

I don't know how to pause threads.
I tried to look to look at the Game process with Spy++ (A tool following with the installation of Visual Studios 2008 Pro)
It had several threads.

How to pause them or freeze them I don't know. I hope one is able to do so without altering the application itself.
 
Thank you both, S S Muu and Zeriab, for helping me out with this problem, but I think I'm going to abandon my effort to pause the music when the window isn't selected.

Instead I'll just use calls to the System clock, and resync the music with the game based on how long the game's script has been paused for.  The only downside to this, is I'll have to do constant checks between the time elapsed in the game's clock and the time elapsed in the System clock to check for when the music got out-of-sync due to an unselected game window.  And from what I've read and have been taught, System calls are laggy.. and I'll be using them frequently with this approach.  But, at least it'll be working!
 

Zeriab

Sponsor

All Ruby threads will be paused when the window is unfocused.
That's why I want to start a new native thread through a DLL call.

@Frostt: I am sorry you have to abandon your attempt but I am glad you found a work-around for your specific problem
 

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