LCOV - code coverage report
Current view: top level - src - gametimer.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 18 18
Test Date: 2026-04-03 02:26:39 Functions: 100.0 % 3 3

            Line data    Source code
       1              : //////////////////////////////////////////////////////////////////
       2              : // Description : Handles timing and delay during gameplay       //
       3              : //////////////////////////////////////////////////////////////////
       4              : #ifdef _WIN32
       5              : #include <windows.h>
       6              : #endif
       7              : 
       8              : #include <sys/time.h>
       9              : #include <cstdio>
      10              : #include "gametimer.h"
      11              : 
      12              : //------------------------------------------------------------------------------
      13           21 : bool GameTimer::init() {
      14              : #ifdef _WIN32
      15              :   if (!QueryPerformanceFrequency(&m_ticksPerSecond)) {
      16              :     // system doesn't support high-res timer
      17              :     return false;
      18              :   }
      19              : 
      20              :   QueryPerformanceCounter(&m_startTime);
      21              : #else
      22              :   timeval tv;
      23           21 :   gettimeofday(&tv, 0);
      24              :   //m_startTime = ((double)tv.tv_usec / 1000.0 + (double)tv.tv_sec * 1000.0);
      25           21 :   m_startTime = tv.tv_usec / 1000 + tv.tv_sec * 1000;
      26              : 
      27           21 :   fps = 0.0f;
      28              : #endif
      29              : 
      30           21 :   return true;
      31              : }
      32              : 
      33              : //------------------------------------------------------------------------------
      34            4 : float GameTimer::getElapsedSeconds() {
      35              : #ifdef _WIN32
      36              :   static LARGE_INTEGER s_lastTime = m_startTime;
      37              :   LARGE_INTEGER currentTime;
      38              : 
      39              :   QueryPerformanceCounter(&currentTime);
      40              : 
      41              :   float seconds = ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart) /
      42              :     (float)m_ticksPerSecond.QuadPart;
      43              : 
      44              :   // reset the timer
      45              :   s_lastTime = currentTime;
      46              : #else
      47            4 :   float seconds = 0.0f;
      48            4 :   int tries = 0;
      49              : 
      50            4 :   fps = MAX_FPS;
      51              : 
      52      3656725 :   while (fps >= MAX_FPS || ++tries > 100) {
      53              :     timeval tv;
      54      3656721 :     gettimeofday(&tv, 0);
      55              : 
      56              :     //double currentTime = ((double)tv.tv_usec / 1000.0 + (double)tv.tv_sec * 1000.0);
      57              :     //double seconds = (currentTime - m_startTime) / 1000.0;
      58              : 
      59      3656721 :     long currentTime = (tv.tv_usec / 1000 + tv.tv_sec * 1000);
      60      3656721 :     seconds += float(currentTime - m_startTime) / 1000.0f;
      61              : 
      62      3656721 :     fps = 1.0f / seconds;
      63              : 
      64              :     PRINT_DEBUG_LVL(5, "Start time: %ld, current time: %ld\n", m_startTime, currentTime);
      65              :     PRINT_DEBUG_LVL(5, "Seconds: %f\n", seconds);
      66              : 
      67      3656721 :     m_startTime = currentTime;
      68              :   }
      69              : #endif
      70              : 
      71            4 :   return seconds;
      72              : }
      73              : 
      74              : //------------------------------------------------------------------------------
      75            2 : float GameTimer::getFPS() {
      76              : #ifdef _WIN32
      77              :   unsigned long elapsedFrames = 1;
      78              :   static LARGE_INTEGER s_lastTime = m_startTime;
      79              :   LARGE_INTEGER currentTime;
      80              : 
      81              :   QueryPerformanceCounter(&currentTime);
      82              : 
      83              :   float fps = (float)elapsedFrames * (float)m_ticksPerSecond.QuadPart /
      84              :     ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart);
      85              : 
      86              :   // reset the timer
      87              :   s_lastTime = currentTime;
      88              : 
      89              : #endif
      90              :   /*
      91              :      long m_lastTime = m_startTime;
      92              : 
      93              :      timeval tv;
      94              :      gettimeofday(&tv, 0);
      95              :      long currentTime = tv.tv_usec / 1000 + (tv.tv_sec * 1000);
      96              : 
      97              :      float fps = 1.0f / (float)(currentTime - m_lastTime) * 1000.0f;
      98              :      */
      99              : 
     100            2 :   return fps;
     101              : }
        

Generated by: LCOV version 2.4-0