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.7KB

  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 "nanovg_gl_utils.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. struct DemoData data;
  53. struct NVGcontext* vg = NULL;
  54. struct PerfGraph fps;
  55. double prevt = 0;
  56. struct NVGLUframebuffer* fb = NULL;
  57. if (!glfwInit()) {
  58. printf("Failed to init GLFW.");
  59. return -1;
  60. }
  61. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  62. glfwSetErrorCallback(errorcb);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  64. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  65. #ifdef DEMO_MSAA
  66. glfwWindowHint(GLFW_SAMPLES, 4);
  67. #endif
  68. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  69. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  70. if (!window) {
  71. glfwTerminate();
  72. return -1;
  73. }
  74. glfwSetKeyCallback(window, key);
  75. glfwMakeContextCurrent(window);
  76. #ifdef NANOVG_GLEW
  77. if(glewInit() != GLEW_OK) {
  78. printf("Could not init glew.\n");
  79. return -1;
  80. }
  81. #endif
  82. #ifdef DEMO_MSAA
  83. vg = nvgCreateGL2(512, 512, 0);
  84. #else
  85. vg = nvgCreateGL2(512, 512, NVG_ANTIALIAS);
  86. #endif
  87. if (vg == NULL) {
  88. printf("Could not init nanovg.\n");
  89. return -1;
  90. }
  91. if (loadDemoData(vg, &data) == -1)
  92. return -1;
  93. glfwSwapInterval(0);
  94. glfwSetTime(0);
  95. prevt = glfwGetTime();
  96. fb = nvgluCreateFramebuffer(vg, 600, 600);
  97. while (!glfwWindowShouldClose(window))
  98. {
  99. double mx, my, t, dt;
  100. int winWidth, winHeight;
  101. int fbWidth, fbHeight;
  102. float pxRatio;
  103. t = glfwGetTime();
  104. dt = t - prevt;
  105. prevt = t;
  106. updateGraph(&fps, dt);
  107. glfwGetCursorPos(window, &mx, &my);
  108. glfwGetWindowSize(window, &winWidth, &winHeight);
  109. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  110. // Calculate pixel ration for hi-dpi devices.
  111. pxRatio = (float)fbWidth / (float)winWidth;
  112. if (fb != NULL) {
  113. int fboWidth, fboHeight;
  114. nvgImageSize(vg, fb->image, &fboWidth, &fboHeight);
  115. // Draw some stull to an FBO as a test
  116. nvgluBindFramebuffer(fb);
  117. glViewport(0, 0, fboWidth, fboHeight);
  118. glClearColor(0, 0, 0, 0);
  119. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  120. nvgBeginFrame(vg, fboWidth, fboHeight, pxRatio, NVG_PREMULTIPLIED_ALPHA);
  121. renderDemo(vg, mx, my, fboWidth, fboHeight, t, blowup, &data);
  122. nvgEndFrame(vg);
  123. nvgluBindFramebuffer(NULL);
  124. }
  125. // Update and render
  126. glViewport(0, 0, fbWidth, fbHeight);
  127. if (premult)
  128. glClearColor(0,0,0,0);
  129. else
  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. nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA);
  133. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  134. renderGraph(vg, 5,5, &fps);
  135. if (fb != NULL) {
  136. struct NVGpaint img = nvgImagePattern(vg, 0, 0, 150, 150, 0, fb->image, NVG_NOREPEAT, 1.0f);
  137. nvgBeginPath(vg);
  138. nvgTranslate(vg, 540, 300);
  139. nvgRect(vg, 0, 0, 150, 150);
  140. nvgFillPaint(vg, img);
  141. nvgFill(vg);
  142. }
  143. nvgEndFrame(vg);
  144. if (screenshot) {
  145. screenshot = 0;
  146. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  147. }
  148. glfwSwapBuffers(window);
  149. glfwPollEvents();
  150. }
  151. freeDemoData(vg, &data);
  152. nvgDeleteGL2(vg);
  153. glfwTerminate();
  154. return 0;
  155. }