Hi everybody,
Working off some help I received on this list earlier, I’ve been trying
to read a PNG w/ an alpha channel, and then read the pixels off that
surface and put them on a 16-bit surface. The source works correctly w/
32-bit surfaces (thanks to help I received here earlier, thanks!) but
I’d like the “final” image, the surface pointer “backbuffer” to be a
16-bit image, unfortunately, SDL_DisplayFormatAlpha() only makes a
32-bit surface …
So, if I change line 53 from 32 bytes per pixel to 16, the backbuffer
image (when blitted) simply doesn’t appear at all. How can I get that
ship (the sample.png image, which I posted at
chris.luethy.net/sample.png) image copied onto a 16bpp surface, instead
of a 32bpp surface?
(sample.png at chris.luethy.net/sample.png)
#include <stdio.h>
#include <stdlib.h>
#include “SDL.h”
#include “SDL_image.h”
inline Uint32 raw_getpixel(SDL_Surface surface, int x, int y) {
return ((Uint32
)((((Uint8)surface->pixels)+(ysurface->pitch)+(xsurface->format->BytesPerPixel))));
}
inline void raw_putpixel(SDL_Surface surface, int x, int y, Uint32
pixel) {
(Uint32)((((Uint8)surface->pixels)+(ysurface->pitch)+(xsurface->format->BytesPerPixel))) = pixel;
}
int main(int argc, char *argv[])
{
int i, j;
SDL_Surface *screen, *temp, *sample, *backbuffer;
SDL_Rect src, dest;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "Could not init SDL: %s\n", SDL_GetError());
return (-1);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(320, 200, 16, 0);
if (screen == NULL)
{
fprintf(stderr, "Could not set video mode.\n");
return (-1);
}
printf("[SCREEN ] w=%d, h=%d,
bbp=%d\n",screen->w,screen->h,screen->format->BitsPerPixel);
temp = IMG_Load("sample.png");
if (temp == NULL)
{
fprintf(stderr, "Could not load sample.png");
return (-1);
}
printf("[TEMP ] w=%d, h=%d,
bbp=%d\n",temp->w,temp->h,temp->format->BitsPerPixel);
sample = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
printf("[SAMPLE ] w=%d, h=%d,
bbp=%d\n",sample->w,sample->h,sample->format->BitsPerPixel);
backbuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, sample->w, sample->h,
32,
sample->format->Rmask, sample->format->Gmask,
sample->format->Bmask, sample->format->Amask);
printf("[BACKBUFFER] w=%d, h=%d,
bbp=%d\n",backbuffer->w,backbuffer->h,backbuffer->format->BitsPerPixel);
SDL_LockSurface(sample);
SDL_LockSurface(backbuffer);
for (j = 0; j < sample->h; j++)
{
for (i = 0; i < sample->w; i++)
{
Uint32 color;
SDL_Rect src;
color = raw_getpixel(sample, i, j);
raw_putpixel(backbuffer, i, j, color);
}
}
SDL_UnlockSurface(backbuffer);
SDL_UnlockSurface(sample);
dest.x = 0;
dest.y = 0;
dest.w = 320;
dest.h = 200;
SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 255));
src.x = 0;
src.y = 0;
src.w = backbuffer->w;
src.h = backbuffer->h;
dest = src;
SDL_BlitSurface(backbuffer, &src, screen, &dest);
SDL_Flip(screen);
SDL_Delay(3000);
SDL_FreeSurface(sample);
SDL_FreeSurface(backbuffer);
return (0);
}
– chris (@Christopher_Thielen)