From 9f8dcae52a074ba76088da9c94471366f6e4ca8f Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Mon, 4 Jan 2021 21:43:00 +0000 Subject: [PATCH] Add PSP support --- include/bounce/common/time.h | 324 +++++++++++++++++++---------------- 1 file changed, 179 insertions(+), 145 deletions(-) diff --git a/include/bounce/common/time.h b/include/bounce/common/time.h index f7bf0d6..af99c81 100644 --- a/include/bounce/common/time.h +++ b/include/bounce/common/time.h @@ -21,145 +21,131 @@ #include -#define B3_WINDOWS 1 -#define B3_MAC 2 -#define B3_UNIX 3 - -#if defined (_WIN32) -#define B3_PLATFORM B3_WINDOWS -#elif defined( __APPLE__ ) -#define B3_PLATFORM B3_MAC -#elif defined(_arch_dreamcast) -#define B3_PLATFORM B3_DREAMCAST -#else -#define B3_PLATFORM B3_UNIX -#endif - -#if B3_PLATFORM == B3_WINDOWS +#if defined(__WIN32__) #include // A timer class that accumulates time. // Usefull for measuring elapsed times between code sections. -class b3Time +class b3Time { public: - b3Time() - { - LARGE_INTEGER c; - QueryPerformanceCounter(&c); - m_c0 = c.QuadPart; - m_t0 = 0.0; - m_t = 0.0; - } - - // Get the accumulated time in miliseconds from this timer. - float64 GetCurrentMilis() const - { - return m_t; - } + b3Time() + { + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + m_c0 = c.QuadPart; + m_t0 = 0.0; + m_t = 0.0; + } - // Get the elapsed time since this timer was updated. - float64 GetElapsedMilis() const - { - return m_t - m_t0; - } + // Get the accumulated time in miliseconds from this timer. + float64 GetCurrentMilis() const + { + return m_t; + } - // Add the elapsed time since this function was called to this timer. - void Update() - { - static float64 inv_frequency = 0.0; - if (inv_frequency == 0.0) - { - LARGE_INTEGER c; - QueryPerformanceFrequency(&c); - - float64 cycles_per_s = float64(c.QuadPart); - float64 s_per_cycle = 1.0 / cycles_per_s; - float64 ms_per_cycle = 1000.0 * s_per_cycle; - inv_frequency = ms_per_cycle; - } + // Get the elapsed time since this timer was updated. + float64 GetElapsedMilis() const + { + return m_t - m_t0; + } - LARGE_INTEGER c; - QueryPerformanceCounter(&c); + // Add the elapsed time since this function was called to this timer. + void Update() + { + static float64 inv_frequency = 0.0; + if (inv_frequency == 0.0) + { + LARGE_INTEGER c; + QueryPerformanceFrequency(&c); - float64 dt = inv_frequency * float64(c.QuadPart - m_c0); - m_c0 = c.QuadPart; - Add(dt); - } + float64 cycles_per_s = float64(c.QuadPart); + float64 s_per_cycle = 1.0 / cycles_per_s; + float64 ms_per_cycle = 1000.0 * s_per_cycle; + inv_frequency = ms_per_cycle; + } - // Add time to this timer. - void Add(float64 dt) - { - m_t0 = m_t; - m_t += dt; - } + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + + float64 dt = inv_frequency * float64(c.QuadPart - m_c0); + m_c0 = c.QuadPart; + Add(dt); + } + + // Add time to this timer. + void Add(float64 dt) + { + m_t0 = m_t; + m_t += dt; + } private: - u64 m_c0; - float64 m_t0; - float64 m_t; + u64 m_c0; + float64 m_t0; + float64 m_t; }; -#elif B3_PLATFORM == B3_MAC +#elif defined(__APPLE__) #include // A timer class that accumulates time. // Usefull for measuring elapsed times between code sections. -class b3Time +class b3Time { public: - b3Time() - { - m_c0 = mach_absolute_time(); - m_t0 = 0.0; - m_t = 0.0; - } - - // Get the accumulated time in miliseconds from this timer. - double GetCurrentMilis() const - { - return m_t; - } + b3Time() + { + m_c0 = mach_absolute_time(); + m_t0 = 0.0; + m_t = 0.0; + } - // Get the elapsed time since this timer was updated. - double GetElapsedMilis() const - { - return m_t - m_t0; - } + // Get the accumulated time in miliseconds from this timer. + double GetCurrentMilis() const + { + return m_t; + } - // Add the elapsed time since this function was called to this timer. - void Update() - { - static double inv_frequency = 0.0; - if (inv_frequency == 0.0) - { - mach_timebase_info_data_t info; - mach_timebase_info(&info); - inv_frequency = double(info.numer) / (double(info.denom) * 1.0e6); - } - - uint64_t c = mach_absolute_time(); - double dt = inv_frequency * (double)(c - m_c0); - m_c0 = c; - Add(dt); - } + // Get the elapsed time since this timer was updated. + double GetElapsedMilis() const + { + return m_t - m_t0; + } - // Add time to this timer. - void Add(double dt) - { - m_t0 = m_t; - m_t += dt; - } + // Add the elapsed time since this function was called to this timer. + void Update() + { + static double inv_frequency = 0.0; + if (inv_frequency == 0.0) + { + mach_timebase_info_data_t info; + mach_timebase_info(&info); + inv_frequency = double(info.numer) / (double(info.denom) * 1.0e6); + } + + uint64_t c = mach_absolute_time(); + double dt = inv_frequency * (double)(c - m_c0); + m_c0 = c; + Add(dt); + } + + // Add time to this timer. + void Add(double dt) + { + m_t0 = m_t; + m_t += dt; + } private: - uint64_t m_c0; - double m_t0; - double m_t; + uint64_t m_c0; + double m_t0; + double m_t; }; -#elif B3_PLATFORM == B3_DREAMCAST +#elif defined(_arch_dreamcast) #include @@ -207,55 +193,103 @@ private: double m_t; }; +#elif defined(__PSP__) + +#include + +class b3Time +{ +public: + b3Time() + { + m_c0 = sceKernelGetSystemTimeLow(); + m_t0 = 0.0; + m_t = 0.0; + } + + // Get the accumulated time in miliseconds from this timer. + double GetCurrentMilis() const + { + return m_t; + } + + // Get the elapsed time since this timer was updated. + double GetElapsedMilis() const + { + return m_t - m_t0; + } + + // Add the elapsed time since this function was called to this timer. + void Update() + { + uint64_t c = sceKernelGetSystemTimeLow(); + double dt = double(c - m_c0) * 0.000001; + m_c0 = c; + Add(dt); + } + + // Add time to this timer. + void Add(double dt) + { + m_t0 = m_t; + m_t += dt; + } + +private: + uint64_t m_c0; + double m_t0; + double m_t; +}; + #else #include // A timer class that accumulates time. // Usefull for measuring elapsed times between code sections. -class b3Time +class b3Time { public: - b3Time() - { - clock_gettime(CLOCK_MONOTONIC, &m_c0); - m_t0 = 0.0; - m_t = 0.0; - } - - // Get the accumulated time in miliseconds from this timer. - double GetCurrentMilis() const - { - return m_t; - } + b3Time() + { + clock_gettime(CLOCK_MONOTONIC, &m_c0); + m_t0 = 0.0; + m_t = 0.0; + } - // Get the elapsed time since this timer was updated. - double GetElapsedMilis() const - { - return m_t - m_t0; - } + // Get the accumulated time in miliseconds from this timer. + double GetCurrentMilis() const + { + return m_t; + } - // Add the elapsed time since this function was called to this timer. - void Update() - { - struct timespec c; - clock_gettime(CLOCK_MONOTONIC, &c); - double dt = (double)(c.tv_nsec - m_c0.tv_nsec) * 1.0e-6; - m_c0 = c; - Add(dt); - } + // Get the elapsed time since this timer was updated. + double GetElapsedMilis() const + { + return m_t - m_t0; + } - // Add time to this timer. - void Add(double dt) - { - m_t0 = m_t; - m_t += dt; - } + // Add the elapsed time since this function was called to this timer. + void Update() + { + struct timespec c; + clock_gettime(CLOCK_MONOTONIC, &c); + double dt = (double)(c.tv_nsec - m_c0.tv_nsec) * 1.0e-6; + m_c0 = c; + Add(dt); + } + + // Add time to this timer. + void Add(double dt) + { + m_t0 = m_t; + m_t += dt; + } private: - struct timespec m_c0; - double m_t0; - double m_t; + struct timespec m_c0; + double m_t0; + double m_t; }; #endif // B3_PLATFORM