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.

137 lines
3.3KB

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