diff options
| author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-08-30 16:08:44 +0330 | 
|---|---|---|
| committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-08-30 16:08:44 +0330 | 
| commit | 108a4a0c2c14d70c366bbfaa65b7287824fb33da (patch) | |
| tree | baacb1f067d7d78d3197cfd39d08ae1ef8d66e62 | |
| parent | 2199503244bef3a5e89e49a9588c7b260f097baf (diff) | |
added last_frame in nano seconds
updated example
| -rw-r--r-- | src/main.c | 6 | ||||
| -rw-r--r-- | src/ui/tui.c | 8 | ||||
| -rw-r--r-- | src/ui/tui.h | 1 | 
3 files changed, 12 insertions, 3 deletions
@@ -1,3 +1,4 @@ +#include <stdint.h>  #include <stdio.h>  #include <unistd.h> @@ -11,6 +12,9 @@ void on_button_click(const MOUSE_ACTION *mouse_action) {  WIDGET *ui_build(TUI *tui) {    if (is_clicked) { +    char frame[20+3+1]; +    const uint64_t fps = 1000000000/tui->last_frame; +    sprintf(frame, "%ldfps", fps);      return tui_make_box(          -1, -1,          tui_make_column(tui_make_widget_array( @@ -20,7 +24,7 @@ WIDGET *ui_build(TUI *tui) {                  tui_make_box(                      20, 3,                      tui_make_column(tui_make_widget_array( -                        tui_make_text("This is the second page", COLOR_BLUE), +                        tui_make_text(frame, COLOR_BLUE),                          tui_make_button(tui_make_text("       Back", COLOR_RED),                                          on_button_click))),                      COLOR_WHITE))))), diff --git a/src/ui/tui.c b/src/ui/tui.c index 16cef30..5f6acaa 100644 --- a/src/ui/tui.c +++ b/src/ui/tui.c @@ -1,6 +1,7 @@  #include "tui.h"  #include <stdarg.h> +#include <stdint.h>  #include <stdio.h>  #include <stdlib.h>  #include <string.h> @@ -513,10 +514,11 @@ bool widget_eqauls(const WIDGET *restrict left, const WIDGET *restrict right) {  const int NANO_TO_SECOND = 1000000000; -void nano_sleep(long int nano_seconds) { +int64_t nano_sleep(long int nano_seconds) {    struct timespec remaining,        request = {nano_seconds / NANO_TO_SECOND, nano_seconds % NANO_TO_SECOND};    nanosleep(&request, &remaining); +  return remaining.tv_sec * NANO_TO_SECOND + remaining.tv_nsec;  }  long int nano_time() { @@ -528,6 +530,7 @@ long int nano_time() {  void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder, int fps) {    const long int frame_nano =        (fps == FRAME_UNLIMITED) ? 0 : NANO_TO_SECOND / fps; +  int64_t last_remaining = 0;    while (1) {      const long int start = nano_time();      tui_save_cursor(); @@ -546,8 +549,9 @@ void tui_main_loop(TUI *tui, WIDGET_BUILDER widget_builder, int fps) {      tui_restore_cursor();      if (fps != FRAME_UNLIMITED) {        const long int diff = nano_time() - start; -      nano_sleep(frame_nano - diff); +      last_remaining = nano_sleep(frame_nano - diff + last_remaining);      } +    tui->last_frame = nano_time() - start;      if (kbhit()) {        if (handle_input(tui)) {          return; diff --git a/src/ui/tui.h b/src/ui/tui.h index 436e132..baf0c3a 100644 --- a/src/ui/tui.h +++ b/src/ui/tui.h @@ -62,6 +62,7 @@ typedef struct TUI {    int init_cursor_x, init_cursor_y;    TERMINAL_CELL *cells;    size_t cells_length; +  uint64_t last_frame; // in nanoseconds  } TUI;  typedef enum WIDGET_TYPE {  |