GLFW in a Nutshell
  • About GLFW
  • Preparation
    • Project Setup
    • Library Setup
  • Windows
    • Window Basics
    • Properties
    • Actions
    • Creation Hints
    • Attributes
    • (Windowed) Fullscreen
    • Callbacks
    • Icons
  • Contexts
    • Context Basics
    • OpenGL (ES)
    • Vulkan
  • Input
    • Keyboard
    • Mouse
    • Joystick/Gamepad
    • Other
  • Monitors
    • Monitor Basics
    • Video Modes
    • Multiple Monitors
  • Appendix
    • Credits
    • Licensing
    • Glossary
Powered by GitBook
On this page
  • Creation
  • Destruction
  • Buffer Swapping (V-Sync)
  • Event Processing
  • User Pointers

Was this helpful?

  1. Windows

Window Basics

Basics you should to know about GLFW windows

PreviousLibrary SetupNextProperties

Last updated 5 years ago

Was this helpful?

Creation

After the , a GLFW window object can be created using glfwCreateWindow(width,height,title,monitor,share). The parameters monitor (for ) and share () are nullable. Window creation can fail, which should be accounted for:

long window = GLFW.glfwCreateWindow(800,600,"Window",MemoryUtil.NULL,MemoryUtil.NULL);
if(window == MemoryUtil.NULL) {
    GLFW.glfwTerminate();
    throw new RuntimeException("Failed to create GLFW Window");
}
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
}
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
  if (!window) {
    fprintf(stderr, "Failed to create GLFW window\n");
    glfwTerminate();
    return -1;
  }

If window creation was successful, an empty window will be displayed.

Destruction

Windows and their can be destroyed using glfwDestroyWindow(window). On , remaining windows get destroyed automatically.

Buffer Swapping (V-Sync)

By default, GLFW windows are double-buffered. They have a front buffer, which is displayed in the window, and a back buffer, to which is rendered. When the entire frame has been rendered, it is time to swap the back and the front buffers. This is done with glfwSwapBuffers(window). Sometimes it is useful to have control over, when the buffer swap will occur. With the function glfwSwapInterval(interval) it is possible to specify the number of monitor refreshes the graphics card should wait. An interval of 0 swaps the buffer instantly, allowing for as high a framerate as CPU and GPU can serve. An interval of 1 effectively enables vertical sync (V-Sync), capping the frame rate to the monitor's refresh rate.

Event Processing

GLFW needs to poll the operating system's window system for events. There are several functions that signal to GLFW to poll OS events:

  • glfwPollEvents - Processes already received events and returns; best for continuous rendering.

  • glfwWaitEvents - Waits until an event is received, then processes it.

  • glfwWaitEventsTimeout - Waits until an event is received, or a specified time of seconds have elapsed, then processes received event(s).

  • glfwPostEmptyEvent - Wakes the main thread if it's waiting; must be called from different thread.

User Pointers

Each window has a user pointer that can be set with glfwSetWindowUserPointer(window>,<pointer) and queried with glfwGetWindowUserPointer(window). This can be used for any purpose you need and will not be modified by GLFW throughout the life-time of the window.

The initial value of the pointer is NULL.

Some events do not rely on being polled, e.g. the .

GLFW initialization
fullscreen windows
GLFW context sharing
contexts
termination of GLFW
window size callback