I am working on a rather large (and very far along) game off and on in my spare time. I make use of a lot of editors made in DBP to enhance and package together media like sounds, textures, objects in ways that make it easier for the game to organize and load them.
For Example: My Character Editor loads and saves Character files that store texture and object path names along side inventory lists and stats.
I realized that I was only getting about 50 minutes of battery life with the editor open. The old familiar DBP performance behavior was to blame. The frame rate was probably in the high 800's and nothing was happening 99% of the time on screen. A massive astronomical waste of resources.
I managed with just two techniques to reduce the footprint of the editor from the level of "Call of Duty", to something approaching "Notepad".
Problem 1:
Runaway frame rate way higher than anything the screen needed was
solved by implementing a frame limiter. This ensures any time the screen is rendering, it's at least reasonably efficient.
Processor Friendly Frame Limiter Example:
`Declare at beginning of program
global g_LastSyncTime as integer
`Syncs Screen + Frame Limiter
Function SyncScreen()
scFrameTime = 32 - Abs(Timer() - g_LastSyncTime)
While scFrameTime > 0
Sync Sleep 1
scFrameTime = 32 - Abs(Timer() - g_LastSyncTime)
EndWhile
Sync Sleep 0
sync
g_LastSyncTime = Timer()
EndFunction
Problem 2:
99% of the time nothing is happening and drawing the screen over and over is a waste. This was
solved by implementing a Standby Mode feature.
We've all checked task manager. Games run at 100% of 1 CPU core, in some cases with plugins or sound maybe a bit more. Pointless if the screen isn't changing.
Standby Mode Example:
Global standby_Level = 0
Function Standby_Main()
`Promote Standby Level if Idle
If standby_Level = 0
If MouseMoveX() = 0
If MouseMoveY() = 0
If Scancode() = 0
If MouseClick() = 0 Then standby_Level = 1
EndIf
EndIf
EndIf
EndIf
`Execute standby level if in Standby
While standby_Level = 1
sync sleep 1
If MouseMoveX() <> 0 or MouseMoveY() <> 0 then standby_Level = 0
If Scancode() <> 0 then standby_Level = 0
If MouseClick() <> 0 then standby_Level = 0
EndWhile
Sync Sleep 0
EndFunction
Function Standby_Clear()
`Clear Standby Global Variables.
standby_Level = 0
EndFunction
The usage of these functions is as follows:
Global standby_Level = 0
Standby_Clear()
Do
`Operate Program Here...
`Remember to call Standby_Clear() after any input is handled.
Sync
Standby_Main()
Loop
Now the CPU level stays at 0%. The program from the perspective of the user operates exactly the same without any hint of disruption or removal of functionality.
The Principle Behind The Idea:
When the program isn't doing anything it checks to see if the keyboard and mouse are idle and loops into a processor friendly sleep, where it keeps checking the inputs. If an input changes then it exits the loop, and allows the program to continue. Since this happens so fast, it doesn't disturb the user. Because the screen contents aren't changing when the user isn't moving the mouse or typing, having the program do "nothing" doesn't look or feel any different and is about several million times less workload.
The result? Laptop battery lasts over 3 hours. The fans don't turn on. It doesn't get hot.
See my previous tip here...
http://forum.thegamecreators.com/?m=forum_view&t=199942&b=1
Edit: Made a code correction above, and added a demo. No media required.
Rem Project: StandbyDemo
Rem Created: Thursday, January 10, 2013
Rem ***** Main Source File *****
`Setup Screen
Set Window On
Set Display Mode 1024, 768,32
Sync Rate 0
Sync On
Autocam Off
Backdrop On
`Setup Obeject
Make Object Cube 1, 5
Position Object 1, 0, 0, 20
`--------------------------------------------------------|
`Handle Scene with no optimization.
Repeat
`Rotate Cube
If Spacekey() = 1 Then YRotate Object 1, Object Angle Y(1) + 1
`Draw Stats
Text 10, 10, "NO OPTIMIZATION"
Text 300, 500, "A Typical DBP Program. CPU + GPU Are running at 100%."
Text 300, 520, "CPU is Hot. GPU is Hot."
text 300, 600, "PRESS SPACE BUTTON TO ROTATE - PRESS ENTER TO ADVANCE"
Text 10, 720, "Random Number: " + Str$(Rnd(9999))
Text 10, 740, "FPS: " + str$(Screen Fps())
`Draw Screen
Sync
Until ReturnKey()
`Wait until return key is released
While ReturnKey()
EndWhile
`--------------------------------------------------------|
`--------------------------------------------------------|
`Handle Scene with Sync Rate 30 optimization.
Sync Rate 30
Repeat
`Rotate Cube
If Spacekey() = 1 Then YRotate Object 1, Object Angle Y(1) + 1
`Draw Stats
Text 10, 10, "OPTIMIZATION - Sync Rate 30"
Text 300, 500, "SYNC RATE gives the GPU a rest. CPU is still at 100%."
Text 300, 520, "CPU is Hot. GPU is Cool."
text 300, 600, "PRESS SPACE BUTTON TO ROTATE - PRESS ENTER TO ADVANCE"
Text 10, 720, "Random Number: " + Str$(Rnd(9999))
Text 10, 740, "FPS: " + str$(Screen Fps())
`Draw Screen
Sync
Until ReturnKey()
`Wait until return key is released
While ReturnKey()
EndWhile
`--------------------------------------------------------|
`--------------------------------------------------------|
`Handle Scene with CPU Friendly Frame Limiter.
global g_LastSyncTime as integer
Sync Rate 0
Repeat
`Rotate Cube
If Spacekey() = 1 Then YRotate Object 1, Object Angle Y(1) + 1
`Draw Stats
Text 10, 10, "OPTIMIZATION - CPU Friendly Frame Limiter"
Text 300, 500, "CPU Friendly Limiter gives the GPU and CPU a rest."
Text 300, 520, "CPU is Cool. GPU is Cool."
text 300, 600, "PRESS SPACE BUTTON TO ROTATE - PRESS ENTER TO ADVANCE"
Text 10, 720, "Random Number: " + Str$(Rnd(9999))
Text 10, 740, "FPS: " + str$(Screen Fps())
`Draw Screen
SyncScreen()
Until ReturnKey()
`Wait until return key is released
While ReturnKey()
EndWhile
`--------------------------------------------------------|
`--------------------------------------------------------|
`Handle Scene with CPU Friendly Frame Limiter + Standby Mode.
Global standby_Level = 0
Global standby_MouseX = 0
Global standby_MouseY = 0
Repeat
`Rotate Cube
If Spacekey() = 1
YRotate Object 1, Object Angle Y(1) + 1
Standby_Clear()
EndIf
`Draw Stats
Text 10, 10, "OPTIMIZATION - CPU Friendly Frame Limiter + Standby Mode"
Text 300, 500, "Standby Mode stops the program when the cube isn't moving."
Text 300, 520, "CPU is Cold. GPU is Cold."
text 300, 600, "PRESS SPACE BUTTON TO ROTATE - PRESS ENTER TO ADVANCE"
Text 10, 720, "Random Number: " + Str$(Rnd(9999))
Text 10, 740, "FPS: " + str$(Screen Fps())
`Draw Screen
SyncScreen()
`Handle Standby Mode
Standby_Main()
Until ReturnKey()
`Wait until return key is released
While ReturnKey()
EndWhile
`--------------------------------------------------------|
End
Function Standby_Main()
`Promote Standby Level if Idle
If standby_Level = 0
If MouseMoveX() = 0
If MouseMoveY() = 0
If Scancode() = 0
If MouseClick() = 0 Then standby_Level = 1
EndIf
EndIf
EndIf
EndIf
`Execute standby level if in Standby
While standby_Level = 1
sync sleep 1
If MouseMoveX() <> 0 or MouseMoveY() <> 0 then standby_Level = 0
If Scancode() <> 0 then standby_Level = 0
If MouseClick() <> 0 then standby_Level = 0
EndWhile
Sync Sleep 0
EndFunction
Function Standby_Clear()
`Clear Standby Global Variables.
standby_Level = 0
EndFunction
`Syncs Screen + Frame Limiter
Function SyncScreen()
scFrameTime = 32 - Abs(Timer() - g_LastSyncTime)
While scFrameTime > 0
Sync Sleep 1
scFrameTime = 32 - Abs(Timer() - g_LastSyncTime)
EndWhile
Sync Sleep 0
sync
g_LastSyncTime = Timer()
EndFunction