-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_evolution.cpp
109 lines (79 loc) · 2.46 KB
/
snake_evolution.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
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "game.hpp"
#include "utils.hpp"
//Function declarations. see their definition at the bottom for description of function.
void keyCallback(GLFWwindow*, int, int, int, int);
GLFWwindow* init();
//initialize things such as glfw and glew and create a window
GLFWwindow* window = init();
//Global variables
constexpr int window_width = 800;
constexpr int window_height = 600;
Game game(window_width,window_height);
int main()
{
//check for errors
ce();
//running program
while(!glfwWindowShouldClose(window))
{
//clear the window's content
clear_window();
//render game
game.render_game();
//check for errors
ce();
glfwSwapBuffers(window);//swap buffer
glfwPollEvents();//poll for events (such as quit)
}
//print opengl version information
// print_gl_version();
//clean up
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
//End of main ---------------------------------------------------------------------------------
//Handle user inputs
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
game.process_input(window, key, action);
}
//initializes the required components (glfw, glew)
GLFWwindow* init()
{
//set required opengl version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
//Call error callback function to print errors when they happen
glfwSetErrorCallback(&glfwErrorCallback);
//create window
if(!glfwInit())
{
std::cerr << "GLFW was not initialized. \n";
exit(0);
}
GLFWwindow* window = glfwCreateWindow(window_width, window_height, "Snake Evolution", NULL, NULL);
if(!window)
{
std::cerr << "Window was not created. \n";
glfwTerminate();
exit(0);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); //set buffer swap interval to refresh rate of screen
//Key callback for reading user inputs
glfwSetKeyCallback(window, keyCallback);
//Don't allow user to resize window (doesn't work on lab machines)
// glfwSetWindowAttrib(window, GLFW_RESIZABLE, false);
//initialize glew
if(glewInit() != GLEW_OK)
{
std::cout << "Glew was not initialized." << std::endl;
exit(0);
}
return window;
}