Dynamic fullscreen/windowed switching
ok, i''ve got some code which mostly works... i''ll post it up for everyone. any feedback on making it better would be nice =) If ya have questions or anything just post em up!
//===========================
// processing Alt+Enter
//===========================
In my app, i''m using directinput, so in your dinput code do something like
//------
DIDEVICEOBJECTDATA keybuf[10];
DWORD dwitems = 10;
app.dinput.keyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), keybuf, &dwitems, 0);
//Loop through data and process them
for(DWORD i = 0; i < dwitems; i++)
ProcessKey(keybuf);
//------
//------------------------------------------
void ProcessKey(DIDEVICEOBJECTDATA key)
{
...
if((key.dwOfs == DIK_LMENU) // (key.dwOfs == DIK_RMENU))
{
app.keyboard.keystates.alt = 1 && (key.dwData & 0x80);
return;
}
if(app.keyboard.keystates.alt)
{
if(key.dwOfs == DIK_RETURN)
{
app.screen.fullscreen = !app.screen.fullscreen;
SetDisplay();
}
}
...
}
//===========================
// two funcs to change res
//===========================
//------------------------------------------
bool ChangeResolution(bool reset)
{
DWORD dwRet;
DEVMODE devMode;
devMode.dmFields = DM_BITSPERPEL / DM_PELSWIDTH / DM_PELSHEIGHT;
//if exiting set it back how it was!
if(reset)
{
devMode.dmBitsPerPel = app.screen.original.bitdepth;
devMode.dmPelsWidth = app.screen.original.width;
devMode.dmPelsHeight = app.screen.original.height;
dwRet = ChangeDisplaySettings(&devMode, CDS_RESET);
}
else
{
devMode.dmBitsPerPel = app.screen.current.bitdepth;
//original res
if(!app.screen.fullscreen)
{
devMode.dmPelsWidth = app.screen.original.width;
devMode.dmPelsHeight = app.screen.original.height;
}
//Fullscreen
else
{
devMode.dmPelsWidth = app.screen.current.width;
devMode.dmPelsHeight = app.screen.current.height;
}
DEVMODE CurrDevMode;
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &CurrDevMode);
return ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
}
//------------------------------------------
void SetDisplay(void)
{
HWND hWnd;
if(app.hWnd != NULL) DestroyWindow(app.hWnd);
if(!ChangeResolution(false)) return;
if(app.screen.fullscreen)
{
hWnd = CreateWindowEx(WS_EX_APPWINDOW,
szAppName, szAppName,
WS_POPUP / WS_CLIPSIBLINGS,
0, 0, app.screen.current.width, app.screen.current.height,
NULL, NULL, app.hInstance, NULL);
}
else
{
//compensate for frame & caption
LONG h = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CXDLGFRAME) * 2 + app.screen.current.height;
LONG w = GetSystemMetrics(SM_CXDLGFRAME) * 2 + app.screen.current.width;
hWnd = CreateWindowEx(WS_EX_APPWINDOW,
szAppName, szAppName,
WS_BORDER / WS_DLGFRAME / WS_CLIPSIBLINGS /
WS_MAXIMIZEBOX / WS_MINIMIZEBOX / WS_SYSMENU,
50, 50, w, h,
NULL, NULL, app.hInstance, NULL);
}
if(!hWnd) PostQuitMessage(1);
ShowWindow(hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
}
//===========================
// In Winmain just call SetDisplay()
// after registering your wndclass
//===========================
WinMain...
{
...
//register wndclass
SetDisplay();
...
};
//===========================
// In WndProc do something
// similar
//===========================
WndProc...
{
...
WM_CREATE:
{
...
InitDInput <-- if using direct input, init here
GetDC...
SetupPixelFormat...
wglCreateContext...
wglMakeCurrent...
LoadTextures... <--- Need to reload textures after recreating the window
InitGL... <-- setup things such as clear color and whatnot
...
}
WM_DESTROY:
{
...
wglMakeCurrent(NULL, NULL)
wglDeleteContext...
ReleaseDC...
TermDInput <-- if using direct input, kill it here
...
}
...
}
The new article on GameDev should help somewhat as well Just thought I''d state the overly obvious.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement