Question

I have been writing some SDL2 lately and today i rewrote the base of my code to make it more structured and easier to use.

The issue i am having is when trying to blit the sprites surface, there it gives me the SDL error Surfaces must not be locked during blit which seemed reasonable so i added SDL_UnlockSurface to the beginning of my blit function before SDL_BlitSurface but still returned the same error. Since i saw in the documentation that SDL_UnlockSurface is a void, i cannot know if it was successful or not, but since it's not multi-threaded nothing can force lock it while i'm blitting (and it's only drawing to the surface at the start of the application anyway).

Here's the blit function

bool Sprite::blit(SDL_Surface* targetSurface){
    SDL_UnlockSurface(this->surface);
    int success = SDL_BlitSurface( this->surface, NULL, targetSurface, NULL );
    if ( success != 0){
        printf( "Unable to blit surface! SDL Error: %s\n"m SDL_GetError() );
        return false;
        }
    return true;
}

Thanks in advance!

Was it helpful?

Solution

Locks are counted. If you locked surface twice, you have to unlock it twice. Better avoid this scheme - the same function that locked surface should unlock it after its job is done.

You can look at surface->locked variable, which is locks count, but it isn't included in documentation so you can't rely on this name - it could be changed in future versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top