SDL画线 16位(转)

SDL画线 16位

 

void put_pixel_16( SDL_Surface *surface, int x, int y, Uint16 pixel )
{
    Uint16 *pixels = (Uint16 *)surface->pixels;
 
    pixels[ ( y * surface->w ) + x ] = pixel;
    
    return ;
}
 
void draw_line(SDL_Surface *surface, Uint16 x0, Uint16 y0, Uint16 xEnd, Uint16 yEnd, Uint16 pixel)
{
        Uint16 dx = abs (xEnd -x0), dy = abs (yEnd-y0);//fabs
        Uint16 p =2* dy-dx;
        Uint16 twoDy = 2*dy, twoDyMinusDx = 2* (dy - dx);
        Uint16 x,y;
 
        if (x0>xEnd)
        {
                x=xEnd;
                y=yEnd;
                xEnd=x0;
        }
        else{
                x=x0;
                y=y0;
        }
        put_pixel_16( surface, x, y, pixel );
 
        while (x<xEnd)
        {
                x++;
                if(p<0)
                        p+=twoDy;
                else{
                        y++;
                        p+=twoDyMinusDx;
                }
                put_pixel_16( surface, x, y, pixel );
       }
 
}

 

posted @ 2020-07-14 10:52  酒醉的Tiger  阅读(272)  评论(0编辑  收藏  举报