Jump to content
  • Advertisement
Sign in to follow this  
Tyco

I need help with a bug in my program.

This topic is 4968 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

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


#include "SDL.h"	


int main( int argc, char* argv[] )
{
   SDL_Init ( SDL_INIT_VIDEO ) ;
  SDL_Surface* screen;
   screen = SDL_SetVideoMode ( 1024 , 768 , 0, SDL_FULLSCREEN ) ;
  SDL_Surface* pic;
  SDL_Rect dest;
  dest.x = 500;
  dest.y = 400;
  int k = 1;

  pic = SDL_LoadBMP("rect.bmp");
  while(k == 1)
  {
int b=5
	  SDL_Event event;
         if(b==5)
           {
   // line of bug         SDL_BlitSurface(pic,NULL,screen,&dest);
           }
	  SDL_Flip(screen);   
   SDL_PollEvent(&event); 
   
      if ( event.type == SDL_QUIT )  
	  {  
		  k = 2;  
	  }

      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(pic,NULL,screen,&dest);
	  SDL_Flip(screen);
	  SDL_FreeSurface(pic);
		  SDL_Surface* pic = SDL_LoadBMP("rect");
       
SDL_BlitSurface(pic,NULL,screen,&dest);
	  SDL_Flip(screen);
		}
      }
  }
  //done
  return ( 0 ) ;
}

Unhandled exception at 0x100225d0 in paddle ball.exe: 0xC0000005: Access violation reading location 0x00000000. I tried to fix it when I put in an if statment,but that did'nt work.Can someone please tell me how to fix this? BTW: I'm trying to move the image,will this work?

Share this post


Link to post
Share on other sites
Advertisement
SDL_Surface* pic = SDL_LoadBMP("rect");

Needs to be:

SDL_Surface* pic = SDL_LoadBMP("rect.bmp");

I would strongly suggest that you do not release and reload thaat BMP every movement either [smile]. Just load it once, use it as much as you want, then release it at the very end. Take a look at my Starting with SDL tutorial in my sig. It shows how to do this quite well [wink] (It even has the source code!)

Share this post


Link to post
Share on other sites
It still does it,but the only auto left is pic now.I tried
pic = SDL_SetVideoMode(),but it did'nt do anything.

Share this post


Link to post
Share on other sites
And after "pic = SDL_LoadBMP("rect.bmp");"
You should check the result also!


if (! pic) {
cout << "Error: " << SDL_GetError() << endl;
return 1;
}



And do the same for the screen variable.

Share this post


Link to post
Share on other sites
DOH! Also I need to correct my first post -

SDL_Surface* pic = SDL_LoadBMP("rect");

Needs to be:

pic = SDL_LoadBMP("rect.bmp");

What is happening is that you declare another variable in the same scope as the first variable, so it never reloads the correct bmp. Take out that SDL_Surface* and it should work, if not definitly do what qbic has suggested to make sure the image can be found in the first place. That is an important concept to keep in mind as well.

- Drew

Share this post


Link to post
Share on other sites
a couple 'o things(from my humble knowledge of SDL):
It will crash if the img is bigger than the window. Check that.

PollEvent(&event);
should be while(SDL_PollEvent(&event)
{
//end this while at the end of the keys checking,at the last if.
}

at the beginning of the source file, put atexit(SDL_Quit);
and change return 0; to exit(0);(I think you need to include stdlib.h or stdio.h).

Share this post


Link to post
Share on other sites
Quote:
Original post by supercrazy7474
and change return 0; to exit(0);(I think you need to include stdlib.h or stdio.h).


Actually use both, it is not required, but it is ALWAYS good practice to return back to the OS.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!