简体   繁体   English

创建时SDL_renderer变为null

[英]SDL_renderer becomes null when creating

I have this annoying problem that when I create an SDL_renderer in one of my classes it becomes a nullptr but when creating another one in another class it functions normally. 我有一个烦人的问题,当我在一个类中创建一个SDL_renderer时,它变成一个nullptr,而在另一个类中创建另一个时,它正常工作。

Creating a renderer in my Engine class, functions normally: 在我的Engine类中创建渲染器,可以正常运行:

m_window = SDL_CreateWindow("Breakout", 
    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
    m_window_width, m_window_height, 
    SDL_WINDOW_OPENGL);

m_renderer = SDL_CreateRenderer(m_window, -1, 
    SDL_RENDERER_ACCELERATED);

Creating a renderer in my DrawManager class, renderer becomes a nullptr: 在我的DrawManager类中创建一个渲染器,渲染器变为nullptr:

bool DrawManager::Initialize(SDL_Window *window, int width, int height) {
    m_renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    if(m_renderer == nullptr) {
        return false;
    };

    return true;

};

The m_renderer variables are created in each class respective header file. 在每个类各自的头文件中创建m_renderer变量。

The window that is created in the Engine class is sent into the DrawManager's Initialize method and the window is not a nullptr, only the renderer is. 在Engine类中创建的窗口被发送到DrawManager的Initialize方法中,并且该窗口不是nullptr,只有渲染器。 Both classes have the same SDL related includes. 这两个类具有相同的SDL相关包含。

Anyone has any ideas what's wrong with the code? 任何人都知道代码有什么问题吗?

You want to draw with OpenGL? 您要使用OpenGL绘制吗?

If so, you don't need renderer. 如果是这样,则不需要渲染器。 Otherwise, if you want to use simple SDL draw functions, try to remove SDL_WINDOW_OPENGL flag from SDL_CreateWindow() . 否则,如果要使用简单的SDL绘制函数,请尝试从SDL_CreateWindow()删除SDL_WINDOW_OPENGL标志。

Are you calling SDL_CreateRenderer twice? SDL_CreateRenderer两次调用SDL_CreateRenderer吗? If so, try calling it only once and passing the pointer around. 如果是这样,请尝试仅调用一次并传递指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM