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.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. #ifdef NANOVG_GLEW
  20. # include <GL/glew.h>
  21. #endif
  22. #define GLFW_INCLUDE_ES2
  23. #include <GLFW/glfw3.h>
  24. #include "nanovg.h"
  25. #define NANOVG_GLES2_IMPLEMENTATION
  26. #include "nanovg_gles2.h"
  27. #include "demo.h"
  28. void errorcb(int error, const char* desc)
  29. {
  30. printf("GLFW error: %s\n", desc);
  31. }
  32. int blowup = 0;
  33. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  34. {
  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 FPScounter fps;
  46. double prevt = 0;
  47. if (!glfwInit()) {
  48. printf("Failed to init GLFW.");
  49. return -1;
  50. }
  51. initFPS(&fps);
  52. glfwSetErrorCallback(errorcb);
  53. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  54. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  55. // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  56. // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  57. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  58. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  59. if (!window) {
  60. glfwTerminate();
  61. return -1;
  62. }
  63. glfwSetKeyCallback(window, key);
  64. glfwMakeContextCurrent(window);
  65. #ifdef NANOVG_GLEW
  66. glewExperimental = GL_TRUE;
  67. if(glewInit() != GLEW_OK) {
  68. printf("Could not init glew.\n");
  69. return -1;
  70. }
  71. #endif
  72. vg = nvgCreateGL3(512,512);
  73. if (vg == NULL) {
  74. printf("Could not init nanovg.\n");
  75. return -1;
  76. }
  77. if (loadDemoData(vg, &data) == -1)
  78. return -1;
  79. glfwSwapInterval(0);
  80. glfwSetTime(0);
  81. prevt = glfwGetTime();
  82. while (!glfwWindowShouldClose(window))
  83. {
  84. double mx, my, t, dt;
  85. int width, height;
  86. t = glfwGetTime();
  87. dt = t - prevt;
  88. prevt = t;
  89. updateFPS(&fps, dt);
  90. glfwGetCursorPos(window, &mx, &my);
  91. glfwGetFramebufferSize(window, &width, &height);
  92. // Update and render
  93. glViewport(0, 0, width, height);
  94. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  95. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  96. glEnable(GL_BLEND);
  97. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  98. glEnable(GL_CULL_FACE);
  99. glDisable(GL_DEPTH_TEST);
  100. nvgBeginFrame(vg, width, height);
  101. renderDemo(vg, mx,my, width,height, t, blowup, &data);
  102. renderFPS(vg, 5,5, &fps);
  103. glEnable(GL_DEPTH_TEST);
  104. glfwSwapBuffers(window);
  105. glfwPollEvents();
  106. }
  107. freeDemoData(vg, &data);
  108. nvgDeleteGLES2(vg);
  109. glfwTerminate();
  110. return 0;
  111. }