SDL - Get native Screen Resolution

浪子不回头ぞ 提交于 2019-12-21 19:27:43

问题


My code:

  1. window.cpp

    Window::Window(int w, int h, const char *title, const char *icon)
    {
        height = h;
        width = w;
    
        if(SDL_Init( SDL_INIT_EVERYTHING ) == 0)
        {
            SDL_WM_SetCaption(title, NULL);
            SDL_WM_SetIcon(SDL_LoadBMP(icon),NULL);
    
            screen = SDL_SetVideoMode(width, height, 32,
                         SDL_SWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
            if(screen == NULL)
            {
                running = false;
                return;
            }
            fullscreen = false;
        }
        else
            running = false;
            return;
    }
    
    Window::Window()
    {
        const SDL_VideoInfo* info = SDL_GetVideoInfo();
        screenWidth = info->current_w;
        screenHeight = info->current_h;
        Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");
    }
    
  2. window.h

    class Window
    {
    public:
        Window();
        ~Window();
    
        int getWidth() {return width;}
        int getHeight() {return height;}
        bool isFullscreen() {return fullscreen;}
    
        void toggleFullscreen();
    
    private:
        Window(int w, int h, const char *title, const char *icon);
    
        bool fullscreen, running;
        int height, width, screenWidth, screenHeight;
        SDL_Surface *screen;
    };
    

It compiles fine, but then, after compiling, I'm getting this ugly error:

What's the reason of my problem? Why do I get so weird numbers?

My aim is to store original screen resolution for further use (like toggling to fullscreen), and I have to do this before calling SDL_SetVideoMode(). That's why it is in the constructor.


回答1:


You have a problem with calling SDL Video Functions before actually initializing SDL.

SDL_Init( SDL_INIT_EVERYTHING )

has to be called before

SDL_GetVideoInfo(); 

In your case you call SDL_GetVideoInfo(); first

const SDL_VideoInfo* info = SDL_GetVideoInfo();   //<-- calls SDL_GetVideoInfo();   
screenWidth = info->current_w;
screenHeight = info->current_h;
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");    //<-- initializes SDL

So the solution is simple; make the call SDL_Init( SDL_INIT_EVERYTHING ) immediately at the start of your program, then you can call SDL_GetVideoInfo(); as much as you like. You will have to restructure your class Window slightly.




回答2:


To get the best video mode call SDL_GetVideoInfo before setting up the video (before calling SDL_SetVideoMode).

But you still have to initialize the video subsystem before calling it (SDL_Init(SDL_INIT_VIDEO)).




回答3:


I know this is old, but there's a big mistake in the code.

Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");

creates a nameless instance of a Window, so the instance that calls it will still have uninitialized variables. It looks like you were trying to use delegating constructors, but in that case the call to the other constructor must be in the member initializer list. See this page.



来源:https://stackoverflow.com/questions/15575500/sdl-get-native-screen-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!