-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
172 lines (144 loc) · 6.12 KB
/
engine.cpp
File metadata and controls
172 lines (144 loc) · 6.12 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
#include "engine.h"
#include <iostream>
Engine::Engine() : keys() {
this->initWindow();
this->initShaders();
this->initShapes();
}
Engine::~Engine() {}
unsigned int Engine::initWindow(bool debug) {
// glfw: initialize and configure
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
#endif
glfwWindowHint(GLFW_RESIZABLE, false);
// This creates the window using GLFW.
// It's a C function, so we have to pass it a pointer to the window variable.
window = glfwCreateWindow(width, height, "engine", nullptr, nullptr);
if (window == nullptr) {
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
// This sets the OpenGL context to the window we just created.
glfwMakeContextCurrent(window);
// Glad is an OpenGL function loader. It loads all the OpenGL functions that are defined by the driver.
// This is required because OpenGL is a specification, not an implementation.
// The driver is the implementation of OpenGL that is installed on your computer.
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cout << "Failed to initialize GLAD" << endl;
return -1;
}
// OpenGL configuration
// This defines the size of the area OpenGL should render to.
glViewport(0, 0, width, height);
// This enables depth testing which prevents triangles from overlapping.
//turn this off to fix the blurry fuzzyness
//(we want the triangles to overlap in this case)
//it still is a bit fuzzy, but much less bad
glEnable(GL_BLEND);
// Alpha blending allows for transparent backgrounds.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glfwSwapInterval(1);
return 0;
}
void Engine::initShaders() {
// load shader manager
shaderManager = make_unique<ShaderManager>();
// Load shader into shader manager and retrieve it
shapeShader = this->shaderManager->loadShader("../res/shaders/shape.vert",
"../res/shaders/shape.frag",
nullptr, "shape");
// Set uniforms that never change
shapeShader.use().setMatrix4("projection", this->PROJECTION);
}
void Engine::initShapes() {
// TODO: Create shape(s) here! (Code is in README)
// Put this line in initShapes in engine.cpp
shapes.push_back(make_unique<Rect>(shapeShader, vec2(width / 2, height /2), vec2(300,300), color(1,1,1,1)));
}
void Engine::processInput() {
// Set keys to true if pressed, false if released
for (int key = 0; key < 1024; ++key) {
if (glfwGetKey(window, key) == GLFW_PRESS)
keys[key] = true;
else if (glfwGetKey(window, key) == GLFW_RELEASE)
keys[key] = false;
}
// Close window if escape key is pressed
if (keys[GLFW_KEY_ESCAPE])
glfwSetWindowShouldClose(window, true);
// Calculate delta time
float currentFrame;
currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// TODO: Respond to the arrow keys here! (Code is in README)
// Put this code at the end of processInput in engine.cpp
float speed = base_speed * speed_changer * deltaTime;
if (speed_changer < 3 && speed_decreasing == false) {
speed_changer = pow((speed_changer), 2);
}
else if (speed_changer < 1.5) {
speed_decreasing = false;
}
else if (speed_changer > 3) {
speed_decreasing = true;
speed_changer = pow((speed_changer ), 1/2);
}
if (keys[GLFW_KEY_UP]) shapes[0]->moveY(speed);
if (keys[GLFW_KEY_DOWN]) shapes[0]->moveY(-speed);
if (keys[GLFW_KEY_LEFT]) shapes[0]->moveX(-speed);
if (keys[GLFW_KEY_RIGHT]) shapes[0]->moveX(speed);
}
void Engine::update() {
// Calculate delta time
float currentFrame;
currentFrame = glfwGetTime();
//the code below did not fix the jittering
// might come from shaders?
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// TODO: Make updates here! (Code is in README)
// Put this code in update in engine.cpp
float scale = (sin(currentFrame) + 1.0) / 2.0 + .01;// Varies between 0 and 1
scale = std::round(scale * 10) / 10.0; // this does super cool stuff to my code
// scale = std::round(scale * 30000) / 30000.0;
shapes[0]->setSize(vec2(300 * scale, 300 * scale));
// Put this code in update in engine.cpp
// shapes[0]->rotateShape(90.0f, 0.01);
shapes[0]->rotateShape(180.0f, 0.001);
// Put this code in update in engine.cpp
float phaseAngle = currentFrame * 1.5f; // You can adjust this value to make the colors change faster or slower
float red = sin(phaseAngle + 0) * 0.5f + 0.5f; // 0 degrees out of phase
float green = sin(phaseAngle + 2.0f * M_PI / 3.0f) * 0.5f + 0.5f; // 120 degrees out of phase
float blue = sin(phaseAngle + 4.0f * M_PI / 3.0f) * 0.5f + 0.5f; // 240 degrees out of phase
shapes[0]->setColor(vec4(red, green, blue, 1.0f));
// This function polls for events like keyboard input and mouse movement
// It needs to be called every frame
// Without this function, the window will freeze and become unresponsive
glfwPollEvents();
}
void Engine::render() {
// glClearColor(0, 0, 0, 1);
// glClear(GL_COLOR_BUFFER_BIT);
// Render shapes
// For each shape, call it's setUniforms() function and then call it's draw() function
for (const unique_ptr<Shape>& s : shapes) {
s->setUniforms();
s->draw();
}
// This is glfw function call is required to display the final image on the screen
// The front buffer contains the final image that is displayed.
// The back buffer contains the image that is currently being rendered.
glfwSwapBuffers(window);
}
bool Engine::shouldClose() {
return glfwWindowShouldClose(window);
}