Moved most of the sleep code into the Window class.

Input is now consumed on wake.
This commit is contained in:
Aloshi 2014-06-01 19:14:22 -05:00
parent 61d45532cf
commit 489298ef26
3 changed files with 46 additions and 36 deletions

View file

@ -11,7 +11,7 @@
#include "components/ImageComponent.h" #include "components/ImageComponent.h"
Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10), Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10),
mAllowSleep(true) mAllowSleep(true), mSleeping(false), mTimeSinceLastInput(0)
{ {
mViewController = new ViewController(this); mViewController = new ViewController(this);
mHelp = new HelpComponent(this); mHelp = new HelpComponent(this);
@ -108,6 +108,17 @@ void Window::textInput(const char* text)
void Window::input(InputConfig* config, Input input) void Window::input(InputConfig* config, Input input)
{ {
if(mSleeping)
{
// wake up
mTimeSinceLastInput = 0;
mSleeping = false;
onWake();
return;
}
mTimeSinceLastInput = 0;
if(config->getDeviceId() == DEVICE_KEYBOARD && input.value && input.id == SDLK_g && SDL_GetModState() & KMOD_LCTRL && Settings::getInstance()->getBool("Debug")) if(config->getDeviceId() == DEVICE_KEYBOARD && input.value && input.id == SDLK_g && SDL_GetModState() & KMOD_LCTRL && Settings::getInstance()->getBool("Debug"))
{ {
// toggle debug grid with Ctrl-G // toggle debug grid with Ctrl-G
@ -169,6 +180,8 @@ void Window::update(int deltaTime)
mFrameCountElapsed = 0; mFrameCountElapsed = 0;
} }
mTimeSinceLastInput += deltaTime;
if(peekGui()) if(peekGui())
peekGui()->update(deltaTime); peekGui()->update(deltaTime);
} }
@ -193,16 +206,6 @@ void Window::render()
} }
} }
// draw everything
/*const unsigned int drawBGAfter = mGuiStack.size() > 1 ? mGuiStack.size() - 2 : mGuiStack.size();
for(unsigned int i = 0; i < mGuiStack.size(); i++)
{
mGuiStack.at(i)->render(transform);
if(i == drawBGAfter)
mBackgroundOverlay->render(transform);
}*/
if(!mRenderedHelpPrompts) if(!mRenderedHelpPrompts)
mHelp->render(transform); mHelp->render(transform);
@ -211,6 +214,14 @@ void Window::render()
Renderer::setMatrix(Eigen::Affine3f::Identity()); Renderer::setMatrix(Eigen::Affine3f::Identity());
mDefaultFonts.at(1)->renderTextCache(mFrameDataText.get()); mDefaultFonts.at(1)->renderTextCache(mFrameDataText.get());
} }
unsigned int screensaverTime = (unsigned int)Settings::getInstance()->getInt("ScreenSaverTime");
if(mTimeSinceLastInput >= screensaverTime && screensaverTime != 0 && mAllowSleep)
{
// go to sleep
mSleeping = true;
onSleep();
}
} }
void Window::normalizeNextUpdate() void Window::normalizeNextUpdate()
@ -325,3 +336,16 @@ void Window::setHelpPrompts(const std::vector<HelpPrompt>& prompts, const HelpSt
mHelp->setPrompts(addPrompts); mHelp->setPrompts(addPrompts);
} }
void Window::onSleep()
{
Renderer::setMatrix(Eigen::Affine3f::Identity());
unsigned char opacity = Settings::getInstance()->getString("ScreenSaverBehavior") == "dim" ? 0xA0 : 0xFF;
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x00000000 | opacity);
}
void Window::onWake()
{
}

View file

@ -32,6 +32,7 @@ public:
void normalizeNextUpdate(); void normalizeNextUpdate();
inline bool isSleeping() const { return mSleeping; }
bool getAllowSleep(); bool getAllowSleep();
void setAllowSleep(bool sleep); void setAllowSleep(bool sleep);
@ -41,6 +42,9 @@ public:
void setHelpPrompts(const std::vector<HelpPrompt>& prompts, const HelpStyle& style); void setHelpPrompts(const std::vector<HelpPrompt>& prompts, const HelpStyle& style);
private: private:
void onSleep();
void onWake();
ViewController* mViewController; ViewController* mViewController;
HelpComponent* mHelp; HelpComponent* mHelp;
ImageComponent* mBackgroundOverlay; ImageComponent* mBackgroundOverlay;
@ -58,6 +62,9 @@ private:
bool mNormalizeNextUpdate; bool mNormalizeNextUpdate;
bool mAllowSleep; bool mAllowSleep;
bool mSleeping;
unsigned int mTimeSinceLastInput;
bool mRenderedHelpPrompts; bool mRenderedHelpPrompts;
}; };

View file

@ -238,8 +238,6 @@ int main(int argc, char* argv[])
//generate joystick events since we're done loading //generate joystick events since we're done loading
SDL_JoystickEventState(SDL_ENABLE); SDL_JoystickEventState(SDL_ENABLE);
bool sleeping = false;
unsigned int timeSinceLastEvent = 0;
int lastTime = SDL_GetTicks(); int lastTime = SDL_GetTicks();
bool running = true; bool running = true;
@ -260,11 +258,7 @@ int main(int argc, char* argv[])
case SDL_TEXTEDITING: case SDL_TEXTEDITING:
case SDL_JOYDEVICEADDED: case SDL_JOYDEVICEADDED:
case SDL_JOYDEVICEREMOVED: case SDL_JOYDEVICEREMOVED:
if(InputManager::getInstance()->parseEvent(event, &window)) InputManager::getInstance()->parseEvent(event, &window);
{
sleeping = false;
timeSinceLastEvent = 0;
}
break; break;
case SDL_QUIT: case SDL_QUIT:
running = false; running = false;
@ -272,10 +266,10 @@ int main(int argc, char* argv[])
} }
} }
if(sleeping) if(window.isSleeping())
{ {
lastTime = SDL_GetTicks(); lastTime = SDL_GetTicks();
SDL_Delay(1); //this doesn't need to be accurate SDL_Delay(1); // this doesn't need to be accurate, we're just giving up our CPU time until something wakes us up
continue; continue;
} }
@ -283,27 +277,12 @@ int main(int argc, char* argv[])
int deltaTime = curTime - lastTime; int deltaTime = curTime - lastTime;
lastTime = curTime; lastTime = curTime;
//cap deltaTime at 1000 // cap deltaTime at 1000
if(deltaTime > 1000 || deltaTime < 0) if(deltaTime > 1000 || deltaTime < 0)
deltaTime = 1000; deltaTime = 1000;
window.update(deltaTime); window.update(deltaTime);
window.render(); window.render();
//sleep if we're past our threshold
//sleeping entails setting a flag to start skipping frames
//and initially drawing a black semi-transparent rect to dim the screen
timeSinceLastEvent += deltaTime;
if(timeSinceLastEvent >= (unsigned int)Settings::getInstance()->getInt("ScreenSaverTime") && Settings::getInstance()->getInt("ScreenSaverTime") != 0 && window.getAllowSleep())
{
sleeping = true;
timeSinceLastEvent = 0;
Renderer::setMatrix(Eigen::Affine3f::Identity());
unsigned char opacity = Settings::getInstance()->getString("ScreenSaverBehavior") == "dim" ? 0xA0 : 0xFF;
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x00000000 | opacity);
}
Renderer::swapBuffers(); Renderer::swapBuffers();
Log::flush(); Log::flush();