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.

155 lines
3.7KB

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