Sign in to follow this  
wyrzy

SDL + Center Window Position

Recommended Posts

I'm working on a cross-platform game, and I was trying to find a way to center the window using SDL (working in windowed mode). I tried google and the SDL docs, but the best I was able to come up with was an SDL_VIDEO_CENTERED that is supposed to be supported on windows. I wasn't sure as how to set it, and I was wonding if anyone knew of an easy way to center a window in SDL (maintaining the Cross-Platform support).

Share this post


Link to post
Share on other sites
I think that you can't do this with SDL.

But why the hell you would want to do this anyway? The user can center the window if he/she wishes to. You can also go fullscreen.

I think that there is no such function, because it would not be supported on all platforms. You can tweak the windows to be centered on X11/Linux (with setenv and SDL_VIDEO_CENTERED), but it wouldn't work on other systems.

-Richardo

Share this post


Link to post
Share on other sites
It would just be a matter of finding the user's current screen resolution (not through SDL) and then positioning the window at:


// x,y for top left of window.
x = (ScreenWidth / 2) - (WindowWidth / 2)
y = (ScreenHeight / 2) - (WindowHeight / 2)

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
It would just be a matter of finding the user's current screen resolution (not through SDL) and then positioning the window at:


// x,y for top left of window.
x = (ScreenWidth / 2) - (WindowWidth / 2)
y = (ScreenHeight / 2) - (WindowHeight / 2)


The problem is that SDL creates the window, not the user.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sijmen
Quote:
Original post by Rhaal
It would just be a matter of finding the user's current screen resolution (not through SDL) and then positioning the window at:


// x,y for top left of window.
x = (ScreenWidth / 2) - (WindowWidth / 2)
y = (ScreenHeight / 2) - (WindowHeight / 2)


The problem is that SDL creates the window, not the user.


Ah! Looks like SDL could benefit from the following functions:

SDL_WM_GetWindowXY
SDL_WM_SetWindowXY

Share this post


Link to post
Share on other sites
Warning: this is a windows-only solution

you can use the SDL_GetWMInfo function to get the HWND of the main window, this works as follow:


#include <SDL/SDL_syswm.h> // you must include this, as it's not
// included in SDL.h

int main(int argc,char *argv[])
{
SDL_SysWMInfo wminfo;
HWND hwnd;
//SDL initialization, main window creation, etc
//...
SDL_VERSION(&wminfo.version);
SDL_GetWMInfo(&wminfo);
hwnd=wminfo.window;
//do whatever you want with the window
}




with the HWND of the main window, you can use Win32 API functions to do whatever you want with the window.

Share this post


Link to post
Share on other sites
I think your best bet if you want cross-platform functionality is to isolate any platform-specific stuff in one module.

So to center the window, you could call something like CenterWindow() where the OS-specific code would reside.

Yes, it does mean reimplementing the module containing this code, but there shouldn't be all that much OS-specific stuff to reimplement. It should definitely be isolated though, so that the rest of you program in OS-independent.

If you do find acceptable code for UNIX, Windows, and MacOS, it might be nice to donate it to SDL so others can use it. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by hpolloni
Warning: this is a windows-only solution

you can use the SDL_GetWMInfo function to get the HWND of the main window, this works as follow:

*** Source Snippet Removed ***

with the HWND of the main window, you can use Win32 API functions to do whatever you want with the window.


BAM, there you go. Use the HWND of that window and use the method I mentioned :)

Edit: For Windows

Share this post


Link to post
Share on other sites
Quote:
Original post by RichardoX
But why the hell you would want to do this anyway? The user can center the window if he/she wishes to. You can also go fullscreen.

Well, I want to offer the option of playing in windowed mode, and if the window auto-centered on startup, then the player wouldn't need to move it themself into a centered position every time they started playing.

Thanks for the confirmations that there's no easy way to center a window in SDL. I think I'm just going to take the recommendations of the posters and write some code with like, #ifdef WIN32 .... #ifdef MACINTOSH ... #ifdef LINUX ....
in order to get the window centered. I know how to center it on windows and linux, but does anyone know how to center a window on a Mac? If not, I'm sure I could find it on google, but I thought I might ask anyways.

Share this post


Link to post
Share on other sites
Could be difficult on mac. Depending on whether SDL uses Carbon or Cocoa to create the window, you'll have to do it differently. SDL usually uses Cocoa for Mac OS X, and I think you'll have to use Objective-C code to access Cocoa. Could mean you have to hunt around SDLs objective-c source files and add a function or two.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this