Skip to content

Instantly share code, notes, and snippets.

@mikesart
Created August 29, 2018 23:40
Show Gist options
  • Save mikesart/038fa085c5738a15b27015145d08c5f3 to your computer and use it in GitHub Desktop.
Save mikesart/038fa085c5738a15b27015145d08c5f3 to your computer and use it in GitHub Desktop.
sdl opengl gamma
// gcc:
// g++ sdl2_opengl.cpp -lSDL2 -lGL -o sdl2_opengl
#include <stdint.h>
#include <stdio.h>
#include <GL/gl.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
int main( int argc, char **argv )
{
uint32_t WindowFlags =
SDL_WINDOW_OPENGL |
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_ALLOW_HIGHDPI;
SDL_GL_SetAttribute( SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG );
// SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 2 );
SDL_GL_SetAttribute( SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0 );
SDL_Window *window = SDL_CreateWindow( "OpenGL Window",
0, 0, 600, 200, WindowFlags );
SDL_GLContext Context = SDL_GL_CreateContext( window );
int quit = 0;
while ( !quit )
{
SDL_Event Event;
while ( SDL_PollEvent( &Event ) )
{
if ( Event.type == SDL_KEYDOWN )
{
switch ( Event.key.keysym.sym )
{
case SDLK_ESCAPE:
quit = 1;
break;
default:
break;
}
}
else if ( Event.type == SDL_QUIT )
{
quit = 1;
}
}
int w, h;
SDL_GetWindowSize( window, &w, &h );
glViewport( 0, 0, w, h );
glClearColor( 1.f, 0.f, 1.f, 0.f );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 100.0, 100.0, 0.0, -1.0, 1.0 );
for ( int pass = 0; pass < 2; pass++ )
{
float x = 1;
float y = 5 + 45 * pass;
float exp = pass ? ( 1.0f / 2.2f ) : 1.0f;
for ( float c = 0.0f; c <= 1.0f; c += 0.1f )
{
float color = pow( c, exp );
glColor3f( color, color, color );
glBegin( GL_POLYGON );
glVertex3f( x, y, 0.0 );
glVertex3f( x + 8, y, 0.0 );
glVertex3f( x + 8, y + 40, 0.0 );
glVertex3f( x, y + 40, 0.0 );
glEnd();
x += 10;
}
}
SDL_GL_SwapWindow( window );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment