Sign in to follow this  

SDL : Help needed in per pixel access

This topic is 2392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating a program where I want to read all pixels from an image and store it in a RGBA structure. Then draw those pixels.
My problem is that when I am drawing pixels I am not getting transparency. But I am able to read the alpha channel.

I have use putpixel and getpixel from documentation.

typedef struct RGBA
{
Uint8 red;
Uint8 green;
Uint8 blue;
Uint8 alpha;
}RGBA;

typedef struct Image
{
int width;
int height;
RGBA *data;
}Image;

Image img;

void load(char *file)
{
SDL_Surface *tmp = IMG_Load(file);
bmp = SDL_DisplayFormatAlpha(tmp);
SDL_FreeSurface(tmp);

img.width = bmp->w;
img.height = bmp->h;
img.data = new RGBA[img.width * img.height];

Uint32 pixel;
for(int y=0; y<img.height; y++)
{
for(int x=0; x<img.width; x++)
{
pixel = getpixel(bmp,x,y);
SDL_GetRGBA(pixel,bmp->format,&img.data[y*img.width + x].red,&img.data[y*img.width + x].green,&img.data[y*img.width + x].blue,&img.data[y*img.width + x].alpha);
}
}
SDL_FreeSurface(bmp);
}

void draw()
{
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,128,128,128));
Uint32 pixel;
for(int y=0; y<img.height; y++)
{
for(int x=0; x<img.width; x++)
{
Uint32 Color = SDL_MapRGBA(screen->format,img.data[y*img.width + x].red,img.data[y*img.width + x].green,img.data[y*img.width + x].blue,img.data[y*img.width + x].alpha);
putpixel(screen,x,y,Color);
}
}

SDL_Flip(screen);
}

int main(int argc,char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

load("smiley.png");

bool run = true;
while(run)
{
SDL_Event evnt;
SDL_PollEvent(&evnt);
if(evnt.type == SDL_QUIT)
run = false;

draw();
}
SDL_Quit();

return 0;
}

Share this post


Link to post
Share on other sites

This topic is 2392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this