cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

timer.mm (3288B)


      1/*
      2 * Gearboy - Nintendo Game Boy Emulator
      3 * Copyright (C) 2012  Ignacio Sanchez
      4 
      5 * This program is free software: you can redistribute it and/or modify
      6 * it under the terms of the GNU General Public License as published by
      7 * the Free Software Foundation, either version 3 of the License, or
      8 * any later version.
      9 
     10 * This program is distributed in the hope that it will be useful,
     11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13 * GNU General Public License for more details.
     14 
     15 * You should have received a copy of the GNU General Public License
     16 * along with this program.  If not, see http://www.gnu.org/licenses/
     17 *
     18 */
     19
     20#include "timer.h"
     21
     22Timer::Timer(void)
     23{
     24    m_bCalculateFPS = false;
     25
     26    Reset();
     27}
     28
     29void Timer::Reset(void)
     30{
     31    mach_timebase_info_data_t timebase;
     32    mach_timebase_info(&timebase);
     33    
     34    m_fResolution = (float) timebase.numer / (float) timebase.denom;
     35    
     36    m_fFPS = 0;
     37    m_bIsRunning = false;
     38    m_i64StopedTicks = 0;
     39    m_i64BaseTicks = 0;
     40
     41    m_fLastUpdate = 0;
     42    m_iFrameCount = 0;
     43
     44    m_fFrameTime = 0;
     45    m_fDeltaTime = 0.016f; ///--- 60 FPS
     46
     47    m_fOffset = 0;
     48
     49    for (int i=0; i<FRAME_RATE_AVERAGE; i++)
     50    {
     51        m_fDeltaTimeAccumulation[i] = 0.016f;
     52    }
     53
     54    m_iCurrentAccumulator = 0;
     55}
     56
     57Timer::~Timer(void)
     58{
     59
     60}
     61
     62void Timer::Start(void)
     63{
     64    m_i64BaseTicks = mach_absolute_time();
     65    m_bIsRunning = true;
     66    m_fLastUpdate = 0;
     67    m_iFrameCount = 0;
     68    m_fFrameTime = 0;
     69    m_fDeltaTime = 0;
     70    m_fOffset = 0;
     71}
     72
     73void Timer::Stop(void)
     74{
     75    if (m_bIsRunning)
     76    {
     77        m_i64StopedTicks = mach_absolute_time();
     78        m_bIsRunning = false;
     79    }
     80}
     81
     82void Timer::Continue(void)
     83{
     84    if (!m_bIsRunning)
     85    {
     86        u64 Ticks;
     87        Ticks = mach_absolute_time();
     88        m_i64BaseTicks += Ticks - m_i64StopedTicks;
     89        m_bIsRunning = true;
     90    }
     91}
     92
     93float Timer::GetActualTime(void) const
     94{
     95    u64 Ticks;
     96
     97    if (m_bIsRunning)
     98    {
     99        Ticks = mach_absolute_time();
    100    }
    101    else
    102        Ticks = m_i64StopedTicks;
    103
    104    Ticks -= m_i64BaseTicks;
    105
    106    return((((float) Ticks) * m_fResolution) / 1000000000.0f) +m_fOffset;
    107}
    108
    109void Timer::Update(void)
    110{
    111    m_fDeltaTime = GetActualTime() - m_fFrameTime;    
    112
    113    if (m_fDeltaTime > 0.1f)
    114    {
    115        m_fDeltaTime = 0.1f;
    116    }
    117
    118    m_fFrameTime += m_fDeltaTime;
    119
    120    m_fDeltaTimeAccumulation[m_iCurrentAccumulator] = m_fDeltaTime;
    121    m_iCurrentAccumulator++;
    122    m_iCurrentAccumulator %= FRAME_RATE_AVERAGE;
    123
    124    float finalDeltaFrame = 0.0f;
    125
    126    for (int i=0; i<FRAME_RATE_AVERAGE; i++)
    127    {
    128        finalDeltaFrame += m_fDeltaTimeAccumulation[i];
    129    }
    130
    131    m_fDeltaTime = finalDeltaFrame / FRAME_RATE_AVERAGE;
    132
    133    if (m_bCalculateFPS)
    134    {
    135        m_iFrameCount++;
    136
    137        if (m_fFrameTime - m_fLastUpdate > FPS_REFRESH_TIME)
    138        {
    139            m_fFPS = m_iFrameCount / (m_fFrameTime - m_fLastUpdate);
    140            m_iFrameCount = 0;
    141            m_fLastUpdate = m_fFrameTime;
    142        }
    143    }
    144}
    145
    146float Timer::GetFPS(void) const
    147{
    148    return m_fFPS;
    149}
    150
    151float Timer::GetFrameTime(void) const
    152{
    153    return m_fFrameTime;
    154}
    155
    156float Timer::GetDeltaTime(void) const
    157{
    158    return m_fDeltaTime;
    159}
    160
    161bool Timer::IsRunning(void) const
    162{
    163    return m_bIsRunning;
    164}