SDL C ++窗口立即关闭

时间:2013-07-18 03:22:47

标签: c++ sdl

#include "SDL/SDL.h"
#include "SDL/SDL_Image.h"
#include <string>
using namespace std;

//Const screen variables
const int SCREEN_WIDTH           = 640;
const int SCREEN_HEIGHT          = 480;
const int SCREEN_BPP             = 32;
const char* SCREEN_CAPTION       = "Pyro";
const float GRAVITY              = 9.8; //Metres per second **NEEDS TO BE ADJUSTED BECAUSE IT'S IN METRES, NOT PIXELS**
const int jumpHeight             = 10;

//Non-Const variables
bool running                     = true;
bool isJumping                   = true;
int jump                         = 0;
int frame                        = 0;
int level                        = 1;
SDL_Event event;
Uint8 *keystate                  = NULL;

//SDL Surfaces
SDL_Surface *screen              = NULL;
SDL_Surface *background          = NULL;
SDL_Surface *sprite              = NULL;
SDL_Surface *bEnemy[10];

//Structs
typedef struct entity {
    int health;
    int damage;
    SDL_Rect hitbox;
    bool evolved;
} playerType, enemyType;
playerType player;
enemyType basicEnemy[10];


//Functions
SDL_Surface *loadImage( std::string filename )
{
    SDL_Surface *loadedImage    = NULL;
    SDL_Surface *optimizedImage = NULL;
    loadedImage = IMG_Load( filename.c_str() );
    if( loadedImage !=  NULL )
    {
            optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
            SDL_FreeSurface( loadedImage );
    }
    return optimizedImage;
}

void applySurface( int x, int y, SDL_Surface* source, SDL_Surface* location )
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface( source, NULL, location, &offset );
}

//Main Function
int main( int argc, char* argv[] )
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
            return 1;
    }
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    if( screen == NULL )
    {
            return 1;
    }

    SDL_WM_SetCaption( SCREEN_CAPTION, NULL );
    background = loadImage( "images/background.png" );
    sprite     = loadImage( "images/player.png" );


    SDL_FreeSurface( sprite );
    SDL_FreeSurface( background );

    while( running )
    {
            //Main game loop
            if( SDL_PollEvent( &event ) )
            {
                    switch( event.type )
                    {
                            case SDL_QUIT:
                                running = false;
                                break;
                            case SDL_KEYDOWN:
                                    switch( event.key.keysym.sym )
                                    {
                                            case SDLK_q:
                                                    running = false;
                                                       break;
                                    }
                                    break;
                    }
            }
            keystate = SDL_GetKeyState(NULL);
            if( keystate[SDLK_q] ) player.evolved = !player.evolved;
            if( keystate[SDLK_UP] )
            {
                    if(isJumping != true)
                    {
                            isJumping = true;
                    }
            }
            if( keystate[SDLK_LEFT] )  player.hitbox.x -= 1;
            if( keystate[SDLK_RIGHT] ) player.hitbox.y += 1;
            //Window collision
            if( player.hitbox.x < 0 ) {player.hitbox.x  = 0;}
            else if( player.hitbox.x > SCREEN_WIDTH  - player.hitbox.w ) {player.hitbox.x = SCREEN_WIDTH  - player.hitbox.w;}
            if( player.hitbox.y < 0 ) {player.hitbox.y  = 0;}
            else if( player.hitbox.y > SCREEN_HEIGHT - player.hitbox.h ) {player.hitbox.y = SCREEN_HEIGHT - player.hitbox.h;}

            //Jumping
            if( isJumping == true )
            {
                    if(jump >= jumpHeight)
                    {
                            jump--;
                            player.hitbox.y++;
                    }else {
                            jump++;
                            player.hitbox.y--;
                    }
            }

            //Updating the screen
            applySurface( 0, 0, background, screen );
            applySurface( player.hitbox.x, player.hitbox.y, sprite, screen );

            if( SDL_Flip( screen ) == -1 )
            {
                    return 1;
            }
    }
    SDL_Quit();

    return 0;
}

^这就是我的确切代码。当我运行文件时,它会立即关闭。它编译为:g++ -o myprogram.exe mysource.cpp -lmingw32 -lSDLmain -lSDL -lSDL_image -static-libgcc -static-libstdc++。 我链接的文件确实存在;它们目前是占位符(背景是我找到的一些随机png图像,播放器是8位马里奥的图像)。 如何阻止我的程序立即关闭?

1 个答案:

答案 0 :(得分:1)

SDL_FreeSurface( sprite );
SDL_FreeSurface( background );

这就是你的问题所在。

在您致电SDL_Quit()之前,这些行应出现在计划的最后。

目前,你正在将释放的表面放到窗口上。