example_gl2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #ifdef NANOVG_GLEW
  20. # include <GL/glew.h>
  21. #endif
  22. #ifdef __APPLE__
  23. # define GLFW_INCLUDE_GLCOREARB
  24. #endif
  25. #ifdef NANOVG_GLAD
  26. # include <glad/glad.h>
  27. #else
  28. # define GLFW_INCLUDE_GLEXT
  29. #endif
  30. #include <GLFW/glfw3.h>
  31. #ifndef DEMO_ANTIALIAS
  32. # define DEMO_ANTIALIAS 1
  33. #endif
  34. #ifndef DEMO_STENCIL_STROKES
  35. # define DEMO_STENCIL_STROKES 1
  36. #endif
  37. #ifndef DEMO_MSAA
  38. # define DEMO_MSAA 0
  39. #endif
  40. #include "nanovg.h"
  41. #define NANOVG_GL2_IMPLEMENTATION
  42. #include "nanovg_gl.h"
  43. #include "demo.h"
  44. #include "perf.h"
  45. void errorcb(int error, const char* desc)
  46. {
  47. printf("GLFW error %d: %s\n", error, desc);
  48. }
  49. int blowup = 0;
  50. int screenshot = 0;
  51. int premult = 0;
  52. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  53. {
  54. NVG_NOTUSED(scancode);
  55. NVG_NOTUSED(mods);
  56. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  57. glfwSetWindowShouldClose(window, GL_TRUE);
  58. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  59. blowup = !blowup;
  60. if (key == GLFW_KEY_S && action == GLFW_PRESS)
  61. screenshot = 1;
  62. if (key == GLFW_KEY_P && action == GLFW_PRESS)
  63. premult = !premult;
  64. }
  65. int main()
  66. {
  67. GLFWwindow* window;
  68. DemoData data;
  69. NVGcontext* vg = NULL;
  70. PerfGraph fps;
  71. double prevt = 0;
  72. if (!glfwInit()) {
  73. printf("Failed to init GLFW.");
  74. return -1;
  75. }
  76. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  77. glfwSetErrorCallback(errorcb);
  78. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  79. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  80. #if DEMO_MSAA
  81. glfwWindowHint(GLFW_SAMPLES, 4);
  82. #endif
  83. window = glfwCreateWindow(1000, 600, "NanoVG OpenGL 2.0", NULL, NULL);
  84. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  85. if (!window) {
  86. glfwTerminate();
  87. return -1;
  88. }
  89. glfwSetKeyCallback(window, key);
  90. glfwMakeContextCurrent(window);
  91. #ifdef NANOVG_GLEW
  92. if(glewInit() != GLEW_OK) {
  93. printf("Could not init glew.\n");
  94. return -1;
  95. }
  96. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  97. glGetError();
  98. #endif
  99. #ifdef NANOVG_GLAD
  100. if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
  101. printf("Could not init glad.\n");
  102. return -1;
  103. }
  104. #endif
  105. int flags = 0;
  106. #ifndef NDEBUG
  107. flags |= NVG_DEBUG;
  108. #endif
  109. #if !DEMO_MSAA && DEMO_ANTIALIAS
  110. flags |= NVG_ANTIALIAS;
  111. #endif
  112. #if DEMO_STENCIL_STROKES
  113. flags |= NVG_STENCIL_STROKES;
  114. #endif
  115. vg = nvgCreateGL2(flags);
  116. if (vg == NULL) {
  117. printf("Could not init nanovg.\n");
  118. return -1;
  119. }
  120. if (loadDemoData(vg, &data) == -1)
  121. return -1;
  122. glfwSwapInterval(0);
  123. glfwSetTime(0);
  124. prevt = glfwGetTime();
  125. while (!glfwWindowShouldClose(window))
  126. {
  127. double mx, my, t, dt;
  128. int winWidth, winHeight;
  129. int fbWidth, fbHeight;
  130. float pxRatio;
  131. t = glfwGetTime();
  132. dt = t - prevt;
  133. prevt = t;
  134. updateGraph(&fps, dt);
  135. glfwGetCursorPos(window, &mx, &my);
  136. glfwGetWindowSize(window, &winWidth, &winHeight);
  137. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  138. // Calculate pixel ration for hi-dpi devices.
  139. pxRatio = (float)fbWidth / (float)winWidth;
  140. // Update and render
  141. glViewport(0, 0, fbWidth, fbHeight);
  142. if (premult)
  143. glClearColor(0,0,0,0);
  144. else
  145. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  146. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  147. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  148. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  149. renderGraph(vg, 5,5, &fps);
  150. nvgEndFrame(vg);
  151. if (screenshot) {
  152. screenshot = 0;
  153. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  154. }
  155. glfwSwapBuffers(window);
  156. glfwPollEvents();
  157. }
  158. freeDemoData(vg, &data);
  159. nvgDeleteGL2(vg);
  160. glfwTerminate();
  161. return 0;
  162. }