Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualJTippetts

Posted 15 December 2012 - 09:59 PM

Hey guys, I've been having a little trouble with vectors recently. I've used this same code on another file, and it complies flawlessly. However, here it does compile but the program ends immediately.

#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<cstdlib>
#include<ctime>
#include<sstream>
#include"string"
#include<vector>
//Applies a sprite to the screen
int apply_sprite( SDL_Surface* src, SDL_Rect* offsets = NULL, SDL_Rect* clip = NULL )
{
	 //Directly applies the sprite to the screen and check for errors
	 if( SDL_BlitSurface( src, clip, SDL_GetVideoSurface(), offsets ) == -1 )
	 {
		 return 0;
	 }
	
	 return 1;
}
//Opens a sprite/spritesheet and returns it
SDL_Surface* open_sprite( std::string path )
{
			 //Opens the image and stores it in a temp surface
			 SDL_Surface* temp = IMG_Load( path.c_str() );
			
			 //If the image didn't load
			  if( temp == NULL )
			  {
				  return NULL;
			  }
			  
			   //Optimises the temp surface
			   SDL_Surface* optimized = SDL_DisplayFormat( temp );
			  
			   //Map the color key
			   Uint32 colorkey = SDL_MapRGB( optimized->format, 255, 0, 255 );
	
			   //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
			   SDL_SetColorKey( optimized, SDL_SRCCOLORKEY, colorkey );
	
			  
			   //Free's the memory consumed by temp
			   SDL_FreeSurface( temp );
			  
			   return optimized;
		  
}

class present
{
	  private:
	
	  int x, y, w, h, yVel;
	
	  SDL_Rect offsets;
	
	  SDL_Surface* sprite;
	
	  public:
	
	  present();
	
	  ~present();
	
	  void move();
	
	  void show();
	
};
present::~present()
{
				   SDL_FreeSurface( sprite );
}
present::present():
w( 51 ),
h( 48 ),
yVel( 1 ),
x( rand() % SDL_GetVideoSurface()->clip_rect.w ),
y( 48 )

{
		  
		   std::stringstream temp;
		   temp << "Pres" << (rand() % 5) + 1 << ".bmp";
		  
		   sprite = open_sprite( temp.str().c_str() );
}

void present::move()
{
	  y += yVel;
	
	  if( (y + h) >= 589 )
	  {
		  y += -yVel;
	  }
}

void present::show()
{
	  offsets.x = x;
	  offsets.y = y;
	
	  apply_sprite( sprite, &amp;amp;offsets );
}


int main( int argv, char* argc[] )
{
	 srand( static_cast<unsigned int>(time(0)));
	
	 SDL_Init( SDL_INIT_EVERYTHING );  
	
	 SDL_Surface* screen = SDL_SetVideoMode( 500, 657, 32, SDL_SWSURFACE );
	
	 if( screen == NULL )
	 {
		 return 1;
	 }
	
	 SDL_Surface* backround = open_sprite( "Backround.bmp" );
	
	 if( backround == NULL )
	 {
		 return 1;
	 }
	
	 SDL_WM_SetCaption( "Merry Christmas!", NULL );
	
	 bool quit = false;
	
	 int maximum_presents = 2;
	
	 std::vector<present> presents;
	 std::vector<present>::iterator it;

// Begin OP's bolded code
	 for( int i = 0; i < maximum_presents; i++ )
	 {
		 present temp;
		 presents.push_back( temp );
	 }
// End OP's bolded code

	 SDL_Event event;
	
	 int time = SDL_GetTicks();
	
	 while( !quit )
	 {  
	

		 while( SDL_PollEvent(&amp;amp;event) )
		 {
				if( event.type == SDL_QUIT )
				{
					quit = true;
				}
		 }

		 SDL_FillRect( screen, &amp;amp;screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255) );
		
		 if( !apply_sprite( backround ) )
		 {
			 return 1;
		 }
		
			  
		 for( it = presents.begin(); it != presents.end(); it++ )
		 {
			  it->move();
		 }
		
		
		 for( it = presents.begin(); it != presents.end(); it++ )
		 {
			it->show();
		 }	  
		
		 SDL_Flip( screen );		
	 }
	
	 SDL_Quit();
	 SDL_FreeSurface( backround );
	
	 return 0;
}
I've highlighted the problematic lines.
If you could help, I'd be grateful.

