Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (117 loc) · 5.15 KB
/
Copy pathmain.cpp
File metadata and controls
136 lines (117 loc) · 5.15 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "../Engine/Engine.h"
#include <cstdio>
#include <iostream>
#include <GLFW/glfw3.h>
#include <glad/glad.h>
namespace {
struct ApplicationState {
Engine *engine = nullptr;
bool spawnLightWasDown = false;
bool resetWasDown = false;
bool playerWasDown = false;
};
void mouseCallback(GLFWwindow *window, const double xpos, const double ypos) {
auto *state = static_cast<ApplicationState *>(glfwGetWindowUserPointer(window));
if (state != nullptr && state->engine != nullptr) {
state->engine->handleMouseInput(xpos, ypos);
}
}
Engine::CameraInput readCameraInput(GLFWwindow *window) {
Engine::CameraInput input;
input.forward = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS;
input.backward = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS;
input.left = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS;
input.right = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS;
input.up = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS;
input.down = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
input.brake = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS;
input.shiftUp = glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS;
input.shiftDown = glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS;
return input;
}
}
int main() {
if (glfwInit() != GLFW_TRUE) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800, 600, "RacecaR", nullptr, nullptr);
if (window == nullptr) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
std::cerr << "Failed to initialize GLAD" << std::endl;
glfwDestroyWindow(window);
glfwTerminate();
return 1;
}
int result = 0;
{
Engine engine;
ApplicationState app{&engine};
glfwSetWindowUserPointer(window, &app);
glfwSetCursorPosCallback(window, mouseCallback);
if (!engine.initialize()) {
result = 1;
} else { //track one is degenerate, it wont work
constexpr char trackPath[] = ASSET_RESOURCES_PATH "F1_track2.glb";
constexpr char collisionPath[] = ASSET_RESOURCES_PATH "track2_collision.glb";
constexpr char lightPath[] = ASSET_RESOURCES_PATH "Light.glb";
engine.addStaticModel(trackPath, collisionPath, glm::vec3(0.0f));
engine.addLight(lightPath, glm::vec3(900.0f, 1500.0f, 650.0f),
glm::vec3(1.0f), false, true);
glClearColor(0.4f, 0.6f, 0.8f, 1.0f);
float lastFrame = static_cast<float>(glfwGetTime());
while (!glfwWindowShouldClose(window)) {
const float currentFrame = static_cast<float>(glfwGetTime());
const float deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
const bool playerDown = glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS;
if (playerDown && !app.playerWasDown) {
engine.togglePlayer();
}
app.playerWasDown = playerDown;
engine.handleKeyboardInput(readCameraInput(window), deltaTime);
const bool lightDown = glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS;
if (lightDown && !app.spawnLightWasDown) {
engine.addLight(lightPath, engine.getCameraPosition());
}
app.spawnLightWasDown = lightDown;
const bool resetDown = glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS;
if (resetDown && !app.resetWasDown) {
engine.clearDynamicObjects();
engine.clearLights();
}
app.resetWasDown = resetDown;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
engine.update(deltaTime);
if (engine.hasPlayer()) {
char title[80];
std::snprintf(title, sizeof(title), "RacecaR | Gear %d | %.0f RPM",
engine.getPlayerGear(), engine.getPlayerRPM());
glfwSetWindowTitle(window, title);
} else {
glfwSetWindowTitle(window, "RacecaR");
}
int width = 0;
int height = 0;
glfwGetFramebufferSize(window, &width, &height);
engine.render(width, height);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
}
glfwDestroyWindow(window);
glfwTerminate();
return result;
}