Fix framerate issue

This commit is contained in:
Dusk 2022-07-29 12:37:29 +02:00
parent e4f7fca02d
commit acc3cd890d
3 changed files with 28 additions and 40 deletions

View File

@ -53,8 +53,8 @@ fn getTab(self: *Self) MenuTab {
} }
pub fn tick(self: *Self) State { pub fn tick(self: *Self) State {
const sel = self.getSel(); //const sel = self.getSel();
const tab = self.getTab(); //const tab = self.getTab();
var key_state = SDL.getKeyboardState(); var key_state = SDL.getKeyboardState();
for (self.action_list) |*action| { for (self.action_list) |*action| {
@ -72,11 +72,11 @@ pub fn tick(self: *Self) State {
action.*.call(self); action.*.call(self);
} }
} }
std.debug.print( //std.debug.print(
\\Tab: {s} //\\Tab: {s}
\\Selection: {s} //\\Selection: {s}
\\ //\\
, .{ tab.name, sel.name }); //, .{ tab.name, sel.name });
return self.state; return self.state;
} }

View File

@ -21,16 +21,10 @@ pub fn init() !Self {
try sdl.gl.setAttribute(.{ .multisamplebuffers = true }); try sdl.gl.setAttribute(.{ .multisamplebuffers = true });
try sdl.gl.setAttribute(.{ .multisamplesamples = 4 }); try sdl.gl.setAttribute(.{ .multisamplesamples = 4 });
const window = try sdl.createWindow( const window = try sdl.createWindow("USG", .{ .centered = {} }, .{ .centered = {} }, 1280, 720, .{ .context = .opengl, .vis = .shown });
"USG",
.{ .centered = {} },
.{ .centered = {} },
1280,
720,
.{ .context = .opengl, .vis = .shown }
);
const ctx = try sdl.gl.createContext(window); const ctx = try sdl.gl.createContext(window);
_ = try sdl.gl.setSwapInterval(.immediate);
var mvp_loc: u32 = undefined; var mvp_loc: u32 = undefined;
var color_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); 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(); var vertex_array = gl.VertexArray.gen();
vertex_array.bind(); vertex_array.bind();
@ -73,13 +67,7 @@ pub fn init() !Self {
gl.enable(.blend); gl.enable(.blend);
gl.blendFunc(.src_alpha, .one_minus_src_alpha); gl.blendFunc(.src_alpha, .one_minus_src_alpha);
return Self{ return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, .color = .{ 0, 0, 0, 0 } };
.window = window,
.context = ctx,
.color_loc = color_loc,
.buffer = buf,
.color = .{0,0,0,0}
};
} }
pub fn render(self: Self) void { 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 { pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void {
self.*.color = .{ self.*.color = .{
@intToFloat(f32, r)/255.0, @intToFloat(f32, r) / 255.0,
@intToFloat(f32, g)/255.0, @intToFloat(f32, g) / 255.0,
@intToFloat(f32, b)/255.0, @intToFloat(f32, b) / 255.0,
@intToFloat(f32, a)/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); gl.vertexAttribPointer(0, 2, .float, false, 0, 0);
const vertex_buffer = [_]f32{ const vertex_buffer = [_]f32{
xf, yf , xf, yf,
xf + wf, yf , xf + wf, yf,
xf + wf, yf + hf, xf + wf, yf + hf,
xf, 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); self.buffer.data(f32, &vertex_buffer, .static_draw);
gl.uniform4fv(self.color_loc, &.{ self.color }); gl.uniform4fv(self.color_loc, &.{self.color});
_ = indices; _ = indices;
gl.drawElements(.lines, 8, .u8, @ptrToInt(&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); gl.vertexAttribPointer(0, 2, .float, false, 0, 0);
const vertex_buffer = [_]f32{ const vertex_buffer = [_]f32{
xf + wf, yf , xf + wf, yf,
xf, yf + hf, xf, yf + hf,
xf, yf , xf, yf,
xf + wf, yf + hf, 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); 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)); gl.drawElements(.triangles, 6, .u8, @ptrToInt(&indices));

View File

@ -19,6 +19,8 @@ pub fn main() !void {
try SDL.image.init(.{ .jpg = true }); try SDL.image.init(.{ .jpg = true });
mainLoop: while (true) { mainLoop: while (true) {
const start = SDL.getTicks64();
while (SDL.pollEvent()) |ev| { while (SDL.pollEvent()) |ev| {
switch (ev) { switch (ev) {
.quit => break :mainLoop, .quit => break :mainLoop,
@ -26,20 +28,18 @@ pub fn main() !void {
} }
} }
const start = SDL.getTicks64();
current_state = switch (current_state) { current_state = switch (current_state) {
.main_menu => main_menu.tick(), .main_menu => main_menu.tick(),
.game => game.tick(), .game => game.tick(),
}; };
renderer.render(); renderer.render();
const delay = SDL.getTicks64() - start; const delay = SDL.getTicks64() - start;
std.debug.print("{} ms\n", .{delay}); std.debug.print("{} ms\n", .{delay});
if (delay < 15) { if (delay < 16) {
SDL.delay(15 - @intCast(u32, delay)); SDL.delay(16 - @intCast(u32, delay));
} }
} }
} }