: Code tags


#2JTippetts

Posted 15 December 2012 - 09:57 PM

Hey guys, I've been having a little trouble with vectors recently. I've used this same code on another file, and it complies flawlessly. However, here it does compile but the program ends immediately.

#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<cstdlib>
#include<ctime>
#include<sstream>
#include"string"
#include<vector>
//Applies a sprite to the screen
int apply_sprite( SDL_Surface* src, SDL_Rect* offsets = NULL, SDL_Rect* clip = NULL )
{
	 //Directly applies the sprite to the screen and check for errors
	 if( SDL_BlitSurface( src, clip, SDL_GetVideoSurface(), offsets ) == -1 )
	 {
		 return 0;
	 }
	
	 return 1;
}
//Opens a sprite/spritesheet and returns it
SDL_Surface* open_sprite( std::string path )
{
			 //Opens the image and stores it in a temp surface
			 SDL_Surface* temp = IMG_Load( path.c_str() );
			
			 //If the image didn't load
			  if( temp == NULL )
			  {
				  return NULL;
			  }
			  
			   //Optimises the temp surface
			   SDL_Surface* optimized = SDL_DisplayFormat( temp );
			  
			   //Map the color key
			   Uint32 colorkey = SDL_MapRGB( optimized->format, 255, 0, 255 );
	
			   //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
			   SDL_SetColorKey( optimized, SDL_SRCCOLORKEY, colorkey );
	
			  
			   //Free's the memory consumed by temp
			   SDL_FreeSurface( temp );
			  
			   return optimized;
		  
}

class present
{
	  private:
	
	  int x, y, w, h, yVel;
	
	  SDL_Rect offsets;
	
	  SDL_Surface* sprite;
	
	  public:
	
	  present();
	
	  ~present();
	
	  void move();
	
	  void show();
	
};
present::~present()
{
				   SDL_FreeSurface( sprite );
}
present::present():
w( 51 ),
h( 48 ),
yVel( 1 ),
x( rand() % SDL_GetVideoSurface()->clip_rect.w ),
y( 48 )

{
		  
		   std::stringstream temp;
		   temp << "Pres" << (rand() % 5) + 1 << ".bmp";
		  
		   sprite = open_sprite( temp.str().c_str() );
}

void present::move()
{
	  y += yVel;
	
	  if( (y + h) >= 589 )
	  {
		  y += -yVel;
	  }
}

void present::show()
{
	  offsets.x = x;
	  offsets.y = y;
	
	  apply_sprite( sprite, &amp;offsets );
}


int main( int argv, char* argc[] )
{
	 srand( static_cast<unsigned int>(time(0)));
	
	 SDL_Init( SDL_INIT_EVERYTHING );  
	
	 SDL_Surface* screen = SDL_SetVideoMode( 500, 657, 32, SDL_SWSURFACE );
	
	 if( screen == NULL )
	 {
		 return 1;
	 }
	
	 SDL_Surface* backround = open_sprite( "Backround.bmp" );
	
	 if( backround == NULL )
	 {
		 return 1;
	 }
	
	 SDL_WM_SetCaption( "Merry Christmas!", NULL );
	
	 bool quit = false;
	
	 int maximum_presents = 2;
	
	 std::vector<present> presents;
	 std::vector<present>::iterator it;
[b]	 [/b]
[b]	 for( int i = 0; i < maximum_presents; i++ )
	 {[/b]
[b]		 present temp;[/b]
[b]		 presents.push_back( temp );
	 }[/b]

	 SDL_Event event;
	
	 int time = SDL_GetTicks();
	
	 while( !quit )
	 {  
	

		 while( SDL_PollEvent(&amp;event) )
		 {
				if( event.type == SDL_QUIT )
				{
					quit = true;
				}
		 }

		 SDL_FillRect( screen, &amp;screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255) );
		
		 if( !apply_sprite( backround ) )
		 {
			 return 1;
		 }
		
			  
		 for( it = presents.begin(); it != presents.end(); it++ )
		 {
			  it->move();
		 }
		
		
		 for( it = presents.begin(); it != presents.end(); it++ )
		 {
			it->show();
		 }	  
		
		 SDL_Flip( screen );		
	 }
	
	 SDL_Quit();
	 SDL_FreeSurface( backround );
	
	 return 0;
}
I've highlighted the problematic lines.
If you could help, I'd be grateful.

