-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
189 lines (142 loc) · 4.71 KB
/
Main.cpp
File metadata and controls
189 lines (142 loc) · 4.71 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <chrono>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GL/gl.h>
#include <fstream>
#include <string>
#include <stb/stb_image.h>
#include "Texture.h"
#include "Raycast.h"
#include "BoxCollider.h"
#include "shaderClass.h"
#include "VBO.h"
#include "EBO.h"
#include "Camera.h"
#include "SoundManager.h"
#include "Model.h"
#include "modelLoader.h"
#include "UITextLoader.h"
#include "EngineUserHooks.h"
#include "UserMain.h"
std::string xmlFile;
std::ifstream startupFile("RUNTIME_STARTUP_CONFIG");
const unsigned int width = 1000;
const unsigned int height = 800;
Camera* globalCamera = nullptr;
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
if (globalCamera != nullptr) {
globalCamera->width = width;
globalCamera->height = height;
globalCamera->updateMatrix(90.0f, 0.1f, 100.0f);
}
}
//actual Game
int main()
{
//sound
SoundManager soundPlayer("Assets/rainbowdash.mp3");
soundPlayer.Loop();
soundPlayer.Play();
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWwindow* window = glfwCreateWindow(width, height, "Game", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
gladLoadGL();
glViewport(0, 0, width, height);
Shader shaderProgram("Shaders/Default.vert", "Shaders/Default.frag");
glm::vec4 lightColor = glm::vec4(0.6f, 0.6f, 0.95f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Camera camera(width, height, glm::vec3(0.0f, 0.0f, 2.0f));
globalCamera = &camera;
//init instance and use user_scripts
FishEngine::GameInstance currentInstance(camera);
start(¤tInstance);
//find which xml file to open
if (!startupFile.is_open()) {
std::cerr << "Failed to open STARTUP_RUNTIME" << std::endl;
}
// Read first line
if (!std::getline(startupFile, xmlFile) || xmlFile.empty()) {
std::cerr << "STARTUP_RUNTIME is empty or invalid" << std::endl;
}
// load models from XML
ModelLoader modelLoader;
if (!modelLoader.loadFromXML(xmlFile)) {
std::cerr << "Failed to load models from XML file" << std::endl;
} else {
std::cout << "Successfully loaded " << modelLoader.getModelCount() << " models from XML" << std::endl;
}
//delta time stuff
float print_time = 0;
auto lastTick = std::chrono::system_clock::now();
float deltaTime = 0;
float fps = 0;
float moving_Test = 0;
std::cout << "\033[2J\033[1;1H"; // Clear screen & move cursor to top-left
//testing Text
TextLoader textLoader;
//actual runtime
while (!glfwWindowShouldClose(window))
{
moving_Test += deltaTime / 2;
auto now = std::chrono::system_clock::now();
std::chrono::duration<float> elapsed = now - lastTick;
deltaTime = elapsed.count();
lastTick = now;
print_time += deltaTime;
//----FPS--//
fps = 1.0f / deltaTime;
update(¤tInstance, deltaTime);
if (print_time >= 1.0f) {
std::cout << "\033[2J\033[1;1H"; // Clear screen & move cursor to top-left
std::cout << "FPS: " << fps << '\n';
print_time = 0.0f;
}
//----FPS--//
glClearColor(0.549f, 0.671f, 0.631f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram.Activate();
textLoader.drawText(shaderProgram, "Test", 1, 1);
//player Init
camera.Inputs(window, deltaTime);
camera.updateMatrix(90.0f, 0.1f, 100.0f);
camera.RigidBody(deltaTime);
for (BoxCollider currentCollider : currentInstance.Getpushback())
{
camera.CollisionPush(¤tCollider);
}
camera.Matrix(shaderProgram, "camMatrix");
glm::mat4 defaultModel = glm::mat4(1.0f);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE, glm::value_ptr(defaultModel));
glm::mat3 normalMatrix = glm::transpose(glm::inverse(glm::mat3(defaultModel)));
glUniformMatrix3fv(glGetUniformLocation(shaderProgram.ID, "normalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
glUniform4f(glGetUniformLocation(shaderProgram.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos[0]"), 0.0f, 0.2f, 0.2f);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos[1]"), moving_Test, 0.2f, 0.2f);
modelLoader.drawModels(shaderProgram);
glfwSwapBuffers(window);
glfwPollEvents();
}
globalCamera = nullptr;
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}