2013-07-18 120 views
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位馬里奧的圖像)。 如何阻止我的程序立即關閉?SDL C++窗口立即關閉

回答

1
SDL_FreeSurface(sprite); 
SDL_FreeSurface(background); 

這是你的問題所在。

在您致電SDL_Quit()之前,這些行應出現在程序的最後。

目前,您正在將已釋放的表面塗抹到窗戶上。