Finally implement texture and text rendering
This commit is contained in:
parent
9d23bbc39e
commit
7dec3d9386
|
@ -2,10 +2,10 @@ const MainMenu = @import("../MainMenu.zig");
|
|||
|
||||
const Self = @This();
|
||||
|
||||
name: []const u8,
|
||||
name: [:0]const u8,
|
||||
action: *const fn (*MainMenu) void,
|
||||
|
||||
pub fn init(name: []const u8, action: *const fn (*MainMenu) void) Self {
|
||||
pub fn init(name: [:0]const u8, action: *const fn (*MainMenu) void) Self {
|
||||
return Self{
|
||||
.name = name,
|
||||
.action = action,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
const std = @import("std");
|
||||
const SDL = @import("sdl2");
|
||||
|
||||
const MainMenu = @import("../MainMenu.zig");
|
||||
|
@ -76,22 +77,18 @@ pub fn render(self: Self, main_menu: MainMenu) void {
|
|||
self.renderMenu(x, y, curr_tab, curr_tab.sel, alpha, true);
|
||||
}
|
||||
|
||||
{
|
||||
for (range(n_sel_below)) |_, i| {
|
||||
const aux_sel: i32 = @intCast(i32, i + 1);
|
||||
const curr_sel: usize = main_menu.getSelIdx(aux_sel);
|
||||
const y = @intCast(i32, sel_y_offset + ((y_spacing + height) * (aux_sel)));
|
||||
self.renderMenu(x - (skew * aux_sel), y, curr_tab, curr_sel, alpha, false);
|
||||
}
|
||||
for (range(n_sel_below)) |_, i| {
|
||||
const aux_sel: i32 = @intCast(i32, i + 1);
|
||||
const curr_sel: usize = main_menu.getSelIdx(aux_sel);
|
||||
const y = @intCast(i32, sel_y_offset + ((y_spacing + height) * (aux_sel)));
|
||||
self.renderMenu(x - ((skew + 8) * aux_sel), y, curr_tab, curr_sel, alpha, false);
|
||||
}
|
||||
|
||||
{
|
||||
for (range(n_sel_above)) |_, i| {
|
||||
const aux_sel: i32 = -@intCast(i32, i + 1);
|
||||
const curr_sel: usize = main_menu.getSelIdx(aux_sel);
|
||||
const y = @intCast(i32, sel_y_offset + ((y_spacing + height) * (aux_sel)));
|
||||
self.renderMenu(x, y, curr_tab, curr_sel, alpha, false);
|
||||
}
|
||||
for (range(n_sel_above)) |_, i| {
|
||||
const aux_sel: i32 = -@intCast(i32, i + 1);
|
||||
const curr_sel: usize = main_menu.getSelIdx(aux_sel);
|
||||
const y = @intCast(i32, sel_y_offset + ((y_spacing + height) * (aux_sel)));
|
||||
self.renderMenu(x - ((skew + 8) * aux_sel), y, curr_tab, curr_sel, alpha, false);
|
||||
}
|
||||
|
||||
// Tab header
|
||||
|
@ -109,7 +106,7 @@ fn renderMenu(self: Self, x: i32, y: i32, tab: MenuTab, sel: usize, a: u8, selec
|
|||
// White background
|
||||
self.renderer.setColor(255, 255, 255, a);
|
||||
self.renderer.fillRectangleEx(x, y, width, height, skew);
|
||||
_ = sel;
|
||||
//_ = sel;
|
||||
if (selected) {
|
||||
// Set color if selected
|
||||
self.renderer.setColor(tab.color[0], tab.color[1], tab.color[2], a);
|
||||
|
@ -120,7 +117,12 @@ fn renderMenu(self: Self, x: i32, y: i32, tab: MenuTab, sel: usize, a: u8, selec
|
|||
|
||||
const margin = 20;
|
||||
|
||||
self.renderer.fillRectangleEx(x + margin, y + margin, width - margin * 2, height - margin * 2, skew);
|
||||
//self.renderer.setColorF(1, 1, 1, 1);
|
||||
|
||||
var text = Renderer.Texture.fromText(tab.contents[sel].name, height - margin * 2);
|
||||
self.renderer.renderTexture(text, x + margin, y + margin);
|
||||
|
||||
//self.renderer.fillRectangleEx(x + margin, y + margin, width - margin * 2, height - margin * 2, skew);
|
||||
self.renderer.fillRectangleEx(x + width - 6, y, 6, height, skew);
|
||||
self.renderer.fillRectangleEx(x + width - 12, y, 3, height, skew);
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ vbo_index: [nPrograms]usize = .{0} ** nPrograms,
|
|||
|
||||
// The index of the program is the enum converted to int
|
||||
programs: [nPrograms]gl.Program = .{undefined} ** nPrograms,
|
||||
textures: [max_objects]Texture = .{undefined} ** max_objects,
|
||||
|
||||
const max_objects: usize = 16384;
|
||||
|
||||
|
@ -84,10 +85,6 @@ pub fn init() !Self {
|
|||
|
||||
gl.clearColor(0.91, 0.85, 0.65, 1.00);
|
||||
|
||||
var text = Texture.fromText("testing123");
|
||||
_ = text;
|
||||
//text.deinit();
|
||||
|
||||
return renderer;
|
||||
}
|
||||
|
||||
|
@ -139,7 +136,12 @@ pub fn render(self: *Self) void {
|
|||
gl.drawArrays(.triangles, 0, self.vbo_index[program_i]);
|
||||
},
|
||||
.texture => {
|
||||
gl.drawArrays(.triangles, 0, self.vbo_index[program_i]);
|
||||
var i: usize = 0;
|
||||
const amount = self.vbo_index[program_i] / 48;
|
||||
while (i < amount) : (i += 1) {
|
||||
gl.bindTexture(self.textures[i].texture, .@"2d");
|
||||
gl.drawArrays(.triangles, i * 6, 6);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -190,7 +192,18 @@ pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
|
|||
}
|
||||
|
||||
pub fn fillRectangleEx(self: *Self, x: i32, y: i32, w: i32, h: i32, skew_x: i32) void {
|
||||
const program_i = @enumToInt(shaderProgram.texture);
|
||||
renderRectangle(self, x, y, w, h, skew_x, shaderProgram.color);
|
||||
}
|
||||
|
||||
pub fn renderTexture(self: *Self, texture: Texture, x: i32, y: i32) void {
|
||||
const programEnum = shaderProgram.texture;
|
||||
|
||||
self.textures[self.vbo_index[@enumToInt(programEnum)] / 48] = texture;
|
||||
renderRectangle(self, x, y, @intCast(i32, texture.width), @intCast(i32, texture.height), 0, programEnum);
|
||||
}
|
||||
|
||||
fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32, skew_x: i32, program: shaderProgram) void {
|
||||
const program_i = @enumToInt(program);
|
||||
|
||||
var xf = @intToFloat(f32, x);
|
||||
var yf = @intToFloat(f32, y);
|
||||
|
@ -230,10 +243,6 @@ pub fn fillRectangleEx(self: *Self, x: i32, y: i32, w: i32, h: i32, skew_x: i32)
|
|||
self.vbo_index[program_i] += vertex_data.len;
|
||||
}
|
||||
|
||||
fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
|
||||
self.renderRectangleEx(x, y, w, h, 0);
|
||||
}
|
||||
|
||||
pub const OutputSize = struct { width: c_int, height: c_int };
|
||||
pub fn getOutputSize(self: Self) OutputSize {
|
||||
var wsize = self.window.getSize();
|
||||
|
@ -243,8 +252,10 @@ pub fn getOutputSize(self: Self) OutputSize {
|
|||
};
|
||||
}
|
||||
|
||||
const Texture = struct {
|
||||
pub const Texture = struct {
|
||||
texture: gl.Texture,
|
||||
width: usize,
|
||||
height: usize,
|
||||
|
||||
pub fn init(data: [*]const u8, width: usize, height: usize) Texture {
|
||||
var tex = gl.genTexture();
|
||||
|
@ -260,11 +271,13 @@ const Texture = struct {
|
|||
|
||||
return Texture{
|
||||
.texture = tex,
|
||||
.width = width,
|
||||
.height = height,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fromText(text: [:0]const u8) Texture {
|
||||
var font = sdl.ttf.openFont("res/fonts/MarginaliaRegular-8XlZ.ttf", 200) catch unreachable;
|
||||
pub fn fromText(text: [:0]const u8, size: c_int) Texture {
|
||||
var font = sdl.ttf.openFont("res/fonts/MarginaliaRegular-8XlZ.ttf", size) catch unreachable;
|
||||
defer font.close();
|
||||
|
||||
var surface = font.renderTextBlended(text, sdl.Color.white) catch unreachable;
|
||||
|
|
Loading…
Reference in New Issue