Skip to content

Instantly share code, notes, and snippets.

@exavolt
Created April 11, 2012 16:36
Show Gist options
  • Save exavolt/2360410 to your computer and use it in GitHub Desktop.
Save exavolt/2360410 to your computer and use it in GitHub Desktop.
Very basic SDL2 OpenGL application
#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;
}
@sergnechaev
Copy link

@haxpor
Copy link

haxpor commented Sep 24, 2016

SDL2 + OpenGL with event polling (get rid of delay) https://gist.github.com/haxpor/8cd82700b5b6d0a40702901c6bbd6757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment