From acc3cd890d5ce78461d4d6b1ea1edaab1c8bded0 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Fri, 29 Jul 2022 12:37:29 +0200 Subject: [PATCH] Fix framerate issue --- src/MainMenu.zig | 14 +++++++------- src/Renderer.zig | 44 ++++++++++++++++---------------------------- src/main.zig | 10 +++++----- 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/MainMenu.zig b/src/MainMenu.zig index dfa8cb2..4916e87 100644 --- a/src/MainMenu.zig +++ b/src/MainMenu.zig @@ -53,8 +53,8 @@ fn getTab(self: *Self) MenuTab { } pub fn tick(self: *Self) State { - const sel = self.getSel(); - const tab = self.getTab(); + //const sel = self.getSel(); + //const tab = self.getTab(); var key_state = SDL.getKeyboardState(); for (self.action_list) |*action| { @@ -72,11 +72,11 @@ pub fn tick(self: *Self) State { action.*.call(self); } } - std.debug.print( - \\Tab: {s} - \\Selection: {s} - \\ - , .{ tab.name, sel.name }); + //std.debug.print( + //\\Tab: {s} + //\\Selection: {s} + //\\ + //, .{ tab.name, sel.name }); return self.state; } diff --git a/src/Renderer.zig b/src/Renderer.zig index 17f5e82..354f301 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -21,16 +21,10 @@ pub fn init() !Self { try sdl.gl.setAttribute(.{ .multisamplebuffers = true }); try sdl.gl.setAttribute(.{ .multisamplesamples = 4 }); - const window = try sdl.createWindow( - "USG", - .{ .centered = {} }, - .{ .centered = {} }, - 1280, - 720, - .{ .context = .opengl, .vis = .shown } - ); + const window = try sdl.createWindow("USG", .{ .centered = {} }, .{ .centered = {} }, 1280, 720, .{ .context = .opengl, .vis = .shown }); const ctx = try sdl.gl.createContext(window); + _ = try sdl.gl.setSwapInterval(.immediate); var mvp_loc: u32 = undefined; var color_loc: u32 = undefined; @@ -60,7 +54,7 @@ pub fn init() !Self { const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0); - gl.uniformMatrix4fv(mvp_loc, false, &.{ ortho.fields }); + gl.uniformMatrix4fv(mvp_loc, false, &.{ortho.fields}); var vertex_array = gl.VertexArray.gen(); vertex_array.bind(); @@ -73,13 +67,7 @@ pub fn init() !Self { gl.enable(.blend); gl.blendFunc(.src_alpha, .one_minus_src_alpha); - return Self{ - .window = window, - .context = ctx, - .color_loc = color_loc, - .buffer = buf, - .color = .{0,0,0,0} - }; + return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, .color = .{ 0, 0, 0, 0 } }; } pub fn render(self: Self) void { @@ -96,10 +84,10 @@ pub fn deinit(self: Self) void { pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void { self.*.color = .{ - @intToFloat(f32, r)/255.0, - @intToFloat(f32, g)/255.0, - @intToFloat(f32, b)/255.0, - @intToFloat(f32, a)/255.0, + @intToFloat(f32, r) / 255.0, + @intToFloat(f32, g) / 255.0, + @intToFloat(f32, b) / 255.0, + @intToFloat(f32, a) / 255.0, }; } @@ -113,17 +101,17 @@ pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { gl.vertexAttribPointer(0, 2, .float, false, 0, 0); const vertex_buffer = [_]f32{ - xf, yf , - xf + wf, yf , + xf, yf, + xf + wf, yf, xf + wf, yf + hf, xf, yf + hf, }; - const indices = [_]u8{0, 1, 1, 2, 2, 3, 3, 0}; + const indices = [_]u8{ 0, 1, 1, 2, 2, 3, 3, 0 }; self.buffer.data(f32, &vertex_buffer, .static_draw); - gl.uniform4fv(self.color_loc, &.{ self.color }); + gl.uniform4fv(self.color_loc, &.{self.color}); _ = indices; gl.drawElements(.lines, 8, .u8, @ptrToInt(&indices)); @@ -141,17 +129,17 @@ pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { gl.vertexAttribPointer(0, 2, .float, false, 0, 0); const vertex_buffer = [_]f32{ - xf + wf, yf , + xf + wf, yf, xf, yf + hf, - xf, yf , + xf, yf, xf + wf, yf + hf, }; - const indices = [_]u8{0, 1, 2, 0, 1, 3}; + const indices = [_]u8{ 0, 1, 2, 0, 1, 3 }; self.buffer.data(f32, &vertex_buffer, .static_draw); - gl.uniform4fv(self.color_loc, &.{ self.color }); + gl.uniform4fv(self.color_loc, &.{self.color}); gl.drawElements(.triangles, 6, .u8, @ptrToInt(&indices)); diff --git a/src/main.zig b/src/main.zig index bb30e58..746cc9b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -19,6 +19,8 @@ pub fn main() !void { try SDL.image.init(.{ .jpg = true }); mainLoop: while (true) { + const start = SDL.getTicks64(); + while (SDL.pollEvent()) |ev| { switch (ev) { .quit => break :mainLoop, @@ -26,20 +28,18 @@ pub fn main() !void { } } - const start = SDL.getTicks64(); - current_state = switch (current_state) { .main_menu => main_menu.tick(), .game => game.tick(), }; - renderer.render(); const delay = SDL.getTicks64() - start; + std.debug.print("{} ms\n", .{delay}); - if (delay < 15) { - SDL.delay(15 - @intCast(u32, delay)); + if (delay < 16) { + SDL.delay(16 - @intCast(u32, delay)); } } }