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.2KB

  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. #include <GLFW/glfw3.h>
  23. #include "nanovg.h"
  24. #define NANOVG_GL2_IMPLEMENTATION
  25. #include "nanovg_gl2.h"
  26. #include "demo.h"
  27. void errorcb(int error, const char* desc)
  28. {
  29. printf("GLFW error: %s\n", desc);
  30. }
  31. int blowup = 0;
  32. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  33. {
  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_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. #ifdef NANOVG_GLEW
  63. if(glewInit() != GLEW_OK) {
  64. printf("Could not init glew.\n");
  65. return -1;
  66. }
  67. #endif
  68. vg = nvgCreateGL2(512,512);
  69. if (vg == NULL) {
  70. printf("Could not init nanovg.\n");
  71. return -1;
  72. }
  73. if (loadDemoData(vg, &data) == -1)
  74. return -1;
  75. glfwSwapInterval(0);
  76. glfwSetTime(0);
  77. prevt = glfwGetTime();
  78. while (!glfwWindowShouldClose(window))
  79. {
  80. double mx, my, t, dt;
  81. int width, height;
  82. t = glfwGetTime();
  83. dt = t - prevt;
  84. prevt = t;
  85. updateFPS(&fps, dt);
  86. glfwGetCursorPos(window, &mx, &my);
  87. glfwGetFramebufferSize(window, &width, &height);
  88. // Update and render
  89. glViewport(0, 0, width, height);
  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_TEXTURE_2D);
  96. glDisable(GL_DEPTH_TEST);
  97. glColor4ub(255,255,255,255);
  98. nvgBeginFrame(vg, width, height);
  99. renderDemo(vg, mx,my, width,height, t, blowup, &data);
  100. renderFPS(vg, 5,5, &fps);
  101. glEnable(GL_DEPTH_TEST);
  102. glfwSwapBuffers(window);
  103. glfwPollEvents();
  104. }
  105. freeDemoData(vg, &data);
  106. nvgDeleteGL2(vg);
  107. glfwTerminate();
  108. return 0;
  109. }