-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (58 loc) · 1.81 KB
/
main.cpp
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
// Currently, I have dynamic shader compilation working on Windows, but not on
// macOS. If this is set to 0, you must do the following before executing:
// glslc shader.vert -o vert.spv
// glslc shader.frag -o frag.spv
#ifdef __APPLE__
#define PHALANX_DYNAMIC_SHADER_COMPILATION 0
#else
#define PHALANX_DYNAMIC_SHADER_COMPILATION 1
#endif
#include "camera.h"
#include "model.h"
#include "renderer.h"
#include "texture.h"
#include "vertex.h"
#include "window_handler.h"
#include <chrono>
#include <iostream>
#include <stdexcept>
#include <vector>
void maybeLogFPS() {
static auto lastPrintTime = std::chrono::steady_clock::now();
static uint32_t fps = 0;
fps++;
auto now = std::chrono::steady_clock::now();
auto elapsedTime = now - lastPrintTime;
if (elapsedTime >= std::chrono::seconds(1)) {
std::cout << "FPS: " << fps << std::endl;
lastPrintTime = now;
fps = 0;
}
}
int main() {
try {
WindowHandler windowHandler{};
Camera camera{};
// load the model
Texture vikingRoomTexture = Texture::load("textures/viking_room.png");
Model vikingRoomModel = Model::load("models/viking_room.obj");
vikingRoomModel.texture = &vikingRoomTexture;
Renderer renderer(&windowHandler, &camera, &vikingRoomModel);
auto lastFrameTime = std::chrono::steady_clock::now();
while (renderer.isRunning()) {
windowHandler.pollEvents();
renderer.draw();
maybeLogFPS();
auto now = std::chrono::steady_clock::now();
auto frameDurationUs = std::chrono::duration_cast<std::chrono::microseconds>(now - lastFrameTime);
lastFrameTime = now;
camera.update(windowHandler.getKeyStates(), windowHandler.getMouseState(), frameDurationUs);
}
renderer.cleanup();
windowHandler.cleanup();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}