Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get screen size in SDL

Tags:

c++

sdl-2

I'm trying to make a program using SDL and C++. How can I get screen's width and height in pixels in SDL?I'm trying to get screen's width not the window's width. . . .

like image 795
tam bakaka Avatar asked Oct 28 '15 14:10

tam bakaka


2 Answers

In SDL2, use SDL_GetCurrentDisplayMode or SDL_GetDesktopDisplayMode depending on your needs. Usage example:

SDL_DisplayMode DM;
SDL_GetCurrentDisplayMode(0, &DM);
auto Width = DM.w;
auto Height = DM.h;

On high-DPI displays this will return the virtual resolution, not the physical resolution.

From the SDL2 wiki:

There's a difference between [SDL_GetDesktopDisplayMode()] and SDL_GetCurrentDisplayMode() when SDL runs fullscreen and has changed the resolution. In that case [SDL_GetDesktopDisplayMode()] will return the previous native display mode, and not the current display mode.

like image 100
emlai Avatar answered Nov 02 '22 23:11

emlai


On Fullscreen: it can be done really easily using SDL_GetRendererOutputSize

You just have to pass in a your SDL_Renderer* like so:

int w, h;

SDL_GetRendererOutputSize(renderer, &w, &h);

void SDL_GetRendererOutputSize(SDL_Renderer* renderer, int* w, int* h)

renderer a rendering context

w a pointer filled in with the width of the renderer

h a pointer filled in with the height of the renderer

On Non-Fullscreen:

Using SDL_GetDesktopDisplayMode()

SDL_DisplayMode dm;

if (SDL_GetDesktopDisplayMode(0, &dm) != 0)
{
     SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError());
     return 1;
}

int w, h;
w = dm.w;
h = dm.h;

Just please do an error checking! Or you'll hate your life when SDL_GetDesktopDisplayMode fails!

like image 7
kovacsmarcell Avatar answered Nov 02 '22 22:11

kovacsmarcell