如何在C ++中修复SDL2'在SDL_RenderClear_REAL断言失败'?

时间:2019-08-20 20:37:59

标签: c++ macos runtime-error sdl-2

我正在按照教程学习使用SDL2创建窗口的基础知识。该代码运行良好,甚至可以正确编译,但是在运行时,它会显示错误消息“在SDL_RenderClear_REAL断言失败”。

我尝试重新安装SDL2,并将其移动到Mac上的用户库文件夹中,但是这些都不能解决问题

main.cpp

#include "game.hpp"

Game *game = nullptr;

int main() {

    game = new Game();

    game->init("GUI", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

    while (game->running()) {
        game->handleEvents();
        game->update();
        game->render();
    }
    game->clean();
    return 0;

}

game.cpp

#include "game.hpp"

Game::Game(){}

Game::~Game(){}

void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) {
    int flags = 0;
    if (fullscreen == true) {
        flags = SDL_WINDOW_FULLSCREEN;
    }

    if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
        std::cout << "Sub-systems initialized\n";

        window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
        if (window) {
            std::cout << "Window created.\n";
        }

        renderer = SDL_CreateRenderer(window, -1, 0);

        if (renderer) {
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
            std::cout << "Renderer created\n";
        }

        isRunning = true;
    } else {
        isRunning = false;
    }

}

void Game::handleEvents() {

    SDL_Event event;
    SDL_PollEvent(&event);
    switch (event.type) {
        case SDL_QUIT:
            isRunning = false;
            break;
        default:
            break;
    }

}

void Game::update() {

    cnt++;
    std::cout << cnt << "\n";

}

void Game::render() {
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
}

void Game::clean() {
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    std::cout << "Game cleaned\n";
}

game.hpp

#ifndef game_hpp
#define game_hpp
#include "SDL2/SDL.h"
#include <iostream>

class Game {

public:
    Game();
    ~Game();

    void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);

    void handleEvents();
    void update();
    void render();
    void clean();

    bool running() { return isRunning; }

private:
    int cnt = 0;
    bool isRunning;
    SDL_Window *window;
    SDL_Renderer *renderer;
};

#endif

缩小代码范围后,似乎问题仅限于SL_RenderClear。这是它在控制台中产生的确切消息:

2019-08-20 15:26:15.508900-0500 GUI[26436:7287503] WARN: 

Assertion failure at SDL_RenderClear_REAL (/Users/valve/release/SDL/SDL2-2.0.10-source/src/render/SDL_render.c:2235), triggered 1 time:
  'renderer && renderer->magic == &renderer_magic'
Program ended with exit code: 42

如何解决此问题? 编辑:添加了完整的代码,以帮助发现问题。

1 个答案:

答案 0 :(得分:1)

为SDL创建窗口并清除屏幕颜色的基本“问候世界”需要一些样板初始化步骤

  1. 初始化SDL
  2. 创建一个SDL_Window
  3. 创建一个SDL_Renderer

之后您可以做 SDL_RenderClear并使用SDL_RenderDrawColor设置自定义颜色,以使其更明显地表明已​​正确清除了Renderer。

没有错误检查 ,它看起来像:

SDL_Init(flags);
SDL_Window* window = SDL_CreateWindow("window title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, more_flags);

SDL_SetRenderDrawColor(renderer, red, green, blue, alpha);
SDL_RenderClear(renderer);

如果您希望在没有机会看到窗口之前不立即关闭程序,请记住调用SDL_Delay(ms)。