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.

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