From f6ec61b15c261d3936e4ae44567ef0097093a049 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 23 Dec 2016 15:36:47 +0100 Subject: [PATCH] Create rarch_timer functions and have menu_dialog.c use them --- menu/widgets/menu_dialog.c | 24 +++++++--------------- performance_counters.c | 42 ++++++++++++++++++++++++++++++++++++++ performance_counters.h | 20 ++++++++++++++++++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/menu/widgets/menu_dialog.c b/menu/widgets/menu_dialog.c index a4f19879ab..de64357a88 100644 --- a/menu/widgets/menu_dialog.c +++ b/menu/widgets/menu_dialog.c @@ -35,6 +35,7 @@ #include "../../tasks/tasks_internal.h" #include "../../input/input_config.h" +#include "../../performance_counters.h" static bool menu_dialog_pending_push = false; static bool menu_dialog_active = false; @@ -54,31 +55,20 @@ int menu_dialog_iterate(char *s, size_t len, const char *label) { case MENU_DIALOG_WELCOME: { - static int64_t timeout_end; - int64_t timeout; - static bool timer_begin = false; - static bool timer_end = false; - int64_t current = cpu_features_get_time_usec(); + static rarch_timer_t timer; - if (!timer_begin) - { - timeout_end = cpu_features_get_time_usec() + - 3 /* seconds */ * 1000000; - timer_begin = true; - timer_end = false; - } + if (!rarch_timer_is_running(&timer)) + rarch_timer_begin(&timer, 3); - timeout = (timeout_end - current) / 1000000; + rarch_timer_tick(&timer); menu_hash_get_help_enum( MENU_ENUM_LABEL_WELCOME_TO_RETROARCH, s, len); - if (!timer_end && timeout <= 0) + if (rarch_timer_has_expired(&timer)) { - timer_end = true; - timer_begin = false; - timeout_end = 0; + rarch_timer_end(&timer); do_exit = true; } } diff --git a/performance_counters.c b/performance_counters.c index dda97c3f09..cb0c4a50d7 100644 --- a/performance_counters.c +++ b/performance_counters.c @@ -147,3 +147,45 @@ void performance_counter_stop(struct retro_perf_counter *perf) perf->total += cpu_features_get_perf_counter() - perf->start; } + +void rarch_timer_tick(rarch_timer_t *timer) +{ + if (!timer) + return; + timer->current = cpu_features_get_time_usec(); + timer->timeout = (timer->timeout_end - timer->current) / 1000000; +} + +bool rarch_timer_is_running(rarch_timer_t *timer) +{ + if (!timer) + return false; + return timer->timer_begin; +} + +bool rarch_timer_has_expired(rarch_timer_t *timer) +{ + if (!timer) + return true; + if (!timer->timer_end && timer->timeout <= 0) + return true; + return false; +} + +void rarch_timer_end(rarch_timer_t *timer) +{ + if (!timer) + return; + timer->timer_end = true; + timer->timer_begin = false; + timer->timeout_end = 0; +} + +void rarch_timer_begin(rarch_timer_t *timer, uint64_t sec) +{ + if (!timer) + return; + timer->timeout_end = cpu_features_get_time_usec() + sec * 1000000; + timer->timer_begin = true; + timer->timer_end = false; +} diff --git a/performance_counters.h b/performance_counters.h index 9735622f0c..6eaf232bc1 100644 --- a/performance_counters.h +++ b/performance_counters.h @@ -18,6 +18,7 @@ #define _PERFORMANCE_COUNTERS_H #include +#include #include #include @@ -28,6 +29,15 @@ RETRO_BEGIN_DECLS #define MAX_COUNTERS 64 #endif +typedef struct rarch_timer +{ + int64_t current; + int64_t timeout; + int64_t timeout_end; + bool timer_begin; + bool timer_end; +} rarch_timer_t; + struct retro_perf_counter **retro_get_perf_counter_rarch(void); struct retro_perf_counter **retro_get_perf_counter_libretro(void); @@ -62,6 +72,16 @@ void performance_counter_start(struct retro_perf_counter *perf); **/ void performance_counter_stop(struct retro_perf_counter *perf); +void rarch_timer_tick(rarch_timer_t *timer); + +bool rarch_timer_is_running(rarch_timer_t *timer); + +bool rarch_timer_has_expired(rarch_timer_t *timer); + +void rarch_timer_begin(rarch_timer_t *timer, uint64_t ms); + +void rarch_timer_end(rarch_timer_t *timer); + RETRO_END_DECLS #endif