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.

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