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 {
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;
}

View File

@ -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;
@ -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 {

View File

@ -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));
}
}
}