Skip to content

Instantly share code, notes, and snippets.

@CaptainHandyman
Last active November 20, 2020 14:10
Show Gist options
  • Save CaptainHandyman/1e130df7065bc684e68a82159e77714a to your computer and use it in GitHub Desktop.
Save CaptainHandyman/1e130df7065bc684e68a82159e77714a to your computer and use it in GitHub Desktop.
Simple SDL2 + OpenGL + ES3 - window
#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();
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);
SDL_GL_SwapWindow(window);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment