Centering a window

Hi,

I’m looking for a better way to center a window created with SDL.
At present, the only way I could find was to set the 'SDL_VIDEO_CENTERED’
environment variable before calling SDL_SetVideoMode, ie.:

  putenv("SDL_VIDEO_CENTERED=1");

  SDL_Surface *screen = SDL_SetVideoMode(w,h,32,flags);

This seems to work when the application starts, but if the surface is freed and
recreated with different dimensions during the application’s life the window
will still start from the same top-left position.

Specifically, my game has an option menu where the user can pick different
resolutions. The window changes size, but always starts from the same position.
This causes it to go off screen if a higher resolution is picked.

I could make the application window draggable, i guess, but that’s not what i’m
looking for.
How does everyone else handle this? Does anybody use this method of centering,
or a better method? Or is it usually left up to the window manager to place the
display?

Regards,
Damian.

Hi,

If you use the Resolution like (resX, resY), you can:

newX = ((int)((resX - (widgetW - widgetX)) / 2);
newY = ((int)((resY - (widgetH - WidgetY)) /2);

after that you can set you widget X and Y pos using (newX and newY)

hope I helped someway,On Fri, 22 Oct 2004 03:04:12 +0000 (UTC), Damian <damian_rajkowski at technologyonecorp.com> wrote:

Hi,

I’m looking for a better way to center a window created with SDL.
At present, the only way I could find was to set the 'SDL_VIDEO_CENTERED’
environment variable before calling SDL_SetVideoMode, ie.:

  putenv("SDL_VIDEO_CENTERED=1");

  SDL_Surface *screen = SDL_SetVideoMode(w,h,32,flags);

This seems to work when the application starts, but if the surface is freed and
recreated with different dimensions during the application’s life the window
will still start from the same top-left position.

Specifically, my game has an option menu where the user can pick different
resolutions. The window changes size, but always starts from the same position.
This causes it to go off screen if a higher resolution is picked.

I could make the application window draggable, i guess, but that’s not what i’m
looking for.
How does everyone else handle this? Does anybody use this method of centering,
or a better method? Or is it usually left up to the window manager to place the
display?

Regards,
Damian.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Jos? In?cio
Programador J2EE / j2SE
Extreme Softwares

This method works for me in Linux. I use similar code in Stella, the
Atari 2600 emulator. If I resize the window at any time (call
SDL_SetVideoMode), it re-centers based on the new window size. The
same code in Windows only centers once, after the program first runs.
Since SDL_VIDEO_CENTERED support was only recently introduced in
Windows, I consider it a bug there.

SteveOn October 22, 2004 12:34 am, Damian wrote:

Hi,

I’m looking for a better way to center a window created with SDL.
At present, the only way I could find was to set the
’SDL_VIDEO_CENTERED’ environment variable before calling
SDL_SetVideoMode, ie.:

  putenv("SDL_VIDEO_CENTERED=1");

  SDL_Surface *screen = SDL_SetVideoMode(w,h,32,flags);

This seems to work when the application starts, but if the surface is
freed and recreated with different dimensions during the
application’s life the window will still start from the same top-left
position.

Stephen Anthony <stephena users.sourceforge.net> writes:

I’m looking for a better way to center a window created with SDL.

  putenv("SDL_VIDEO_CENTERED=1");

  SDL_Surface *screen = SDL_SetVideoMode(w,h,32,flags);

This seems to work when the application starts, but if the surface is
freed and recreated with different dimensions during the
application’s life the window will still start from the same top-left
position.

This method works for me in Linux. I use similar code in Stella, the
Atari 2600 emulator. If I resize the window at any time (call
SDL_SetVideoMode), it re-centers based on the new window size. The
same code in Windows only centers once, after the program first runs.
Since SDL_VIDEO_CENTERED support was only recently introduced in
Windows, I consider it a bug there.

Hi Steve,

Thanks for that. I think you’re right. There seems to be no way to resize the
window AND have it recentered in the Windows platform. I thought, perhaps, that
it wasn’t working because I was closing the surface before recreating it, but
after trying a number of ways, including just calling SDL_SetVideoMode, it
never seems to center. So, probably a bug.

I looked at the SDL source, but all i got was confused. Does anyone out there
feel like looking into this, and possibly finding a way to fix it?
Can’t offer any help, but you’d have my gratitude. :slight_smile:

Regards,
Damian

Damian wrote:

I looked at the SDL source, but all i got was confused. Does anyone out there
feel like looking into this, and possibly finding a way to fix it?
Can’t offer any help, but you’d have my gratitude. :slight_smile:

Hi, here is a patch fixing the win32 centered window bug

Gautier.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: SDL12-win32-center-fix-kassoulet.patch
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20041025/c30ba7cc/attachment.asc

Gautier Portet <impulse no-log.org> writes:

Hi, here is a patch fixing the win32 centered window bug

Thanks for that. Unfortunately, i don’t have Visual-C++ (only Dev-C++), and
wouldn’t know how to compile the SDL dlls for Windows. Can it be done with bare
MingW or Cygwin?
Pity Sam hasn’t indicated a date for the next build…
ah well, i’m not going to imping on your generosity by asking for precompiled
windows dll’s. Thanks anyway. :slight_smile:

Regards,
Damian

Thanks for that. Unfortunately, i don’t have Visual-C++ (only Dev-C++), and
wouldn’t know how to compile the SDL dlls for Windows. Can it be done with bare
MingW or Cygwin?
You can compile SDL with the latest mingw/msys system - install mingw first!
doesnt work for current cygwin - dont know why, but mingw is fine!

I find compiling using Visual C++ 6 under Win98 gives the best
performing DLL, even better than with MinGW. Of course, you say you
don’t have access to it, so it’s just an FYI :slight_smile:

Maybe I’ll find some time to try the patch. I’ve been distributing a
patched SDL.dll with Stella, so would be nice to have auto-centering
working in Windows as well.

SteveOn October 27, 2004 11:12 pm, Sven Dawitz wrote:

Thanks for that. Unfortunately, i don’t have Visual-C++ (only
Dev-C++), and wouldn’t know how to compile the SDL dlls for
Windows. Can it be done with bare MingW or Cygwin?

You can compile SDL with the latest mingw/msys system - install mingw
first! doesnt work for current cygwin - dont know why, but mingw is
fine!

Damian wrote:

I looked at the SDL source, but all i got was confused. Does anyone out there
feel like looking into this, and possibly finding a way to fix it?
Can’t offer any help, but you’d have my gratitude. :slight_smile:

Hi, here is a patch fixing the win32 centered window bug

Thanks, I added your patch to CVS.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Damian wrote:

Gautier Portet <impulse no-log.org> writes:

Hi, here is a patch fixing the win32 centered window bug

Thanks for that. Unfortunately, i don’t have Visual-C++ (only Dev-C++), and
wouldn’t know how to compile the SDL dlls for Windows. Can it be done with bare
MingW or Cygwin?
Pity Sam hasn’t indicated a date for the next build…
ah well, i’m not going to imping on your generosity by asking for precompiled
windows dll’s. Thanks anyway. :slight_smile:

Regards,
Damian

Actually, there’s a free beta version of the next visual c++. maybe you
ought to give it a try. Here’s the link, requires a .NET account:
http://lab.msdn.microsoft.com/express/visualc/default.aspx---
Akmal (@Akmal)