mirror of
https://github.com/spiffcode/hostile-takeover.git
synced 2026-06-01 12:16:07 -06:00
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:
parent
8f0e81a3f5
commit
d926ef0bbe
@ -89,6 +89,7 @@ bool Display::Init()
|
|||||||
}
|
}
|
||||||
|
|
||||||
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_TARGETTEXTURE);
|
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_TARGETTEXTURE);
|
||||||
|
this->SetShouldRender(true);
|
||||||
|
|
||||||
// Keep the screen size around
|
// Keep the screen size around
|
||||||
s_siz.cx = cxScreen;
|
s_siz.cx = cxScreen;
|
||||||
@ -269,6 +270,9 @@ void Display::FrameComplete(int cfrmm, UpdateMap **apupd, Rect *arc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Display::RenderGameSurface() {
|
void Display::RenderGameSurface() {
|
||||||
|
if (!m_fshouldRender)
|
||||||
|
return;
|
||||||
|
|
||||||
// Create the texture
|
// Create the texture
|
||||||
texture = SDL_CreateTextureFromSurface(renderer, m_gameSurface);
|
texture = SDL_CreateTextureFromSurface(renderer, m_gameSurface);
|
||||||
|
|
||||||
@ -311,4 +315,8 @@ float Display::Density() {
|
|||||||
return m_density;
|
return m_density;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Display::SetShouldRender(bool fsr) {
|
||||||
|
m_fshouldRender = fsr;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace wi
|
} // namespace wi
|
||||||
|
|||||||
@ -220,6 +220,25 @@ bool ProcessSdlEvent(Event *pevt)
|
|||||||
}
|
}
|
||||||
break;
|
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:
|
case SDL_QUIT:
|
||||||
pevt->eType = appStopEvent;
|
pevt->eType = appStopEvent;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -123,6 +123,7 @@ public:
|
|||||||
void SetFormMgrs(FormMgr *pfrmmSim, FormMgr *pfrmmInput);
|
void SetFormMgrs(FormMgr *pfrmmSim, FormMgr *pfrmmInput);
|
||||||
void RenderGameSurface();
|
void RenderGameSurface();
|
||||||
float Density();
|
float Density();
|
||||||
|
void SetShouldRender(bool fsr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_imode;
|
int m_imode;
|
||||||
@ -145,6 +146,7 @@ private:
|
|||||||
Uint32 m_32bppColors[256];
|
Uint32 m_32bppColors[256];
|
||||||
int m_pixelCount;
|
int m_pixelCount;
|
||||||
float m_density;
|
float m_density;
|
||||||
|
bool m_fshouldRender;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define kfDtClearLine 1
|
#define kfDtClearLine 1
|
||||||
|
|||||||
@ -31,11 +31,11 @@
|
|||||||
// as they do something else.
|
// as they do something else.
|
||||||
|
|
||||||
- (void)applicationDidBecomeActive:(NSNotification *)notification {
|
- (void)applicationDidBecomeActive:(NSNotification *)notification {
|
||||||
// See host.cpp case SDL_APP_DIDENTERFOREGROUND
|
wi::HostAppDidEnterForeground();
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)applicationWillResignActive:(NSNotification *)notification {
|
- (void)applicationWillResignActive:(NSNotification *)notification {
|
||||||
// See host.cpp case SDL_APP_WILLENTERBACKGROUND
|
wi::HostAppWillEnterBackground();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user