GLWindow

#import "extra/glwindow"

Warning

This module is currently available only on Windows as experimental.

Set of tools to handle creation of the operating system native window with OpenGL context and handling of user inputs.

The window creation does not cover OpenGL initialization, since its API must be loaded dynamically in runtime, thus the gl_init must be called after a window is successfully created (the propper context is set) and before any rendering.

Example

#import "extra/glwindow"

main :: fn () s32 {
    // Initialize the glwindow module first.
    glwindow.init();
    // And don't forget to terminate it at the end.
    defer glwindow.terminate();

    // Create default instance of window options and override some values.
    opt := glwindow.make_default_create_window_options();

    // Text shown in the window title.
    opt.title  = "My Window";

    // Window resolution
    opt.width  = 1024;
    opt.height = 768;

    // Uncommnet this to enable fullscreen mode.
    //opt.fullscreen = true;

    // Create new window using our options and handle all possible errors.
    window, window_err :: glwindow.create_window(&opt);
    if window_err {
        // Kill the app on error.
        panic(window_err);
    }
    // Destroy the window at the end of the scope.
    defer glwindow.destroy_window(window);

    // Make the window's GL context the current one.
    glwindow.set_window_context_current(window);

    gl_init();
    defer gl_terminate();

    // The "game loop" is looping until the should_quit is false.
    should_quit := false;
    loop !should_quit {
        // Poll all pending window events.
        glwindow.poll_window_events();

        // Process user input events here.
        loop i := 0; i < glwindow.frame_events.len; i += 1 {
            event :: &glwindow.frame_events[i];
            using glwindow.EventKind;
            switch event.kind {
                QUIT { should_quit = true; }
                default;
            }
        }

        // Draw one frame.
        gl.ClearColor(1.f, 0.f, 0.f, 1.f);
        gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Don't forget to swap gl buffers at the end of the frame.
        glwindow.swap_buffers(window);
    }

    print("DONE\n");
    return 0;
}

glwindow.Window

Window :: win32.HWND

File: glwindow.win32.bl

glwindow.EventKind

EventKind :: enum {
    QUIT;
    MOUSE_LB_DOWN;
    MOUSE_LB_UP;
    MOUSE_MB_DOWN;
    MOUSE_MB_UP;
    MOUSE_RB_DOWN;
    MOUSE_RB_UP;
    MOUSE_MOVE;
    KEY_PRESS;
    KEY_RELEASE;
}

File: glwindow.win32.bl

glwindow.KeyCode

KeyCode :: enum u32 {
    LBUTTON = 1;
    RBUTTON = 2;
    CANCEL = 3;
    BACK = 8;
    TAB = 9;
    CLEAR = 12;
    RETURN = 13;
    SHIFT = 16;
    CONTROL = 17;
    MENU = 18;
    PAUSE = 19;
    CAPITAL = 20;
    KANA = 21;
    HANGEUL = 21;
    HANGUL = 21;
    IME_ON = 22;
    JUNJA = 23;
    FINAL = 24;
    HANJA = 25;
    KANJI = 25;
    IME_OFF = 26;
    ESCAPE = 27;
    CONVERT = 28;
    NONCONVERT = 29;
    ACCEPT = 30;
    MODECHANGE = 31;
    SPACE = 32;
    PRIOR = 33;
    NEXT = 34;
    END = 35;
    HOME = 36;
    LEFT = 37;
    UP = 38;
    RIGHT = 39;
    DOWN = 40;
    SELECT = 41;
    PRINT = 42;
    EXECUTE = 43;
    SNAPSHOT = 44;
    INSERT = 45;
    DELETE = 46;
    HELP = 47;
    LWIN = 91;
    RWIN = 92;
    APPS = 93;
    SLEEP = 95;
    NUMPAD0 = 96;
    NUMPAD1 = 97;
    NUMPAD2 = 98;
    NUMPAD3 = 99;
    NUMPAD4 = 100;
    NUMPAD5 = 101;
    NUMPAD6 = 102;
    NUMPAD7 = 103;
    NUMPAD8 = 104;
    NUMPAD9 = 105;
    MULTIPLY = 106;
    ADD = 107;
    SEPARATOR = 108;
    SUBTRACT = 109;
    DECIMAL = 110;
    DIVIDE = 111;
    F1 = 112;
    F2 = 113;
    F3 = 114;
    F4 = 115;
    F5 = 116;
    F6 = 117;
    F7 = 118;
    F8 = 119;
    F9 = 120;
    F10 = 121;
    F11 = 122;
    F12 = 123;
    F13 = 124;
    F14 = 125;
    F15 = 126;
    F16 = 127;
    F17 = 128;
    F18 = 129;
    F19 = 130;
    F20 = 131;
    F21 = 132;
    F22 = 133;
    F23 = 134;
    F24 = 135;
    NUMBER_0 = 48;
    NUMBER_1 = 49;
    NUMBER_2 = 50;
    NUMBER_3 = 51;
    NUMBER_4 = 52;
    NUMBER_5 = 53;
    NUMBER_6 = 54;
    NUMBER_7 = 55;
    NUMBER_8 = 56;
    NUMBER_9 = 57;
    A = 65;
    B = 66;
    C = 67;
    D = 68;
    E = 69;
    F = 70;
    G = 71;
    H = 72;
    I = 73;
    J = 74;
    K = 75;
    L = 76;
    M = 77;
    N = 78;
    O = 79;
    P = 80;
    Q = 81;
    R = 82;
    S = 83;
    T = 84;
    U = 85;
    V = 86;
    W = 87;
    X = 88;
    Y = 89;
    Z = 90;
}

