source: opengl-game/game.cpp@ a6edbfb

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

Create a simple window and a Linux makefile

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