Skip to content

Instantly share code, notes, and snippets.

@dbasden
Created February 20, 2020 10:47
Show Gist options
  • Save dbasden/cf3cb4cdcf21cf3a54f22ce9f54d5492 to your computer and use it in GitHub Desktop.
Save dbasden/cf3cb4cdcf21cf3a54f22ce9f54d5492 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdint>
#include <GL/glew.h>
#include "SDL.h"
#define INITIAL_WINDOW_WIDTH 600
#define INITIAL_WINDOW_HEIGHT 600
static inline void bailSDL(char *msg) {
std::cerr << msg << " : SDL Error: " << SDL_GetError() << std::endl;
SDL_Quit();
}
static SDL_Window * setupSDL() {
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return bailSDL("Accidentally the entire SDL_Init"), NULL;
SDL_Window *sdlWin = SDL_CreateWindow("goddamnit conan",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
INITIAL_WINDOW_WIDTH, INITIAL_WINDOW_HEIGHT,
SDL_WINDOW_OPENGL);
if (sdlWin == NULL)
return bailSDL("SDL_CreateWindow()"), NULL;
return sdlWin;
}
void initGL(SDL_GLContext gl, SDL_Window* sdlWin) {
glClearColor(0, 0, 1, 1);
}
void updateGL(SDL_GLContext gl, SDL_Window* sdlWin) {
int w, h;
SDL_GetWindowSize(sdlWin, &w, &h);
glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc, char* argv[]) {
std::cout << "Hello, World!" << std::endl;
SDL_Window* sdlWindow = setupSDL();
SDL_GLContext gl = SDL_GL_CreateContext(sdlWindow);
initGL(gl, sdlWindow);
bool running = true;
while (running) {
SDL_Event ev;
// Empty the SDL event queue before talking to the GL
//
// Because that will never bite us in the ass or stall frames
// *facepalm*
while (SDL_PollEvent(&ev) && running)
{
if (ev.type == SDL_KEYDOWN)
{
switch (ev.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
running = false;
break;
default:
break;
}
}
else if (ev.type == SDL_QUIT)
{
running = false;
}
}
if (running) {
updateGL(gl, sdlWindow);
SDL_GL_SwapWindow(sdlWindow);
}
}
SDL_Quit();
std::cout << "I don't believe that worked." << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment