#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main(void){
    // GLFW init
    if (!glfwInit()) {
        std::cout << "GLFW could not be initialized!" << std::endl;
        return -1;
    }    

    //Main window creation
    GLFWwindow* window;

    window = glfwCreateWindow(640, 480, "OpenGl practice", NULL, NULL);
    if (!window){
        std::cout << "Window could not be created!" << std::endl;
        glfwTerminate();
        return -1;
    }
   
    glfwMakeContextCurrent(window);

    //GLEW init
    if (glewInit() != GLEW_OK) {
        std::cout << "GLEW Error" << std::endl;
        return -1;
    }

    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;

    //vertex positions
    float positions[6] = {
        -0.5f, -0.5f,  //Vertex2f
         0.0f,  0.5f,  //Vertex2f
         0.5f, -0.5f   //Vertex2f
    };

    //Vertex buffer(s)
    unsigned int buffer;
    glGenBuffers(1, &buffer); //number of buffer to generate
    glBindBuffer(GL_ARRAY_BUFFER, buffer); //binding the buffer
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0); //unbinding the buffer

   //Main loop
    while (!glfwWindowShouldClose(window)){
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3); //without index buffer 

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}