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.

159 lines
3.8KB

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