Skip to content

Instantly share code, notes, and snippets.

@ja6z
Created March 19, 2016 04:44
Show Gist options
  • Save ja6z/af7f8992d48eb3cbfb77 to your computer and use it in GitHub Desktop.
Save ja6z/af7f8992d48eb3cbfb77 to your computer and use it in GitHub Desktop.
Attempt at combining nanovg and SDL2. Getting 'Error 00000500 after init' after creating the GL3 context in nanovg. Any ideas what's happening here?
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include "nanovg.h"
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg_gl.h"
#include "nanovg_gl_utils.h"
int sdl_init( SDL_Window **w );
void sdl_cleanup();
int main( int argc, char *argv[] )
{
SDL_Window *window = NULL;
if ( sdl_init( &window ) != 0 )
{
// Report SDL's error
printf( "Couldn't init SDL: %s\n", SDL_GetError() );
return 1;
}
struct NVGcontext *vg = NULL;
struct NVGLUframebuffer *fb = NULL;
printf( "Creating nanovg context\n" );
vg = nvgCreateGL3( 512, 512, NVG_ANTIALIAS|NVG_STENCIL_STROKES );
if ( !vg )
{
printf( "Couldn't create nanovg gl3 context\n" );
// Couldn't create context
sdl_cleanup();
return 2;
}
printf( "Creating nanovg framebuffer\n" );
fb = nvgluCreateFramebuffer( vg, 600, 600 );
if ( !fb )
{
// Couldn't create framebuffer
printf( "Couldn't create nanovg framebuffer\n" );
sdl_cleanup();
return 3;
}
printf( "Nanovg initialized!\n" );
int quit = 0;
float angle = 0.0f;
while ( quit == 0 )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
nvgBeginFrame( vg, 800, 600, pxRatio, NVG_STRAIGHT_ALPHA );
nvgResetTransform( vg );
nvgTranslate( vg, 300.0f, 300.0f );
//angle = fmod( angle + 0.1f, 360.0f );
//nvgRotate( vg, angle );
nvgBeginPath( vg );
nvgRect( vg, -100, -25, 200, 50 );
nvgStrokeColor( vg, nvgRGBA( 255, 0, 0, 100 ) );
nvgStrokeWidth( vg, 1.0f );
nvgStroke( vg );
nvgEndFrame( vg );
nvgluBindFramebuffer( NULL );
SDL_Event e;
while ( SDL_PollEvent( &e ) )
{
switch( e.type )
{
case SDL_KEYDOWN:
case SDL_KEYUP:
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
break;
case SDL_MOUSEMOTION:
break;
case SDL_MOUSEWHEEL:
break;
case SDL_QUIT:
quit = 1;
break;
}
}
SDL_GL_SwapWindow( window );
}
nvgluDeleteFramebuffer( vg, fb );
nvgDeleteGL3( vg );
sdl_cleanup();
return 0;
}
int sdl_init( SDL_Window **w )
{
if ( SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS ) == 0 )
{
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
//SDL_GL_SetAttribute( SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG ); // May be a performance booster in *nix?
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
*w = SDL_CreateWindow( "Mayhem v3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, SDL_WINDOW_OPENGL );
if ( *w )
{
SDL_GL_CreateContext( *w );
// Activate glew
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if ( err == GLEW_OK )
{
glEnable(GL_STENCIL_TEST);
return 0;
}
else
{
// Send GLEW error as an SDL Error
// SDL's error should be clear, since any previous error would stop further initialization
SDL_SetError( "GLEW Reported an Error: %s", glewGetErrorString(err) );
}
}
}
if ( *w ) SDL_DestroyWindow( *w );
*w = NULL;
return 1;
}
void sdl_cleanup()
{
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment