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.

163 lines
3.9KB

  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_INCLUDE_GLEXT
  23. #include <GLFW/glfw3.h>
  24. #include "nanovg.h"
  25. #define NANOVG_GL2_IMPLEMENTATION
  26. #include "nanovg_gl.h"
  27. #include "demo.h"
  28. #include "perf.h"
  29. void errorcb(int error, const char* desc)
  30. {
  31. printf("GLFW error %d: %s\n", error, desc);
  32. }
  33. int blowup = 0;
  34. int screenshot = 0;
  35. int premult = 0;
  36. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  37. {
  38. NVG_NOTUSED(scancode);
  39. NVG_NOTUSED(mods);
  40. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  41. glfwSetWindowShouldClose(window, GL_TRUE);
  42. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  43. blowup = !blowup;
  44. if (key == GLFW_KEY_S && action == GLFW_PRESS)
  45. screenshot = 1;
  46. if (key == GLFW_KEY_P && action == GLFW_PRESS)
  47. premult = !premult;
  48. }
  49. int main()
  50. {
  51. GLFWwindow* window;
  52. DemoData data;
  53. NVGcontext* vg = NULL;
  54. PerfGraph fps;
  55. double prevt = 0;
  56. if (!glfwInit()) {
  57. printf("Failed to init GLFW.");
  58. return -1;
  59. }
  60. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  61. glfwSetErrorCallback(errorcb);
  62. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  64. #ifdef DEMO_MSAA
  65. glfwWindowHint(GLFW_SAMPLES, 4);
  66. #endif
  67. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  68. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  69. if (!window) {
  70. glfwTerminate();
  71. return -1;
  72. }
  73. glfwSetKeyCallback(window, key);
  74. glfwMakeContextCurrent(window);
  75. #ifdef NANOVG_GLEW
  76. if(glewInit() != GLEW_OK) {
  77. printf("Could not init glew.\n");
  78. return -1;
  79. }
  80. #endif
  81. #ifdef DEMO_MSAA
  82. vg = nvgCreateGL2(NVG_STENCIL_STROKES | NVG_DEBUG);
  83. #else
  84. vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
  85. #endif
  86. if (vg == NULL) {
  87. printf("Could not init nanovg.\n");
  88. return -1;
  89. }
  90. if (loadDemoData(vg, &data) == -1)
  91. return -1;
  92. glfwSwapInterval(0);
  93. glfwSetTime(0);
  94. prevt = glfwGetTime();
  95. while (!glfwWindowShouldClose(window))
  96. {
  97. double mx, my, t, dt;
  98. int winWidth, winHeight;
  99. int fbWidth, fbHeight;
  100. float pxRatio;
  101. t = glfwGetTime();
  102. dt = t - prevt;
  103. prevt = t;
  104. updateGraph(&fps, dt);
  105. glfwGetCursorPos(window, &mx, &my);
  106. glfwGetWindowSize(window, &winWidth, &winHeight);
  107. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  108. // Calculate pixel ration for hi-dpi devices.
  109. pxRatio = (float)fbWidth / (float)winWidth;
  110. // Update and render
  111. glViewport(0, 0, fbWidth, fbHeight);
  112. if (premult)
  113. glClearColor(0,0,0,0);
  114. else
  115. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  116. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  117. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  118. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  119. renderGraph(vg, 5,5, &fps);
  120. nvgEndFrame(vg);
  121. if (screenshot) {
  122. screenshot = 0;
  123. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  124. }
  125. glfwSwapBuffers(window);
  126. glfwPollEvents();
  127. }
  128. freeDemoData(vg, &data);
  129. nvgDeleteGL2(vg);
  130. glfwTerminate();
  131. return 0;
  132. }