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.

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