source: opengl-game/game.cpp@ 5540132

feature/imgui-sdl points-test
Last change on this file since 5540132 was 5540132, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 8 years ago

Start detecting the OS in the preprocessor to support running OS-specific code

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