SDL窗口的错误

Error with SDL window?

本文关键字:错误 窗口 SDL      更新时间:2023-10-16

在我的代码中,我有这些东西可以检查我的代码在创建SDL窗口时是否有错误,初始化Glew等等。他们会脱颖而出,我的程序仍然可以正常工作,在我关注的指南中,没有任何错误消失。有人可以告诉我我做错了什么吗?

另外,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;
}
  • 不要用空指针调用SDL_GL_CreateContext()
  • 请勿致电SDL_GL_SetAttribute() 您创建一个窗口,不会做任何有用的事情。
  • 清理您的窗户&amp;destructor中的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;
}