简体   繁体   中英

Alpha value of pixel color and Per-Pixel-Collision using SDL

I am trying to implement per-pixel collision into my SDL game framework. I have a problem with finding out the alpha value (RGBA) of a specific pixel in a surface. I am using the SDL function in order to do that.

SDL_GetRGBA(pixelColor, pixelFormat, &red, &green, &blue, &alpha);

Game crashes: The game crashes when the pixelColor variable is going to be initialized (see the comments in the code). It seems that the problem has to do with the *p pointer and the pixelColor variable which is later used as a parameter in the SDL_GetRGBA() method.

variable bpp : I get the value 108 for the variable bpp, sometimes it's 106 and in some cases the bpp (Bytes per Pixel) is 255. But those values are completely wrong aren't they? It should be 1 or 2 or 3 or 4 bytes per pixel... ( http://wiki.libsdl.org/SDL_PixelFormat )

p : 0x00c1e11c {???}
pixelFormat : SDL2.dll!0x6c80d62c {format=10746688 palette=0x00a3fb40}

(Those are the values of the other important variables)

Here is my complete function for getting the alpha value of a pixel for the collision detection. Parts of it are taken from this tutorial ( http://www.sdltutorials.com/sdl-per-pixel-collision ).

int CollisionHandler::GetAlphaXY(SDL_Surface* surface, int x, int y)
{
    // get the specific pixel for checking the alpha value at x/y

    SDL_PixelFormat* pixelFormat = surface->format;
    int bpp = pixelFormat->BytesPerPixel;   

    Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;

    // ! here the game crashes
    Uint32 pixelColor = *p;     

    // get the RGBA values

    Uint8 red, green, blue, alpha;

    // this function fails, sometimes the game crashes here
    SDL_GetRGBA(pixelColor, pixelFormat, &red, &green, &blue, &alpha);  

    return alpha;
}

I think the code is right, but I get a runtime error when the two objects that I am using for testing collide. The program doesn't crash when the per-pixel collision should notice a collision but rather when the rectangles collide.

This is how my Per-Pixel-Collision method looks like:

bool CollisionHandler::Per_Pixel_Collision(GameObject* obj1, GameObject* obj2)
{
    // only do pixel collision if rectangles collide

    if (Rect_Collision(obj1, obj2))
    {   
        int top = max(top1, top2);
        int left = max(left1, left2);
        int bottom = min(bottom1, bottom2);
        int right = min(right1, right2);

        // from the top to the bottom 

        for (int y = top; y < bottom; y++)
        {
            // from the left to the right 

            for (int x = left; x < right; x++)
            {
                // if both alpha values are zero -> both pixels transparent

                if (GetAlphaXY(obj1->getTexture().getSurface(), x, y) == 0 && GetAlphaXY(obj2->getTexture().getSurface(), x, y) == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }

            }
        }
    }
}

Ok, the mistake I did is dumb. The reason why the game crashed is that the surface was empty and so were the members of the surface, because in my Texture class, where I am handling the initialization of textures, I called
SDL_FreeSurface() That is why nothing works. The pixelFormat, the pixelColor values and so on, they were all set to 0 through FeeSurface. Now I solved the issue, I just wanted to write it down for other poeple who maybe did the same mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM