SDL 窗口出错? [关闭]

Posted

技术标签:

【中文标题】SDL 窗口出错? [关闭]【英文标题】:Error with SDL window? [closed] 【发布时间】:2018-03-31 08:32:11 【问题描述】:

在我的代码中,我有这些东西可以检查我的代码在创建 SDL 窗口、初始化 GLEW 等方面是否有错误。他们会消失,我的程序仍然可以工作,并且在the guide I'm following 中,没有任何错误发生过。谁能告诉我我做错了什么?

另外,glClearColor() 似乎不起作用。我认为它与上述错误有关。

我的代码:

// g++ `pkg-config --cflags glew sdl2` main.cpp `pkg-config --libs glew sdl2`
#include <GL/glew.h>
#include <SDL.h>
#include <iostream>
#include <string>

enum class GameState  PLAY, EXIT ;

class MainGame

public:
    MainGame();
    ~MainGame();

    void run();

private:
    void initSystems();
    void gameLoop();
    void processInput();
    void drawGame();

    SDL_Window* _window;
    int _screenWidth;
    int _screenHeight;

    int _errorCount;

    GameState _gameState;
;

//Function To Display A Error Message When Something Doesnt Work As Inteded/Properly.
void fatalError(std::string errorMsg)

    std::cout << errorMsg << std::endl;


//When Called, Inits Most Of The Important Vars, Sets Game State And Does An Error Check
MainGame::MainGame()

    _errorCount = 0;
    _window = nullptr;
    _screenWidth = 1024;
    _screenHeight = 768;
    _gameState = GameState::PLAY;

    if (_window == nullptr) 
        fatalError("SDL Window Could Not Be Created.");
        _errorCount += 1;
    

    SDL_GLContext glContext = SDL_GL_CreateContext(_window);

    if (glContext == nullptr) 
        fatalError("SDL_GL Context Could Not Be Created.");
        _errorCount += 1;
    

    GLenum error = glewInit();

    if (error != GLEW_OK) 
        fatalError("Could Not Initialize Glew.");
        _errorCount += 1;
    

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);


MainGame::~MainGame()



void MainGame::run()

    initSystems();
    gameLoop();


// Initializes The SDL Window.
void MainGame::initSystems()

    SDL_Init(SDL_INIT_EVERYTHING);
    _window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);


void MainGame::gameLoop()

    while (_gameState != GameState::EXIT) 
        processInput();
        drawGame();
    


void MainGame::processInput()

    SDL_Event evnt;
    while (SDL_PollEvent(&evnt) == true) 
        switch (evnt.type) 
        case SDL_QUIT:
            _gameState = GameState::EXIT;
            break;
        case  SDL_MOUSEMOTION:
            std::cout << evnt.motion.x << ", " << evnt.motion.y << "\n";
            break;
        
    


void MainGame::drawGame()

    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);

    SDL_GL_SwapWindow(_window);


int main(int argc, char** argv)

    MainGame mainGame;
    mainGame.run();

    return 0;

【问题讨论】:

我不明白你的问题,而且你的代码很难找到(应该在问题本身) - 我想这会大大减少你获得帮助的机会。请阅读“如何提问”部分或/并查看其他问题作为参考。即便如此,看一眼你的代码就足以说明它是错误的——你在构造函数中检查窗口,但在其他方法中创建它——这显然发生在构造函数之后。同样在创建上下文后设置 GL 属性对已创建的上下文没有影响。 【参考方案1】: 不要使用 NULL 指针调用 SDL_GL_CreateContext()。 不要在创建窗口后调用SDL_GL_SetAttribute(),不会做任何有用的事情。 在析构函数中清理窗口和 GL 上下文。 每次通过绘图功能设置清晰的颜色以保持理智。 您可以在一次调用中清除颜色和深度平面。 “致命”错误通常应该尽快终止程序。这里我使用了exit(),但throw 也可以。

大家一起:

// g++ `pkg-config --cflags sdl2` main.cpp `pkg-config --libs glew sdl2`
#include <GL/glew.h>
#include <SDL.h>
#include <iostream>
#include <string>
#include <cstdlib> 

enum class GameState  PLAY, EXIT ;

class MainGame

public:
    MainGame();
    ~MainGame();

    void run();

private:
    void processInput();
    void drawGame();

    SDL_Window* _window;
    SDL_GLContext _context;
    int _screenWidth;
    int _screenHeight;

    int _errorCount;

    GameState _gameState;
;

//Function To Display A Error Message When Something Doesnt Work As Inteded/Properly.
void fatalError(std::string errorMsg)

    std::cout << errorMsg << std::endl;
    exit( EXIT_FAILURE );


//When Called, Inits Most Of The Important Vars, Sets Game State And Does An Error Check
MainGame::MainGame()

    _errorCount = 0;
    _window = nullptr;
    _screenWidth = 1024;
    _screenHeight = 768;
    _gameState = GameState::PLAY;

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   _window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
    if (_window == nullptr)
    
        fatalError("SDL Window Could Not Be Created.");
        _errorCount += 1;
    

    _context = SDL_GL_CreateContext(_window);
    if (_context == nullptr)
    
        fatalError("SDL_GL Context Could Not Be Created.");
        _errorCount += 1;
    

    GLenum error = glewInit();
    if (error != GLEW_OK)
    
        fatalError("Could Not Initialize Glew.");
        _errorCount += 1;
    


MainGame::~MainGame()

    SDL_GL_DeleteContext( _context );
    SDL_DestroyWindow( _window );


void MainGame::run()

    while (_gameState != GameState::EXIT)
    
        processInput();
        drawGame();
    


void MainGame::processInput()

    SDL_Event evnt;
    while (SDL_PollEvent(&evnt) == true)
    
        switch (evnt.type)
        
        case SDL_QUIT:
            _gameState = GameState::EXIT;
            break;
        case  SDL_MOUSEMOTION:
            std::cout << evnt.motion.x << ", " << evnt.motion.y << "\n";
            break;
        
    


void MainGame::drawGame()

    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    SDL_GL_SwapWindow(_window);


int main(int argc, char** argv)

    MainGame mainGame;
    mainGame.run();
    return EXIT_SUCCESS;

【讨论】:

以上是关于SDL 窗口出错? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

使用 OpenGL 正确关闭 SDL

未绘制三角形。 C++/GLEW/SDL [关闭]

令牌SDL错误之前的预期类名[关闭]

delphi中 用close关子窗口为啥老是出错

C编程的SDL2.0有好的教程吗? (不是 C++)[关闭]

最近打开电脑,老是弹出说Upgrade 出错,需要关闭,是啥原因,谢谢