Skip to main content
GameDev.net gamedev.net
🔒 Locked

Creating an SDL window centered on screen

Started by Fingers_ Jun 26, 2005 at 1:24 AM 5 replies 25.3k views
Original Post
Fingers_
Fingers_
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?
basement
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.
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().


Mhondoz
Mhondoz
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().
Fingers_
Fingers_
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.
Dave Hunt
Dave Hunt
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...)
basement
basement
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.
Fingers_
Fingers_
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.