develop/SDL

점 찍기

shellbt 2011. 12. 7. 16:01
void DrawPixel(SDL_Surface *dst, Uint32 x, Uint32 y, Uint8 R, Uint8 G, Uint8 B)
{
    Uint32 color = SDL_MapRGB(dst->format, R, G, B);

    switch( dst->format->BytesPerPixel )
    {
        case 1: // Assuming 8-bpp
            {
                Uint8 *bufp;
                bufp = (Uint8 *)dst->pixels + y * dst->pitch + x;
                *bufp = color;
            }
            break;

        case 2: // probably 15-bpp or 16-bpp
            {
                Uint16 *bufp;
                bufp = (Uint16 *)dst->pixels + y * dst->pitch/2 + x;
                *bufp = color;
            }
            break;

        case 3: // slow 24-bpp mode, usually not used
            {
                Uint8 *bufp;
                bufp = (Uint8 *)dst->pixels + y * dst->pitch + x * 3;
                if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
                {
                    bufp[0] = color;
                    bufp[1] = color >> 8;
                    bufp[2] = color >> 16;
                }
                else
                {
                    bufp[2] = color;
                    bufp[1] = color >> 8;
                    bufp[0] = color >> 16;
                }
            }
            break;

        case 4: // probably 32-bpp
            {
                Uint32 *bufp;
                bufp = (Uint32 *)dst->pixels + y * dst->pitch/4 + x;
                *bufp = color;
            }
            break;
    }

}