summaryrefslogtreecommitdiff
path: root/render_gui.ha
blob: 8bde4cace6f3e37480d25b9a934e8c3bb482be52 (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
use gl::*;
use glm;

type GuiVertex = struct {
	position: [2]f32,
	texcoord: [2]f32,
	color: [4]u8,
};

fn render_rect_textured(
	trans: *glm::m4,
	x: f32, y: f32,
	w: f32, h: f32,
	color: [4]u8,
	u: f32, v: f32,
	sprw: f32, sprh: f32,
	tex: uint, texw: u32, texh: u32,
) void = {
	const x0 = x;
	const x1 = x + w;
	const y0 = y;
	const y1 = y + h;
	const u0 = u / texw: f32;
	const u1 = (u + sprw) / texw: f32;
	const v0 = v / texh: f32;
	const v1 = (v + sprh) / texh: f32;
	const quad = [
		GuiVertex {
			position = [x0, y0],
			texcoord = [u0, v0],
			color = color,
		},
		GuiVertex {
			position = [x0, y1],
			texcoord = [u0, v1],
			color = color,
		},
		GuiVertex {
			position = [x1, y1],
			texcoord = [u1, v1],
			color = color,
		},
		GuiVertex {
			position = [x1, y0],
			texcoord = [u1, v0],
			color = color,
		},
	];
	const vertices = [
		quad[0], quad[1], quad[2],
		quad[2], quad[3], quad[0],
	];

	glDisable(GL_CULL_FACE);
	glDisable(GL_DEPTH_TEST);
	glUseProgram(SHADER_GUI);
	glUniformMatrix4fv(0, 1, 0, trans: *f32);
	glBindTexture(GL_TEXTURE_2D, tex);

	let vbo = 0u;
	glGenBuffers(1, &vbo);
	defer glDeleteBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER,
		(size(GuiVertex) * len(vertices)): uintptr,
		&vertices: *[*]GuiVertex,
		GL_STREAM_DRAW);

	glBindVertexArray(VAO_GUI);
	glBindVertexBuffer(0, vbo, 0, size(GuiVertex): i32);
	glDrawArrays(GL_TRIANGLES, 0, len(vertices): i32);
	glBindVertexArray(0);
};