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.

130 lines
3.1KB

  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_GL2_IMPLEMENTATION
  23. #define NANOVG_GLES2
  24. #include "nanovg_gl2.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, 2);
  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 = nvgCreateGL2(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 width, height;
  76. t = glfwGetTime();
  77. dt = t - prevt;
  78. prevt = t;
  79. updateFPS(&fps, dt);
  80. glfwGetCursorPos(window, &mx, &my);
  81. glfwGetFramebufferSize(window, &width, &height);
  82. // Update and render
  83. glViewport(0, 0, width, height);
  84. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  85. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  86. glEnable(GL_BLEND);
  87. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  88. glEnable(GL_CULL_FACE);
  89. glDisable(GL_DEPTH_TEST);
  90. nvgBeginFrame(vg, width, height);
  91. renderDemo(vg, mx,my, width,height, t, blowup, &data);
  92. renderFPS(vg, 5,5, &fps);
  93. glEnable(GL_DEPTH_TEST);
  94. glfwSwapBuffers(window);
  95. glfwPollEvents();
  96. }
  97. freeDemoData(vg, &data);
  98. nvgDeleteGL2(vg);
  99. glfwTerminate();
  100. return 0;
  101. }