Last active
November 1, 2024 00:07
-
-
Save gcatlin/45bc4ae4a6f099e8aed292f8c7fd2a7a to your computer and use it in GitHub Desktop.
Minimal C SDL2 OpenGL example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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