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.

140 lines
3.5KB

  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_gl2.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. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  32. {
  33. NVG_NOTUSED(scancode);
  34. NVG_NOTUSED(mods);
  35. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  36. glfwSetWindowShouldClose(window, GL_TRUE);
  37. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  38. blowup = !blowup;
  39. }
  40. int main()
  41. {
  42. GLFWwindow* window;
  43. struct DemoData data;
  44. struct NVGcontext* vg = NULL;
  45. struct PerfGraph fps;
  46. double prevt = 0;
  47. if (!glfwInit()) {
  48. printf("Failed to init GLFW.");
  49. return -1;
  50. }
  51. initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
  52. glfwSetErrorCallback(errorcb);
  53. glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
  54. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  55. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  56. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  57. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  58. if (!window) {
  59. glfwTerminate();
  60. return -1;
  61. }
  62. glfwSetKeyCallback(window, key);
  63. glfwMakeContextCurrent(window);
  64. vg = nvgCreateGLES2(512, 512, NVG_ANTIALIAS);
  65. if (vg == NULL) {
  66. printf("Could not init nanovg.\n");
  67. return -1;
  68. }
  69. if (loadDemoData(vg, &data) == -1)
  70. return -1;
  71. glfwSwapInterval(0);
  72. glfwSetTime(0);
  73. prevt = glfwGetTime();
  74. while (!glfwWindowShouldClose(window))
  75. {
  76. double mx, my, t, dt;
  77. int winWidth, winHeight;
  78. int fbWidth, fbHeight;
  79. float pxRatio;
  80. t = glfwGetTime();
  81. dt = t - prevt;
  82. prevt = t;
  83. updateGraph(&fps, dt);
  84. glfwGetCursorPos(window, &mx, &my);
  85. glfwGetWindowSize(window, &winWidth, &winHeight);
  86. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  87. // Calculate pixel ration for hi-dpi devices.
  88. pxRatio = (float)fbWidth / (float)winWidth;
  89. // Update and render
  90. glViewport(0, 0, fbWidth, fbHeight);
  91. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  92. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  93. glEnable(GL_BLEND);
  94. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  95. glEnable(GL_CULL_FACE);
  96. glDisable(GL_DEPTH_TEST);
  97. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  98. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  99. renderGraph(vg, 5,5, &fps);
  100. nvgEndFrame(vg);
  101. glEnable(GL_DEPTH_TEST);
  102. glfwSwapBuffers(window);
  103. glfwPollEvents();
  104. }
  105. freeDemoData(vg, &data);
  106. nvgDeleteGLES2(vg);
  107. glfwTerminate();
  108. return 0;
  109. }