/*******************************************************************1* *2* Using SDL With OpenGL *3* *4* Tutorial by Kyle Foley (sdw) *5* *6* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *7* *8*******************************************************************/910/*11THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION12AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.1314THE ORIGINAL AUTHOR IS KYLE FOLEY.1516THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY17OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF18MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,19ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE20RESULTING FROM THE USE, MODIFICATION, OR21REDISTRIBUTION OF THIS SOFTWARE.22*/2324#ifndef __EMSCRIPTEN__25#define USE_GLEW 026#endif2728#if USE_GLEW29#include "GL/glew.h"30#endif3132#include "SDL/SDL.h"33#include "SDL/SDL_image.h"34#if !USE_GLEW35#include "SDL/SDL_opengl.h"36#endif3738#include <stdio.h>39#include <string.h>40#include <assert.h>4142int main(int argc, char *argv[]) {43SDL_Surface *screen;4445// Slightly different SDL initialization46if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {47printf("Unable to initialize SDL: %s\n", SDL_GetError());48return 1;49}5051SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*5253screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*54if ( !screen ) {55printf("Unable to set video mode: %s\n", SDL_GetError());56return 1;57}5859// Set the OpenGL state after creating the context with SDL_SetVideoMode6061glClearColor( 0, 0, 0, 0 );6263#ifndef __EMSCRIPTEN__64glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL65#endif6667glViewport( 0, 0, 640, 480 );6869glMatrixMode( GL_MODELVIEW );70glLoadIdentity();7172// Clear the screen before drawing73glClear( GL_COLOR_BUFFER_BIT );7475typedef struct Vertex {76GLfloat x;77GLfloat y;78} Vertex;7980typedef struct Color {81GLubyte r;82GLubyte g;83GLubyte b;84GLubyte a;85} Color;8687Vertex vertices[3] = {88{-1.0, 0.0},89{ 0.0, 1.0},90{ 1.0, 0.0}91};9293Color colors[3] = {94{0xFF, 0x00, 0x00, 0xFF},95{0x00, 0xFF, 0x00, 0xFF},96{0x00, 0x00, 0xFF, 0xFF}97};9899Vertex vertices2[3] = {100{-1.0, 0.0},101{ 1.0, 0.0},102{ 0.0, -1.0}103};104105Color colors2[3] = {106{0xFF, 0x00, 0x00, 0xFF},107{0x00, 0x00, 0xFF, 0xFF},108{0x00, 0xFF, 0x00, 0xFF}109};110111// DRAW112113// Clear the screen before drawing114glClear( GL_COLOR_BUFFER_BIT );115116// This test ensures that we can use two separate arrays in memory for different117// attributes, and that they each can have different stride.118// The first test shows implicit striding (the zero indicates tightly packed)119// The second test shows explicit striding where the stride is passed in120// even though it also is tightly packed121122glEnableClientState(GL_VERTEX_ARRAY);123glEnableClientState(GL_COLOR_ARRAY);124125// TEST 1126127glVertexPointer(2, GL_FLOAT, 0, vertices);128glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);129glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);130131// TEST 2132133glVertexPointer(2, GL_FLOAT, 8, vertices2);134glColorPointer(4, GL_UNSIGNED_BYTE, 4, colors2);135glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);136137glDisableClientState(GL_COLOR_ARRAY);138glDisableClientState(GL_VERTEX_ARRAY);139140SDL_GL_SwapBuffers();141142#ifndef __EMSCRIPTEN__143// Wait for 3 seconds to give us a chance to see the image144SDL_Delay(3000);145#endif146147SDL_Quit();148149return 0;150}151152153