I'm following the examples from the book Beginning OpenGL Game Programming, Second Edition and I'm trying to get the first example, that utilizes the Kazmath library for matrix stack functionality, to work. The example runs fine with no rendering problems, but when the execution reaches the SDL_Quit function, the program reports a segmentation fault. The error occurs, if I execute kmGLMatrixMode(KM_GL_PROJECTION) or kmGLLoadIdentity() during execution. The error does not occur if no Kazmath code is executed. My hunch is that it has something to do with the threads the library uses. The line src/main.cpp:107 refers to SDL_Quit().
Does the Kazmath library require some sort of de-initialization to shutdown properly?
I am building this project on Ubuntu 16.10 with g++ compiler, SDL version 2.04.
Sorry if this is the wrong board for this sort of technical help.
GDB error log:
Kazmath code:
https://github.com/Kazade/kazmath/blob/master/kazmath/GL/matrix.c
main.cpp
#include "SDL2/SDL.h"
#include "example.h"
int main(int argc, char** argv)
{
//Set our window settings
const int windowWidth = 1024;
const int windowHeight = 768;
const int windowBPP = 16;
const int windowFullscreen = false;
SDL_Window* gWindow = NULL;
SDL_GLContext gContext;
bool quit = false;
SDL_Event e;
float prevTime = 0;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not be initialized! SDL Error: %s\n", SDL_GetError() );
}
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowWidth, windowHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if(gWindow == NULL)
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
}
//Use OpenGL 2.1
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
//~ SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
gContext = SDL_GL_CreateContext(gWindow);
if(gContext == NULL)
{
printf( "Context could not be created! SDL Error: %s\n", SDL_GetError() );
}
//The example OpenGL code
Example example;
//Attach our example to our window
if (!example.init()) //Initialize our example
{
return 1;
}
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Handle keypress with current mouse position
else if( e.type == SDL_TEXTINPUT )
{
//int x = 0, y = 0; SDL_GetMouseState( &x, &y );
//handleKeys( e.text.text[ 0 ], x, y );
}
else if(e.type == SDL_KEYDOWN)
{
if(e.key.keysym.sym == SDLK_ESCAPE)
{
quit = true;
}
}
else if (e.type == SDL_WINDOWEVENT)
{
if(e.window.event == SDL_WINDOWEVENT_RESIZED &&
e.window.windowID == SDL_GetWindowID(gWindow))
{
example.onResize(e.window.data1, e.window.data2);
}
}
}
float currentTime = SDL_GetTicks() / 1000.0f;
example.prepare(currentTime - prevTime);
prevTime = currentTime;
example.render();
//~ //Update screen
SDL_GL_SwapWindow( gWindow );
}
example.shutdown(); //Free any resources
SDL_GL_DeleteContext(gContext);
SDL_DestroyWindow(gWindow);
SDL_Quit();
return 0; //Return success
}
example.cpp (Partial)
void Example::onResize(int width, int height)
{
//~ printf("onResize");
glViewport(0, 0, width, height);
kmGLMatrixMode(KM_GL_PROJECTION);
kmGLLoadIdentity();
//~ gluPerspective(52.0f, float(width) / float(height), 1.0f, 1000.0f);
kmMat4 perspective;
kmMat4PerspectiveProjection(&perspective, 52.0f, float(width) / float(height), 1.0f, 1000.0f);
kmGLMultMatrix(&perspective);
//~ glMatrixMode(GL_PROJECTION);
//~ glLoadIdentity();
//~ glMultMatrixf(perspective.mat);
//~ glMatrixMode(GL_MODELVIEW);
kmGLMatrixMode(KM_GL_MODELVIEW);
kmGLLoadIdentity();
}