创建SDL_Window unique_ptr

时间:2018-11-09 15:54:04

标签: c++ sdl-2 unique-ptr

对于我的游戏,我尝试在SDL_GLContext的帮助下使用unique_ptr创建一个SDL_GL_CreateContext();

我为我的unique_ptr创建了一个自定义删除器

struct sdl_deleter
{
    void operator()(SDL_Window *p) const { SDL_DestroyWindow(p); }
    void operator()(SDL_Renderer *p) const { SDL_DestroyRenderer(p); }
    void operator()(SDL_Texture *p) const { SDL_DestroyTexture(p); }
};

class MainGame{
    std::unique_ptr<SDL_Window, sdl_deleter> _window;

public: 
    run();
}





MainGame::MainGame() : _window(nullptr, sdl_deleter), _screenWidth(1024), _screenHeight(768), _gameState(GameState::PLAY)
{

}

void MainGame::run(){
        SDL_Init(SDL_INIT_EVERYTHING);
        _window.reset(SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL));
}

我收到一条错误消息,指出不允许使用该类型,而且我不知道该如何解决。

Error   C2664   'std::unique_ptr<SDL_Window,sdl_deleter>::unique_ptr(const std::unique_ptr<SDL_Window,sdl_deleter> &)': cannot convert argument 2 from 'void (__cdecl *)(SDL_Window *)' to 'const _Dx &'

错误发生在指针初始化程序上:

: _window(nullptr, sdl_deleter)

1 个答案:

答案 0 :(得分:3)

此处_window(nullptr, sdl_deleter) sdl_deleter是类型名称,而应使用对象。您应该提供实际的删除程序对象,例如_window(nullptr, sdl_deleter{})或直接将其保留为unique_ptr本身的默认构造:_window(nullptr)

此外,当您向SDL_GL_CreateContext喂食时,SDL_Window *期望有unique_ptr。您可能想要这样:SDL_GL_CreateContext(_window.get())