File: glwindow.win32.bl

glwindow.Event

Event :: struct {
    kind: EventKind;
    sender: Window;
    data: union {
        mouse_move: struct {
            x: s32;
            y: s32;
        };
        key_code: KeyCode;
    };
 }

General window event representation.

Members

  • kind - Window event kind.
  • sender - Window which recieved this event.
  • x - Related to MOUSE_MOVE event.
  • key_code - Related to KEY_* events.

File: glwindow.win32.bl

glwindow.frame_events

frame_events : [..]Event = 

Array of all collected window events in the current frame, this array is updated by calling poll_window_events.

File: glwindow.win32.bl

glwindow.init

init :: fn () 

Initialize module internals.

File: glwindow.win32.bl

glwindow.terminate

terminate :: fn () 

Release all module internals.

File: glwindow.win32.bl

glwindow.CreateWindowOptions

CreateWindowOptions :: struct {
    title: string_view;
    width: s32;
    height: s32;
    position_x: s32;
    position_y: s32;
    gl_major_version: s32;
    gl_minor_version: s32;
    vsync: bool;
    fullscreen: bool;
 }

Window initialization options. Use make_default_create_window_options to create default options.

Members

  • title - Window title text shown in the title bar.
  • width - Window width in pixels. (Must be greater than 0.)
  • height - Window height in pixels. (Must be greater than 0.)
  • position_x - Window initial X position in pixels.
  • position_y - Window initial Y position in pixels.
  • gl_major_version - Required OpenGL major version.
  • gl_minor_version - Required OpenGL minor version.
  • vsync - Enable VSync.
  • fullscreen - Enable fullscreen mode.

File: glwindow.win32.bl

glwindow.make_default_create_window_options

make_default_create_window_options :: fn () CreateWindowOptions #inline

Creates instance of default CreateWindowOptions

File: glwindow.win32.bl

glwindow.create_window

create_window :: fn (options: *CreateWindowOptions) (window: Window, error: Error)

Create a new instance of window with OpenGL buffer initialized; the OpenGL API initialization must be handled explicitly on caller side with gl_init and gl_terminate.

File: glwindow.win32.bl

glwindow.destroy_window

destroy_window :: fn (window: Window) 

Destroy window created by create_window.

File: glwindow.win32.bl

glwindow.set_window_context_current

set_window_context_current :: fn (window: Window) Error

Set the OpenGL window context as current used context. This must be called before OpenGL is initialized.

File: glwindow.win32.bl

glwindow.swap_buffers

swap_buffers :: fn (window: Window)  #inline

Swaps current buffers (presents changes to the window's OpenGL viewport).

File: glwindow.win32.bl

glwindow.poll_window_events

poll_window_events :: fn () 

Poll all window events and update frame_events array.

File: glwindow.win32.bl