1
/*******************************************************************
3
* Using SDL With OpenGL *
5
* Tutorial by Kyle Foley (sdw) *
7
* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
9
*******************************************************************/
12
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
13
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
15
THE ORIGINAL AUTHOR IS KYLE FOLEY.
17
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
18
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
19
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
20
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
21
RESULTING FROM THE USE, MODIFICATION, OR
22
REDISTRIBUTION OF THIS SOFTWARE.
26
#include "SDL/SDL_image.h"
27
#include "SDL/SDL_opengl.h"
33
int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
35
int len = strlen(ext);
36
if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
38
if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
43
int main(int argc, char *argv[])
47
// Slightly different SDL initialization
48
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
49
printf("Unable to initialize SDL: %s\n", SDL_GetError());
53
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
55
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
57
printf("Unable to set video mode: %s\n", SDL_GetError());
63
const char *exts = (const char *)glGetString(GL_EXTENSIONS);
64
assert(hasext(exts, "GL_ARB_texture_compression"));
65
assert(hasext(exts, "GL_EXT_texture_compression_s3tc"));
67
// Set the OpenGL state after creating the context with SDL_SetVideoMode
69
glClearColor( 0, 0, 0, 0 );
72
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
75
glViewport( 0, 0, 640, 480 );
77
glMatrixMode( GL_PROJECTION );
78
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
82
glLoadMatrixf(matrixData); // test loadmatrix
84
glMatrixMode( GL_MODELVIEW );
88
// Load the OpenGL texture
92
#define DDS_SIZE 262272
93
FILE *dds = fopen("screenshot.dds", "rb");
94
char *ddsdata = (char*)malloc(512*512*4);//DDS_SIZE);
95
assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
98
glGenTextures( 1, &texture );
99
glBindTexture( GL_TEXTURE_2D, texture );
101
assert(!glGetError());
102
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 512, 512, 0, DDS_SIZE-128, ddsdata+128);
103
assert(!glGetError());
105
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
106
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
109
// Prepare and Render
111
// Clear the screen before drawing
112
glClear( GL_COLOR_BUFFER_BIT );
114
// Bind the texture to which subsequent calls refer to
115
glBindTexture( GL_TEXTURE_2D, texture );
117
// Use clientside vertex pointers to render two items
118
GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
127
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
128
glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
129
glEnableClientState(GL_VERTEX_ARRAY);
130
glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
132
glDrawArrays(GL_QUADS, 0, 8);
134
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
135
glDisableClientState(GL_VERTEX_ARRAY);
137
// Render the last item using oldschool glBegin etc
138
glBegin( GL_TRIANGLE_STRIP );
139
glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
140
glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 0 );
141
glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 0 );
142
glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
145
SDL_GL_SwapBuffers();
148
// Wait for 3 seconds to give us a chance to see the image
152
// Now we can delete the OpenGL texture and close down SDL
153
glDeleteTextures( 1, &texture );