0
#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 位马里奥的图像)。如何阻止我的程序立即关闭?

4

1 回答 1

1
SDL_FreeSurface( sprite );
SDL_FreeSurface( background );

这就是你的问题所在。

这些行应该出现在您调用之前的程序末尾SDL_Quit()

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

于 2013-07-18T03:35:36.967 回答