2

With SDL2, I manage to get the resolutions and positions of my displays just fine using SDL_GetCurrentDisplayMode() and SDL_GetDisplayBounds(), however when I change the resolution externally (in this case with the Windows 7 control panel) or the respective position of the displays and call these two functions again I get the same old values, not the new resolutions and positions. That is until I restart my program of course.

I suppose SDL doesn't update those. What do I need to do to get updated values without restarting the program?

esote
  • 831
  • 12
  • 25
Michel Rouzic
  • 1,013
  • 1
  • 9
  • 22

1 Answers1

1

AFAIK it is not possible with SDL to get the updated resolutions (anybody please correct me if I am wrong).

A way you could approach this though, is use your OS's API. In your case you were saying that you are using Windows. So you could go ahead and use the Windows API to retrieve updated resolution information. This obviously is not portable to other OS's - so you would have to do this for every OS you want to support.

I have added a minimal example at the bottom of my answer, that shows how you can retrieve the resolution of the primary display in C++. If you want to do more elaborate handling of multiple monitors and their relative positions etc., you should take a look at this question.

#include "wtypes.h"
#include <SDL.h>
#include <iostream>
using namespace std;

void GetDesktopResolution(int& w, int& h)
{
   RECT r;
   GetWindowRect(GetDesktopWindow(), &r);
   w = r.right;
   h = r.bottom;
}


int main(int argc, char *argv[])
{
   SDL_Init(SDL_INIT_EVERYTHING);
   SDL_Window* window = SDL_CreateWindow("SDL", 0, 0, 640, 480, SDL_WINDOW_RESIZABLE);

   bool running = true;
   while(running) {
      SDL_Event game_event;
      if(SDL_PollEvent(&game_event)) {
         switch(game_event.type) {
            case SDL_QUIT:
               running = false;
               break;
         }
      }

      SDL_DisplayMode current;
      SDL_GetCurrentDisplayMode(0, &current);
      cout << "SDL" << current.w << "," << current.h << '\n';

      int w, h;
      GetDesktopResolution(w, h);
      cout << "winapi" << w << "," << h << '\n';
   }

   SDL_DestroyWindow(window);
   SDL_Quit();

   return 0;
}
Community
  • 1
  • 1
lmNt
  • 3,822
  • 1
  • 16
  • 22