: Code tags


#1Lewis De Angelo

Posted 15 December 2012 - 09:29 PM

Hey guys, I've been having a little trouble with vectors recently. I've used this same code on another file, and it complies flawlessly. However, here it does compile but the program ends immediately.


#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<cstdlib>
#include<ctime>
#include<sstream>
#include"string"
#include<vector>
//Applies a sprite to the screen
int apply_sprite( SDL_Surface* src, SDL_Rect* offsets = NULL, SDL_Rect* clip = NULL )
{
//Directly applies the sprite to the screen and check for errors
if( SDL_BlitSurface( src, clip, SDL_GetVideoSurface(), offsets ) == -1 )
{
return 0;
}

return 1;
}
//Opens a sprite/spritesheet and returns it
SDL_Surface* open_sprite( std::string path )
{
//Opens the image and stores it in a temp surface
SDL_Surface* temp = IMG_Load( path.c_str() );

//If the image didn't load
if( temp == NULL )
{
return NULL;
}

//Optimises the temp surface
SDL_Surface* optimized = SDL_DisplayFormat( temp );

//Map the color key
Uint32 colorkey = SDL_MapRGB( optimized->format, 255, 0, 255 );

//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey( optimized, SDL_SRCCOLORKEY, colorkey );


//Free's the memory consumed by temp
SDL_FreeSurface( temp );

return optimized;

}

class present
{
private:

int x, y, w, h, yVel;

SDL_Rect offsets;

SDL_Surface* sprite;

public:

present();

~present();

void move();

void show();

};
present::~present()
{
SDL_FreeSurface( sprite );
}
present::present():
w( 51 ),
h( 48 ),
yVel( 1 ),
x( rand() % SDL_GetVideoSurface()->clip_rect.w ),
y( 48 )

{

std::stringstream temp;
temp << "Pres" << (rand() % 5) + 1 << ".bmp";

sprite = open_sprite( temp.str().c_str() );
}

void present::move()
{
y += yVel;

if( (y + h) >= 589 )
{
y += -yVel;
}
}

void present::show()
{
offsets.x = x;
offsets.y = y;

apply_sprite( sprite, &offsets );
}


int main( int argv, char* argc[] )
{
srand( static_cast<unsigned int>(time(0)));

SDL_Init( SDL_INIT_EVERYTHING );

SDL_Surface* screen = SDL_SetVideoMode( 500, 657, 32, SDL_SWSURFACE );

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

SDL_Surface* backround = open_sprite( "Backround.bmp" );

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

SDL_WM_SetCaption( "Merry Christmas!", NULL );

bool quit = false;

int maximum_presents = 2;

std::vector<present> presents;
std::vector<present>::iterator it;

for( int i = 0; i < maximum_presents; i++ )
{

present temp;
presents.push_back( temp );
}


SDL_Event event;

int time = SDL_GetTicks();

while( !quit )
{


while( SDL_PollEvent(&event) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}

SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255) );

if( !apply_sprite( backround ) )
{
return 1;
}


for( it = presents.begin(); it != presents.end(); it++ )
{
it->move();
}


for( it = presents.begin(); it != presents.end(); it++ )
{
it->show();
}

SDL_Flip( screen );
}

SDL_Quit();
SDL_FreeSurface( backround );

return 0;
}

I've highlighted the problematic lines.
If you could help, I'd be grateful.

PARTNERS