Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Engine.cpp
Go to the documentation of this file.
1#include "Engine.h"
2#include "Application.h"
3#include "scene/GameObject.h"
4#include "scene/Component.h"
5#include "scene/components/CameraComponent.h"
6#include <GL/glew.h>
7#include <GLFW/glfw3.h>
8#include <iostream>
9
10namespace eng
11{
12 void keyCallback(GLFWwindow* window, int key, int, int action, int)
13 {
14 auto& inputManager = eng::Engine::GetInstance().GetInputManager();
15 if (action == GLFW_PRESS)
16 {
17 inputManager.SetKeyPressed(key, true);
18 }
19 else if (action == GLFW_RELEASE)
20 {
21 inputManager.SetKeyPressed(key, false);
22 }
23 }
24
25 void mouseButtonCallback(GLFWwindow* window, int button, int action, int)
26 {
27 auto& inputManager = eng::Engine::GetInstance().GetInputManager();
28 if (action == GLFW_PRESS)
29 {
30 inputManager.SetMouseButtonPressed(button, true);
31 }
32 else if (action == GLFW_RELEASE)
33 {
34 inputManager.SetMouseButtonPressed(button, false);
35 }
36 }
37
38 void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos)
39 {
40 auto& inputManager = eng::Engine::GetInstance().GetInputManager();
41
42 inputManager.SetMousePositionOld(inputManager.GetMousePositionCurrent());
43
44 glm::vec2 currentPos(static_cast<float>(xpos), static_cast<float>(ypos));
45 inputManager.SetMousePositionCurrent(currentPos);
46
47 inputManager.SetMousePositionChanged(true);
48 }
49
51 {
52 static Engine instance;
53 return instance;
54 }
55
56 bool Engine::Init(int width, int height)
57 {
58 if (!m_application)
59 {
60 return false;
61 }
62
64 m_application->RegisterTypes();
65
66#if defined (__linux__)
67 glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
68#endif
69
70 if (!glfwInit())
71 {
72 return false;
73 }
74
75 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
76 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
77 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
78
79 m_window = glfwCreateWindow(width, height, "GameDevelopmentProject", nullptr, nullptr);
80
81 if (m_window == nullptr)
82 {
83 std::cout << "Error creating window" << std::endl;
84 glfwTerminate();
85 return false;
86 }
87
88 glfwSetKeyCallback(m_window, keyCallback);
89 glfwSetMouseButtonCallback(m_window, mouseButtonCallback);
90 glfwSetCursorPosCallback(m_window, cursorPositionCallback);
91 glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
92
93 glfwMakeContextCurrent(m_window);
94
95 if (glewInit() != GLEW_OK)
96 {
97 glfwTerminate();
98 return false;
99 }
100
101 m_graphicsAPI.Init();
102 m_physicsManager.Init();
103 m_audioManager.Init();
104 m_rederQueue.Init();
105 return m_application->Init();
106 }
107
109 {
110 if (!m_application)
111 {
112 return;
113 }
114
115 m_lastTimePoint = std::chrono::steady_clock::now();
116
117 while (!glfwWindowShouldClose(m_window) && !m_application->NeedsToBeClosed())
118 {
119 glfwPollEvents();
120
121 auto now = std::chrono::steady_clock::now();
122 float deltaTime = std::chrono::duration<float>(now - m_lastTimePoint).count();
123 m_lastTimePoint = now;
124
125 m_physicsManager.Update(deltaTime);
126
127 m_application->Update(deltaTime);
128
129 m_graphicsAPI.SetClearColor(1.0f, 1.0f, 1.0f, 1.0f);
130 m_graphicsAPI.ClearBuffers();
131
132 CameraData cameraData;
133 std::vector<LightData> lights;
134
135 int width = 0;
136 int height = 0;
137 glfwGetWindowSize(m_window, &width, &height);
138 float aspect = static_cast<float>(width) / static_cast<float>(height);
139
140 if (m_currentScene)
141 {
142 if (auto cameraObject = m_currentScene->GetMainCamera())
143 {
144 // logic for matrices
145 auto cameraComponent = cameraObject->GetComponent<CameraComponent>();
146 if (cameraComponent)
147 {
148 cameraData.viewMatrix = cameraComponent->GetViewMatrix();
149 cameraData.projectionMatrix = cameraComponent->GetProjectionMatrix(aspect);
150 cameraData.orthoMatrix = glm::ortho(
151 0.0f, static_cast<float>(width),
152 0.0f, static_cast<float>(height)
153 );
154 cameraData.position = cameraObject->GetWorldPosition();
155 }
156 }
157
158 lights = m_currentScene->CollectLights();
159 }
160
161 m_rederQueue.Draw(m_graphicsAPI, cameraData, lights);
162
163 glfwSwapBuffers(m_window);
164
165 m_inputManager.SetMousePositionChanged(false);
166 }
167 }
168
170 {
171 if (m_application)
172 {
173 m_application->Destroy();
174 m_application.reset();
175 glfwTerminate();
176 m_window = nullptr;
177 }
178 }
179
181 {
182 m_application.reset(app);
183 }
184
186 {
187 return m_application.get();
188 }
189
191 {
192 return m_inputManager;
193 }
194
196 {
197 return m_graphicsAPI;
198 }
199
201 {
202 return m_rederQueue;
203 }
204
206 {
207 return m_fileSystem;
208 }
209
211 {
212 return m_textureManager;
213 }
214
216 {
217 return m_physicsManager;
218 }
219
221 {
222 return m_audioManager;
223 }
224
226 {
227 m_currentScene.reset(scene);
228 }
229
231 {
232 return m_currentScene.get();
233 }
234}
Application * GetApplication()
Definition Engine.cpp:185
Scene * GetScene()
Definition Engine.cpp:230
void SetApplication(Application *app)
Definition Engine.cpp:180
GraphicsAPI & GetGraphicsAPI()
Definition Engine.cpp:195
AudioManager & GetAudioManager()
Definition Engine.cpp:220
FileSystem & GetFileSystem()
Definition Engine.cpp:205
bool Init(int width, int height)
Definition Engine.cpp:56
InputManager & GetInputManager()
Definition Engine.cpp:190
TextureManager & GetTextureManager()
Definition Engine.cpp:210
void Run()
Definition Engine.cpp:108
void SetScene(Scene *scene)
Definition Engine.cpp:225
static Engine & GetInstance()
Definition Engine.cpp:50
RenderQueue & GetRenderQueue()
Definition Engine.cpp:200
void Destroy()
Definition Engine.cpp:169
PhysicsManager & GetPhysicsManager()
Definition Engine.cpp:215
void SetClearColor(float r, float g, float b, float a)
void SetMousePositionChanged(bool changed)
void SetKeyPressed(int key, bool pressed)
void SetMouseButtonPressed(int button, bool pressed)
void SetMousePositionOld(const glm::vec2 &pos)
void Update(float deltaTime)
void Draw(GraphicsAPI &graphicsAPI, const CameraData &cameraData, const std::vector< LightData > &lights)
static void RegisterTypes()
Definition Scene.cpp:17
Abstract base class for the user-defined game or lab application.
Central singleton that owns and coordinates all engine subsystems.
void keyCallback(GLFWwindow *window, int key, int, int action, int)
Definition Engine.cpp:12
void mouseButtonCallback(GLFWwindow *window, int button, int action, int)
Definition Engine.cpp:25
void cursorPositionCallback(GLFWwindow *window, double xpos, double ypos)
Definition Engine.cpp:38
glm::vec3 position
Definition Common.h:13
glm::mat4 orthoMatrix
Definition Common.h:12
glm::mat4 viewMatrix
Definition Common.h:10
glm::mat4 projectionMatrix
Definition Common.h:11