1
votes

I'm trying to draw Xiaolin Wu's line algorithm in SDL2, due to alpha channel problem i get nothing , i am drawing it in surface ,then creating texture from surface

i have tried int SDL_SetTextureBlendMode(SDL_Texture* texture, SDL_BlendMode blendMode) with all possible blending modes

getting color:(there's no error here i think)

void LINE_PlotPoint(SDL_Surface * surface,int x,int y, double alpha)
{
    Uint32 *pixels = (Uint32 *)surface->pixels;
    Uint32 pixel=SYS_GetForegroundColor();

    Uint8 a= alpha*255;

    pixel&=~amask;
    pixel |= a;

    pixels[ ( y * surface->w ) + x ] =pixel;
}

the main loop for this task is:

if(event.type==SDL_MOUSEMOTION)
{
    SDL_GetMouseState(&i,&j);
    i-=grect.x;
    j-=grect.y;
    TOOL_DrawLine(tempSurface,x,y,i,j,1);//Xiaolin Wu's line algorithm

    if(tempTexture)
    {
        SDL_DestroyTexture(tempTexture);
    }

    tempTexture=TOOL_CreateLineTexture(tempSurface,&srect,&drect);//create texture from surface and calculating rect(src & dest)
    if(tempTexture==NULL)
    {
        puts("error");
    }
    SDL_SetTextureBlendMode(tempTexture,SDL_BLENDMODE_BLEND);


    SDL_FillRect(tempSurface,NULL,NULL);//delete pixel from surface (no need for it)

    }

i have tried it before with alpha channel =255 and it's work normally,but with varies alpha values no thing apear

1
Uint8 a= alpha*255; won't it overflow Uint8 with alpha>1? - kiran Biradar
0<=alpha<=1 but the values are correct in a ,I have checked them ,we have varied values of a but i can't show them, the problem i think is between surface and texture - user8821501
How do you create your tempSurface, texture, and how do you draw? Patching alpha doesn't look right at all, but to know correct way one have to see your pixel format. It is also unclear how it could be correct without alpha if you fill surface with constant colour - wouldn't it be just a solid rectangle? - keltar

1 Answers

1
votes

now i have found the problem

just i have forgot to shift the alpha value to it's right place

pixel&=~amask;
pixel |= a << ashift;