AllenYuan

window - debug

Apr 21st, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 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. // given an error, put it into the error buffer
  7. #define GL_ERROR_CASE(glerror) \
  8.   case glerror:                \
  9.     snprintf(error, sizeof(error), "%s", #glerror)
  10.  
  11. // while there are errors, print error, which file it came from, and which line of code.
  12. inline void gl_debug(const char *file, int line)
  13. {
  14.   GLenum err;
  15.   while ((err = glGetError()) != GL_NO_ERROR)
  16.   {
  17.     char error[128];
  18.     // put error description in the buffer
  19.     switch (err)
  20.     {
  21.       GL_ERROR_CASE(GL_INVALID_ENUM);
  22.       break;
  23.       GL_ERROR_CASE(GL_INVALID_VALUE);
  24.       break;
  25.       GL_ERROR_CASE(GL_INVALID_OPERATION);
  26.       break;
  27.       GL_ERROR_CASE(GL_INVALID_FRAMEBUFFER_OPERATION);
  28.       break;
  29.       GL_ERROR_CASE(GL_OUT_OF_MEMORY);
  30.       break;
  31.     default:
  32.       snprintf(error, sizeof(error), "%s", "UNKNOWN_ERROR");
  33.       break;
  34.     }
  35.     // print "error - file: line\n"
  36.     fprintf(stderr, "%s - %s: %d\n", error, file, line);
  37.   }
  38. }
  39.  
  40. #undef GL_ERROR_CASE
  41.  
  42. // callback to print errors to console
  43. void error_callback(int error, const char *description)
  44. {
  45.   fprintf(stderr, "Error: %s\n", description);
  46. }
  47.  
  48. // play the game
  49. int main(int argc, char *argv[])
  50. {
  51.   // on error, print errors to console
  52.   glfwSetErrorCallback(error_callback);
  53.  
  54.   // initialize glfw. if it fails, exit the program.
  55.   if (!glfwInit())
  56.   {
  57.     return -1;
  58.   }
  59.  
  60.   // create a 640x480 window titled "Space Invaders"
  61.   GLFWwindow *window = glfwCreateWindow(640, 480, "Space Invaders", NULL, NULL);
  62.   // window doesn't render => close the program
  63.   if (!window)
  64.   {
  65.     glfwTerminate();
  66.     return -1;
  67.   }
  68.   // bind OpenGL calls to this window (allows us to draw on this window)
  69.   glfwMakeContextCurrent(window);
  70.  
  71.   // initialize GLEW so we can make OpenGL calls. Print an error and close the window if it fails
  72.   GLenum err = glewInit();
  73.   if (err != GLEW_OK)
  74.   {
  75.     fprintf(stderr, "Error initializing GLEW.\n");
  76.     glfwTerminate();
  77.     return -1;
  78.   }
  79.  
  80.   // if we see the below log statements, we've successfully attached OpenGL to the window
  81.   // query the openGL version
  82.   int glVersion[2] = {-1, 1};
  83.   glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]);
  84.   glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);
  85.  
  86.   gl_debug(__FILE__, __LINE__);
  87.  
  88.   // print some information about our version of OpenGL.
  89.   printf("Using OpenGL: %d.%d\n", glVersion[0], glVersion[1]);
  90.   printf("Renderer used: %s\n", glGetString(GL_RENDERER));
  91.   printf("Shading Language: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
  92.  
  93.   // set the buffer clear color to red (rgba code)
  94.   glClearColor(1.0, 0.0, 0.0, 1.0);
  95.   // persist the window until user closes it
  96.   while (!glfwWindowShouldClose(window))
  97.   {
  98.     // 'buffer' means an image which can be displayed on the screen
  99.     // OpenGL has a 'front buffer' which is displayed on the screen and a 'back buffer'
  100.     // which is used for drawing.
  101.  
  102.     // every tick, clear the front buffer and swap in the back buffer
  103.     glClear(GL_COLOR_BUFFER_BIT);
  104.     glfwSwapBuffers(window);
  105.     // process pending events (for example, clicking the close button)
  106.     glfwPollEvents();
  107.   }
  108.  
  109.   // clean up our program and exit
  110.   glfwDestroyWindow(window);
  111.   glfwTerminate();
  112.   return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment