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.

148 lines
3.5KB

  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: %s\n", desc);
  32. }
  33. int blowup = 0;
  34. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  35. {
  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, 3);
  55. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  56. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  57. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  58. #ifdef DEMO_MSAA
  59. glfwWindowHint(GLFW_SAMPLES, 4);
  60. #endif
  61. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  62. // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
  63. if (!window) {
  64. glfwTerminate();
  65. return -1;
  66. }
  67. glfwSetKeyCallback(window, key);
  68. glfwMakeContextCurrent(window);
  69. #ifdef NANOVG_GLEW
  70. glewExperimental = GL_TRUE;
  71. if(glewInit() != GLEW_OK) {
  72. printf("Could not init glew.\n");
  73. return -1;
  74. }
  75. #endif
  76. #ifdef DEMO_MSAA
  77. vg = nvgCreateGL3(512, 512, 0);
  78. #else
  79. vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS);
  80. #endif
  81. if (vg == NULL) {
  82. printf("Could not init nanovg.\n");
  83. return -1;
  84. }
  85. if (loadDemoData(vg, &data) == -1)
  86. return -1;
  87. glfwSwapInterval(0);
  88. glfwSetTime(0);
  89. prevt = glfwGetTime();
  90. while (!glfwWindowShouldClose(window))
  91. {
  92. double mx, my, t, dt;
  93. int width, height;
  94. t = glfwGetTime();
  95. dt = t - prevt;
  96. prevt = t;
  97. updateFPS(&fps, dt);
  98. glfwGetCursorPos(window, &mx, &my);
  99. glfwGetFramebufferSize(window, &width, &height);
  100. // Update and render
  101. glViewport(0, 0, width, height);
  102. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  103. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  104. glEnable(GL_BLEND);
  105. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  106. glEnable(GL_CULL_FACE);
  107. glDisable(GL_DEPTH_TEST);
  108. nvgBeginFrame(vg, width, height);
  109. renderDemo(vg, mx,my, width,height, t, blowup, &data);
  110. renderFPS(vg, 5,5, &fps);
  111. glEnable(GL_DEPTH_TEST);
  112. glfwSwapBuffers(window);
  113. glfwPollEvents();
  114. }
  115. freeDemoData(vg, &data);
  116. nvgDeleteGL3(vg);
  117. glfwTerminate();
  118. return 0;
  119. }