#include <glad/gl.h>
#include <GL/freeglut.h>

#include <cstdio>

void display() {
    glClearColor(0.1f, 0.3f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glutSwapBuffers();
}

void reshape(int width, int height) {
    if (width > 0 && height > 0) glViewport(0, 0, width, height);
}

void keyboard(unsigned char key, int, int) {
    if (key == 27) glutLeaveMainLoop();
    else glutPostRedisplay();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitContextVersion(3, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);

    int window = glutCreateWindow("RefDock freeglut");
    if (window <= 0) return 1;
    if (!gladLoadGL(reinterpret_cast<GLADloadfunc>(glutGetProcAddress)) ||
        !GLAD_GL_VERSION_3_3) {
        std::fprintf(stderr, "OpenGL 3.3 initialization failed\n");
        glutDestroyWindow(window);
        return 1;
    }

    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

    glutMainLoop();

    // No GL objects are created. A closed window may already have lost its context.
    return 0;
}
