0

I have a problem, where when I build my code it works fine, but when I run it, I get an access violation error, and ive tracked it to this little bit of code here:

void thePlayer::show_player()
{
SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer);
SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255)); //this line causes the problem
}

However I have no idea how to fix this or why it is stopping. Any help would be much appreciated!

EDIT: I have found why I have a memory violation. If I move my picture loading into the same function, it works.

HOWEVER, now I have the problem of that when I run my code, nothing happens, nothing is Blitted to the Screen.

Edited show_player code:

void thePlayer::show_player()
{
Player = SDL_LoadBMP("spaceship.bmp");
if (Player == NULL)
{
    cout<<"Error in loading player."<<SDL_GetError()<<endl;
}
SDL_BlitSurface(Player, NULL, ScreenSurface, &posPlayer);
SDL_SetColorKey(Player, SDL_TRUE, SDL_MapRGB(Player->format, 255, 255, 255));
SDL_UpdateWindowSurface(Window);
}

Like I said, everything builds, but nothing is shown on screen.

Tom
  • 2,372
  • 4
  • 25
  • 45
  • Probably something's wrong with Player. Is it null? – user3344003 May 15 '14 at 17:14
  • it is intially, but it is then loaded from a BMP picture file – Tom May 15 '14 at 18:07
  • ive figured out that if i move this function into the main .cpp, it manages to blit to the surface – Tom May 16 '14 at 13:57
  • 1
    You should not mix the load and render funtion together, because you want to load it once, but keep on bliting it inside some kind of loop. – jofra May 16 '14 at 17:39
  • ive taken your advice, and put the bliting inside a while loop in main, and the load in a function, however, nothing is appearing on screen when i blit? – Tom May 16 '14 at 18:57
  • and i still get the access violation error – Tom May 16 '14 at 19:06
  • I think at this point you should start writing multiple [sscce](http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions)'s that do some minimal desired behavior and when one doesn't work you can post it here so others can try it. It is impossible to determine what logical errors exist in your code with the snippets you post. Remember make the sscce *as short as possible* while still performing the function you want. – this May 16 '14 at 20:22
  • The code you show looks alright. I don´t think you are showing us the problem (at least i cant see any). – jofra May 16 '14 at 22:14
  • sorry, the problem i have is that if i put the show player function in main.cpp, it blits, but if i move it back into a separate cpp, it doesnt – Tom May 17 '14 at 18:03

0 Answers0