I need help with a bug in my program.

Started by
7 comments, last by Deranged 19 years, 1 month ago
#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?
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!)
Try debugging and see where it crashes (F5 in VS 6.0).

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
It still does it,but the only auto left is pic now.I tried
pic = SDL_SetVideoMode(),but it did'nt do anything.
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.
- Jezper Blixt
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
The image shows up,but when I hit awroo key up it freezes.
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).
I pity the fool that uses numerical operators as boolean evaluators!
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.

This topic is closed to new replies.

Advertisement