Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Opengl GLFW basic window

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }    

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: javidx9 age 
Cpp :: linq select where string equals "String" 
Cpp :: viewlist exaple win32 
Cpp :: second smallest element in array using one loop 
Cpp :: c++ over load oprator to print variable of clas 
Cpp :: passing reference to thread c++ 
Cpp :: Missing GL version 
Cpp :: ue4 c++ enum variable declaration 
Cpp :: C++ Automatic Conversion from double to int 
Cpp :: Polycarp found a rectangular table consisting of n rows and m columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": codeforces solution 
Cpp :: variadic template constructor matches better than copy constructor 
Cpp :: how to use run total in C++ 
Cpp :: comment installer boost c++ sur windows 
Cpp :: std::ifstream cant read file to large 
Cpp :: how are c++ references implemented 
Cpp :: segment tree lazy propogation 
Cpp :: output sum of a range 
Cpp :: c++ comments 
Cpp :: Accepting multiple inputs on the SAME LINE C++ 
Cpp :: 136. Single Number leetcode solution in c++ 
Cpp :: std::hash 
Cpp :: can you use rand to read in from an external file inc++ 
Cpp :: multiple objects in vector C++ 
Cpp :: c++ void to avoid functions 
Cpp :: 1491. Average Salary Excluding the Minimum and Maximum Salary leetcode solution in c++ 
Cpp :: floating point exception 
Cpp :: 2d stl array 
Cpp :: lambda - print-out array and add comment 
Cpp :: QMetaObject_invokeMethod 
Cpp :: c++ cin accept only numbers 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =