Last active
November 20, 2020 14:10
-
-
Save CaptainHandyman/74c2659337bb8b21efa08e4042063d7d to your computer and use it in GitHub Desktop.
SDL2 + OpenGL + ES3 - Triangle
This file contains 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 <GLES3/gl3.h> | |
#include <SDL2/SDL.h> | |
struct window_data { | |
const unsigned int width = 800, height = 600, x = SDL_WINDOWPOS_CENTERED, | |
y = SDL_WINDOWPOS_CENTERED; | |
SDL_Color fill_color; | |
} window_data; | |
SDL_Window *window; | |
SDL_Event event; | |
SDL_GLContext gl_context; | |
void init_GL() { | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
gl_context = SDL_GL_CreateContext(window); | |
SDL_GL_SetSwapInterval(SDL_FALSE); | |
} | |
int main() { | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", | |
"Failed to initialize video!", NULL); | |
return EXIT_FAILURE; | |
} | |
window = SDL_CreateWindow("window", window_data.x, window_data.y, | |
window_data.width, window_data.height, | |
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); | |
init_GL(); | |
GLuint VertexArrayID; | |
glGenVertexArrays(1, &VertexArrayID); | |
glBindVertexArray(VertexArrayID); | |
static const GLfloat vertex_buffer_data[] = { | |
-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, | |
}; | |
GLuint vertex_buffer; | |
glGenBuffers(1, &vertex_buffer); | |
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), | |
vertex_buffer_data, GL_STATIC_DRAW); | |
while (window != NULL) { | |
if (SDL_WaitEvent(&event)) { | |
if (event.type == SDL_QUIT) | |
window = NULL; | |
} | |
glClearColor(window_data.fill_color.r, window_data.fill_color.g, | |
window_data.fill_color.b, window_data.fill_color.a); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glDisableVertexAttribArray(0); | |
SDL_GL_SwapWindow(window); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment