1 | #include <iostream>
|
---|
2 |
|
---|
3 | // GLEW
|
---|
4 | #define GLEW_STATIC
|
---|
5 | #include <GL/glew.h>
|
---|
6 |
|
---|
7 | // GLFW
|
---|
8 | #include <GLFW/glfw3.h>
|
---|
9 |
|
---|
10 | #if defined(__linux__)
|
---|
11 | #define LINUX
|
---|
12 | #elif defined(_WIN32)
|
---|
13 | #define WINDOWS
|
---|
14 | #elif defined(__APPLE__)
|
---|
15 | #define MAC
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | using namespace std;
|
---|
19 |
|
---|
20 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
---|
21 |
|
---|
22 | const GLuint WIDTH = 800, HEIGHT = 600;
|
---|
23 |
|
---|
24 | int main(int argc, char* argv[]) {
|
---|
25 | cout << "Starting OpenGL game..." << endl;
|
---|
26 |
|
---|
27 | cout << "Starting GLFW context, OpenGL 3.3" << endl;
|
---|
28 | // Init GLFW
|
---|
29 | glfwInit();
|
---|
30 |
|
---|
31 | // Set all the required options for GLFW
|
---|
32 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
33 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
34 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
35 | glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
---|
36 |
|
---|
37 | #ifdef MAC // required in OSX
|
---|
38 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | // Create a GLFWwindow object that we can use for GLFW's functions
|
---|
42 | GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
|
---|
43 | glfwMakeContextCurrent(window);
|
---|
44 | if (window == NULL) {
|
---|
45 | cout << "Failed to create GLFW window" << endl;
|
---|
46 | glfwTerminate();
|
---|
47 | return -1;
|
---|
48 | }
|
---|
49 |
|
---|
50 | // Set the required callback functions
|
---|
51 | glfwSetKeyCallback(window, key_callback);
|
---|
52 |
|
---|
53 | // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
|
---|
54 | glewExperimental = GL_TRUE;
|
---|
55 | // Initialize GLEW to setup the OpenGL Function pointers
|
---|
56 | if (glewInit() != GLEW_OK) {
|
---|
57 | cout << "Failed to initialize GLEW" << endl;
|
---|
58 | return -1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Define the viewport dimensions
|
---|
62 | int width, height;
|
---|
63 | glfwGetFramebufferSize(window, &width, &height);
|
---|
64 | glViewport(0, 0, width, height);
|
---|
65 |
|
---|
66 | // Game loop
|
---|
67 | while (!glfwWindowShouldClose(window)) {
|
---|
68 | // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
|
---|
69 | glfwPollEvents();
|
---|
70 |
|
---|
71 | // Render
|
---|
72 | // Clear the colorbuffer
|
---|
73 | glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
---|
74 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
75 |
|
---|
76 | // Swap the screen buffers
|
---|
77 | glfwSwapBuffers(window);
|
---|
78 | }
|
---|
79 |
|
---|
80 | glfwTerminate();
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Is called whenever a key is pressed/released via GLFW
|
---|
85 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
|
---|
86 | std::cout << key << std::endl;
|
---|
87 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
---|
88 | glfwSetWindowShouldClose(window, GL_TRUE);
|
---|
89 | }
|
---|