Search code examples
c++csdlsdl-2

drawing with varies alpha values in sdl


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


Solution

  • 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;