Program stänger sig direkt C++
Jag har ett problem, när jag kör mitt program så stängs det av direkt.
Kod (vissa onödiga bitar bortplockade)
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include "Sprite.h"
#include <string>
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface* screen = NULL;
SDL_Surface* background = NULL;
SDL_Surface* spriteObject = NULL;
SDL_Surface* asteroids = NULL;
SDL_Surface* message = NULL;
SDL_Surface* lives = NULL;
//Stores event data to be used.
SDL_Event event;
//The portions of the sprite map to be blitted
//Only use incase of sprite sheets.
SDL_Rect astro[16];
//The fonts thats gonna be used.
TTF_Font* font = NULL;
TTF_Font* lfont = NULL;
//The color of the font.
SDL_Color textColor = { 255, 255, 255 };
//Sounds
//Music
Mix_Music* music = NULL;
//Sound Effects
Mix_Chunk* laser = NULL;
Mix_Chunk* explosion = NULL;
Mix_Chunk* GameOver = NULL;
[...]
SDL_Surface* load_image(std::string filename) {
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
/*
//If the optimized image was done just fine.
if( optimizedImage != NULL )
{
//Map the color key
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
//Sets all the pixels of color R 0, G 0xFF to be
//transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
*/
//returns the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* astro = NULL )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, astro, destination, &offset );
}
//Here is the initialization function. This function starts up SDL, sets up the window,
//sets the caption and returns false if there are any errors.
bool init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//check if screen loads correctly
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
///Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Space Invaders", NULL );
return true;
}
bool load_files()
{
//Load images
spriteObject = load_image( "Images\\spaceship.png" );
background = load_image( "Images\\stars-1.jpeg" );
asteroids = load_image( "Images\\asteroid-spread-sheet.png" );
//Open the font
font = TTF_OpenFont( "Fonts\\SLANT.ttf", 20 );
lfont = TTF_OpenFont( "Fonts\\SLANT.ttf", 22 );
//If there was any error loading the images.
if( spriteObject == NULL || background == NULL
|| asteroids == NULL )
{
return false;
}
//If there was any error loading the fonts.
if( font == NULL || lfont == NULL )
{
return false;
}
//load music
music = Mix_LoadMUS( "Sounds\\earlycarengine.wav" );
//If there was any error loading the music
if( music == NULL )
{
return false;
}
//Load sound effects
laser = Mix_LoadWAV( "Sounds\\ray-gun" );
//If there was any error loading the sound effects.
if( laser == NULL )
{
return false;
}
//If everything loaded fine.
return true;
}
void clean_up()
{
//Free's the loaded images
SDL_FreeSurface( spriteObject );
SDL_FreeSurface( background );
SDL_FreeSurface( message );
SDL_FreeSurface( asteroids );
//Close the font that was used
TTF_CloseFont( font );
TTF_CloseFont( lfont );
//Free the music files
Mix_FreeMusic( music );
//Free the sound effects
Mix_FreeChunk( laser );
//Quit SDL_mixer
Mix_CloseAudio();
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
[...]
int main( int argc, char* args[] )
{
/*********************************************************************/
//Initialize the sprites
//Sprite s1("spaceship", "spaceship-sprite.gif", spriteObject);
/*********************************************************************/
//Make sure the program waits for quit.
bool quit = false;
//Calls the init and file loading methods.
if( init() == false )
return 1;
if( load_files() == false )
return 1;
//Render the fonts
message = TTF_RenderText_Solid( font, "Score:", textColor );
lives = TTF_RenderText_Solid( lfont, "Lives:", textColor );
//If there was an error rendering the text
if( message == NULL || lives == NULL )
{
return 1;
}
/*************************************************************************/
Sprite s1("spaceship", "spaceship.png", spriteObject);
/************************************************************************/
[...]
//Update Screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Main loop- will keep going until the user sets quit to true.
while( quit == false )
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
switch(event.type)
{
case SDL_QUIT://If the user presses X ( close ).
//Quits the program
quit = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_UP:
s1.setSpeed( 0.0, -1.0 );
break;
case SDLK_DOWN:
s1.setSpeed( 0.0, 1.0 );
break;
case SDLK_LEFT:
s1.setSpeed( -1.0, 0.0 );
break;
case SDLK_RIGHT:
s1.setSpeed( 1.0, 0.0 );
break;
case SDLK_SPACE:
{
//Play laser sound
if( Mix_PlayChannel( -1, laser, 0 ) == -1 )
return 1;
}
}
}
}
}
//Update Screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
clean_up();
return 0;
}
Men när jag kommenterar bort följande rader
//If there was any error loading the music
if( music == NULL )
{
return false;
}
och följande:
//If there was any error loading the sound effects.
if( laser == NULL )
{
return false;
}
Så körs programmet utmärkt men när jag klickar på space (skall spela upp ett ljud) så avslutas programmet.
Nu undrar jag varför och hur det kommer sig att det stängs av för lite problematiskt när man inte hittar det själv.
Tack för svar