[SDL] Window in the center

Stephen Anthony stephena at users.sourceforge.net
Wed May 12 13:34:29 PDT 2004


On May 12, 2004 05:30 pm, TomT64 wrote:
> NighTiger wrote:
> >How can I see my window in the center of the screen?
> >tnx
> >
> >
> >_______________________________________________
> >SDL mailing list
> >SDL at libsdl.org
> >http://www.libsdl.org/mailman/listinfo/sdl
>
> Try this:
>
> void CenterWindow()
> {
>     SDL_Surface *screen=SDL_GetVideoSurface();
>     SDL_SysWMinfo info;
>
>     SDL_VERSION(&info.version);
>     if ( SDL_GetWMInfo(&info) > 0 ) {
>         #ifdef __unix
>             if ( info.subsystem == SDL_SYSWM_X11 ) {
>                 info.info.x11.lock_func();
>                 int w = DisplayWidth(info.info.x11.display,
>                                 
> DefaultScreen(info.info.x11.display)); int h =
> DisplayHeight(info.info.x11.display,
> DefaultScreen(info.info.x11.display)); int x = (w - screen->w)/2;
>                 int y = (h - screen->h)/2;
>                 XMoveWindow(info.info.x11.display,
> info.info.x11.wmwindow, x, y);
>                 info.info.x11.unlock_func();
>             }
>         #endif // unix
>         #ifdef WIN32
>             {
>             RECT rc;
>             HWND hwnd = info.window;
>             int w=GetSystemMetrics(SM_CXSCREEN);
>             int h=GetSystemMetrics(SM_CYSCREEN);
>             GetWindowRect(hwnd, &rc);
>             int x = (w - (rc.right-rc.left))/2;
>             int y = (h - (rc.bottom-rc.top))/2;
>             SetWindowPos(hwnd, NULL, x, y, 0, 0,
> SWP_NOSIZE|SWP_NOZORDER); }
>         #endif
>     }
> }
>
> I am not sure what to do in the case of Macs or general other
> systems.
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl

Or try this:

#include <stdlib.h>

putenv("SDL_VIDEO_CENTERED=1");

In Linux, it centers the window every time SDL_SetVideoMode() is called.  
In Windows, it centers the window upon program invocation, but not for 
subsequent SDL_SetVideoMode() calls (within the same run).

Steve




More information about the SDL mailing list