-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
117 lines (99 loc) · 3.12 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
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
// OpenGL linker
#include <glad/glad.h>
// Windows initialization
#include <GLFW/glfw3.h>
// Engine
#include "engine/model/Model.h"
#include "engine/player/Player.h"
#include "engine/game/GameInstance.h"
// GLM
#include <glm/fwd.hpp>
// C++
#include <exception>
#include <iostream>
#include <stdexcept>
int width = 1600;
int height = 900;
GameInstance *gameInstance;
void resizeCallback(GLFWwindow *window, int newWidth, int newHeight) {
glViewport(0, 0, newWidth, newHeight);
width = newHeight;
height = newHeight;
gameInstance->processResize(newWidth, newHeight);
}
bool firstMouse = true;
float lastX, lastY;
void mouseCallback(GLFWwindow *window, double xPos, double yPos) {
if (firstMouse) {
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
float xOffset = xPos - lastX;
float yOffset = lastY - yPos;
lastX = xPos;
lastY = yPos;
gameInstance->processMouseMovement(xOffset, yOffset);
}
GLFWwindow *createWindow() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window =
glfwCreateWindow(width, height, "Low Poly Adventure", nullptr, nullptr);
if (window == nullptr) {
throw std::runtime_error("Failed to initialize window");
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, resizeCallback);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
return window;
}
void initGlad() {
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
throw std::runtime_error("Failed to initialize GLAD");
}
}
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
void mainLoop(GLFWwindow *window) {
gameInstance = new GameInstance("resources/models/level_1/level_1.gltf", "resources/models/player/player.gltf",
width,
height);
// timing
float deltaTime; // time between current frame and last frame
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window)) {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
gameInstance->tick(window, deltaTime);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gameInstance->draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
int main() {
try {
GLFWwindow *window = createWindow();
initGlad();
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
mainLoop(window);
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
return -1;
}
return 0;
}