Creating a window

To start off, we're going to need a surface to do our rendering on. Ironically, we're not going to be using OpenGL for this. After all, the Open Graphics Library is for doing graphics. There are lots of ways to do this. I'm going to be using GLFW (version 3), which is a library that creates OpenGL capable windows and allows us to handle user input.

I'm going to leave the GLFW installation up to you as it varies from language to language.

Initialising GLFW

Before we use GLFW, we must initialise it. We do this by calling glfwInit. It will return GLFW_TRUE if it succeeded, and GLFW_FALSE if it failed.

At the end of the program, we also need to call glfwTerminate to clear up any resources that GLFW was using.

Making the window

By calling glfwWindowHint we can specify what kind of window we'd like. To simplify things, let's say that we want a non-resizable window. But before that, we will call glfwDefaultWindowHints to make sure we're on the same page.

glfwDefaultWindowHints(); // Use default window attributes
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // Make the window non-resizable

Now that we've done that, we can actually create the window. For this, we use the following function:

glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share);

The parameters width and height specify the width and height of the window in pixels, title is the text that appears above the window. If we want our window to be fullscreen, we can pass a GLFWmonitor to monitor. An easy way to obtain such a monitor is via glfwGetPrimaryMonitor, which returns the user's main display. Alternatively, pass NULL if you don't want a fullscreen window. Let's not worry about share for now, just pass NULL to it.

With that knowledge, let's make a 640x480 non-fullscreen window.

// Window hints...

GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Tutorial", NULL, NULL);

This function will also create an OpenGL context on the window. An OpenGL context is bound to a thread. To make sure that we can use the library in our thread, we call glfwMakeContextCurrent on the window:

glfwMakeContextCurrent(window);

Now any calls to OpenGL in our current thread will go towards our window.

After we're done with the window, we must call glfwDestroyWindow before glfwTerminate to close it and free up its resources.

By the end of this chapter, your program should look something like this:

int main(int argc, char *argv[]) {
    if (glfwInit() == GLFW_FALSE) {
        fprintf(stderr, "Failed to initialise GLFW!\n");
        return 1;
    }

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

    GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Tutorial", NULL, NULL);
    glfwMakeContextCurrent(window);

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

If you've done everything correctly, this code should make a window appear for a brief moment.

results matching ""

    No results matching ""