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.

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