Added FPS counter.

Not forcing vsync.
This commit is contained in:
Sander Schobers 2023-06-02 14:21:50 +02:00
parent 5bfa8b2c51
commit 0fb3650a9a
4 changed files with 57 additions and 7 deletions

View File

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

View File

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

25
src/engine/fps.zig Normal file
View File

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

View File

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