Skip to content
Antonie Blom edited this page Jul 8, 2013 · 1 revision

GLFW is a window manager/input library intended for use with OpenGL. It's been in development for a long time, and the latest stable version is GLFW 3. Pencil.Gaming has supported GLFW 3 since its alpha days, but now version 3 is stable enough to be used in large scale projects (provided that I will fix the x86 linux and osx versions...).

Initializing/terminating GLFW

NOTE: The code provided on this page requires you to reference either of the GLFW 3 assemblies provided by Pencil.Gaming, and a using Pencil.Gaming; at the top of your code file

In order for it to be used, GLFW has to be initialized. The initialization is very much the same as in previous versions:

if (!Glfw.Init()) {
    Console.Error.WriteLine("ERROR: Could not initialize GLFW, shutting down.");
    Environment.Exit(1);
}

The Glfw.Init() function initializes GLFW, along with returning a value indicating whether said initialization was successful. When the application is finished, it's probably a good idea to terminate GLFW as well. To ensure that GLFW is terminated, it may be wise to put the whole lot in a try...finally block:

try {
    if (!Glfw.Init()) {
        Console.Error.WriteLine("ERROR: Could not initialize GLFW, shutting down.");
        Environment.Exit(1);
    }

    // The rest of the main method here
} finally {
    Glfw.Terminate();
}

Error callback

NOTE: This functionality doesn't work flawlessly on windows yet, will be fixed shortly

GLFW 3 provides a way of telling the user when something goes wrong in a somewhat more descriptive way than just crashing the application. If you set an error callback, GLFW will tell you what went wrong when something doesn't go quite according to plan. Doing this is fairly easy:

Glfw.SetErrorCallback((code, des) => {
    Console.Error.WriteLine("ERROR ({0}): {1}", code, des);
});

This will ensure that the user will be informed of the error prior to closing the application.

Opening a window

Here lies another difference between GLFW 2 and GLFW 3: GLFW 2 only provided one window to be opened, whereas in GLFW 3 you can open a whole lot of windows, but for this you will need to store the GlfwWindowPtrs.

Opening a window is done using the GlfwWindowPtr CreateWindow(int width, int height, string title, GlfwMonitorPtr monitor, GlfwWindowPtr share); function. All parameters should speak for themselves, except for maybe the last ones. We won't make use of them in this tutorial, so we won't go into further detail right now.

An example of usage of this function might be as such:

GlfwWindowPtr window = Glfw.CreateWindow(800, 600, "GLFW 3 Window", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
if (window == GlfwWindowPtr.Null) { // Does this line actually work???
    Console.Error.WriteLine("ERROR: Failed to create GLFW window, shutting down");
    Environment.Exit(1);
}

After that, the OpenGL context of that window has to be made current. For this you need to call Glfw.MakeContextCurrent(GlfwWindowPtr):

Glfw.MakeContextCurrent(window);

Game loop

A game loop can be established quite simply in GLFW 3:

while (!Glfw.WindowShouldClose(window)) {
    // Game loop stuffs here
    Glfw.PollEvents();
}

Keeping track of time

This section is the same as the GLFW 2 equivalent