• Advertisement
Sign in to follow this  

Creating an SDL window centered on screen

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a "launcher window" to set videomodes and such for a game... One thing that kind of annoys me is that it's showing up in the top left corner instead of center. Also, I can't figure out if it's possible to make a borderless window that you can drag with the mouse (think various media players). Both of these would be easy if I just knew a way to move the window in the code, but I can't find any help in the docs. Does SDL have functions for this?

Share this post


Link to post
Share on other sites
Advertisement
in SDL, window centering/position is unfortunately handled via environment variables so what you would have to do is adding them manually to your system configuration (I think in Windows this is Control Panel -> System Environment Variables or something) or calling putenv() in your code like so:

putenv("SDL_VIDEO_WINDOW_POS");
putenv("SDL_VIDEO_CENTERED=1");

getenv("SDL_VIDEO_WINDOW_POS");
getenv("SDL_VIDEO_CENTERED");

Unfortunately you have to reboot (?) so this actually works.
I'm not quite sure why the developers favoured this way over just adding a few window management functions to SDL. but I believe in the SDL docs there's also some X and Win32 example code to move a window (MoveWindow() on Windows, XMoveWindow() on Linux).

BTW you can create a window without border with the SDL_NOFRAME flag to SDL_SetVideoMode().


Share this post


Link to post
Share on other sites
It is possible to move the SDL window, but I think you have to do it in a platform specific way (not directly supported by SDL, and so not portable). In Windows it can be done like this:


#ifdef WIN32
#include <SDL_syswm.h>
SDL_SysWMinfo i;
SDL_VERSION( &i.version );
if ( SDL_GetWMInfo ( &i) ) {
HWND hwnd = i.window;
SetWindowPos( hwnd, HWND_TOP, x, y, width, height, flags );
}
#endif // WIN32



To make it portable you will have to do similar code for the other target platforms using #ifdef's. Check with MSDN for options in SetWindowPos().

Share this post


Link to post
Share on other sites
Thanks for the tips, I knew about the noframe flag but wanted to center the window (or be able to drag it). I'll look for the MoveWindow code and see if I can use it. The game's going to be released for Win32 and MaxOS X so it would've been more convenient if SDL included a portable way to do this.

Share this post


Link to post
Share on other sites
Quote:
Original post by basement
in SDL, window centering/position is unfortunately handled via environment variables so what you would have to do is adding them manually to your system configuration (I think in Windows this is Control Panel -> System Environment Variables or something) or calling putenv() in your code like so:

putenv("SDL_VIDEO_WINDOW_POS");
putenv("SDL_VIDEO_CENTERED=1");

getenv("SDL_VIDEO_WINDOW_POS");
getenv("SDL_VIDEO_CENTERED");

Unfortunately you have to reboot (?) so this actually works.


There is no need to reboot if you use putenv in your code. If you make the putenv calls before initializing SDL, then SDL should see them in the current process' environment.

edit - (unless SDL reads those environment variables before calling your main...)

Share this post


Link to post
Share on other sites
oh yes, I wasn't 100% sure about that. I just had a look at SDL source, it reads the variables when initializing the window.

Share this post


Link to post
Share on other sites
Hmm... I put these in the beginning of main() before doing anything else, but they don't seem to affect anything:

putenv("SDL_VIDEO_WINDOW_POS=center");
putenv("SDL_VIDEO_CENTERED=1");

The launcher still shows up near the top left corner and if I'm running the game in a window, the top left corner of the game window is where the launcher's was. I can confirm the environment variables are there with getenv()... They just don't have any effect.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement