diff --git a/src/main.zig b/src/main.zig index 8d69191..06b7f7d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -44,6 +44,9 @@ pub fn main() !void { var shifted = false; var start_time: u32 = 0; var timer = Timer{}; + var fps_timer = Timer{}; + fps_timer.start(); + var frames: f32 = 0; while (!quit) { var event: sdl.SDL_Event = undefined; while (sdl.SDL_PollEvent(&event) != 0) { @@ -104,7 +107,7 @@ pub fn main() !void { .{ .x = @intCast(game_state.SCREEN_WIDTH - text.w), .y = 24 }, ); - var buf: [8]u8 = undefined; + var buf: [16]u8 = undefined; const timer_ms = timer.getTicks(); // const timer_base = std.math.log10(timer_ms) + 3; const time_str: [*:0]const u8 = try std.fmt.bufPrintZ(&buf, "{d}ms", .{timer_ms}); @@ -114,6 +117,14 @@ pub fn main() !void { .{ .x = @intCast(game_state.SCREEN_WIDTH - time_text.w), .y = 64 }, ); + const fps_time: f32 = @floatFromInt(fps_timer.getTicks() / 1000); + const avg_fps: f32 = @round((frames / fps_time) * 100) / 100.0; + var fps_text = try GameText.loadFromRenderedText(try std.fmt.bufPrintZ(&buf, "{d} fps", .{avg_fps}), RGBAColor.whiteSmoke().tosdl()); + try fps_text.render( + &game_state, + .{ .x = 5, .y = 5 }, + ); + // Render red rect // const fill_rect: sdl.struct_SDL_Rect = sdl.SDL_Rect{ .x = SCREEN_WIDTH / 4, .y = SCREEN_HEIGHT / 4, .w = SCREEN_WIDTH / 2 + img_pos.x, .h = SCREEN_HEIGHT / 2 + img_pos.y }; // _ = sdl.SDL_SetRenderDrawColor(renderer, 0xff, 0x00, 0x00, 0xff); @@ -121,11 +132,12 @@ pub fn main() !void { // _ = sdl.SDL_RenderSetViewport(renderer, &.{ .x = SCREEN_WIDTH / 2, .y = SCREEN_HEIGHT / 2, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT / 2 }); // _ = sdl.SDL_RenderCopy(renderer, texture, null, null); _ = sdl.SDL_RenderPresent(renderer); + frames += 1; // var dest_rect = sdl.SDL_Rect{ .x = src_rect.x + img_pos.x, .y = src_rect.y + img_pos.y, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT }; // _ = sdl.SDL_BlitScaled(zig_image, &src_rect, screen_surface, &dest_rect); // _ = sdl.SDL_UpdateWindowSurface(window); - sdl.SDL_Delay(100); + // sdl.SDL_Delay(100); } return; }