FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_timer.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
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#ifndef _FGE_C_TIMER_HPP_INCLUDED
18#define _FGE_C_TIMER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_callback.hpp"
22#include <chrono>
23#include <mutex>
24#include <string>
25
26namespace fge
27{
28
34class FGE_API Timer
35{
36public:
37 Timer(fge::Timer const& timer);
38 Timer(fge::Timer&& timer) noexcept;
39
45 explicit Timer(std::chrono::milliseconds const& goal);
52 Timer(std::chrono::milliseconds const& goal, bool paused);
60 Timer(std::chrono::milliseconds const& goal, std::string name, bool paused);
61
67 void setName(std::string const& name);
73 std::string const& getName() const;
74
83 void setGoalDuration(std::chrono::milliseconds const& t);
91 void addToGoal(std::chrono::milliseconds const& t);
99 void subToGoal(std::chrono::milliseconds const& t);
100
106 void setElapsedTime(std::chrono::milliseconds const& t);
112 void addToElapsedTime(std::chrono::milliseconds const& t);
118 void subToElapsedTime(std::chrono::milliseconds const& t);
119
127 std::chrono::steady_clock::time_point const& getLifeTimePoint() const;
133 std::chrono::milliseconds getLifeDuration() const;
134
140 std::chrono::milliseconds const& getElapsedTime() const;
146 std::chrono::milliseconds const& getGoalDuration() const;
147
153 std::chrono::milliseconds getTimeLeft() const;
154
160 bool goalReached() const;
166 void restart();
167
171 void pause();
175 void resume();
181 bool isPaused() const;
182
184
185private:
186 std::chrono::steady_clock::time_point const g_lifeTimePoint;
187
188 std::chrono::milliseconds g_elapsedTime;
189 std::chrono::milliseconds g_goalDuration;
190
191 bool g_isPaused;
192
193 std::string g_name;
194
195 mutable std::mutex g_mutex;
196};
197
198} // namespace fge
199
200#endif // _FGE_C_TIMER_HPP_INCLUDED
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
A timer that can be used with the timer manager to handle time.
Definition C_timer.hpp:35
void setGoalDuration(std::chrono::milliseconds const &t)
Set the goal duration of the timer.
void pause()
Pause the timer.
std::chrono::milliseconds getTimeLeft() const
Get the remaining time of the timer.
void subToGoal(std::chrono::milliseconds const &t)
Subtract an amount of time to the goal duration of the timer.
void restart()
Restart the timer.
fge::CallbackHandler< fge::Timer & > _onTimeReached
The callback called when the timer reaches the goal.
Definition C_timer.hpp:183
std::chrono::milliseconds const & getGoalDuration() const
Get the goal time of the timer.
std::chrono::milliseconds getLifeDuration() const
Get the life time of the timer as milliseconds.
void setName(std::string const &name)
Set the timer name.
void setElapsedTime(std::chrono::milliseconds const &t)
Set the elapsed time of the timer.
void resume()
Unpause the timer.
bool goalReached() const
Check if the goal has been reached.
Timer(std::chrono::milliseconds const &goal)
Create a new timer with the given time goal.
std::string const & getName() const
Get the timer name.
std::chrono::steady_clock::time_point const & getLifeTimePoint() const
Get the life time of the timer.
std::chrono::milliseconds const & getElapsedTime() const
Get the elapsed time of the timer.
void subToElapsedTime(std::chrono::milliseconds const &t)
Subtract an amount of time to the elapsed time of the timer.
Timer(std::chrono::milliseconds const &goal, bool paused)
Create a new timer with the given time goal and pause state.
void addToElapsedTime(std::chrono::milliseconds const &t)
Add an amount of time to the elapsed time of the timer.
void addToGoal(std::chrono::milliseconds const &t)
Add an amount of time to the goal duration of the timer.
bool isPaused() const
Check if the timer is paused.
Timer(std::chrono::milliseconds const &goal, std::string name, bool paused)
Create a new timer with the given time goal, name and pause state.