Skip to content

Instantly share code, notes, and snippets.

@Ancurio
Last active August 29, 2015 14:02
Show Gist options
  • Save Ancurio/001987a2e0b172a036b0 to your computer and use it in GitHub Desktop.
Save Ancurio/001987a2e0b172a036b0 to your computer and use it in GitHub Desktop.
#include <SDL.h>
#include <SDL_opengl.h>
typedef void (APIENTRYP PFNGLCLEARCOLORPROC) (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
typedef void (APIENTRYP PFNGLCLEARPROC) (GLbitfield mask);
struct
{
PFNGLCLEARCOLORPROC ClearColor;
PFNGLCLEARPROC Clear;
} gl;
struct ThreadData
{
SDL_Window *win;
volatile int done;
};
int thread_fun(void *_data)
{
struct ThreadData *data = _data;
SDL_GLContext ctx = SDL_GL_CreateContext(data->win);
gl.ClearColor = (PFNGLCLEARCOLORPROC) SDL_GL_GetProcAddress("glClearColor");
gl.Clear = (PFNGLCLEARPROC) SDL_GL_GetProcAddress("glClear");
float prog = 0.0;
while (!data->done)
{
if (prog < 1.0)
prog += 0.002;
gl.ClearColor(1.0-prog, 0, prog, 1.0);
gl.Clear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(data->win);
SDL_Delay(16);
}
SDL_GL_DeleteContext(ctx);
return 0;
}
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win = SDL_CreateWindow("Subthread GL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
struct ThreadData thread_data = { win, 0 };
SDL_Thread *thread = SDL_CreateThread(thread_fun, "", &thread_data);
SDL_Event e;
int done = 0;
while (!done)
{
if (!SDL_WaitEvent(&e))
break;
if (e.type == SDL_QUIT)
break;
}
thread_data.done = 1;
SDL_WaitThread(thread, 0);
SDL_DestroyWindow(win);
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment