-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cpp
161 lines (142 loc) · 4.94 KB
/
Engine.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
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
/*
* The class declaration for the Engine.
*/
#include <cassert>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include "Engine.h"
#include "Network/Controller.h"
#include <Messages/Declarations.h>
#include "TextureManager.h"
Engine::Engine()
: m_Controller(NULL), m_CurrentState(NULL)
{
this->m_Running = true;
this->m_WindowOpen = false;
this->m_Translation = new ClientObjectTranslation();
// Initailize GLFW.
glfwInit();
// Open an 800x600 that matches the parameters as closely as possible.
this->m_GLFWWindow = glfwCreateWindow(800, 600, GLFW_WINDOWED, "Mir", 0);
this->m_WindowOpen = true;
glfwMakeContextCurrent(this->m_GLFWWindow);
// Initialize GLEW.
std::cout << "Minimum Required: OpenGL 1.4" << std::endl;
GLenum err = glewInit();
if (err != GLEW_OK)
{
std::cout << "Result: FAIL!" << std::endl;
std::cerr << "Unable to load GLEW to inspect OpenGL support." << std::endl;
std::cerr << "Ensure the GLEW libraries are installed and available for use." << std::endl;
exit(1);
}
else
{
// Display OpenGL support status.
std::cout << "OpenGL 1.1: " << (GLEW_VERSION_1_1 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 1.2: " << (GLEW_VERSION_1_2 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 1.3: " << (GLEW_VERSION_1_3 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 1.4: " << (GLEW_VERSION_1_4 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 1.5: " << (GLEW_VERSION_1_5 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 2.0: " << (GLEW_VERSION_2_0 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 2.1: " << (GLEW_VERSION_2_1 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 3.0: " << (GLEW_VERSION_3_0 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 3.1: " << (GLEW_VERSION_3_1 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 3.2: " << (GLEW_VERSION_3_2 ? "Available" : "MISSING") << std::endl;
std::cout << "OpenGL 3.3: " << (GLEW_VERSION_3_3 ? "Available" : "MISSING") << std::endl;
if (GLEW_VERSION_1_4)
std::cout << "Result: OK!" << std::endl;
else
{
std::cout << "Result: FAIL!" << std::endl;
std::cerr << "Upgrade your OpenGL drivers." << std::endl;
exit(1);
}
}
// Load textures!
TextureManager::LoadTexture("starfield", "Resources/Textures/Starfield.png");
TextureManager::LoadTexture("planet", "Resources/Textures/Planet.png");
}
Engine::~Engine()
{
this->m_CurrentState->Deactivate();
delete this->m_Translation;
delete this->m_CurrentState;
glfwTerminate();
}
void Engine::Connect(std::string address, int port)
{
if (this->m_Controller != NULL)
this->m_Controller->Close();
this->m_Controller = new Network::Controller(Network::ControllerMode::Client, *this->m_Translation, address, port);
}
void Engine::Disconnect()
{
this->m_Controller->Close();
this->m_Controller = NULL;
}
bool Engine::IsRunning()
{
return this->m_Running;
}
void Engine::Run()
{
// If there is no window open, we must first open it!
if (!this->m_WindowOpen)
{
// Open an 800x600 that matches the parameters as closely as possible.
this->m_GLFWWindow = glfwCreateWindow(800, 600, GLFW_WINDOWED, "Mir", 0);
this->m_WindowOpen = true;
glfwMakeContextCurrent(this->m_GLFWWindow);
}
// Render the game.
glfwPollEvents();
if (this->m_CurrentState != NULL)
{
this->m_CurrentState->Update();
this->m_CurrentState->Render();
}
glfwSwapBuffers(this->m_GLFWWindow);
// If the user has pressed the escape key, close the program.
// In future, this close handler will be much more advanced!
if (glfwGetKey(this->m_GLFWWindow, GLFW_KEY_ESC) ||
glfwGetWindowParam(this->m_GLFWWindow, GLFW_CLOSE_REQUESTED))
{
// Exit. The window will be closed by Engine::Cleanup.
this->m_Running = false;
}
// Synchronise the network.
if (this->m_Controller != NULL)
this->m_Controller->Synchronise();
}
GLFWwindow& Engine::GetWindow()
{
// Return the currently open window.
return this->m_GLFWWindow;
}
bool Engine::HasNetworkController()
{
// Return whether we are connected to a network.
return this->m_Controller != NULL;
}
Network::Controller& Engine::GetNetworkController()
{
// Return the current network controller.
assert(this->HasNetworkController());
return *this->m_Controller;
}
void Engine::Cleanup()
{
glfwDestroyWindow(this->m_GLFWWindow);
}
void Engine::Switch(BaseState* state)
{
assert(state != NULL);
if (this->m_CurrentState != NULL)
{
this->m_CurrentState->Deactivate();
delete this->m_CurrentState;
}
this->m_CurrentState = state;
this->m_CurrentState->Activate();
}