• Advertisement
Sign in to follow this  

SDL_FreeSurface and exit(0) are not working.

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

freesurface does not delete the imageand I can't get the program to quit with out ctrl alt del.
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif


#include "SDL.h"	
#include "stdio.h"
#include <cstdlib>
int main( int argc, char* argv[] )
{
atexit(SDL_Quit);

   SDL_Init ( SDL_INIT_VIDEO ) ;
  SDL_Surface* screen;
   screen = SDL_SetVideoMode ( 1024 , 768 , 0, SDL_FULLSCREEN ) ;
 SDL_Surface* pic;
   pic = SDL_SetVideoMode ( 1024 , 768 , 0, SDL_FULLSCREEN ) ;
  SDL_Surface* hold = SDL_LoadBMP("rect.bmp");
  SDL_Rect dest;	
  dest.x = 500;
  dest.y = 400;
  int k = 1;
  int m = 0;
  pic = SDL_LoadBMP("rect.bmp");
  while(k == 1)
  {
int b=5;
	  SDL_Event event;
	  if(b==5)
	  {
        SDL_BlitSurface(pic,NULL,screen,&dest);
	  }
	  SDL_Flip(screen);   
   SDL_PollEvent(&event); 
   
      if ( event.type == SDL_QUIT )  
	  {  
		  k = 2;  
	  }
          while(SDL_PollEvent(&event))
		  {
      if ( event.type == SDL_KEYDOWN )
      {
        if ( event.key.keysym.sym == SDLK_ESCAPE ) 
		{ 
			k = 2; 
		}
      }
  }
       
	  if ( event.type == SDL_KEYDOWN )
      {
        if ( event.key.keysym.sym == SDLK_UP ) 
		{ 
			dest.x +=1;
			dest.y +=1;

SDL_BlitSurface(hold,NULL,screen,&dest);
	  SDL_Flip(screen);
	  SDL_FreeSurface(hold);
		  SDL_Surface* hold = SDL_LoadBMP("rect.bmp");
       
SDL_BlitSurface(pic,NULL,screen,&dest);
	  SDL_Flip(screen);
		}
      }
  }
  //done
  exit(0);
  return ( 0 ) ;
}

Share this post


Link to post
Share on other sites
Advertisement
Well, why are you calling SDL_SetVideoMode( ) twice?
Also why are "pic" and "hold" loaded from the same bitmap?
What you need to do is load only the surfaces you need in the
beginning, and release them all at the end.
As to why you can't exit, you should structure you're event loop
differently. SDL_PollEvent( ) should be part of an if statement ie:

if( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
k = 2;
}
else if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_ESCAPE )
{
k = 2;
}
}
}



And please take out the second call to SDL_SetVideoMode( ) - that can't be good.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
its not. its a memory leak.

how do you know that it is not deleting the surface? If you don't clear the video memory the image you copied to video memory will stay there.

also, you don't need to free the surface everytime you use it. Just keep it around. On one line you are bliting the surface, then the next you free it, then you reload it with the same image. this is a waste.

//in beginning of code
SDL_Surface* hold = SDL_LoadBMP("rect.bmp");
.
.
.
SDL_BlitSurface(hold,NULL,screen,&dest);
//SDL_Flip(screen); //don't do this until all drawing is done.
//SDL_FreeSurface(hold); //don't do this until end of program
//SDL_Surface* hold = SDL_LoadBMP("rect.bmp"); //don't do this either

Share this post


Link to post
Share on other sites
Rather than us all pull our teeth out trying to explain what to do, this is what it should look like. Please read it, it has the comments there to explain. I would also strongly suggest that you take a look at the Cone3D tutorials on SDL as well so you can get familure with how things work.


#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif

#include "SDL.h"
#include "stdio.h"
#include <cstdlib>

// This is the main screen surface
SDL_Surface* screen;

// This is the surface for your picture
SDL_Surface* pic;

// Let's use a more descriptive name for this variable ;)
bool done = 0;

int main( int argc, char* argv[] )
{
if( SDL_Init ( SDL_INIT_VIDEO ) == -1)
{
// Error message here
return -1;
}

// This comes afterwards only if SDL was init'ed in the first place
atexit(SDL_Quit);

// Only call this once, you will need SDL_DOUBLEBUF for double buffering
screen = SDL_SetVideoMode ( 1024 , 768 , 0, SDL_DOUBLEBUF /*| SDL_FULLSCREEN*/ );

// Load in the image
pic = SDL_LoadBMP("rect.bmp");

// Now see if it worked, always check!
if( pic == NULL )
{
// Image could not be loaded
return -1;
}

// Rect for destination
SDL_Rect dest;

// Your X,Y coords go here
dest.x = 500;
dest.y = 300;

// Declare this outside of the loop
SDL_Event event;

while( done == false )
{
// Only should poll in one loop
while( SDL_PollEvent(&event) )
{
// If we need to quite now set variable
if ( event.type == SDL_QUIT )
{
done = 1;
}

// If escape was hit, set done
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
done = 1;
}
}

if ( event.type == SDL_KEYDOWN )
{
// If UP was hit
if ( event.key.keysym.sym == SDLK_UP )
{
// You decrease the y value!
dest.y -= 5;
}

// If DOWN was hit
if ( event.key.keysym.sym == SDLK_DOWN )
{
// You increase the y value!
dest.y += 5;
}

// If LEFT was hit
if ( event.key.keysym.sym == SDLK_LEFT )
{
// You dencrease the x value!
dest.x -= 5;
}

// If RIGHT was hit
if ( event.key.keysym.sym == SDLK_RIGHT )
{
// You increase the x value!
dest.x += 5;
}
}
}

// Clear the screen to black
SDL_FillRect(screen,0,0);

// Now this is where the draw/update code gies
SDL_BlitSurface( pic, NULL, screen, &dest);

// Only flip the screen, not the image itself
SDL_Flip(screen);
}

// Free the image, but NOT the screen
SDL_FreeSurface(pic);

// Return now b/c we are all done ;)
return ( 0 );
}





If you have any more questions or comments feel free to ask. Make sure to have a look at the SDL documentation as well for the various functions and their tasks. Best of luck! [smile]

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
Edit: Drew beat me [crying]

*** Source Snippet Removed ***

You had a lot of design issues so it was hard for me to figure out what you were trying to do. Take a look at the above code and I'd be happy to answer any specific questions like "Why the hell did you make this change?" :)

Note: I do not have a compiler handy and did this pretty quickly. If anyone sees problems, point 'em out!


[inlove][grin]

I see a few things, but the biggest no, no in SDL I must scold you for is SDL_FreeSurface(screen); [smile] Other than that, pretty nice [wink]

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Quote:
Original post by Rhaal
Edit: Drew beat me [crying]

*** Source Snippet Removed ***

You had a lot of design issues so it was hard for me to figure out what you were trying to do. Take a look at the above code and I'd be happy to answer any specific questions like "Why the hell did you make this change?" :)

Note: I do not have a compiler handy and did this pretty quickly. If anyone sees problems, point 'em out!


[inlove][grin]

I see a few things, but the biggest no, no in SDL I must scold you for is SDL_FreeSurface(screen); [smile] Other than that, pretty nice [wink]

- Drew


Dammit, that's the first thing I learned with SDL too! I should have known. And looking back at my code (the code I wrote earlier and was referencing, not the code I wrote here), it doesn't do it :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement