Skip to content

Instantly share code, notes, and snippets.

@gcatlin
Last active September 2, 2023 17:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gcatlin/45bc4ae4a6f099e8aed292f8c7fd2a7a to your computer and use it in GitHub Desktop.
Save gcatlin/45bc4ae4a6f099e8aed292f8c7fd2a7a to your computer and use it in GitHub Desktop.
Minimal C SDL2 OpenGL example
//
// cc main.c glad.c -lSDL2
//
#include "glad.h" // https://glad.dav1d.de/
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <stdbool.h>
int main()
{
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window *window = SDL_CreateWindow("SDL OpenGL", -1, -1, 1280, 720, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
switch (e.type) {
case SDL_QUIT: quit = true; break;
}
}
glClearColor(0.3, 0.3, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment