获取本机屏幕分辨率

获取本机屏幕分辨率

本文介绍了SDL-获取本机屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

  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");
}

  • 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?

    我的目标是存储原始屏幕分辨率以供进一步使用(例如切换到全屏),我必须在致电SDL_SetVideoMode()之前执行此操作.这就是为什么它在构造函数中.

    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.

    推荐答案

    在实际初始化SDL之前,调用SDL视频功能会遇到问题.

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

    SDL_Init( SDL_INIT_EVERYTHING )
    

    必须被称为之前

    SDL_GetVideoInfo();
    

    在您的情况下,您首先致电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
    

    因此解决方案很简单;在程序开始时立即调用SDL_Init( SDL_INIT_EVERYTHING ),然后您可以随意调用SDL_GetVideoInfo();.您将不得不稍微重新调整您的类Window.

    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.

    这篇关于SDL-获取本机屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-21 17:16