Added fps timer and fps clock

This commit is contained in:
Nathan Anderson 2025-01-06 09:51:43 -07:00
parent 5680b1a5a5
commit ba53a7dd66

View File

@ -44,6 +44,9 @@ pub fn main() !void {
var shifted = false; var shifted = false;
var start_time: u32 = 0; var start_time: u32 = 0;
var timer = Timer{}; var timer = Timer{};
var fps_timer = Timer{};
fps_timer.start();
var frames: f32 = 0;
while (!quit) { while (!quit) {
var event: sdl.SDL_Event = undefined; var event: sdl.SDL_Event = undefined;
while (sdl.SDL_PollEvent(&event) != 0) { while (sdl.SDL_PollEvent(&event) != 0) {
@ -104,7 +107,7 @@ pub fn main() !void {
.{ .x = @intCast(game_state.SCREEN_WIDTH - text.w), .y = 24 }, .{ .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_ms = timer.getTicks();
// const timer_base = std.math.log10(timer_ms) + 3; // const timer_base = std.math.log10(timer_ms) + 3;
const time_str: [*:0]const u8 = try std.fmt.bufPrintZ(&buf, "{d}ms", .{timer_ms}); 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 }, .{ .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 // 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 }; // 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); // _ = 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_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_RenderCopy(renderer, texture, null, null);
_ = sdl.SDL_RenderPresent(renderer); _ = 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 }; // 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_BlitScaled(zig_image, &src_rect, screen_surface, &dest_rect);
// _ = sdl.SDL_UpdateWindowSurface(window); // _ = sdl.SDL_UpdateWindowSurface(window);
sdl.SDL_Delay(100); // sdl.SDL_Delay(100);
} }
return; return;
} }