Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Creating an SDL window centered on screen


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Fingers_   Members   -  Reputation: 410

Like
0Likes
Like

Posted 25 June 2005 - 07:24 PM

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?

Sponsor:

#2 basement   Members   -  Reputation: 229

Like
0Likes
Like

Posted 26 June 2005 - 06:26 AM

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().




#3 Mhondoz   Members   -  Reputation: 136

Like
0Likes
Like

Posted 29 June 2005 - 02:44 AM

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().

#4 Fingers_   Members   -  Reputation: 410

Like
0Likes
Like

Posted 29 June 2005 - 06:29 AM

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.

#5 Dave Hunt   Crossbones+   -  Reputation: 2479

Like
0Likes
Like

Posted 29 June 2005 - 06:42 AM

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...)

#6 basement   Members   -  Reputation: 229

Like
0Likes
Like

Posted 30 June 2005 - 12:28 AM

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.

#7 Fingers_   Members   -  Reputation: 410

Like
0Likes
Like

Posted 02 July 2005 - 08:12 AM

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.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS