[SDL] OpenGL / SDL_Surface Blit

Arne Kreutzmann Arne-Kreutz at gmx.de
Wed Aug 15 12:42:01 PDT 2001


>I would like to blit to an OpenGL context using an alpha value, is it
>possible ?
>Where it should be transparency, it's just green !?!

Well without Demeter ( http://www.terrainengine.com/ )
the transparent parts are white !!

>> Can you post a minimal example that shows this problem?

The test file I used was simply a BMP file with black background
with "SDL" in red written on it.
Only the red should visiable.

So here comes the code:

#ifdef _WIN32
	#include <windows.h>
#endif

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <iostream.h>

static SDL_Surface * surf = NULL;

static void quit( int code )
{
    SDL_Quit( );

    exit( code );
}

static draw_2d()
{
	SDL_Rect r;
	r.x = 0;
	r.y = 0;
	r.w = surf->w;
	r.h = surf->h;

	SDL_BlitSurface(surf, &r, SDL_GetVideoSurface(), &r);
	SDL_UpdateRect( SDL_GetVideoSurface(), r.x, r.y, r.w, r.h );
};

static void handle_key_down( SDL_keysym* keysym )
{
    switch( keysym->sym ) 
	{
    case SDLK_ESCAPE:
        quit( 0 );
        break;
    default:
        break;
    }
}

static void process_events( void )
{
    SDL_Event event;

    while( SDL_PollEvent( &event ) ) 
	{
        switch( event.type ) 
		{
        case SDL_KEYDOWN:
            handle_key_down( &event.key.keysym );
            break;
        case SDL_QUIT:
            quit( 0 );
            break;
        }
    }
}

static void draw_screen( void )
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );

	glTranslatef( -.5f, -.5f, -1.0f );

	float mat[3] = {0.1f,0.1f,0.1f};
    glMaterialfv(GL_FRONT,GL_AMBIENT,mat);
    mat[0] = 1.0f; mat[1] = 1.0f; mat[2] = 1.0f;
    glMaterialfv(GL_FRONT,GL_DIFFUSE,mat);
    glMaterialfv(GL_FRONT,GL_SPECULAR,mat);
    glMaterialf(GL_FRONT,GL_SHININESS,0.0f);

	glBegin( GL_QUADS );
	glColor3f( 1.0f, 1.0f, 0.0f );
		glVertex3f( 1.0f, 1.0f, 0.0f );
		glVertex3f( 0.0f, 1.0f, 0.0f );
	glColor3f( 0.0f, 0.0f, 1.0f );
		glVertex3f( 0.0f, 0.0f, 0.0f );
		glVertex3f( 1.0f, 0.0f, 0.0f );
	glEnd();

	draw_2d();

    SDL_GL_SwapBuffers( );
}

static void setup_opengl( int width, int height )
{
    glShadeModel( GL_SMOOTH );

    glCullFace( GL_BACK );
    glFrontFace( GL_CCW );
    glEnable( GL_CULL_FACE );

    glClearColor( 0, 0, 0, 0 );
    glViewport( 0, 0, width, height );
    
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );
    
	glFrustum(-0.5f,0.5f,-0.5f,0.5f,0.65f,2500);	

	glEnable(GL_BLEND);

	glBlendFunc(GL_SRC_ALPHA, GL_ONE);

}

int main( int argc, char* argv[] )
{
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) 
	{
        cerr << "Video initialization failed: " << SDL_GetError( ) << endl;
        quit( 1 );
    }

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    if( SDL_SetVideoMode( 800, 600, 32, SDL_OPENGLBLIT ) == 0 ) 
	{
        cerr  << "Video mode set failed: " << SDL_GetError( ) << endl;
        quit( 1 );
    }

    setup_opengl( 800, 600 );

	surf = SDL_LoadBMP("test.bmp");
	SDL_SetColorKey( surf, SDL_SRCCOLORKEY, SDL_MapRGB(surf->format, 18, 18,
28) );

    while( 1 ) 
	{
        process_events( );

        draw_screen( );
    }

    return 0;
}







More information about the SDL mailing list