Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1/*
2 Copyright 2021 natinusala
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#include <libretro-common/features/features_cpu.h>
20#include <libretro-common/libretro.h>
21
22#include <atomic>
23#include <ctime>
24
25#include <functional>
26#include <vector>
27
28namespace aurore
29{
30
31typedef retro_time_t Time;
32
33namespace detail
34{
35 // A negative value: the real clock
36 inline std::atomic<Time> simulatedTimeUsec { -1 };
37 inline std::atomic<int64_t> simulatedWallTime { -1 };
38 inline std::atomic<Time> simulatedWallTimeSetAt { 0 };
39}
40
46{
47 Time simulated = detail::simulatedTimeUsec.load(std::memory_order_relaxed);
48 return simulated >= 0 ? simulated : cpu_features_get_time_usec();
49}
50
56{
57 return cpu_features_get_time_usec();
58}
59
66inline void setSimulatedTimeUsec(Time usec)
67{
68 detail::simulatedTimeUsec.store(usec, std::memory_order_relaxed);
69}
70
71inline bool isTimeSimulated()
72{
73 return detail::simulatedTimeUsec.load(std::memory_order_relaxed) >= 0;
74}
75
81inline std::time_t getWallTime()
82{
83 int64_t start = detail::simulatedWallTime.load(std::memory_order_relaxed);
84 if (start < 0)
85 return std::time(nullptr);
86 return (std::time_t)(start + (getCPUTimeUsec() - detail::simulatedWallTimeSetAt.load(std::memory_order_relaxed)) / 1000000);
87}
88
94inline void setSimulatedWallTime(int64_t seconds)
95{
96 detail::simulatedWallTimeSetAt.store(getCPUTimeUsec(), std::memory_order_relaxed);
97 detail::simulatedWallTime.store(seconds, std::memory_order_relaxed);
98}
99
104inline std::tm localTime(std::time_t time)
105{
106 std::tm result {};
107#ifdef _WIN32
108 localtime_s(&result, &time);
109#else
110 localtime_r(&time, &result);
111#endif
112 return result;
113}
114
115typedef std::function<void()> TickingGenericCallback;
116
117typedef std::function<void(bool)> TickingEndCallback;
119
120// Interface representing something that "ticks" every frame for a certain amount of frames,
121// like a timer, an animation, a background task...
122// The library manages a list of running tickings. Each ticking is reponsible for managing its own
123// lifetime by returning true or false in onUpdate.
125{
126 public:
127 virtual ~Ticking();
128
134 void start();
135
139 void stop();
140
148
157
161 bool isRunning();
162
167 static void updateTickings();
168
169 inline static std::vector<Ticking*> runningTickings;
170
171 protected:
180 virtual bool onUpdate(Time delta) = 0;
181
185 virtual void onStart() {};
186
191 virtual void onStop() {};
192
193 private:
194 void stop(bool finished);
195
196 bool running = false;
197
198 TickingEndCallback endCallback = [](bool finished) {};
199 TickingTickCallback tickCallback = [] {};
200};
201
202// Represents a "finite" ticking that runs for a known amount of time
203// and can be seek / reset / rewound.
204class FiniteTicking : public Ticking
205{
206 public:
213 void rewind();
214
220 void reset();
221
222 protected:
226 virtual void onRewind() {};
227
231 virtual void onReset() {};
232};
233
234} // namespace aurore
Definition time.hpp:205
virtual void onReset()
Definition time.hpp:231
virtual void onRewind()
Definition time.hpp:226
Definition time.hpp:125
virtual ~Ticking()
static std::vector< Ticking * > runningTickings
Definition time.hpp:169
virtual void onStop()
Definition time.hpp:191
virtual bool onUpdate(Time delta)=0
void setEndCallback(TickingEndCallback endCallback)
void setTickCallback(TickingTickCallback tickCallback)
virtual void onStart()
Definition time.hpp:185
static void updateTickings()
std::atomic< Time > simulatedWallTimeSetAt
Definition time.hpp:38
std::atomic< int64_t > simulatedWallTime
Definition time.hpp:37
std::atomic< Time > simulatedTimeUsec
Definition time.hpp:36
Definition actions.hpp:25
Time getCPUTimeUsec()
Definition time.hpp:45
bool isTimeSimulated()
Definition time.hpp:71
void setSimulatedWallTime(int64_t seconds)
Definition time.hpp:94
std::time_t getWallTime()
Definition time.hpp:81
void setSimulatedTimeUsec(Time usec)
Definition time.hpp:66
std::function< void()> TickingGenericCallback
Definition time.hpp:115
retro_time_t Time
Definition time.hpp:31
std::function< void(bool)> TickingEndCallback
Definition time.hpp:117
TickingGenericCallback TickingTickCallback
Definition time.hpp:118
std::tm localTime(std::time_t time)
Definition time.hpp:104
Time getRealTimeUsec()
Definition time.hpp:55