From 0fb3650a9ad1a14816cbd0d3870f7d475e4194ca Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Fri, 2 Jun 2023 14:21:50 +0200 Subject: [PATCH] Added FPS counter. Not forcing vsync. --- src/context.zig | 29 ++++++++++++++++++++++------- src/engine.zig | 1 + src/engine/fps.zig | 25 +++++++++++++++++++++++++ src/main.zig | 9 +++++++++ 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/engine/fps.zig diff --git a/src/context.zig b/src/context.zig index 9051967..7d37823 100644 --- a/src/context.zig +++ b/src/context.zig @@ -11,24 +11,38 @@ pub const Context = struct { allocator: std.mem.Allocator, palette: Palette = undefined, shouldQuit: bool = false, + showFPS: bool = false, scene: ?Scene = null, display: allegro.Display, events: allegro.EventQueue, viewport: engine.Viewport, + fps: engine.FPS, fonts: engine.Fonts, textures: engine.Textures, pub fn init(allocator: std.mem.Allocator) !Context { - _ = allegro.init(); - _ = allegro.initImageAddon(); - _ = allegro.installKeyboard(); - _ = allegro.installMouse(); - _ = allegro.initFontAddon(); - _ = allegro.initTtfAddon(); + if (!allegro.init()) { + return error.FailedToInitialize; + } + if (!allegro.initImageAddon()) { + return error.FailedToInitialize; + } + if (!allegro.installKeyboard()) { + return error.FailedToInitialize; + } + if (!allegro.installMouse()) { + return error.FailedToInitialize; + } + if (!allegro.initFontAddon()) { + return error.FailedToInitialize; + } + if (!allegro.initTtfAddon()) { + return error.FailedToInitialize; + } - allegro.setNewDisplayOption(allegro.NewDisplayOption.VSYNC, 1, allegro.OptionImportance.REQUIRE); + allegro.setNewDisplayOption(allegro.NewDisplayOption.VSYNC, 1, allegro.OptionImportance.SUGGEST); allegro.setNewDisplayOption(allegro.NewDisplayOption.SAMPLE_BUFFERS, 1, allegro.OptionImportance.REQUIRE); allegro.setNewDisplayOption(allegro.NewDisplayOption.SAMPLES, 4, allegro.OptionImportance.REQUIRE); allegro.setNewDisplayFlags(allegro.NewDisplayFlags{ .RESIZABLE = true }); @@ -47,6 +61,7 @@ pub const Context = struct { .display = display, .events = events, .viewport = viewport, + .fps = engine.FPS{}, .fonts = engine.Fonts.init(allocator), .textures = engine.Textures.init(allocator), }; diff --git a/src/engine.zig b/src/engine.zig index 5ad1421..c2b3520 100644 --- a/src/engine.zig +++ b/src/engine.zig @@ -1,6 +1,7 @@ const assets = @import("engine/assets.zig"); pub const Fonts = assets.Fonts; +pub const FPS = @import("engine/fps.zig").FPS; pub const OpaquePtr = @import("engine/opaque_ptr.zig").OpaquePtr; pub const Point = @import("engine/point.zig").Point; pub const PointF = @import("engine/point_f.zig").PointF; diff --git a/src/engine/fps.zig b/src/engine/fps.zig new file mode 100644 index 0000000..ece6f0e --- /dev/null +++ b/src/engine/fps.zig @@ -0,0 +1,25 @@ +pub const FPS = struct { + const Frames: usize = 100; + + frames: [Frames]f32 = [_]f32{0} ** Frames, + total: f32 = 0, + index: usize = 0, + framesCounted: usize = 0, + + pub fn update(self: *FPS, dt: f32) void { + self.total -= self.frames[self.index]; + self.total += dt; + self.frames[self.index] = dt; + self.index += 1; + if (self.index == Frames) { + self.index = 0; + } + if (self.framesCounted < Frames) { + self.framesCounted += 1; + } + } + + pub fn fps(self: FPS) usize { + return @floatToInt(usize, @intToFloat(f32, self.framesCounted) / self.total); + } +}; diff --git a/src/main.zig b/src/main.zig index ad4e4ed..54323f1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,6 +50,7 @@ pub fn main() !void { try context.switchToScene(TitleScene, null); var t = allegro.getTime(); + var fpsBuffer = [_]u8{0} ** 32; while (!context.shouldQuit) { const scene = if (context.scene) |scene| scene else { break; @@ -67,6 +68,7 @@ pub fn main() !void { allegro.c.ALLEGRO_EVENT_KEY_CHAR => { switch (event.keyboard.keycode) { allegro.c.ALLEGRO_KEY_ESCAPE => context.quit(), + allegro.c.ALLEGRO_KEY_F9 => context.showFPS = !context.showFPS, allegro.c.ALLEGRO_KEY_F11 => context.toggleFullScreen(), else => {}, } @@ -83,8 +85,15 @@ pub fn main() !void { const deltaT = @floatCast(f32, newT - t); t = newT; scene.update(&context, deltaT); + context.fps.update(deltaT); scene.render(&context); + if (context.showFPS) { + if (context.fonts.get("default")) |font| { + const fps = try std.fmt.bufPrintZ(fpsBuffer[0..], "FPS: {}", .{context.fps.fps()}); + allegro.drawText(font, context.palette.background.text, 10, 10, allegro.DrawTextFlags.ALIGN_LEFT, fps); + } + } allegro.flipDisplay(); }