rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/tukan/glmain.sc -rw-r--r-- 2.3 KiB View raw Log this file
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
                                                                                
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import .stage

using import glm
using import .gl
using import .sdl

global context = (nullof SDL_GLContext)
global window = (nullof (pointer SDL_Window 'mutable))

fn option (default value)
    if (none? value) default
    else value

fn init (opts...)
    SDL_Init
        | SDL_INIT_AUDIO
            SDL_INIT_VIDEO
            SDL_INIT_GAMECONTROLLER
            #SDL_INIT_JOYSTICK

    SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1

    SDL_GL_SetAttribute SDL_GL_STENCIL_SIZE 8
    SDL_GL_SetAttribute SDL_GL_DEPTH_SIZE 24

    SDL_GL_SetAttribute SDL_GL_CONTEXT_MAJOR_VERSION 4
    SDL_GL_SetAttribute SDL_GL_CONTEXT_MINOR_VERSION 5
    #if IS_OSX
        SDL_GL_SetAttribute
            SDL_GL_CONTEXT_PROFILE_MASK
            SDL_GL_CONTEXT_PROFILE_CORE

    let resizable = (option false (va@ 'resizable opts...))

    window =
        SDL_CreateWindow
            option "OpenGL" (va@ 'title opts...)
            SDL_WINDOWPOS_UNDEFINED
            SDL_WINDOWPOS_UNDEFINED
            option 640 (va@ 'width opts...)
            option 480 (va@ 'height opts...)
            | SDL_WINDOW_OPENGL
                SDL_WINDOW_SHOWN
                ? resizable (u32 SDL_WINDOW_RESIZABLE) 0:u32

    context = (SDL_GL_CreateContext window)
    SDL_GL_SetSwapInterval 1
    SDL_GL_MakeCurrent window context

    do
        let result = (gladLoadGL)
        assert (result != 0) "failed to load GL core functions"

    print-gl-info;

    hook-gl-debug;
    stage.init-gfx;

fn window-size ()
    var w = 0
    var h = 0
    SDL_GetWindowSize window w h
    return w h

fn shutdown ()
    stage.shutdown-gfx;
    SDL_GL_DeleteContext context
    SDL_DestroyWindow window

    SDL_Quit;

fn swap ()
    glBindFramebuffer GL_FRAMEBUFFER 0
    SDL_GL_SwapWindow window

let on-event = (fnchain "glmain-on-event")
let on-draw = (fnchain "glmain-on-draw")

global quit = false
fn handle_events ()
    var event = (SDL_Event)
    while ((SDL_PollEvent event) != 0)
        if (event.type == SDL_QUIT)
            quit = true
        else
            on-event event

fn loop ()
    while (not quit)
        handle_events;
        let w h = (window-size)
        on-draw 0.0 (ivec2 w h)
        swap;

do
    let init
        shutdown
        swap
        loop
        window-size
        window
        context
        on-event
        on-draw

    locals;