Skip to content

Instantly share code, notes, and snippets.

@jcreedcmu
Last active December 28, 2022 16:49
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 jcreedcmu/a0876b0c25b894565fa7d029c5f2c069 to your computer and use it in GitHub Desktop.
Save jcreedcmu/a0876b0c25b894565fa7d029c5f2c069 to your computer and use it in GitHub Desktop.
minimal sdl2/opengl example
// g++ -I/usr/local/include/SDL2 -I/usr/include/GL b.cc -lSDL2 -lGL -lGLU -o b
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <gl.h>
static SDL_Window *window;
static SDL_Renderer *renderer;
#define width 640
#define height 480
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("sdl opengl test", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
SDL_GLContext context = SDL_GL_CreateContext(window);
for (;;) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
case SDL_KEYDOWN:
goto done;
}
}
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1, 0.5, 0.5);
glRectf(-0.9, -0.9, 0.9, 0.9);
SDL_GL_SwapWindow(window);
SDL_Delay(1);
}
done:
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