liveBook · Manning

GUI window

As explained in Systems Programming with Zig

A graphical window is a bounded on-screen region managed by a windowing system for displaying and interacting with application content.

A GUI window is a bounded on-screen region for displaying and interacting with application content. For OpenGL, it is the canvas you need before you can draw. A desktop environment provides the window and generally coordinates user input as well. section 7.3 section 7.1.3

On Linux, a display server such as X11 or Wayland coordinates input and output between clients, including GUI windows. Platform expectations vary, so an OpenGL specification alone does not provide a portable window. section 7.1.3

Listing 7.3 Ready, set, loop!
pub fn main() !void {
    const init_success = c.glfwInit(); #1
    if (init_success == c.GLFW_FALSE) return error.GlfwInitFailed; #1
    defer c.glfwTerminate(); #2

    const window = c.glfwCreateWindow(1280, 720,
    "learning opengl", null, null); #3
    if (window == null) return error.GlfwCreateWindowFailed; #3
    defer c.glfwDestroyWindow(window);

    c.glfwMakeContextCurrent(window); #4

    while (c.glfwWindowShouldClose(window) == c.GLFW_FALSE) { #5
        // ... handle input and draw stuff ...

        c.glfwSwapBuffers(window); #6
        c.glfwPollEvents(); #7
    }
}

This Zig loop initializes GLFW, creates a 1280-by-720 window, makes its context current, and runs until the window should close. It checks initialization and creation failures and defers cleanup. section 7.3.1

The loop swaps buffers and polls events each frame. GLFW windows are double-buffered by default: drawing goes to a hidden back framebuffer, then swapping makes the completed frame visible instead of exposing an incomplete picture. section 7.3.1

Listing 6.11 All your window
const c = @cImport({ #1
    @cInclude("raylib.h"); #2
});

pub fn main() !void {
    c.InitWindow(640, 480, "All your window are belong to us!"); #3
    defer c.CloseWindow(); #4

    c.SetTargetFPS(60); #5

    while (!c.WindowShouldClose()) { #6
        c.BeginDrawing(); #7
        defer c.EndDrawing(); #7

        c.ClearBackground(c.BLACK); #8
    }
}

Raylib takes a framework-shaped approach: import its C header, create a window, set a target frame rate, and redraw inside its application loop. This example clears a 640-by-480 window to black. section 6.4.2

How Systems Programming with Zig covers it

How deep it goes
Works through window creation, cleanup, event polling, and buffer swapping, including the rationale for double buffering.
Examples
Uses blank GLFW and Raylib windows with frame-by-frame application loops.
What it assumes
Teaches the Raylib project without requiring C knowledge, while noting that deeper customization requires reading C headers and function definitions.
What you can do after
Apply it via GLFW or Raylib to create and run a blank graphical window.
How it connects
Connects windows to OpenGL, framebuffers, desktop environments, display servers, and user input.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.