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.

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