-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics_manager.cpp
118 lines (89 loc) · 2.83 KB
/
graphics_manager.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
#include "graphics_manager.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
//glcolor3...
//glVertex3f
// Error callback function that gets called in case of errors by glfw
static void error_callback(int error, const char* description) {
fprintf(stderr, "GLWF error [%d]: %s\n", error, description);
}
// Key callback function that gest called by the open window
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE); // close on escape
else if (key == GLFW_KEY_ENTER && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE); // close on enter
}
// Constructor
Graphics_manager::Graphics_manager() {
window = NULL;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(WINDOW_SIZE_X, WINDOW_SIZE_Y, WINDOW_NAME, NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
// Load all OpenGL functions using the glfw loader function
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fprintf(stderr, "ERROR: Failed to initialize OpenGL context.\n");
if (window != NULL)
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSwapInterval(0);
glfwSetKeyCallback(window, key_callback);
}
// Destructor
Graphics_manager::~Graphics_manager() {
if (window != NULL)
glfwDestroyWindow(window);
glfwTerminate();
}
Dimension Graphics_manager::getDimensions() {
return Dimension(WINDOW_SIZE_X, WINDOW_SIZE_Y);
}
void Graphics_manager::draw_birds(Bird **birds, int n) {
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClearColor(BACKGROUND_COLOR_R, BACKGROUND_COLOR_G, BACKGROUND_COLOR_B, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width/2, width/2, -height/2, height/2, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
// Draw the birds
for (int i = 0; i < n; i++) {
draw_bird(birds[i]);
}
return;
}
void Graphics_manager::draw_bird(Bird *bird) {
glPushMatrix();
glTranslatef(bird->position.x, bird->position.y, 0.0);
glRotatef(bird->rotation, 0, 0, 1);
glBegin(GL_TRIANGLES);
glColor3f(bird->color.red, bird->color.green, bird->color.blue);
glVertex3f(-4.0f, -4.0f, 0.00f);
glVertex3f( 4.0f, -4.0f, 0.00f);
glColor3f(1.f, 0.f, 1.f);
glVertex3f(0.00f, 5.0f, 0.00f);
glEnd();
glPopMatrix();
}
bool Graphics_manager::loop() {
glfwPollEvents();
if (glfwWindowShouldClose(window))
return false;
return true;
}
void Graphics_manager::swap_buffers() {
glfwSwapBuffers(window);
}