Jump to content
  • Advertisement
Sign in to follow this  
zealkaiser

SDL : Help needed in per pixel access

This topic is 2610 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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!