Video Events OpenGL Audio CD-ROM Threads Time

OpenGL Context Manager Examples


Introduction Function List Function Reference Examples


Initializing an OpenGL video mode

	int video_flags;
	int w, h, bpp;

        /* Initialize SDL with SDL_Init() */

	w = 640;
	h = 480;
	bpp = 16;

	video_flags = SDL_OPENGL;
/*	video_flags = SDL_OPENGL | SDL_FULLSCREEN; */

	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( w, h, bpp, video_flags ) == NULL ) 
	{
		fprintf(stderr, "Couldn't set GL mode: %s\n",
		SDL_GetError());
		SDL_Quit();
		exit(1);
	}

        /* Use GL functions normally, followed by SDL_GL_SwapBuffers() ... */

        /* Clean up SDL with SDL_Quit() */

Taking a screenshot and saving it

	SDL_Surface *image;

	image = SDL_CreateRGBSurface(SDL_SWSURFACE, screenWidth, screenHeight,
                                     24, 0x0000FF, 0x00FF00, 0xFF0000);
	glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB,
	             GL_UNSIGNED_BYTE, image->pixels);
	SDL_SaveBMP(image, "screenshot.bmp");
	SDL_FreeSurface(image);

Using a surface as a texture

Note: This example uses SDL_Image, a free companion library to SDL available at http://www.devolution.com/~slouken/SDL/projects/SDL_image/.
The code can be modified, however, to suit whatever method of loading your images you prefer (SDL_LoadBMP(), your own image loading library, dynamic lightmap generation, etc.)

	GLuint textureHandle;
	SDL_Surface *image;
	image = IMG_Load("test.jpg");	// or whatever.
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);
	glBindTexture(GL_TEXTURE_2D,textureHandle);

/* change these settings to your choice */

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,
		GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
		GL_NEAREST);

	if (image->format->BytesPerPixel == 3) 
	{
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,
		image->w,
		image->h,
		0, GL_RGB, GL_UNSIGNED_BYTE,
		image->pixels);
	}
	else
	{
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
		image->w,
		image->h,
		0, GL_RGBA, GL_UNSIGNED_BYTE,
		image->pixels);
	}
	
	...