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.

139 lines
3.4KB

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