Advertisement

SDL + Center Window Position

Started by October 16, 2004 07:21 PM
9 comments, last by Rebooted 19 years, 10 months ago
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).
No one ever centered the main window in SDL (using code)?
Advertisement
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
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)

- A momentary maniac with casual delusions.
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.
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
- A momentary maniac with casual delusions.
Advertisement
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.hint 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.
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. :)
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
- A momentary maniac with casual delusions.
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.

This topic is closed to new replies.

Advertisement