Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sealfin/d22f4ba4d1022e1b89dd to your computer and use it in GitHub Desktop.
Save sealfin/d22f4ba4d1022e1b89dd to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
#include "seal_quadColours_triangles.h"
#include "seal_texture.h"
int main( const int argc, const char * const argv[] )
{
const int W = 320, H = 240;
SDL_Init( SDL_INIT_VIDEO );
/* Request a stencil buffer of at least 1bit per pixel. */
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1 );
SDL_Window *window = SDL_CreateWindow( "SDL2 & OpenGL (stencil buffer)", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, W, H, SDL_WINDOW_OPENGL );
SDL_GLContext context = SDL_GL_CreateContext( window );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, W, 0, H, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glClearColor( 0, 0, 0, 1 );
glEnable( GL_STENCIL_TEST );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
seal_QuadColours_Triangles quadColours( 1, 1, 1, 1 );
seal_Texture *texture = new seal_Texture( "texture.png", 128, 128 );
seal_Quad_Triangles geometryQuad( W / 2 - 64, H / 2 - 64, 128, 128 ), textureQuad;
texture->p_GetSubTextureQuad( 0, &textureQuad );
seal_Quad_Triangles maskGeometry;
maskGeometry.p_SetTopLeftCorner( W / 2, H / 2 + 64 ); // Top
maskGeometry.p_SetTopRightCorner( W / 2 + 64, H / 2 ); // Right
maskGeometry.p_SetBottomRightCorner( W / 2, H / 2 - 64 ); // Bottom
maskGeometry.p_SetBottomLeftCorner( W / 2 - 64, H / 2 ); // Left
seal_QuadColours_Triangles maskColour( 1, 1, 1, 1 );
bool quit = false;
do
{
SDL_Event event;
while( SDL_PollEvent( &event ))
if( event.type == SDL_QUIT )
quit = true;
glClear( GL_STENCIL_BUFFER_BIT );
glStencilFunc( GL_ALWAYS, 1 , 0xffffffff );
glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
glDisable( GL_TEXTURE_2D );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glVertexPointer( 2, GL_FLOAT, 0, maskGeometry.m_coordinates );
glColorPointer( 4, GL_FLOAT, 0, maskColour.m_rgba );
glDrawArrays( GL_TRIANGLES, 0, 6 );
glStencilFunc( GL_EQUAL, 1, 0xffffffff );
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
glEnable( GL_TEXTURE_2D );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glClear( GL_COLOR_BUFFER_BIT );
glVertexPointer( 2, GL_FLOAT, 0, geometryQuad.m_coordinates );
glColorPointer( 4, GL_FLOAT, 0, quadColours.m_rgba );
glTexCoordPointer( 2, GL_FLOAT, 0, textureQuad.m_coordinates );
texture->p_Bind();
glDrawArrays( GL_TRIANGLES, 0, 6 );
SDL_GL_SwapWindow( window );
}
while( !quit );
delete texture;
SDL_GL_DeleteContext( context );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment