source: opengl-game/game.cpp@ 92bc4fe

feature/imgui-sdl points-test
Last change on this file since 92bc4fe was b33f87b, checked in by Dmitry Portnoy <dmp1488@…>, 8 years ago

Use EXIT_SUCCESS and EXIT_FAILURE and add basic fullscreen support

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