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.
34
#include "SDL/SDL_image.h"
36
#include "SDL/SDL_opengl.h"
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());
61
// Set the OpenGL state after creating the context with SDL_SetVideoMode
63
glClearColor( 0, 0, 0, 0 );
66
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
69
glViewport( 0, 0, 640, 480 );
71
glMatrixMode( GL_MODELVIEW );
74
// Clear the screen before drawing
75
glClear( GL_COLOR_BUFFER_BIT );
77
typedef struct Vertex {
82
typedef struct Color {
89
Vertex vertices[3] = {
96
{0xFF, 0x00, 0x00, 0xFF},
97
{0x00, 0xFF, 0x00, 0xFF},
98
{0x00, 0x00, 0xFF, 0xFF}
101
Vertex vertices2[3] = {
108
{0xFF, 0x00, 0x00, 0xFF},
109
{0x00, 0x00, 0xFF, 0xFF},
110
{0x00, 0xFF, 0x00, 0xFF}
115
// Clear the screen before drawing
116
glClear( GL_COLOR_BUFFER_BIT );
118
// This test ensures that we can use two separate arrays in memory for different
119
// attributes, and that they each can have different stride.
120
// The first test shows implicit striding (the zero indicates tightly packed)
121
// The second test shows explicit striding where the stride is passed in
122
// even though it also is tightly packed
124
glEnableClientState(GL_VERTEX_ARRAY);
125
glEnableClientState(GL_COLOR_ARRAY);
129
glVertexPointer(2, GL_FLOAT, 0, vertices);
130
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
131
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
135
glVertexPointer(2, GL_FLOAT, 8, vertices2);
136
glColorPointer(4, GL_UNSIGNED_BYTE, 4, colors2);
137
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
139
glDisableClientState(GL_COLOR_ARRAY);
140
glDisableClientState(GL_VERTEX_ARRAY);
142
SDL_GL_SwapBuffers();
145
// Wait for 3 seconds to give us a chance to see the image