Jump to content

Getting The Screen Resolution Under Linux


reepblue
 Share

Recommended Posts

With my application, if a settings file does not exist, the main function creates one, and fills it with default values. One of these is to grab the main screen res of the user's monitor.

 

In Windows, I can do:

 

unsigned int width = GetSystemMetrics(SM_CXSCREEN);

unsigned int height = GetSystemMetrics(SM_CYSCREEN);

 

However, since my HDD with Ubuntu on it died, I can't test the linux variant I've found on the internet.

 

#include <X11/Xlib.h>

 

Display* disp = XOpenDisplay(NULL);

Screen* scrn = DefaultScreenOfDisplay(disp);

unsigned int width = scrn->width;

unsigned int height = scrn->height;

 

Is this correct/will work, or is there a different/better way? I tried using the Count/GetGraphicsModes function, and although it did work on my Linux machine, it did not work on my Windows Machine, and I've read that it's not perfect.

Thanks.

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Thanks to macklebee, the correct answer is to use the count/getnodes function like:

 

unsigned int xo = System::GetGraphicsMode(System::CountGraphicsModes() - 1).x;

unsigned int yo = System::GetGraphicsMode(System::CountGraphicsModes() - 1).y;

 

Then, use those int values for x and y. In this case, I need it to be a char to save to an XML file.

 

namespace patch

{

template < typename T > std::string to_string(const T& n)

{

std::ostringstream stm;

stm << n;

return stm.str();

}

}

 

std::string sx = patch::to_string(xo);

char const *pchar_resX = sx.c_str();

 

std::string sy = patch::to_string(yo);

char const *pchar_resY = sy.c_str();

  • Upvote 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...