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.

162 lines
3.9KB

  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. double prevt = 0;
  53. if (!glfwInit()) {
  54. printf("Failed to init GLFW.");
  55. return -1;
  56. }
  57. initFPS(&fps);
  58. glfwSetErrorCallback(errorcb);
  59. #ifndef _WIN32 // don't require this on win32, and works with more cards
  60. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  61. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  62. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  63. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  64. #endif
  65. #ifdef DEMO_MSAA
  66. glfwWindowHint(GLFW_SAMPLES, 4);
  67. #endif
  68. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  69. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  70. if (!window) {
  71. glfwTerminate();
  72. return -1;
  73. }
  74. glfwSetKeyCallback(window, key);
  75. glfwMakeContextCurrent(window);
  76. #ifdef NANOVG_GLEW
  77. glewExperimental = GL_TRUE;
  78. if(glewInit() != GLEW_OK) {
  79. printf("Could not init glew.\n");
  80. return -1;
  81. }
  82. #endif
  83. #ifdef DEMO_MSAA
  84. vg = nvgCreateGL3(512, 512, 0);
  85. #else
  86. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  87. #endif
  88. if (vg == NULL) {
  89. printf("Could not init nanovg.\n");
  90. return -1;
  91. }
  92. if (loadDemoData(vg, &data) == -1)
  93. return -1;
  94. glfwSwapInterval(0);
  95. glfwSetTime(0);
  96. prevt = glfwGetTime();
  97. while (!glfwWindowShouldClose(window))
  98. {
  99. double mx, my, t, dt;
  100. int winWidth, winHeight;
  101. int fbWidth, fbHeight;
  102. float pxRatio;
  103. t = glfwGetTime();
  104. dt = t - prevt;
  105. prevt = t;
  106. updateFPS(&fps, dt);
  107. glfwGetCursorPos(window, &mx, &my);
  108. glfwGetWindowSize(window, &winWidth, &winHeight);
  109. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  110. // Calculate pixel ration for hi-dpi devices.
  111. pxRatio = (float)fbWidth / (float)winWidth;
  112. // Update and render
  113. glViewport(0, 0, fbWidth, fbHeight);
  114. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  115. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  116. glEnable(GL_BLEND);
  117. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  118. glEnable(GL_CULL_FACE);
  119. glDisable(GL_DEPTH_TEST);
  120. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  121. renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
  122. renderFPS(vg, 5,5, &fps);
  123. nvgEndFrame(vg);
  124. glEnable(GL_DEPTH_TEST);
  125. glfwSwapBuffers(window);
  126. glfwPollEvents();
  127. }
  128. freeDemoData(vg, &data);
  129. nvgDeleteGL3(vg);
  130. glfwTerminate();
  131. return 0;
  132. }