-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathegine.cpp
More file actions
109 lines (84 loc) · 2.84 KB
/
Copy pathegine.cpp
File metadata and controls
109 lines (84 loc) · 2.84 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
#include "engine.h"
Engine::Engine(const char* name, int width, int height)
{
m_WINDOW_NAME = name;
m_WINDOW_WIDTH = width;
m_WINDOW_HEIGHT = height;
}
Engine::~Engine()
{
delete m_window;
delete m_graphics;
m_window = NULL;
m_graphics = NULL;
}
bool Engine::Initialize()
{
// Start a window
m_window = new Window(m_WINDOW_NAME, &m_WINDOW_WIDTH, &m_WINDOW_HEIGHT);
if (!m_window->Initialize())
{
printf("The window failed to initialize.\n");
return false;
}
// Start the graphics
m_graphics = new Graphics();
if (!m_graphics->Initialize(m_WINDOW_WIDTH, m_WINDOW_HEIGHT))
{
printf("The graphics failed to initialize.\n");
return false;
}
// No errors
return true;
}
void Engine::Run()
{
m_running = true;
while (!glfwWindowShouldClose(m_window->getWindow()))
{
ProcessInput();
Display(m_window->getWindow(), glfwGetTime());
glfwPollEvents();
}
m_running = false;
}
void Engine::ProcessInput()
{
if (glfwGetKey(m_window->getWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(m_window->getWindow(), true);
if (glfwGetKey(m_window->getWindow(), GLFW_KEY_UP) == GLFW_RELEASE &&
glfwGetKey(m_window->getWindow(), GLFW_KEY_DOWN) == GLFW_RELEASE &&
glfwGetKey(m_window->getWindow(), GLFW_KEY_LEFT) == GLFW_RELEASE &&
glfwGetKey(m_window->getWindow(), GLFW_KEY_RIGHT) == GLFW_RELEASE)
m_graphics->getCube()->setSpeed(glm::vec3(0., 0., 0.));
if (glfwGetKey(m_window->getWindow(), GLFW_KEY_UP) == GLFW_PRESS)
m_graphics->getCube()->setSpeed(glm::vec3(0., .1, 0.));
if (glfwGetKey(m_window->getWindow(), GLFW_KEY_DOWN) == GLFW_PRESS)
m_graphics->getCube()->setSpeed(glm::vec3(0., -.1, 0.));
if (glfwGetKey(m_window->getWindow(), GLFW_KEY_LEFT) == GLFW_PRESS)
m_graphics->getCube()->setSpeed(glm::vec3(.1, 0., 0.));
if (glfwGetKey(m_window->getWindow(), GLFW_KEY_RIGHT) == GLFW_PRESS)
m_graphics->getCube()->setSpeed(glm::vec3(-.1, 0., 0.));
}
unsigned int Engine::getDT()
{
//long long TimeNowMillis = GetCurrentTimeMillis();
//assert(TimeNowMillis >= m_currentTimeMillis);
//unsigned int DeltaTimeMillis = (unsigned int)(TimeNowMillis - m_currentTimeMillis);
//m_currentTimeMillis = TimeNowMillis;
//return DeltaTimeMillis;
return glfwGetTime();
}
long long Engine::GetCurrentTimeMillis()
{
//timeval t;
//gettimeofday(&t, NULL);
//long long ret = t.tv_sec * 1000 + t.tv_usec / 1000;
//return ret;
return (long long)glfwGetTime();
}
void Engine::Display(GLFWwindow* window, double time) {
m_graphics->Render();
m_window->Swap();
m_graphics->Update(time, speed);
}