-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainController.cpp
91 lines (82 loc) · 2.44 KB
/
MainController.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
#include "MainController.h"
#include <iostream>
using namespace std;
queue<KeyboardEvent> MainController::inputBuffer;
MouseChange MainController::mouseListener;
long long timeNow;
void MainController::ProcessInput(GLFWwindow* window, int key, int scancode, int action, int mods)
{
KeyboardEvent ev;
ev.code = key;
ev.eventType = action;
inputBuffer.push(ev);
}
void MainController::ProcessCursorInput(GLFWwindow * window, double xpos, double ypos)
{
mouseListener.prevPosition = mouseListener.position;
mouseListener.position = vec2(xpos, ypos);
mouseListener.changed = true;
}
MainController::MainController()
{
this->wind = new Window(40, 40, 1024, 684, "Space Voyager");
graphicsController = new Graphics();
this->sceneController = new Scene();
sceneController->SetInputBuffer(&inputBuffer);
sceneController->SetMouseListener(&this->mouseListener);
}
MainController::~MainController()
{
delete wind;
delete graphicsController;
delete sceneController;
}
bool MainController::Initialize()
{
if (!wind->Initialize())
{
cout << "Failed to initialize the window.\n";
return 0;
}
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return 0;
}
sceneController->Initialize();
graphicsController->Initialize(1024, 684, sceneController);
return true;
}
void MainController::MainLoop()
{
glfwSetKeyCallback(wind->GetGLFWPointer(), ProcessInput);
glfwSetCursorPosCallback(wind->GetGLFWPointer(), ProcessCursorInput);
while (!wind->ShouldClose() && glfwGetKey(wind->GetGLFWPointer(), GLFW_KEY_ENTER) != GLFW_PRESS)
{
timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count();
wind->WindowLoop();
graphicsController->Render();
}
sceneController->LoadActors();
int timer = -1;
while (!wind->ShouldClose() && timer != 0)
{
timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count();
wind->WindowLoop();
if (!sceneController->UpdateScene())
{
if (timer < 0)
timer = 400; // find a way to properly set this
}
timer--;
graphicsController->Render();
}
sceneController->FinalLoad();
while (!wind->ShouldClose())
{
timeNow = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count();
wind->WindowLoop();
glfwPollEvents();
graphicsController->Render();
}
}