Reputation: 562
I have a 16:9 display where I'd like to show fullscreen SDL window which is in 4:3 mode.SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN)
sets the window to the left side of the screen and leaves a big black bar to the right.
I'd like to center the window and have black bars on the left and the right side.
It seems that SDL_SetWindowPosition(window, x, y)
has no effect on window when it is in fullscreen mode. Can I center the fullscreen window in SDL2?
Upvotes: 1
Views: 1568
Reputation: 127
There are two situation: (1) display with renderer and texture base on window size. (2) display with screen and surface base on pixel.
For (1) here is a simple solution base on setting view port for renderer.(no testing but a guideline)
void SDL_SetRendererViewportRatio_4_3(SDL_Window *window,
SDL_Renderer *renderer
SDL_Rect *viewport) {
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
int w, h;
SDL_GetWindowSize(window, &w, &h);
if (w * 3 > h * 4) {
viewport->w = h * 4 / 3;
viewport->h = h;
} else {
viewport->w = w;
viewport->h = w * 3 / 4;
}
viewport->x = (w - viewport->w) / 2;
viewport->y = (h - viewport->h) / 2;
SDL_RenderSetViewport(renderer, viewport);
}
Note that you should call this function whenever window changed size.
For (2) I guess you should calculate the coordinate of surface and draw big black bars by yourself. It is more difficult that I cannot prove simple solution.
Upvotes: 2