Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Pro Tip: Put Your Game To Sleep When The Screen Is Inactive

Author
Message
Mage
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 22nd May 2013 13:13 Edited at: 20th Oct 2018 00:34
The purpose of this post is to help people build better programs.
In this case to demonstrate how to put your program into an ultra low power mode when the screen is not active, or tabbed out.

If your program is full screen, why have it run full blast when minimized?
If you close a laptop or tablet, why have the game suck the juice out of your device?
If you lock your computer, why is the game still chugging along?




The Bar To Enter Is Set Rather High.
In order for this to even be possible, you need to prevent your program from crashing when your program loses the screen. You Lock Windows and then come back, and all your loaded media is flushed from memory. Your program crashes trying to access media that isn't there unless you reload it.
Here is a complete example on how to accomplish this:
http://forum.thegamecreators.com/?m=forum_view&t=202959&b=1

You need this just to get in the door. Otherwise your game crashes, and it's all wasted effort.


So your game doesn't crash anymore...
You've protected your game from crashing when you Lock Windows/Tab Out/Etc like I mentioned above. Congratulations you are now the 1 percent. So let's move on to the Billionaire's Club.

I've proposed the idea of a Standby Mode before. This is where you put the game into a special CPU FRIENDLY loop where it sleeps until it's supposed to wake up. Before I was talking about editors, apps, and input driven programs where the screen does not change until the user provides input. Like notepad.

In this case we are instead going to start and stop Standby Mode when the games window is deactivated and restored.


The Basic Functions.
Here is an example of a function that will get and store the "Handle" identifier for the games window. Getting this is the first step.


Now in your Game Loop, you need to check if your game is still the active window, and enable StandBy Mode if it isn't. You can call a function like this from your game loop.





Full Demonstration. No Media Required.
Here is an example with just the Standby Mode code shown. The example is text only, so it's immune to crashing. It will show you how to sleep the game when the game is tabbed out or the screen is lost.


This example is more complete, showing you how to recover from a Lost Screen Device and implement Standby Mode. This is how to reload your media, animate the loading screen, and put the game into a low power sleep while the game is inactive.


There you go, you're nolonger by default building a substandard game. You can now reach commercial quality software. Your game won't crash when you tab out. Your game is intelligent enough to conserve power when idle. Welcome to the modern world of mobile computing.


This work is improved and based upon these discussions:
http://forum.thegamecreators.com/?m=forum_view&t=202959&b=1
http://forum.thegamecreators.com/?m=forum_view&t=202812&b=1


See my previous tip here...
http://forum.thegamecreators.com/?m=forum_view&t=202959&b=1

One more thing... Give me some feedback. Let me know if people are actually reading this stuff.
It would be nice to know I'm not wasting my time posting this stuff your for guys. Thanks.

Mage's Modular Code - Get awesome UI controls and powerful Bitmap Fonts!
Screen Device Recovery - Stop your apps from crashing when minimized/tabbed/screen locked.
CPU Friendly Frame Limiter - Make your app generate less computer heat + noise, and use less battery life!
Jeff Miller
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 22nd May 2013 14:22
Well written, and on a topic I have been avoiding for too long. Have you played around with the commands in Matrix1Util_15, like "Suspend Program"? The help advice for that command scared me away.
Mage
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 22nd May 2013 14:46 Edited at: 22nd May 2013 14:46
Thanks for the response.

SUSPEND PROGRAM
This command will cause your program to pause until a windows system message is received. It can be used when you know that your program is not being used, such as when it has been minimised.
Do not use this command unless you know what you are doing - inappropriate use of this command may cause your program to appear to freeze.



Some of his commands in this section are good. I have not tested this command so it might have quirks.

A big problem with this command is that you can't do anything while it's being used. If your game was networked, then you would most certainly lose your connection. You can't sneak a little bit of work in to keep things running.

His command also doesn't address the hard part, which is detecting when to suspend. His Nice Wait command works really well in this scenario however.

Jeff Miller
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 22nd May 2013 15:10
I'm not sure that this is the correct place to ask this, but I have never seen a definitive list of exactly what assets are flushed out of memory and thus must be reloaded. Images, objects, and sprites have been mentioned. Will music, sounds, shaders, memblocks, and data arrays created by me (or acquired with the Load Array command) survive the flush?
Zero G Scott
16
Years of Service
User Offline
Joined: 14th Dec 2007
Location: California, USA, Earth, Sol Sys
Posted: 22nd May 2013 15:17
Yes I'm reading and learning from these posts, thank you Mage. They've helped me get my head around some of the problems that require attention with software development and how to approach solutions for them in DBP. Often times stretching my mind about how simple programing done in the proper sequence dramatically affects program stability and everything I plan on doing going forward. I still haven't reached a point in my program where I'm ready to implement this but when I do if I have any specific questions I'll bring them up. Usually I find though I just need to work through things myself and solutions eventually present themselves. Often times as well I can't ask questions I have no clue how to frame in the first place which is why I don't post much. Typically once I do get a point where I know what to ask for the answers are self evident. Anyway, as stated, yes this helps, keep them coming!
Mage
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: Canada
Posted: 22nd May 2013 15:56 Edited at: 22nd May 2013 16:02
Quote: "I'm not sure that this is the correct place to ask this, but I have never seen a definitive list of exactly what assets are flushed out of memory and thus must be reloaded. Images, objects, and sprites have been mentioned. Will music, sounds, shaders, memblocks, and data arrays created by me (or acquired with the Load Array command) survive the flush? "


I haven't tested everything but i know for certain that none of the arrays and variables are flushed. images, sprites, and objects are. I did a quick test and it appears shaders aren't flushed (effect exist() reports they are still loaded). I haven't tested music or sounds. If you are organized properly then none of this is an issue. This has been a side project, and I haven't begun large scale project integration since i'm still polishing a recent move to enhanced animations plugin.

Another issue at the back of my mind, I haven't tested what happens to secondary cameras. Such as when Set Camera To Image syncs camera output while the screen is inactive. Does it work? is it garbled? Does the image not get created? Flush?

And yet more, Some plugins are crash prone if certain things are deleted in the wrong order.


Quote: "Yes I'm reading and learning from these posts, thank you Mage. They've helped me get my head around some of the problems that require attention with software development and how to approach solutions for them in DBP."

Thanks, I'm glad to hear the feedback. Glad to help.

Quote: "Often times as well I can't ask questions I have no clue how to frame in the first place which is why I don't post much. Typically once I do get a point where I know what to ask for the answers are self evident."
Even if you ask general questions, people here can usually narrow it down for you. I know what you mean, a couple of days ago I was asking about a plugin and i managed to solve it on my own. Even If I'm close like that I still like to ask since I might get a better solution and because other people will see what I did to solve the problem. I always try to followup my posts with a resolution.

I have this annoying issue with texture seams appearing when I use evolved's normal mapping. I've got no idea how to fix it and I've been putting off posting about it for a long time. So don't feel bad, we all got our issues.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd May 2013 23:44
Quote: "I have this annoying issue with texture seams appearing when I use evolved's normal mapping. I've got no idea how to fix it and I've been putting off posting about it for a long time."


Sounds like a new thread is called for.

Login to post a reply

Server time is: 2024-03-28 18:59:13
Your offset time is: 2024-03-28 18:59:13