[SDL] Alpha bliting problem

Jan Kratochvil honza at twin.jikos.cz
Wed Jun 13 12:54:29 PDT 2001


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);
}




More information about the SDL mailing list