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