AllenYuan

window - create window

Apr 21st, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. // import stdio and rendering libraries
  2. #include <cstdio>
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5.  
  6. // callback to print errors to console
  7. void error_callback(int error, const char* description) {
  8.   fprintf(stderr, "Error: %s\n", description);
  9. }
  10.  
  11. // play the game
  12. int main(int argc, char* argv[])
  13. {
  14.   // on error, print errors to console
  15.   glfwSetErrorCallback(error_callback);
  16.  
  17.   // initialize glfw. if it fails, exit the program.
  18.   if(!glfwInit())
  19.   {
  20.     return -1;
  21.   }
  22.  
  23.   // create a 640x480 window titled "Space Invaders"
  24.   GLFWwindow* window = glfwCreateWindow(640, 480, "Space Invaders", NULL, NULL);
  25.   // window doesn't render => close the program
  26.   if(!window)
  27.   {
  28.     glfwTerminate();
  29.     return -1;
  30.   }
  31.   // bind OpenGL calls to this window (allows us to draw on this window)
  32.   glfwMakeContextCurrent(window);
  33.  
  34.   // initialize GLEW so we can make OpenGL calls. Print an error and close the window if it fails
  35.   GLenum err = glewInit();
  36.   if(err != GLEW_OK)
  37.   {
  38.     fprintf(stderr, "Error initializing GLEW.\n");
  39.     glfwTerminate();
  40.     return -1;
  41.   }
  42.  
  43.   // if we see the below log statements, we've successfully attached OpenGL to the window
  44.   // query the openGL version
  45.   int glVersion[2] = {-1, 1};
  46.   glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]);
  47.   glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);
  48.  
  49.   // on error, log filepath and line number
  50.   gl_debug(__FILE__, __LINE__);
  51.   // print some information about our version of OpenGL.
  52.   printf("Using OpenGL: %d.%d\n", glVersion[0], glVersion[1]);
  53.   printf("Renderer used: %s\n", glGetString(GL_RENDERER));
  54.   printf("Shading Language: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
  55.  
  56.   return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment