added frame rate
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Dec 2021 00:06:41 +0000 (18:06 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 21 Dec 2021 00:06:41 +0000 (18:06 -0600)
src/font.onyx
src/main.onyx

index 6e91de49cdd7d986ed573609bb01e31120482cbc..fd8114b6af66bd58bd3bc5f54474501ccec39672 100644 (file)
@@ -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;
index e6be5afc0a99d6b25d2c07342ceb2aaa06cc5aa5..1d99298364cf9fe6635414e3f4b760b5a793b5d5 100644 (file)
@@ -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();
     }