Prevent SDL from rendering when app is backgrounded

On some platforms, the game will crash if SDL attempts to render to the
screen/window while the game is backgrounded. To prevent this, stop
rendering when the game enters a background state and resume rendering
when the game enters a foreground state.
This commit is contained in:
Nathan Fulton 2016-03-14 14:35:15 -04:00
parent 8f0e81a3f5
commit d926ef0bbe
4 changed files with 31 additions and 2 deletions

View File

@ -89,6 +89,7 @@ bool Display::Init()
}
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_TARGETTEXTURE);
this->SetShouldRender(true);
// Keep the screen size around
s_siz.cx = cxScreen;
@ -269,6 +270,9 @@ void Display::FrameComplete(int cfrmm, UpdateMap **apupd, Rect *arc,
}
void Display::RenderGameSurface() {
if (!m_fshouldRender)
return;
// Create the texture
texture = SDL_CreateTextureFromSurface(renderer, m_gameSurface);
@ -311,4 +315,8 @@ float Display::Density() {
return m_density;
}
void Display::SetShouldRender(bool fsr) {
m_fshouldRender = fsr;
}
} // namespace wi

View File

@ -220,6 +220,25 @@ bool ProcessSdlEvent(Event *pevt)
}
break;
case SDL_APP_DIDENTERFOREGROUND:
// Allow the display to render
gpdisp->SetShouldRender(true);
// SDL may have released its graphics context if the app was previously
// backgrounded. This leaves the screen black when the user returns.
// Hack: Draw dib and render
gpmfrmm->DrawFrame(true);
gpdisp->RenderGameSurface();
break;
case SDL_APP_WILLENTERBACKGROUND:
// Stop display rendering; SDL may release its graphics context when
// backgrounded, so we don't want to try to render to a non-existant context.
gpdisp->SetShouldRender(false);
break;
case SDL_QUIT:
pevt->eType = appStopEvent;
break;

View File

@ -123,6 +123,7 @@ public:
void SetFormMgrs(FormMgr *pfrmmSim, FormMgr *pfrmmInput);
void RenderGameSurface();
float Density();
void SetShouldRender(bool fsr);
private:
int m_imode;
@ -145,6 +146,7 @@ private:
Uint32 m_32bppColors[256];
int m_pixelCount;
float m_density;
bool m_fshouldRender;
};
#define kfDtClearLine 1

View File

@ -31,11 +31,11 @@
// as they do something else.
- (void)applicationDidBecomeActive:(NSNotification *)notification {
// See host.cpp case SDL_APP_DIDENTERFOREGROUND
wi::HostAppDidEnterForeground();
}
- (void)applicationWillResignActive:(NSNotification *)notification {
// See host.cpp case SDL_APP_WILLENTERBACKGROUND
wi::HostAppWillEnterBackground();
}
#endif