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.

154 lines
3.6KB

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