Alt-tab releases memory?

Started by
2 comments, last by Hans 24 years, 3 months ago
I''m doing a fullscreen game using DDraw, but when I jump out of the game using alt-tab and come back to game, screen is black. I can still exit pressing esc. Does pressing alt-tab (or ctrl-esc etc.) release surfaces'' memory? Or what does it exactly do? And what should I do to make my game working properly?? And does the game still keep going on when it''s not active? Heelp!
Advertisement
If you have a surface in video memory (which includes all primary and backbuffer surfaces in full screen mode), normally they are dealocated during Alt-Tab to give the memory to windows to display things on the screen. At that point, any attempt to modify a surface will return DDERR_SURFACELOST. To fix that, when your program becomes active again,

LPDIRECTDRAWSURFACE7 theSurf; //Your DD surface
HRESULT err; //The error
... initialization / loss of focus...
... focus regained ...
err = theSurf->Blt(......);
if (err == DDERR_SURFACELOST) {
theSurf->Restore(); //Re alocate memory. Will fail
//if your app is not active.
... reload the graphics/ retry the blt...
}
Note that surfaces allocated in System Memory usually do not get deallocated on alt-tab.

As for detection of when your program loses focus, here is a snippet, but for a view of the code in actual use, see any of the full screen samples with the SDK.

In your window proc, you would have this....
BOOL g_bActive = TRUE; //If this is true, your app is in the
//Foreground.

Then in your window proc, you would insert:
switch (message)
{
case WM_ACTIVATE: //Used when window is changed
// Pause if minimized
g_bActive = !((BOOL)HIWORD(wParam));
return 0L;
... other code ...
}

so then, to tell if your aplication is active,
if (g_bActive) {
ExecuteGame() //Some imaginary code
}
else {
.. do something that does not need the program to be active..
}

For this last snippet on how to detect being active, Ive seen the best setup to understand it in the DX SDK once again, and reccommend you check out one of the sample applications.

Good luck,
Fungame
Thanks, got it working! The problem was I forgot to reacquire direct input, and because of that, it always jumped out before it drew anything =)
hint: encapsulate all surfaces into classes to have the restored and even reloaded dynamicly.

This topic is closed to new replies.

Advertisement