summaryrefslogtreecommitdiffstats
path: root/src/main.zig
blob: f3d342a72761707ddcfdb7f352445ddb57f09039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/// this is a port of cimgui/imgui/examples/example_glfw_opengl3/main.cpp
const c = @import("c.zig");
const std = @import("std");
const panic = std.debug.panic;
const debug_gl = @import("debug_gl.zig");
const glfw_impl = @import("glfw_impl.zig");
const gl3_impl = @import("gl3_impl.zig");

extern fn errorCallback(err: c_int, description: [*c]const u8) void {
    panic("Error: {}\n", description);
}

var window: *c.GLFWwindow = undefined;

pub fn main() !void {
    _ = c.glfwSetErrorCallback(errorCallback);

    if (c.glfwInit() == c.GL_FALSE) {
        panic("GLFW init failure\n");
    }
    defer c.glfwTerminate();

    c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3);
    c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2);
    c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GLFW_TRUE);
    c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, debug_gl.is_on);
    c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE);
    // c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0);
    // c.glfwWindowHint(c.GLFW_STENCIL_BITS, 8);
    c.glfwWindowHint(c.GLFW_RESIZABLE, c.GL_TRUE);

    const window_width = 640;
    const window_height = 480;
    window = c.glfwCreateWindow(window_width, window_height, c"ImGUI Test", null, null) orelse {
        panic("unable to create window\n");
    };
    defer c.glfwDestroyWindow(window);

    c.glfwMakeContextCurrent(window);
    c.glfwSwapInterval(1);

    const context = c.igCreateContext(null);
    defer c.igDestroyContext(context);

    const io = c.igGetIO();
    io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_NavEnableKeyboard);
    io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_DockingEnable);
    io.*.ConfigFlags |= @enumToInt(c.ImGuiConfigFlags_ViewportsEnable);

    const style = c.igGetStyle();
    c.igStyleColorsDark(style);

    if (io.*.ConfigFlags & @enumToInt(c.ImGuiConfigFlags_ViewportsEnable) != 0) {
        style.*.WindowRounding = 0.0;
        style.*.Colors[@enumToInt(c.ImGuiCol_WindowBg)].w = 1.0;
    }

    glfw_impl.Init(window, true, glfw_impl.ClientApi.OpenGL);
    defer glfw_impl.Shutdown();

    gl3_impl.Init(); // #version 150
    defer gl3_impl.Shutdown();

    const start_time = c.glfwGetTime();
    var prev_time = start_time;

    while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
        c.glfwPollEvents();

        try gl3_impl.NewFrame();
        glfw_impl.NewFrame();
        c.igNewFrame();

        // main part
        c.igShowDemoWindow(null);

        c.igRender();
        var w: c_int = undefined;
        var h: c_int = undefined;
        c.glfwGetFramebufferSize(window, &w, &h);
        c.glViewport(0, 0, w, h);
        c.glClearColor(0.0, 0.0, 0.0, 0.0);
        c.glClear(c.GL_COLOR_BUFFER_BIT);
        gl3_impl.RenderDrawData(c.igGetDrawData());

        // const now_time = c.glfwGetTime();
        // const elapsed = now_time - prev_time;
        // prev_time = now_time;
        // nextFrame(t, elapsed);
        // draw(t, @This());

        c.glfwSwapBuffers(window);
    }
}