You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.2KB

  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. #define GLFW_NO_GLU
  23. #ifndef _WIN32
  24. # define GLFW_INCLUDE_GLCOREARB
  25. #endif
  26. #include <GLFW/glfw3.h>
  27. #include "nanovg.h"
  28. #define NANOVG_GL3_IMPLEMENTATION
  29. //#include "nanovg_gl3.h"
  30. #include "nanovg_gl3buf.h"
  31. #include "demo.h"
  32. void errorcb(int error, const char* desc)
  33. {
  34. printf("GLFW error %d: %s\n", error, desc);
  35. }
  36. int blowup = 0;
  37. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  38. {
  39. NVG_NOTUSED(scancode);
  40. NVG_NOTUSED(mods);
  41. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  42. glfwSetWindowShouldClose(window, GL_TRUE);
  43. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  44. blowup = !blowup;
  45. }
  46. enum numqueries {
  47. NUM_QUERIES = 5
  48. };
  49. int main()
  50. {
  51. GLFWwindow* window;
  52. struct DemoData data;
  53. struct NVGcontext* vg = NULL;
  54. struct FPScounter fps, cpuTimes, gpuTimes;
  55. double prevt = 0, cpuTime = 0;
  56. int timerquery = GL_FALSE, currquery = 0, retquery = 0;
  57. GLuint timerqueryid[NUM_QUERIES];
  58. if (!glfwInit()) {
  59. printf("Failed to init GLFW.");
  60. return -1;
  61. }
  62. initFPS(&fps);
  63. initFPS(&cpuTimes);
  64. initFPS(&gpuTimes);
  65. glfwSetErrorCallback(errorcb);
  66. #ifndef _WIN32 // don't require this on win32, and works with more cards
  67. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  68. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  69. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  70. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  71. #endif
  72. #ifdef DEMO_MSAA
  73. glfwWindowHint(GLFW_SAMPLES, 4);
  74. #endif
  75. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  76. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  77. if (!window) {
  78. glfwTerminate();
  79. return -1;
  80. }
  81. glfwSetKeyCallback(window, key);
  82. glfwMakeContextCurrent(window);
  83. #ifdef NANOVG_GLEW
  84. glewExperimental = GL_TRUE;
  85. if(glewInit() != GLEW_OK) {
  86. printf("Could not init glew.\n");
  87. return -1;
  88. }
  89. #endif
  90. #ifdef DEMO_MSAA
  91. vg = nvgCreateGL3(512, 512, 0);
  92. #else
  93. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  94. #endif
  95. if (vg == NULL) {
  96. printf("Could not init nanovg.\n");
  97. return -1;
  98. }
  99. if (loadDemoData(vg, &data) == -1)
  100. return -1;
  101. glfwSwapInterval(0);
  102. timerquery = glfwExtensionSupported("GL_ARB_timer_query");
  103. if( timerquery ) {
  104. glGenQueries(NUM_QUERIES, timerqueryid);
  105. }
  106. glfwSetTime(0);
  107. prevt = glfwGetTime();
  108. while (!glfwWindowShouldClose(window))
  109. {
  110. double mx, my, t, dt;
  111. int winWidth, winHeight;
  112. int fbWidth, fbHeight;
  113. float pxRatio;
  114. t = glfwGetTime();
  115. dt = t - prevt;
  116. prevt = t;
  117. updateFPS(&fps, dt);
  118. updateFPS(&cpuTimes, cpuTime);
  119. if( timerquery ) {
  120. glBeginQuery(GL_TIME_ELAPSED, timerqueryid[currquery % NUM_QUERIES] );
  121. currquery = ++currquery;
  122. }
  123. glfwGetCursorPos(window, &mx, &my);
  124. glfwGetWindowSize(window, &winWidth, &winHeight);
  125. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  126. // Calculate pixel ration for hi-dpi devices.
  127. pxRatio = (float)fbWidth / (float)winWidth;
  128. // Update and render
  129. glViewport(0, 0, fbWidth, fbHeight);
  130. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  131. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  132. glEnable(GL_BLEND);
  133. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  134. glEnable(GL_CULL_FACE);
  135. glDisable(GL_DEPTH_TEST);
  136. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  137. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  138. renderFPS(vg, 5,5, &fps, RENDER_FPS);
  139. renderFPS(vg, 310,5, &cpuTimes, RENDER_MS);
  140. renderFPS(vg, 615,5, &gpuTimes, RENDER_MS);
  141. nvgEndFrame(vg);
  142. glEnable(GL_DEPTH_TEST);
  143. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  144. cpuTime = glfwGetTime() - t;
  145. if( timerquery ) {
  146. GLint available = 1;
  147. glEndQuery(GL_TIME_ELAPSED);
  148. while( available && retquery <= currquery ) {
  149. // check for results if there are any
  150. glGetQueryObjectiv(timerqueryid[retquery % NUM_QUERIES], GL_QUERY_RESULT_AVAILABLE, &available);
  151. if( available ) {
  152. GLuint64 timeElapsed = 0;
  153. double gpuTime;
  154. glGetQueryObjectui64v(timerqueryid[retquery % NUM_QUERIES], GL_QUERY_RESULT, &timeElapsed);
  155. retquery = ++retquery ;
  156. gpuTime = (double)timeElapsed * 1e-9;
  157. updateFPS(&gpuTimes, (float)gpuTime);
  158. }
  159. }
  160. }
  161. glfwSwapBuffers(window);
  162. glfwPollEvents();
  163. }
  164. freeDemoData(vg, &data);
  165. nvgDeleteGL3(vg);
  166. glfwTerminate();
  167. return 0;
  168. }