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.

189 lines
4.8KB

  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. #include "perf.h"
  33. void errorcb(int error, const char* desc)
  34. {
  35. printf("GLFW error %d: %s\n", error, desc);
  36. }
  37. int blowup = 0;
  38. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  39. {
  40. NVG_NOTUSED(scancode);
  41. NVG_NOTUSED(mods);
  42. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  43. glfwSetWindowShouldClose(window, GL_TRUE);
  44. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  45. blowup = !blowup;
  46. }
  47. int main()
  48. {
  49. GLFWwindow* window;
  50. struct DemoData data;
  51. struct NVGcontext* vg = NULL;
  52. struct GPUtimer gpuTimer;
  53. struct PerfGraph fps, cpuGraph, gpuGraph;
  54. double prevt = 0, cpuTime = 0;
  55. if (!glfwInit()) {
  56. printf("Failed to init GLFW.");
  57. return -1;
  58. }
  59. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  60. initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
  61. initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
  62. glfwSetErrorCallback(errorcb);
  63. #ifndef _WIN32 // don't require this on win32, and works with more cards
  64. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  65. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  66. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  67. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  68. #endif
  69. #ifdef DEMO_MSAA
  70. glfwWindowHint(GLFW_SAMPLES, 4);
  71. #endif
  72. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  73. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  74. if (!window) {
  75. glfwTerminate();
  76. return -1;
  77. }
  78. glfwSetKeyCallback(window, key);
  79. glfwMakeContextCurrent(window);
  80. #ifdef NANOVG_GLEW
  81. glewExperimental = GL_TRUE;
  82. if(glewInit() != GLEW_OK) {
  83. printf("Could not init glew.\n");
  84. return -1;
  85. }
  86. #endif
  87. #ifdef DEMO_MSAA
  88. vg = nvgCreateGL3(512, 512, 0);
  89. #else
  90. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  91. #endif
  92. if (vg == NULL) {
  93. printf("Could not init nanovg.\n");
  94. return -1;
  95. }
  96. if (loadDemoData(vg, &data) == -1)
  97. return -1;
  98. glfwSwapInterval(0);
  99. initGPUTimer(&gpuTimer);
  100. glfwSetTime(0);
  101. prevt = glfwGetTime();
  102. while (!glfwWindowShouldClose(window))
  103. {
  104. double mx, my, t, dt;
  105. int winWidth, winHeight;
  106. int fbWidth, fbHeight;
  107. float pxRatio;
  108. float gpuTimes[3];
  109. int i, n;
  110. t = glfwGetTime();
  111. dt = t - prevt;
  112. prevt = t;
  113. startGPUTimer(&gpuTimer);
  114. glfwGetCursorPos(window, &mx, &my);
  115. glfwGetWindowSize(window, &winWidth, &winHeight);
  116. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  117. // Calculate pixel ration for hi-dpi devices.
  118. pxRatio = (float)fbWidth / (float)winWidth;
  119. // Update and render
  120. glViewport(0, 0, fbWidth, fbHeight);
  121. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  122. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  123. glEnable(GL_BLEND);
  124. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  125. glEnable(GL_CULL_FACE);
  126. glDisable(GL_DEPTH_TEST);
  127. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  128. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  129. renderGraph(vg, 5,5, &fps);
  130. renderGraph(vg, 5+200+5,5, &cpuGraph);
  131. if (gpuTimer.supported)
  132. renderGraph(vg, 5+200+5+200+5,5, &gpuGraph);
  133. nvgEndFrame(vg);
  134. glEnable(GL_DEPTH_TEST);
  135. // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
  136. cpuTime = glfwGetTime() - t;
  137. updateGraph(&fps, dt);
  138. updateGraph(&cpuGraph, cpuTime);
  139. // We may get multiple results.
  140. n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
  141. for (i = 0; i < n; i++)
  142. updateGraph(&gpuGraph, gpuTimes[i]);
  143. glfwSwapBuffers(window);
  144. glfwPollEvents();
  145. }
  146. freeDemoData(vg, &data);
  147. nvgDeleteGL3(vg);
  148. printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
  149. printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
  150. printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
  151. glfwTerminate();
  152. return 0;
  153. }