2/14

2. Creating a Window

An essential step of a graphical program is to be able to show a window where the graphics will be displayed. Creating a window is, in large part, independent of OpenGL or the way to render graphics. However, in order to be able to efficiently interact and synchronize with an OpenGL application, this window should be compatible with a so-called OpenGL Context.

Creating a window from scratch is a complex operation heavily depending on the operating system. In most case, we use external cross-platform libraries to handle this step.

In our case, we will use the library GLFW. GLFW is a lightweight C library able to create window compatible with an OpenGL context. GLFW can also handle user events such as the use of the keyboard and mouse.

Note that there exists several other Window Toolkits able to handle OpenGL context. .
GLFW has the advantage of being simple and lightweight to setup and use. It focuses on Window creation and event handling, and let a lot of freedom to adapt the code.
At the opposite, Qt, or GTK+ are much larger libraries that you may commonly encounter. These libraries provide widgets and various helper coding tools, while still being able to handle OpenGL context. However, using such library will generally force you to follow their respective framework.

Code

Short code version

Consider the code directory 01_create_window.

The actual source code is in src/ directory. It consists in a single file main.cpp.

[src]

#include <GLFW/glfw3.h>

int main()
{
    // Initialize GLFW - library handling the window
    glfwInit();

    // Create a new Window
    GLFWwindow* window = glfwCreateWindow(500, 500, "My Window", nullptr, nullptr);

    // Loop until receiving closing signal
    while( !glfwWindowShouldClose(window) ) {
        glfwSwapBuffers(window); // Double buffering (will be used to avoid flickering when animating a scene)
        glfwPollEvents();        // Handle GLFW events (ex. mouse clicks, etc)
    }

    glfwTerminate(); // Close the GLFW Window

    return 0;
}

Compiling and executing the code

The other files provide different ways to compile this code, and the README file remind the way to use them using a command line in a Unix system.

  • Makefile: The basic configuration file to automatize compilation using make.

  • cmake/CMakeLists.txt: The configuration file for the CMake tool. This file can be used to automatically generate a Makefile, or can also be used to open project in IDE such as QtCreator. Note that the CMakeLists.txt is set in an external directory to avoid erasing the provided Makefile.

  • meson.build: The configuration file for the Meson build system. Note that using Meson will generate by default a compilation file for ninja, replacing make. (not available on computer lab machine)

Note if you work on your personnal machine: In all case, you should have GLFW installed on your system. On Linux system, GLFW is typically available using the common package system under the name glfw3.

Ex. on ubuntu system

$ sudo apt-get install libglfw3-dev
  • Compile and execute the code. You should obtain a 500px \(\times\) 500px window, with title My Window.

Note that the content of the window is not defined, thus its actual content may depends on your OS and is not relevant (black window, copy of the background, arbitrary looking colors, etc).

Fully commented code

The fully commented corresponding code is the following

// Include for GLFW headers - library handling the window
#include <GLFW/glfw3.h>

int main()
{
    // Initialize GLFW
    //  This step is required before creating the window
    glfwInit();

    // Create a new Window of size 500x500 with title "My Window"
    // The two last parameters are
    //   - the monitor for full screen mode (or nullptr for windowed mode in this case)
    //   - another pointer to a window in the case where ressources are shared (or nullptr to not share ressources).
    // The function returns a pointer to the newly created window (or nullptr in case of error).
    GLFWwindow* window = glfwCreateWindow(500, 500, "My Window", nullptr, nullptr);

    // User defined loop
    // This loop will be active until the user close the window
    while( !glfwWindowShouldClose(window) ) {
        glfwSwapBuffers(window); // Double buffering (will be used to avoid flickering when animating a scene)
        glfwPollEvents();        // Handle GLFW events (ex. mouse clicks, etc)
    }

    glfwTerminate(); // Close the GLFW Window

    return 0;
}
  • Try to adapt the size of the window, and change its title. Note that more information on Window creation can be found in the official GLFW window creation doc.