Hey, I'm kind of new to SDl and I've been writing a couple of wrapping classes and for some reason my ocmpiler just cuts out on a certain line, here's the code:
void Sprite::Draw()
{
x += velx;
y += vely;
//Create a Temporary Rectangle to Hold the Offsets
SDL_Rect offset;
//Give the Offsets to the Rectangle
offset.x = x;
offset.y = y;
if(source != NULL)
{
//Blit the Image to the Screen
while ( SDL_BlitSurface(source, NULL, g_engine->GetScreen(), &offset) == -2 ) {
while ( SDL_LockSurface(source) < 0 )
SDL_Delay(10);
//Write image pixels to image->pixels --
SDL_UnlockSurface(source);
}
//SDL_BlitSurface(source, NULL, g_engine->GetScreen(), &offset);
}
}
now it just cuts out on the SDL_BlitSurface and tells me there is an error:
Unhandled exception at 0x68128c2c in File.exe: 0xC0000005: Access violation reading location 0xccccccf8.
is the error I get I don't know I've tried everything and was wondering if anyone here knew what was going on
either 'source' or 'g_enging->GetScreen()' have not been properly initialized.
You check for a null state, but a uninitialized pointer won't be null.
Set a breakpoint on that BlitSurface line and watch 'source' if source is 0xCCCCCCCC or something similar, then it's the problem. Make sure you initialize it correctly.
Okay this is really weird, apparently (and I'm not kidding here) it is not saving what I'm doing across function methods I swear I put this in a constructor:
1 2 3 4 5 6 7 8
Sprite::Sprite()
{
velx = 0;
vely = 0;
source = new SDL_Surface();
x = 0;
y = 0;
}
And then when I use the interpreter to go through my code line by line I check up on the value of vel x and vely and what do I see?
-43566
This is really bugging me now, this shouldn't happen!.
dude, im writing a wrapper engine in SDL too, why are you locking, unlocking and delaying? That is really gonna slow you down. My screen class handles drawing sprites:
1 2 3 4 5 6 7 8 9 10 11 12 13
void SGEScreen::DrawSprite(SGESprite *Sprite)
{
// Check if the surface is NULL.
if(Sprite->_Surface == NULL) return;
// Set up the destination rectangle.
SDL_Rect DestinationRect;
DestinationRect.x = Sprite->GetX();
DestinationRect.y = Sprite->GetY();
// Draw the sprite to the screen.
SDL_BlitSurface(Sprite->_Surface, NULL, _Screen, &DestinationRect);
}
SGEScreen is a friend of SGESprite, thats why it can access '_Surface', a private member.