Skip to content

Instantly share code, notes, and snippets.

@newvertex
Created September 9, 2021 18:37
Show Gist options
  • Save newvertex/1c21e43f87ebe09e97249c21e92c2916 to your computer and use it in GitHub Desktop.
Save newvertex/1c21e43f87ebe09e97249c21e92c2916 to your computer and use it in GitHub Desktop.
basic SDL window with opengl context
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <iostream>
constexpr float R = 0.2f;
constexpr float G = 0.4f;
constexpr float B = 0.9f;
constexpr float A = 1.0f;
void sdlContext(int width, int height, const char *title)
{
std::cout << "SDL Window Creation\n";
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, 3);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_Window *window = SDL_CreateWindow(title,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width,
height,
SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
bool running{true};
SDL_Event event;
while (running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
running = false;
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:
running = false;
break;
default:
break;
}
}
}
glClearColor(R, G, B, A);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_Quit();
}
int main(int argc, char *argv[])
{
int width = 640;
int height = 480;
const char *title = "Hello World!";
sdlContext(width, height, title);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment