summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 9a6a955a90ffd4ba4dba3fbda3737efcca6e3903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#include <chrono>
#include <cstdio>
#include <cmath>
#include <exception>
#include <thread>
#include <csignal>

#include "asset-manager.hpp"
#include "ecs.hpp"
#include "game.hpp"
#include "window.hpp"
#include "entities.hpp"

void signal_handler(int signal_num) {
	printf("Received signal: %d - shutting down\n", signal_num);
	pkeSettings.isGameRunning = false;
}

PKEWindowProperties windowProps{};

void Tick(double delta) {
	Game_Tick(delta);
}

int main() { 
	signal(SIGTERM, signal_handler);

	printf("PKE ENTERING\n");
	try {
		AM_Init();
		Game_Init();
		ECS_Init();
		CreateWindow(&windowProps);
		EntityType_Init();

		auto steadyClock = std::chrono::steady_clock();
		GameTimePoint lastTimePoint = steadyClock.now();
		double deltaTillNextRender = pkeSettings.deltaPerFrame;
		GameTimePoint lastLogTimePoint = steadyClock.now();
		int64_t tickCount = 0;

		int64_t nsAhead = 0.0;

		while (pkeSettings.isGameRunning) {

			glfwPollEvents();

			if (nsAhead > 0) {
				std::this_thread::sleep_until(lastTimePoint + GameTimeDuration(nsAhead + 1));
				nsAhead = 0;
			}

			GameTimePoint currentTimePoint = steadyClock.now();
			double deltaThisTick = (currentTimePoint - lastTimePoint).count() / NANO_DENOM_DOUBLE;
			deltaThisTick = std::min(deltaThisTick, pkeSettings.minimumDeltaPerFrame);
			deltaTillNextRender -= deltaThisTick;
			bool shouldRender = deltaTillNextRender < deltaThisTick;
			lastTimePoint = currentTimePoint;

			if (shouldRender) {
				if (deltaTillNextRender > 0.0 && deltaThisTick < pkeSettings.deltaPerFrame) {
					/*
					 * We are ahead of schedule && the current tick is faster than our target FPS.
					 *  Simulate the extra time we are ahead and prepare to sleep the difference
					 *  before the next tick.
					 */
					nsAhead = std::ceil(deltaTillNextRender * NANO_DENOM_DOUBLE);
					deltaThisTick += deltaTillNextRender;
				}
				deltaTillNextRender += pkeSettings.deltaPerFrame;
			}

			tickCount += 1;
			Tick(deltaThisTick);

			if (shouldRender) {
				Render();
			}

			if (shouldRender == false && (currentTimePoint - lastLogTimePoint).count() > std::chrono::nanoseconds::period::den) {
				lastLogTimePoint = currentTimePoint;
				printf("TPS: ~%ld - actual:%ld\n", int64_t(1 / deltaThisTick), tickCount);
				tickCount = 0;
			}

			pkeSettings.isGameRunning = !glfwWindowShouldClose(window);
		}

		vkDeviceWaitIdle(vkDevice);

	} catch (const std::exception &exc) {
		printf("EXCEPTION: %s\n", exc.what());
	} catch (const char *err) {
		printf("UNHANDLED EXCEPTION: %s\n", err);
	} catch (...) {
		printf("UNHANDLED EXCEPTION\n");
	}
	printf("PKE SHUTDOWN INITIATED\n");
	Event_Teardown();
	EntityType_Teardown();
	ECS_Teardown();
	AM_DebugPrint();
	AM_Teardown();
	DestroyWindow();
	Pke_DebugPrint();
	printf("PKE EXITING\n");
	return 0;
}