-
-
Save exavolt/2360410 to your computer and use it in GitHub Desktop.
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
void Display_InitGL() | |
{ | |
/* Enable smooth shading */ | |
glShadeModel( GL_SMOOTH ); | |
/* Set the background black */ | |
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); | |
/* Depth buffer setup */ | |
glClearDepth( 1.0f ); | |
/* Enables Depth Testing */ | |
glEnable( GL_DEPTH_TEST ); | |
/* The Type Of Depth Test To Do */ | |
glDepthFunc( GL_LEQUAL ); | |
/* Really Nice Perspective Calculations */ | |
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); | |
} | |
/* function to reset our viewport after a window resize */ | |
int Display_SetViewport( int width, int height ) | |
{ | |
/* Height / width ration */ | |
GLfloat ratio; | |
/* Protect against a divide by zero */ | |
if ( height == 0 ) { | |
height = 1; | |
} | |
ratio = ( GLfloat )width / ( GLfloat )height; | |
/* Setup our viewport. */ | |
glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height ); | |
/* change to the projection matrix and set our viewing volume. */ | |
glMatrixMode( GL_PROJECTION ); | |
glLoadIdentity( ); | |
/* Set our perspective */ | |
gluPerspective( 45.0f, ratio, 0.1f, 100.0f ); | |
/* Make sure we're chaning the model view and not the projection */ | |
glMatrixMode( GL_MODELVIEW ); | |
/* Reset The View */ | |
glLoadIdentity( ); | |
return 1; | |
} | |
void Display_Render() | |
{ | |
/* Set the background black */ | |
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); | |
/* Clear The Screen And The Depth Buffer */ | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
/* Move Left 1.5 Units And Into The Screen 6.0 */ | |
glLoadIdentity(); | |
glTranslatef( -1.5f, 0.0f, -6.0f ); | |
glBegin( GL_TRIANGLES ); /* Drawing Using Triangles */ | |
glVertex3f( 0.0f, 1.0f, 0.0f ); /* Top */ | |
glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left */ | |
glVertex3f( 1.0f, -1.0f, 0.0f ); /* Bottom Right */ | |
glEnd( ); /* Finished Drawing The Triangle */ | |
/* Move Right 3 Units */ | |
glTranslatef( 3.0f, 0.0f, 0.0f ); | |
glBegin( GL_QUADS ); /* Draw A Quad */ | |
glVertex3f( -1.0f, 1.0f, 0.0f ); /* Top Left */ | |
glVertex3f( 1.0f, 1.0f, 0.0f ); /* Top Right */ | |
glVertex3f( 1.0f, -1.0f, 0.0f ); /* Bottom Right */ | |
glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left */ | |
glEnd( ); /* Done Drawing The Quad */ | |
SDL_RenderPresent(displayRenderer); | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window* displayWindow; | |
SDL_Renderer* displayRenderer; | |
SDL_RendererInfo displayRendererInfo; | |
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer); | |
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo); | |
/*TODO: Check that we have OpenGL */ | |
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 || | |
(displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) { | |
/*TODO: Handle this. We have no render surface and not accelerated. */ | |
} | |
Display_InitGL(); | |
Display_SetViewport(800, 600); | |
Display_Render(); | |
SDL_Delay(5000); | |
SDL_Quit(); | |
return 0; | |
} |
Yea...I pushed the renderer and window variables into the global mem, and used a function by NEHE to replace the perspective function: http://nehe.gamedev.net/article/replacement_for_gluperspective/21002/
for osx 10.9/xcode 5....
sdl2 framework, gl framework, glut framework
line 9: #include OpenGL/glu.h
replace line 63 w/void Display_Render(SDL_Renderer* displayRenderer)
line 114: Display_Render(displayRenderer);
You don't really need a renderer in SDL2.
line 90: SDL_GL_SwapWindow(displayWindow);
line 101: displayWindow = SDL_CreateWindow("", 0, 0, width, height, SDL_WINDOW_OPENGL);
Delete everywhere else the renderer appears.
Here is a working example for MacOSX and Ubuntu:
https://gist.github.com/dirkk0/cad259e6a3965abb4178
... and for Win :)
SDL2 + OpenGL with event polling (get rid of delay) https://gist.github.com/haxpor/8cd82700b5b6d0a40702901c6bbd6757
For anyone else who finds this and has trouble, I got it working - you need to create an OpenGL context too. After creating the window + renderer, call SDL_GL_CreateContext() passing the window as a parameter. Then, once you've drawn what you want with OpenGL commands, you need to use SDL_GL_SwapWindow().