I'm trying to use SDL + OpenGL and do the drawing stuff inside a thread. But the app crashes whenever I call some OpenGL routine inside a thread. However it works fine if it's done in the main func. Like this:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
int width = 480;
int height = 272;
int main( int argc, char *argv[] )
{
SDL_Init (SDL_INIT_VIDEO);
if (!SDL_SetVideoMode( width, height, 32, SDL_OPENGL )) {
fprintf (stdout, "Failed to create window\n");
SDL_Quit( );
return 0;
}
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/height, 1, 10);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
static int rotation = 0;
float bheight;
bheight = 3.0f;
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// Clear buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
// Spin round a bit
glRotatef(rotation++, 0, 0, 1);
// Draw bar
glBegin(GL_POLYGON);
glVertex3f(-0.1, -1, 0);
glVertex3f(0.1, -1, 0);
glVertex3f(0.1, bheight, 0);
glVertex3f(-0.1, bheight, 0);
glEnd();
glFinish();
SDL_GL_SwapBuffers();
SDL_Delay( 5000 );
SDL_Quit( );
return 0;
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <GL/gl.h>
#include <GL/glu.h>
SDL_Thread *thread = NULL;
int width = 480;
int height = 272;
int func( void *data )
{
while (1) {
static int rotation = 0;
float bheight;
bheight = 3.0f;
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// Clear buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
// Spin round a bit
glRotatef(rotation++, 0, 0, 1);
// Draw bar
glBegin(GL_POLYGON);
glVertex3f(-0.1, -1, 0);
glVertex3f(0.1, -1, 0);
glVertex3f(0.1, bheight, 0);
glVertex3f(-0.1, bheight, 0);
glEnd();
glFinish();
SDL_GL_SwapBuffers();
SDL_Delay(1000 / 70);
}
}
int main( int argc, char *argv[] )
{
SDL_Init (SDL_INIT_VIDEO);
if (!SDL_SetVideoMode( width, height, 32, SDL_OPENGL )) {
fprintf (stdout, "Failed to create window\n");
SDL_Quit( );
return 0;
}
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/height, 1, 10);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
thread = SDL_CreateThread( func, NULL );
SDL_Delay( 5000 );
SDL_Quit( );
return 0;
}
Sayounara