~jamii/focus

ref: 9129a602ae181370105ef749b628f2b69546ee28 focus/src/draw.zig -rw-r--r-- 5.9 KiB
9129a602Jamie Brandon Nit 5 months 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
usingnamespace @import("common.zig");
usingnamespace c;

pub const atlas = @import("./atlas.zig");

pub const Coord = u16;

pub const Rect = struct {
    x: Coord,
    y: Coord,
    w: Coord,
    h: Coord,

    pub fn splitRight(self: *Rect, w: Coord) Rect {
        assert(self.w >= w);
        const split = Rect{.x=self.x+self.w-w, .y=self.y, .w=w, .h=self.h};
        self.w -= w;
        return split;
    }

    pub fn splitBottom(self: *Rect, h: Coord) Rect {
        assert(self.h >= h);
        const split = Rect{.x=self.x, .y=self.y+self.h-h, .w=self.w, .h=h};
        self.h -= h;
        return split;
    }
};

pub const Vec2 = struct {
    x: Coord,
    y: Coord,
};

pub const Color = packed struct {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
};

const Vec2f = packed struct {
    x: f32,
    y: f32,
};

fn Tri(comptime t: type) type {
    // TODO which direction?
    return packed struct {
        a: t,
        b: t,
        c: t,
    };
}

fn Quad(comptime t: type) type {
    return packed struct {
        tl: t,
        tr: t,
        bl: t,
        br: t,
    };
}

const buffer_size = 2 ^ 14;
var texture_buffer = std.mem.zeroes([buffer_size]Quad(Vec2f));
var vertex_buffer = std.mem.zeroes([buffer_size]Quad(Vec2f));
var color_buffer = std.mem.zeroes([buffer_size]Quad(Color));
var index_buffer = std.mem.zeroes([buffer_size][2]Tri(u32));

// can't fit on my laptop screen, so scale down
pub const scale = if (std.Target.current.cpu.arch == .aarch64) 1 else 2;

pub const screen_width = 720;
pub const screen_height = 1440;

var buffer_ix: usize = 0;

var window: *c.SDL_Window = undefined;

pub fn init() void {
    // init SDL
    _ = SDL_Init(SDL_INIT_EVERYTHING);
    const window_o = SDL_CreateWindow("focus", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @as(c_int, @divTrunc(screen_width, scale)), @as(c_int, @divTrunc(screen_height, scale)), SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
    if (window_o == null) {
        warn("SDL_CreateWindow failed: {s}", .{SDL_GetError()});
        std.os.exit(1);
    }
    window = window_o.?;
    if (std.Target.current.cpu.arch == .aarch64) {
        _ = SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
    }
    _ = SDL_GL_CreateContext(window);

    // init gl
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_CULL_FACE);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    // init texture
    var id: u32 = undefined;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, atlas.width, atlas.height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &atlas.texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    assert(glGetError() == 0);

    // sync with monitor - causes input lag
    _ = SDL_GL_SetSwapInterval(1);
}

fn flush() void {
    if (buffer_ix == 0) {
        return;
    }

    glViewport(0, 0, @divTrunc(screen_width, scale), @divTrunc(screen_height, scale));
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, @intToFloat(f32, screen_width), @intToFloat(f32, screen_height), 0.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glTexCoordPointer(2, GL_FLOAT, 0, &texture_buffer);
    glVertexPointer(2, GL_FLOAT, 0, &vertex_buffer);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, &color_buffer);
    glDrawElements(GL_TRIANGLES, @intCast(c_int, buffer_ix) * 6, GL_UNSIGNED_INT, &index_buffer);

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    buffer_ix = 0;
}

fn quad(dst: Rect, src: Rect, color: Color) void {
    if (buffer_ix == buffer_size) {
        flush();
    }

    const tx = @intToFloat(f32, src.x) / @intToFloat(f32, atlas.width);
    const ty = @intToFloat(f32, src.y) / @intToFloat(f32, atlas.height);
    const tw = @intToFloat(f32, src.w) / @intToFloat(f32, atlas.width);
    const th = @intToFloat(f32, src.h) / @intToFloat(f32, atlas.height);
    texture_buffer[buffer_ix] = .{
        .tl = .{ .x = tx, .y = ty },
        .tr = .{ .x = tx + tw, .y = ty },
        .bl = .{ .x = tx, .y = ty + th },
        .br = .{ .x = tx + tw, .y = ty + th },
    };

    const vx = @intToFloat(f32, dst.x);
    const vy = @intToFloat(f32, dst.y);
    const vw = @intToFloat(f32, dst.w);
    const vh = @intToFloat(f32, dst.h);
    vertex_buffer[buffer_ix] = .{
        .tl = .{ .x = vx, .y = vy },
        .tr = .{ .x = vx + vw, .y = vy },
        .bl = .{ .x = vx, .y = vy + vh },
        .br = .{ .x = vx + vw, .y = vy + vh },
    };

    color_buffer[buffer_ix] = .{
        .tl = color,
        .tr = color,
        .bl = color,
        .br = color,
    };

    const vertex_ix = @intCast(u32, buffer_ix * 4);
    index_buffer[buffer_ix] = .{
        .{
            .a = vertex_ix + 0,
            .b = vertex_ix + 1,
            .c = vertex_ix + 2,
        },
        .{
            .a = vertex_ix + 2,
            .b = vertex_ix + 3,
            .c = vertex_ix + 1,
        },
    };

    buffer_ix += 1;
}

pub fn rect(dst: Rect, color: Color) void {
    quad(dst, atlas.white, color);
}

pub fn text(chars: str, pos: Vec2, color: Color) void {
    var dst: Rect = .{ .x = pos.x, .y = pos.y, .w = 0, .h = 0 };
    for (chars) |char| {
        const src = atlas.chars[min(char, 127)];
        dst.w = src.w * atlas.scale;
        dst.h = src.h * atlas.scale;
        quad(dst, src, color);
        dst.x += dst.w;
    }
}

pub fn clear(color: Color) void {
    flush();
    glClearColor(@intToFloat(f32, color.r) / 255., @intToFloat(f32, color.g) / 255., @intToFloat(f32, color.b) / 255., @intToFloat(f32, color.a) / 255.);
    glClear(GL_COLOR_BUFFER_BIT);
}

pub fn swap() void {
    flush();
    SDL_GL_SwapWindow(window);
}