无效 window 和无效渲染器错误 SDL c++

Invalid window and invalid renderer error SDL c++

这是我的代码

#include <iostream>
#include "SDL.h"

namespace engine {

    class SDL_UI
    {
    protected:
        SDL_Renderer* renderer;
        SDL_Window* window;
        SDL_Event event;

        bool initialised = true;
        bool quit = false;

    public:

        SDL_UI(const char* name, int x, int y, int w = SDL_WINDOWPOS_CENTERED, int h = SDL_WINDOWPOS_CENTERED, bool fullscreen = false)
        {
            if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
            {
                throw std::runtime_error(SDL_GetError());
            }
            window = SDL_CreateWindow(name, x, y, w, h, fullscreen);
            renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
            if (renderer == nullptr)
            {
                std::cout << SDL_GetError() << std::endl;
                //throw std::runtime_error("Renderer creation failed");
                initialised = false;
            }
            if (window == nullptr)
            {
                std::cout << SDL_GetError() << std::endl;
                //throw std::runtime_error("Window creation failed");
                initialised = false;
            }

        }

        ~SDL_UI()
        {
            SDL_DestroyWindow(window);
            SDL_DestroyRenderer(renderer);
        }

        void handleEvents()
        {
            while (SDL_PollEvent(&event))
            {
                if (event.type == SDL_QUIT)
                {
                    quit = true;
                }
            }
        }

        void setColor(int r, int g, int b, int a = 255)
        {
            SDL_SetRenderDrawColor(renderer, r, g, b, a);
        }

        void clear(int r, int g, int b, int a = 255)
        {
            SDL_SetRenderDrawColor(renderer, r, g, b, a);
            SDL_RenderClear(renderer);
        }

        void flip()
        {
            SDL_RenderPresent(renderer);
        }

        virtual void update()
        {

        }

        virtual void render()
        {

        }

        void start()
        {
            if (initialised && !quit)
            {
                handleEvents();
                update();
                render();
                flip();
                
            }
        }
    };

    class Engine : public SDL_UI
    {
    public:
        Engine(const char* name, int width, int height) : SDL_UI(name, width, height){}

    public:

        void drawLine(int x1, int y1, int x2, int y2)
        {
            SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
        }
    };

}

class Game : public engine::Engine
{
private:
    //engine::Draw renderer;

public:
    Game() : engine::Engine{ "engine", 1200, 600 }{}

    void update() override
    {
        
    }

    void render() override
    {
        clear(10, 10, 10);
    }
};

int main(int argc, char* argv[])
{
    Game game;
    game.start();
    return 0;
}

我没有收到任何错误消息,并且“无效 window”和“无效渲染器”被打印到控制台,它们是来自 'SDL_GetError()' 的消息。 Window 和渲染器是在 SDL_UI class 的构造函数中创建的。它检查渲染器和 window 在创建后是否 nullptr 并打印错误,如果它们仍然 nullptr 正在创建。

据此:https://wiki.libsdl.org/SDL_CreateWindow

Function Parameters

title

the title of the window, in UTF-8 encoding

x

the x position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED

y

the y position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED

w

the width of the window, in screen coordinates h

the height of the window, in screen coordinates

flags

0, or one or more SDL_WindowFlags OR'd together; see Remarks for details while in your code you have:

int x, int y, int w = SDL_WINDOWPOS_CENTERED, int h = SDL_WINDOWPOS_CENTERED

尝试

int x = SDL_WINDOWPOS_CENTERED, int y = SDL_WINDOWPOS_CENTERED, int w, int h