|
| 1 | +// gcc $(pkg-config --cflags --libs sdl3) -I${GLAD_PATH}/include example/c/gl_sdl3.c ${GLAD_PATH}/src/gl.c |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#include <glad/gl.h> |
| 6 | +#include <SDL3/SDL.h> |
| 7 | +#include <SDL3/SDL_main.h> |
| 8 | + |
| 9 | +const GLuint WIDTH = 800, HEIGHT = 600; |
| 10 | + |
| 11 | +int main(void) { |
| 12 | + // code without checking for errors |
| 13 | + SDL_Init(SDL_INIT_VIDEO); |
| 14 | + |
| 15 | + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 16 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 17 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); |
| 18 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 19 | + |
| 20 | + SDL_Window *window; |
| 21 | + SDL_Renderer *renderer; |
| 22 | + |
| 23 | + SDL_CreateWindowAndRenderer( |
| 24 | + "[glad] GL with SDL3", |
| 25 | + WIDTH, HEIGHT, |
| 26 | + SDL_WINDOW_OPENGL, |
| 27 | + &window, &renderer |
| 28 | + ); |
| 29 | + |
| 30 | + SDL_GLContext context = SDL_GL_CreateContext(window); |
| 31 | + |
| 32 | + int version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress); |
| 33 | + printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); |
| 34 | + |
| 35 | + int exit = 0; |
| 36 | + while(!exit) { |
| 37 | + SDL_Event event; |
| 38 | + while (SDL_PollEvent(&event)) { |
| 39 | + switch(event.type) { |
| 40 | + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: |
| 41 | + /* fallthrough */ |
| 42 | + case SDL_EVENT_QUIT: |
| 43 | + exit = 1; |
| 44 | + break; |
| 45 | + case SDL_EVENT_KEY_DOWN: |
| 46 | + if (event.key.key == SDLK_ESCAPE) { |
| 47 | + exit = 1; |
| 48 | + } |
| 49 | + break; |
| 50 | + default: |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + glClearColor(0.7f, 0.9f, 0.1f, 1.0f); |
| 56 | + glClear(GL_COLOR_BUFFER_BIT); |
| 57 | + |
| 58 | + SDL_GL_SwapWindow(window); |
| 59 | + SDL_Delay(1); |
| 60 | + } |
| 61 | + |
| 62 | + SDL_GL_DestroyContext(context); |
| 63 | + SDL_DestroyRenderer(renderer); |
| 64 | + SDL_DestroyWindow(window); |
| 65 | + SDL_Quit(); |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
0 commit comments