From 9cf6ae0cabea2c9681c802344ac9fe9b2ab8307d Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 20 Dec 2021 18:06:41 -0600 Subject: [PATCH] added frame rate --- src/font.onyx | 6 ++++++ src/main.onyx | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/font.onyx b/src/font.onyx index 6e91de4..fd8114b 100644 --- a/src/font.onyx +++ b/src/font.onyx @@ -108,6 +108,12 @@ alloca :: macro (size: u32) -> rawptr { return __stack_top; } +font_print :: (font: Font, x, y: f32, format: str, va: ..any) { + buf: [1024] u8; + msg := conv.str_format_va(buf, format, va); + font_draw(font, x, y, msg); +} + font_draw :: (font: Font, x, y: f32, msg: str) { quads: ^stbtt_aligned_quad = alloca(msg.count * sizeof stbtt_aligned_quad); quad_num := 0; diff --git a/src/main.onyx b/src/main.onyx index e6be5af..1d99298 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -139,6 +139,8 @@ update :: (dt: f32) { update_world_matrix(); } +game_fps: i32; + draw :: () { glClearColor(.7, .7, .9, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -146,7 +148,7 @@ draw :: () { shader_use(world_shader); world_draw(world); - font_draw(font, 100, 100, "Hello!"); + font_print(font, 0, 32, "FPS: {}", game_fps); glfwSwapBuffers(window); } @@ -155,12 +157,24 @@ main_loop :: () { last := glfwGetTime(); now := last; + seconds := 0.0; + frame_count := 0; + while !glfwWindowShouldClose(window) { glfwPollEvents(); now = glfwGetTime(); dt := now - last; last = now; + + seconds += dt; + frame_count += 1; + if seconds >= 1 { + game_fps = ~~frame_count; + seconds -= 1; + frame_count = 0; + } + update(~~dt); draw(); } -- 2.25.1