diff options
author | Lassi Pulkkinen <lassi@pulk.fi> | 2024-10-31 03:11:21 +0200 |
---|---|---|
committer | Lassi Pulkkinen <lassi@pulk.fi> | 2024-10-31 03:51:35 +0200 |
commit | ae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch) | |
tree | 5f462459ae4b47d22114eed717d1382d08cf4dfe /render_gui.ha |
Diffstat (limited to 'render_gui.ha')
-rw-r--r-- | render_gui.ha | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/render_gui.ha b/render_gui.ha new file mode 100644 index 0000000..8bde4ca --- /dev/null +++ b/render_gui.ha @@ -0,0 +1,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); +}; |