使用SDL_QUIT事件时程序崩溃?

时间:2019-04-18 17:22:37

标签: c++ sdl

我试图编写一个代码,该代码创建一个窗口,该窗口在按下特定箭头键时显示特定图像,并在单击屏幕上的x按钮时关闭该窗口:

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

using namespace std;

enum Gamestate
{
    Play, Exit
};

enum Keypress
{
    KEY_PRESS_DEFAULT,
    KEY_PRESS_UP,
    KEY_PRESS_DOWN,
    KEY_PRESS_RIGHT,
    KEY_PRESS_LEFT,
    KEY_PRESS_TOTAL,
};

class Game
{
public:
    Game();
    ~Game();
    void run();

private:
    //data members
    int WINDOW_WIDTH;
    int WINDOW_HEIGHT;
    SDL_Window* window = NULL;
    SDL_Surface* screenSurface = NULL;
    SDL_Surface* loadSurface[ KEY_PRESS_TOTAL ];
    SDL_Surface* defaultSurface = NULL;
    Gamestate _gamestate;

private:
    //methods
    void init();
    void gameLoop();
    void processInput();
    void updateWindowSurface();
};

void error( string error1 )
{
    int itq;
    cout << error1 << endl;
    SDL_GetError();
    cout << endl << "Enter any key to quit" << endl;
    cin >> itq;
    SDL_Quit();
}

Game::Game()
{
    WINDOW_WIDTH = 640;
    WINDOW_HEIGHT = 480;
    _gamestate = Play;
}

void Game::run()
{
    init();
    gameLoop();

}

void Game::init()
{
    SDL_Init( SDL_INIT_EVERYTHING );
    window = SDL_CreateWindow( "GAME", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN );

    if( window == NULL )
    {
        error( "SDL could not create a window!" );
    }
    screenSurface = SDL_GetWindowSurface( window );
    if( window == NULL )
    {
        error( "SDL could not create a screen!" );
    }
    loadSurface[ KEY_PRESS_DEFAULT ] = SDL_LoadBMP( "press.bmp" );
    if( loadSurface[ KEY_PRESS_DEFAULT ] == NULL )
    {
        error( "Unable to load the Surface!" );
    }
    loadSurface[ KEY_PRESS_UP ] = SDL_LoadBMP( "up.bmp" );
    if( loadSurface[ KEY_PRESS_UP ] == NULL )
    {
        error( "Unable to load Image!" );
    }
    loadSurface[ KEY_PRESS_DOWN ] = SDL_LoadBMP( "down.bmp" );
    if( loadSurface[ KEY_PRESS_DOWN ] == NULL )
    {
        error( "Unable to load Image!" );
    }
    loadSurface[ KEY_PRESS_RIGHT ] = SDL_LoadBMP( "right.bmp" );
    if( loadSurface[ KEY_PRESS_RIGHT ] == NULL )
    {
        error( "Unable to load Image!" );
    }
    loadSurface[ KEY_PRESS_LEFT ] = SDL_LoadBMP( "left.bmp" );
    if( loadSurface[ KEY_PRESS_LEFT ] == NULL )
    {
        error( "Unable to load Image!" );
    }
    defaultSurface = loadSurface[ KEY_PRESS_DEFAULT ];
}

void Game::gameLoop()
{
    while( _gamestate != Exit )
    {
        processInput();
        updateWindowSurface();
    }
}

void Game::processInput()
{
    SDL_Event e;
    while( SDL_PollEvent( &e ) != 0 )
    {
        if( e.type == SDL_QUIT )
        {
            _gamestate = Exit;
        }
        else if( e.type == SDL_KEYDOWN )
        {
            switch( e.key.keysym.sym )
            {
            case SDLK_UP:
                defaultSurface = loadSurface[ KEY_PRESS_UP ];
                break;
            case SDLK_DOWN:
                defaultSurface = loadSurface[ KEY_PRESS_DOWN ];
                break;
            case SDLK_RIGHT:
                defaultSurface = loadSurface[ KEY_PRESS_RIGHT ];
                break;
            case SDLK_LEFT:
                defaultSurface = loadSurface[ KEY_PRESS_LEFT ];
                break;
            default:
                defaultSurface = loadSurface[ KEY_PRESS_DEFAULT ];
            }
        }
    }
}

void Game::updateWindowSurface()
{
    SDL_BlitSurface( defaultSurface, NULL, screenSurface, NULL );
    SDL_UpdateWindowSurface( window );
}

Game::~Game()
{
    for( int i = 0; i <= KEY_PRESS_TOTAL; i++ )
    {
        SDL_FreeSurface( loadSurface[ i ] );
        loadSurface[ i ] = NULL;
    }

    SDL_DestroyWindow( window );
    window = NULL;

    SDL_Quit();
}

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

当我按下箭头键时,显示的图像正确,但是当我尝试将其关闭时,程序崩溃。但是,当我尝试在按任意箭头键之前关闭窗口时,效果很好。

有人可以帮我吗?

0 个答案:

没有答案