Skip to content

Commit bc4a7fb

Browse files
authored
example: adds SDL3 example (#532)
1 parent ff234d8 commit bc4a7fb

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Examples can be found [in the example directory](/example). Some examples:
6060
* [GL GLFW](example/c/gl_glfw.c)
6161
* [GL GLFW On-Demand loading](example/c/gl_glfw_on_demand.c)
6262
* [GL GLFW Multiple Windows/Contexts](example/c++/multiwin_mx/)
63+
* [GL SDL3](example/c/gl_sdl3.c)
6364
* [GL SDL2](example/c/gl_sdl2.c)
6465
* [Vulkan GLFW](example/c/vulkan_tri_glfw/)
6566
* [GLX](example/c/glx.c)

example/c/gl_sdl3.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)