Here’s a program that changes the requested window size to match the closest available SDL_DisplayMode that is available. It seems that Wayland actually does not stretch the pixels of the window to a true match of the screen, but instead does something similar as this program behind the scenes (I did a red rectangle test on Wayland and there were about 100 extra pixels to the right of the rectangle).
#include <SDL2/SDL.h>
int SCREEN_WIDTH = 500;
int SCREEN_HEIGHT = 500;
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
// redRectangle will represent your originally requested size;
SDL_Rect redRectangle = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
int modeCount = SDL_GetNumDisplayModes(0);
SDL_Log("Mode count: %d", modeCount);
SDL_DisplayMode mode;
// A list of the display sizes available on your device,
// these can often be determined by your OS and Window Manager instead of
// your actual hardware
for(int i = 0; i < modeCount; i ++)
{
SDL_GetDisplayMode(0, i, &mode);
SDL_Log("Mode %d: (w, h) = (%d, %d)", i, mode.w, mode.h);
}
// Fetching the closest mode to your requested size
mode.w = SCREEN_WIDTH;
mode.h = SCREEN_HEIGHT;
SDL_DisplayMode modeRecieved;
SDL_GetClosestDisplayMode(0, &mode, &modeRecieved);
SCREEN_WIDTH = modeRecieved.w;
SCREEN_HEIGHT = modeRecieved.h;
// Creating the window with closest available screen ratio
SDL_Window * win = SDL_CreateWindow("title", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer * screen = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC);
int modeIndex = 0;
uint32_t fsFlag = 0;
bool run = true;
while(run)
{
SDL_Event ev;
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
case SDL_KEYDOWN:
switch(ev.key.keysym.sym)
{
case SDLK_1:
// hit 1 to enter full screen.
fsFlag = SDL_WINDOW_FULLSCREEN;
SDL_SetWindowFullscreen(win, fsFlag);
break;
case SDLK_ESCAPE:
// hit escape to exit fullscreen
fsFlag = 0;
SDL_SetWindowFullscreen(win, fsFlag);
break;
case SDLK_f:
// toggle fullscreen
fsFlag ^= SDL_WINDOW_FULLSCREEN;
SDL_SetWindowFullscreen(win, fsFlag);
break;
// this block is for if you want to try scrolling through different display modes,
// It worked fine on Xorg, but was not very stable on Wayland.
/*
case SDLK_UP:
// while in true fullscreen, hit up or down to try out different display modes.
// These are not aware of best fit, so it starts at modeIndex = 0
if(modeIndex > 0)
{
modeIndex --;
}
SDL_GetDisplayMode(0, modeIndex, &mode);
SDL_SetWindowDisplayMode(win, &mode);
SDL_Log("- Current Mode %d: (w, h) = (%d, %d)", modeIndex, mode.w, mode.h);
break;
case SDLK_DOWN:
if(modeIndex < modeCount)
{
modeIndex ++;
}
SDL_GetDisplayMode(0, modeIndex, &mode);
SDL_SetWindowDisplayMode(win, &mode);
SDL_Log("- Current Mode %d: (w, h) = (%d, %d)", modeIndex, mode.w, mode.h);
break;
*/
}
break;
case SDL_QUIT:
run = false;
break;
}
}
SDL_SetRenderDrawColor(screen, 100, 100, 255, 255);
SDL_RenderClear(screen);
SDL_SetRenderDrawColor(screen, 255, 100, 100, 255);
// drawing redRectangle, we don't have a perfect match, but maybe it's close enough?
// I don't own a Windows computer, does it do the stretch correctly for the rectangle?
SDL_RenderDrawRect(screen, &redRectangle);
SDL_RenderPresent(screen);
}
SDL_DestroyWindow(win);
SDL_Quit();
}
I guess one of my questions to you since I don’t have a Windows machine is this:
On Windows, If you draw a rectangle the size that you expect the screen to be, does it outline the screen correctly in fullscreen mode? Are you instead getting extra pixels or missing some that you weren’t expecting (like Wayland and the above code)?