diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2013-05-04 13:42:23 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2013-05-04 13:42:23 (GMT) |
| commit | cab667001dd1944cae646349430b969912c0b157 (patch) | |
| tree | 53a425ae892e4f7f643822abdf1bb26449a443a0 /src/PowderToySDL.cpp | |
| parent | dd0f5f5efb4f108c2ce329a2ceabc7d8fa2b63f8 (diff) | |
| parent | 630216c2531545c232edb45d3054c903c2c205c4 (diff) | |
| download | powder-cab667001dd1944cae646349430b969912c0b157.zip powder-cab667001dd1944cae646349430b969912c0b157.tar.gz | |
Merge branch 'master' of github.com:FacialTurd/The-Powder-Toy
Diffstat (limited to 'src/PowderToySDL.cpp')
| -rw-r--r-- | src/PowderToySDL.cpp | 100 |
1 files changed, 94 insertions, 6 deletions
diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index c57b3b9..93a8996 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -53,10 +53,6 @@ Atom XA_CLIPBOARD, XA_TARGETS; char *clipboardText = NULL; -#ifdef WIN -extern "C" IMAGE_DOS_HEADER __ImageBase; -#endif - int desktopWidth = 1280, desktopHeight = 1024; SDL_Surface * sdl_scrn; @@ -276,8 +272,11 @@ int SDLOpen() exit(-1); } HWND WindowHandle = SysInfo.window; - HICON hIconSmall = (HICON)LoadImage(reinterpret_cast<HMODULE>(&__ImageBase), MAKEINTRESOURCE(101), IMAGE_ICON, 16, 16, LR_SHARED); - HICON hIconBig = (HICON)LoadImage(reinterpret_cast<HMODULE>(&__ImageBase), MAKEINTRESOURCE(101), IMAGE_ICON, 32, 32, LR_SHARED); + + // Use GetModuleHandle to get the Exe HMODULE/HINSTANCE + HMODULE hModExe = GetModuleHandle(NULL); + HICON hIconSmall = (HICON)LoadImage(hModExe, MAKEINTRESOURCE(101), IMAGE_ICON, 16, 16, LR_SHARED); + HICON hIconBig = (HICON)LoadImage(hModExe, MAKEINTRESOURCE(101), IMAGE_ICON, 32, 32, LR_SHARED); SendMessage(WindowHandle, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall); SendMessage(WindowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIconBig); #elif defined(LIN) @@ -543,6 +542,87 @@ int GetModifiers() return SDL_GetModState(); } +#ifdef WIN + +// Returns true if the loaded position was set +// Returns false if something went wrong: SDL_GetWMInfo failed or the loaded position was invalid +bool LoadWindowPosition(int scale) +{ + SDL_SysWMinfo sysInfo; + SDL_VERSION(&sysInfo.version); + if (SDL_GetWMInfo(&sysInfo) > 0) + { + int windowW = (XRES + BARSIZE) * scale; + int windowH = (YRES + MENUSIZE) * scale; + + int savedWindowX = Client::Ref().GetPrefInteger("WindowX", INT_MAX); + int savedWindowY = Client::Ref().GetPrefInteger("WindowY", INT_MAX); + + // Center the window on the primary desktop by default + int newWindowX = (desktopWidth - windowW) / 2; + int newWindowY = (desktopHeight - windowH) / 2; + + bool success = false; + + if (savedWindowX != INT_MAX && savedWindowY != INT_MAX) + { + POINT windowPoints[] = { + {savedWindowX, savedWindowY}, // Top-left + {savedWindowX + windowW, savedWindowY + windowH} // Bottom-right + }; + + MONITORINFO monitor; + monitor.cbSize = sizeof(monitor); + if (GetMonitorInfo(MonitorFromPoint(windowPoints[0], MONITOR_DEFAULTTONEAREST), &monitor) != 0) + { + // Only use the saved window position if it lies inside the visible screen + if (PtInRect(&monitor.rcMonitor, windowPoints[0]) && PtInRect(&monitor.rcMonitor, windowPoints[1])) + { + newWindowX = savedWindowX; + newWindowY = savedWindowY; + + success = true; + } + else + { + // Center the window on the nearest monitor + newWindowX = monitor.rcMonitor.left + (monitor.rcMonitor.right - monitor.rcMonitor.left - windowW) / 2; + newWindowY = monitor.rcMonitor.top + (monitor.rcMonitor.bottom - monitor.rcMonitor.top - windowH) / 2; + } + } + } + + SetWindowPos(sysInfo.window, 0, newWindowX, newWindowY, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER); + + // True if we didn't use the default, i.e. the position was valid + return success; + } + + return false; +} + +// Returns true if the window position was saved +bool SaveWindowPosition() +{ + SDL_SysWMinfo sysInfo; + SDL_VERSION(&sysInfo.version); + if (SDL_GetWMInfo(&sysInfo) > 0) + { + WINDOWPLACEMENT placement; + placement.length = sizeof(placement); + GetWindowPlacement(sysInfo.window, &placement); + + Client::Ref().SetPref("WindowX", placement.rcNormalPosition.left); + Client::Ref().SetPref("WindowY", placement.rcNormalPosition.top); + + return true; + } + + return false; +} + +#endif + int main(int argc, char * argv[]) { currentWidth = XRES+BARSIZE; @@ -602,7 +682,11 @@ int main(int argc, char * argv[]) tempScale = 1; int sdlStatus = SDLOpen(); +#ifdef WIN + LoadWindowPosition(tempScale); +#endif sdl_scrn = SDLSetScreen(tempScale, tempFullscreen); + #ifdef OGLI SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); //glScaled(2.0f, 2.0f, 1.0f); @@ -734,6 +818,10 @@ int main(int argc, char * argv[]) EngineProcess(); +#ifdef WIN + SaveWindowPosition(); +#endif + ui::Engine::Ref().CloseWindow(); delete gameController; delete ui::Engine::Ref().g; |
