Alpha bliting problem

Hi
i have some problems. I want to blit sprite on high resolution surface
and in this case i need to use alpha channel. When i use
IMG_Load(“something.tga”) and then i blit it everythinng is ok. But if i
use SDL_CreateRGBSurface something goes wrong.
Here is sourcecode please tell me where i made mistake
#include “sdl.h”
#include “sdl_image.h”
#include “sge.h”

#define TICK_INTERVAL 30

Uint32 TimeLeft(void)
{
static Uint32 next_time = 0;
Uint32 now;

now = SDL_GetTicks();
if ( next_time <= now ) {
    next_time = now+TICK_INTERVAL;
    return(0);
}
return(next_time-now);

}

int main(int argc, char *argv[])
{
SDL_Surface *screen;
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)==-1)
{
printf(“Error: cannot initialize SDL\n”);
exit( -1 );
}

#ifdef _DEBUG
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE);
#else
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE |
SDL_FULLSCREEN);
#endif

SDL_Surface *ship;
ship = IMG_Load("player.tga");
SDL_Surface *bcg;
bcg = IMG_Load("space.jpg");

SDL_Surface *ship2;

ship2 = SDL_CreateRGBSurface(SDL_HWSURFACE,

ship->w,

ship->h,

ship->format->BitsPerPixel,

ship->format->Rmask,

ship->format->Gmask,

ship->format->Bmask,

ship->format->Amask);

SDL_Rect srcr,dstr;
srcr.x=srcr.y=dstr.x=dstr.y=0;
srcr.h=dstr.h=ship->h;
srcr.w=dstr.w=ship->w;
SDL_BlitSurface(ship,&srcr,ship2,&dstr);
srcr.h=dstr.h=600;
srcr.w=dstr.w=800;

SDL_BlitSurface(bcg,&srcr,screen,&dstr);
SDL_BlitSurface(ship2,&srcr,screen,&dstr);
SDL_UpdateRect(screen,0,0,0,0);

SDL_Event event;
int quit = 0;
while ( !quit ) {
while( SDL_PollEvent( &event ) ){
	switch (event.key.keysym.sym) {
		case SDLK_ESCAPE:
			quit = 1;
		break;
	}
}
SDL_Delay(TimeLeft());
}

SDL_FreeSurface(ship);
SDL_FreeSurface(ship2);
SDL_FreeSurface(screen);
SDL_Quit();
exit(0);

}

I think you need to include the alpha blending constant in your create rgb
surface code, so that it knows you’ll be using that surface for alpha
blending.>From: Jan Kratochvil

Reply-To: sdl at lokigames.com
To:
Subject: [SDL] Alpha bliting problem
Date: Wed, 13 Jun 2001 21:54:29 +0200 (CEST)

Hi
i have some problems. I want to blit sprite on high resolution surface
and in this case i need to use alpha channel. When i use
IMG_Load(“something.tga”) and then i blit it everythinng is ok. But if i
use SDL_CreateRGBSurface something goes wrong.
Here is sourcecode please tell me where i made mistake
#include “sdl.h”
#include “sdl_image.h”
#include “sge.h”

#define TICK_INTERVAL 30

Uint32 TimeLeft(void)
{
static Uint32 next_time = 0;
Uint32 now;

now = SDL_GetTicks();
if ( next_time <= now ) {
    next_time = now+TICK_INTERVAL;
    return(0);
}
return(next_time-now);

}

int main(int argc, char *argv[])
{
SDL_Surface *screen;
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)==-1)
{
printf(“Error: cannot initialize SDL\n”);
exit( -1 );
}

#ifdef _DEBUG
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE);
#else
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE |
SDL_FULLSCREEN);
#endif

SDL_Surface *ship;
ship = IMG_Load(“player.tga”);
SDL_Surface *bcg;
bcg = IMG_Load(“space.jpg”);

SDL_Surface *ship2;

ship2 = SDL_CreateRGBSurface(SDL_HWSURFACE,

ship->w,

ship->h,

ship->format->BitsPerPixel,

ship->format->Rmask,

ship->format->Gmask,

ship->format->Bmask,

ship->format->Amask);

SDL_Rect srcr,dstr;
srcr.x=srcr.y=dstr.x=dstr.y=0;
srcr.h=dstr.h=ship->h;
srcr.w=dstr.w=ship->w;
SDL_BlitSurface(ship,&srcr,ship2,&dstr);
srcr.h=dstr.h=600;
srcr.w=dstr.w=800;

SDL_BlitSurface(bcg,&srcr,screen,&dstr);
SDL_BlitSurface(ship2,&srcr,screen,&dstr);
SDL_UpdateRect(screen,0,0,0,0);

SDL_Event event;
int quit = 0;
while ( !quit ) {
while( SDL_PollEvent( &event ) ){
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
quit = 1;
break;
}
}
SDL_Delay(TimeLeft());
}

SDL_FreeSurface(ship);
SDL_FreeSurface(ship2);
SDL_FreeSurface(screen);
SDL_Quit();
exit(0);
}


Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

#include “sdl.h”
#include “sdl_image.h”

It’s <SDL.h> and <SDL_image.h>, or your code won’t work on a case-sensitive
file system

SDL_BlitSurface(ship,&srcr,ship2,&dstr);

You have to remove the SDL_SRCALPHA flag from the source surface (ship),
if you want to just copy the pixels. Use SDL_SetAlpha() for this.

Read carefully the documentation (especially for SDL_BlitSurface and
SDL_SetAlpha) to understand what is going on. The online docs reachable
from www.libsdl.org should be those most up to date

Hey, I’m new to SDL. Umm… How do you do a masked
blit?

Herbert__________________________________________________
Do You Yahoo!?
Spot the hottest trends in music, movies, and more.
http://buzz.yahoo.com/

Use the SDL_ColorKey function to set the colour you want to be transparent.

My first post to the list - been lurking for a while - greets all :slight_smile:

James.

At 23:50 14/06/01 +0000, you wrote:

Hey, I’m new to SDL. Umm… How do you do a masked
blit?

Herbert


Do You Yahoo!?
Spot the hottest trends in music, movies, and more.
http://buzz.yahoo.com/

James Taylor BSc. Phone: +618 9226 0011
Systems Administrator Fax : +618 9226 0033
Commerce Australia Pty. Ltd. Email: @James_Taylor