KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
time.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../../ktech.hpp"
25#undef KTECH_DEFINITION
26
27#include <chrono>
28
35{
36public:
38 enum class Measurement : uint8_t
39 {
40 ticks,
41 seconds,
42 milliseconds,
43 microseconds
44 };
45
46 struct Invocation;
47
48 unsigned long tpsLimit;
49 float tps = 0;
50 float tpsPotential = 0;
51 long deltaTime = 0;
52 unsigned long ticksCounter = 0;
53
54 void CallInvocations();
55 void WaitUntilNextTick();
56
57private:
58 bool m_changedThisTick = false;
59 Engine& engine;
60 std::chrono::steady_clock::time_point m_currentTickStart;
61 std::vector<Invocation*> m_invocations;
62
63 inline Time(Engine& engine, unsigned long ticksPerSecondLimit)
64 : engine(engine), tpsLimit(ticksPerSecondLimit) {}
65
66 [[nodiscard]] auto TimeToMicroseconds(long p_time, Measurement p_measurement) const -> long;
67 void RegisterCallback(Invocation* invocation);
68 void DeregisterCallback(Invocation* invocation);
69
70 friend class Invocation;
71 friend class Output;
72 friend class Engine;
73};
Complete engine containing all engine components.
Definition engine.hpp:41
Engine component responsible for outputting rendered images.
Definition output.hpp:42
Engine component responsible for game loop timing.
Definition time.hpp:35
void WaitUntilNextTick()
Sleeps and returns when the next tick should start.
Definition time.cpp:111
unsigned long tpsLimit
Max ticks allowed to occur in a second. You set this value in Engine::Engine(), and you can change it...
Definition time.hpp:48
float tpsPotential
Ticks per second if it wasn't limited by Time::tpsLimit.
Definition time.hpp:50
Measurement
Time measurement.
Definition time.hpp:39
float tps
Actual ticks per second. Corresponds to Time::deltaTime.
Definition time.hpp:49
void CallInvocations()
Call callback functions of finished Invocations.
Definition time.cpp:51
long deltaTime
Duration of the last tick, in microseconds.
Definition time.hpp:51
unsigned long ticksCounter
Total ticks since game started.
Definition time.hpp:52