Skip to content

Instantly share code, notes, and snippets.

@44100hertz
Created April 1, 2017 04:19
Show Gist options
  • Save 44100hertz/21f62ef10c130dea8b3ffb6540ee61c3 to your computer and use it in GitHub Desktop.
Save 44100hertz/21f62ef10c130dea8b3ffb6540ee61c3 to your computer and use it in GitHub Desktop.
Basic SDL + OpenGL rainbow example
#include <SDL2/SDL.h>
#include <GL/glu.h>
#include <cmath>
const float OFF_R = 0;
const float OFF_G = M_PI * 0.75;
const float OFF_B = M_PI * 1.5;
// Doesn't work like a normal hue function; uses sine curve
float toHue(float f, float offset)
{
return sinf((f+1.0)/2 + offset);
}
int main()
{
auto sdl_context = SDL_Init( SDL_INIT_VIDEO );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
SDL_GL_SetSwapInterval( 1 );
auto window = SDL_CreateWindow(
"SDL Rainbow",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
);
auto context = SDL_GL_CreateContext( window );
for (float i=0; i < M_PI * 20; i += M_PI / 20)
{
float r = toHue(i, OFF_R);
float g = toHue(i, OFF_G);
float b = toHue(i, OFF_B);
glClearColor( r, g, b, 1 );
glClear( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow( window );
}
SDL_GL_DeleteContext( context );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment