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.

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