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.

166 lines
4.1KB

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