Last active
June 10, 2025 12:15
-
-
Save sherjilozair/ac2ac5e3002b6f3becaef214ebe3cd7a to your computer and use it in GitHub Desktop.
Minimal SDL2 + OpenGL3 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
| #include <SDL2/SDL.h> | |
| #ifdef __APPLE__ | |
| #include <OpenGL/gl3.h> | |
| #include <OpenGL/gl3ext.h> | |
| #else | |
| #include <GL/gl.h> | |
| #endif | |
| #include <stdbool.h> | |
| #define CODE(...) #__VA_ARGS__ | |
| int main(){ | |
| unsigned int width = 320 * 4; | |
| unsigned int height = 180 * 4; | |
| // create window | |
| SDL_Init(SDL_INIT_VIDEO); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
| SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); | |
| SDL_Window* window = SDL_CreateWindow("Modern Opengl", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0); | |
| SDL_GLContext context = SDL_GL_CreateContext(window); | |
| SDL_GL_SetSwapInterval(1); | |
| glViewport(0, 0, width, height); | |
| // triangle data | |
| float vertices[] = { | |
| -0.5f, -0.5f, 0.0f, | |
| 0.5f, -0.5f, 0.0f, | |
| 0.0f, 0.5f, 0.0f | |
| }; | |
| // create a vertex array object | |
| unsigned int vao; | |
| glGenVertexArrays(1, &vao); | |
| glBindVertexArray(vao); | |
| // create a vertex buffer object | |
| unsigned int vbo; | |
| glGenBuffers(1, &vbo); | |
| glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
| // add the vertex data to the vertex buffer | |
| glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
| glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), NULL); | |
| glEnableVertexAttribArray(0); | |
| // vertex shader | |
| const GLchar* vs_code = "#version 330 core\n" CODE( | |
| layout (location = 0) in vec3 aPos; | |
| void main(){ | |
| gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); | |
| } | |
| ); | |
| unsigned int vs = glCreateShader(GL_VERTEX_SHADER); | |
| glShaderSource(vs, 1, &vs_code, NULL); | |
| glCompileShader(vs); | |
| int success; | |
| char infolog[512]; | |
| glGetShaderiv(vs, GL_COMPILE_STATUS, &success); | |
| if(!success){ | |
| glGetShaderInfoLog(vs, 512, NULL, infolog); | |
| printf("%s\n", infolog); | |
| } | |
| // fragment shader | |
| const GLchar* fs_code = "#version 330 core\n" CODE( | |
| out vec4 color; | |
| void main() | |
| { | |
| color = vec4(1.0f, 0.5f, 0.2f, 1.0f); | |
| } | |
| ); | |
| unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER); | |
| glShaderSource(fs, 1, &fs_code, NULL); | |
| glCompileShader(fs); | |
| glGetShaderiv(fs, GL_COMPILE_STATUS, &success); | |
| if(!success){ | |
| glGetShaderInfoLog(fs, 512, NULL, infolog); | |
| printf("%s\n", infolog); | |
| } | |
| // create program | |
| unsigned int program = glCreateProgram(); | |
| glAttachShader(program, vs); | |
| glAttachShader(program, fs); | |
| glLinkProgram(program); | |
| glGetProgramiv(program, GL_LINK_STATUS, &success); | |
| if(!success){ | |
| glGetProgramInfoLog(program, 512, NULL, infolog); | |
| printf("%s\n", infolog); | |
| } | |
| glDeleteShader(vs); | |
| glDeleteShader(fs); | |
| // start render loop | |
| bool quit = false; | |
| while(!quit){ | |
| SDL_Event event; | |
| while(SDL_PollEvent(&event)){ | |
| if(event.type == SDL_QUIT) | |
| quit = true; | |
| } | |
| glClearColor(1.0, 0.3, 0.3, 1.0); | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| glUseProgram(program); | |
| glBindVertexArray(vao); | |
| glDrawArrays(GL_TRIANGLES, 0, 3); | |
| SDL_GL_SwapWindow(window); | |
| } | |
| glDeleteProgram(program); | |
| 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