2/21
1.1. 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
Consider the code directory 01_create_window.
The actual source code is in src/ directory. It consists in a single file main.cpp.
#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;
}
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.
Note that 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\) black window, with title My Window.
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.
Note for full-screen application
If you want to create a full-screen application, you may use the following syntax when creating the window (code, main.cpp)
const auto monitor = glfwGetPrimaryMonitor();
const auto mode = glfwGetVideoMode(monitor);
auto window = glfwCreateWindow(mode->width, mode->height, "My Window", monitor, nullptr);
(Remember you can exit the application using ALT+F4 touch combination).