example_gles3.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #define GLFW_INCLUDE_GLEXT
  21. #include <GLFW/glfw3.h>
  22. #include "nanovg.h"
  23. #define NANOVG_GLES3_IMPLEMENTATION
  24. #include "nanovg_gl.h"
  25. #include "nanovg_gl_utils.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. DemoData data;
  52. NVGcontext* vg = NULL;
  53. 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_CLIENT_API, GLFW_OPENGL_ES_API);
  62. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  64. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  65. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  66. if (!window) {
  67. glfwTerminate();
  68. return -1;
  69. }
  70. glfwSetKeyCallback(window, key);
  71. glfwMakeContextCurrent(window);
  72. vg = nvgCreateGLES3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
  73. if (vg == NULL) {
  74. printf("Could not init nanovg.\n");
  75. return -1;
  76. }
  77. if (loadDemoData(vg, &data) == -1)
  78. return -1;
  79. glfwSwapInterval(0);
  80. glfwSetTime(0);
  81. prevt = glfwGetTime();
  82. while (!glfwWindowShouldClose(window))
  83. {
  84. double mx, my, t, dt;
  85. int winWidth, winHeight;
  86. int fbWidth, fbHeight;
  87. float pxRatio;
  88. t = glfwGetTime();
  89. dt = t - prevt;
  90. prevt = t;
  91. updateGraph(&fps, dt);
  92. glfwGetCursorPos(window, &mx, &my);
  93. glfwGetWindowSize(window, &winWidth, &winHeight);
  94. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  95. // Calculate pixel ration for hi-dpi devices.
  96. pxRatio = (float)fbWidth / (float)winWidth;
  97. // Update and render
  98. glViewport(0, 0, fbWidth, fbHeight);
  99. if (premult)
  100. glClearColor(0,0,0,0);
  101. else
  102. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  103. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  104. glEnable(GL_BLEND);
  105. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  106. glEnable(GL_CULL_FACE);
  107. glDisable(GL_DEPTH_TEST);
  108. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  109. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  110. renderGraph(vg, 5,5, &fps);
  111. nvgEndFrame(vg);
  112. glEnable(GL_DEPTH_TEST);
  113. if (screenshot) {
  114. screenshot = 0;
  115. saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
  116. }
  117. glfwSwapBuffers(window);
  118. glfwPollEvents();
  119. }
  120. freeDemoData(vg, &data);
  121. nvgDeleteGLES3(vg);
  122. glfwTerminate();
  123. return 0;
  124. }