[SDL] Loading alpha-channeled PNGs?
DARKGuy .
dark.guy.2008 at gmail.com
Tue Sep 12 22:52:14 PDT 2006
Hi ^^
Thanks again for the answer ^_^ however it didn't seem to work =/ the black
border is there again.
Yeah, your suggestion is a good one and I could use it for the ship's sprite
and other stuff, but how would I do when dealing with other transparent
stuff, such as explosions, "windows", plasma and other eye-candy? the
checkered-pixel method is now obsolete ;)
I'm gonna paste the whole source code... hope you don't mind. Isn't much
anyways, and it's more of a test (following a tutorial and modifying it to
suit my needs) rather than a bare program, but here it goes (maybe the error
is in the blitting instructions? dunno :P)
---------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
struct CSpriteFrame
{
SDL_Surface *image;
};
SDL_Surface *screen;
Mix_Music *cancion;
void InitAll()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1){
printf("SDLInit -> Error: %s\n", SDL_GetError());
}
screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);
if (screen == NULL){
printf("SDLInitVideoMode -> Error: %s\n", SDL_GetError());
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1)
{
printf("SDLInitAudio: %s\n", Mix_GetError());
}
}
int main(int argc, char *argv[])
{
SDL_Surface *temp;
SDL_Surface *bg;
SDL_Event event;
CSpriteFrame *mAnim;
int salir = 0;
int count = 0;
int count2 = 0;
InitAll();
SDL_WM_SetCaption("Star Solarian", NULL);
if((temp = IMG_Load("bg.png")) == NULL){ printf("Error cargando
imagen"); return 1; }
bg = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
mAnim = new CSpriteFrame[9];
if((temp = IMG_Load("sprites/ship0001.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[0].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0002.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[1].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0003.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[2].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0004.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[3].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0005.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[4].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0006.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[5].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0007.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[6].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0008.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[7].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0009.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[8].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
if((temp = IMG_Load("sprites/ship0010.png")) == NULL){ printf("Error
cargando imagen"); return 1; }
SDL_SetAlpha(temp, SDL_SRCALPHA, 0xFF);
mAnim[9].image = SDL_DisplayFormat(temp); SDL_FreeSurface(temp);
while (! salir)
{
SDL_Rect dest;
dest.x = 0;
dest.y = 0;
SDL_BlitSurface(bg, NULL, screen, &dest);
dest.x = 10;
dest.y = 10;
SDL_BlitSurface(mAnim[count].image, NULL, screen, &dest);
count2=count2+1;
if(count2>10){ count2=0; count=count+1; }
if(count>9){ count=0; }
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_PollEvent(&event);
if (event.type == SDL_QUIT){ salir = 1; }
}
Mix_CloseAudio();
SDL_Quit();
return 0;
}
---------------------------------------------------------------------------------------------------------------------
On 9/13/06, Ryan C. Gordon <icculus at icculus.org> wrote:
>
>
> > The image has alpha channel as you can notice in the PSP window, and in
> > the SDL, it doesn't seem to get it. That's what happens when I remove
> > the SDL_SetColorKey instruction. Any idea? =/
>
> Oh, you have to enable the alpha channel explicitly:
>
> SDL_SetAlpha(mypngsurface, SDL_SRCALPHA, 0xFF);
>
>
> If all you care about is getting rid of the black border, though, you'll
> want to use color key blitting, since it's much faster: just pick a
> color other than black (like, bright purple) that doesn't appear in the
> image, and convert the border from black to that color in PSP.
>
> --ryan.
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
>
--
- Alemar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20060913/15f6cf97/attachment.htm
More information about the SDL
mailing list