DPF OpenGL examples
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.

245 lines
5.4KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // ------------------------------------------------------
  17. // DGL Stuff
  18. #include "StandaloneWindow.hpp"
  19. // ------------------------------------------------------
  20. // NanoVG Stuff
  21. #define NANOVG_GL2_IMPLEMENTATION
  22. #include "nanovg_src/nanovg_gl.h"
  23. #include "nanovg_res/demo.h"
  24. #include "nanovg_res/perf.h"
  25. #include <sys/time.h>
  26. #include <time.h>
  27. // ------------------------------------------------------
  28. // get time
  29. #ifdef DISTRHO_OS_WINDOWS
  30. #else
  31. struct TimePOSIX {
  32. bool monotonic;
  33. double resolution;
  34. uint64_t base;
  35. TimePOSIX()
  36. : monotonic(false),
  37. resolution(1e-6),
  38. base(0)
  39. {
  40. #if defined(CLOCK_MONOTONIC)
  41. struct timespec ts;
  42. if (::clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  43. {
  44. monotonic = true;
  45. resolution = 1e-9;
  46. }
  47. #endif
  48. base = getRawTime();
  49. }
  50. uint64_t getRawTime()
  51. {
  52. #if defined(CLOCK_MONOTONIC)
  53. if (monotonic)
  54. {
  55. struct timespec ts;
  56. ::clock_gettime(CLOCK_MONOTONIC, &ts);
  57. return (uint64_t) ts.tv_sec * (uint64_t) 1000000000 + (uint64_t) ts.tv_nsec;
  58. }
  59. else
  60. #endif
  61. {
  62. struct timeval tv;
  63. ::gettimeofday(&tv, NULL);
  64. return (uint64_t) tv.tv_sec * (uint64_t) 1000000 + (uint64_t) tv.tv_usec;
  65. }
  66. }
  67. };
  68. static TimePOSIX gTime;
  69. double glfwGetTime()
  70. {
  71. return (double)(gTime.getRawTime() - gTime.base) * gTime.resolution;
  72. }
  73. #endif
  74. // ------------------------------------------------------
  75. // use namespace
  76. using namespace DGL;
  77. // ------------------------------------------------------
  78. // NanoVG Example Widget
  79. int blowup = 0;
  80. int screenshot = 0;
  81. int premult = 0;
  82. int mx = 0;
  83. int my = 0;
  84. double prevt = 0;
  85. class NanoVGExampleWidget : public Widget,
  86. public IdleCallback
  87. {
  88. public:
  89. NanoVGExampleWidget(Window& parent)
  90. : Widget(parent),
  91. fContext(nullptr)
  92. {
  93. parent.addIdleCallback(this);
  94. }
  95. ~NanoVGExampleWidget() override
  96. {
  97. if (fContext == nullptr)
  98. return;
  99. freeDemoData(fContext, &fData);
  100. nvgDeleteGL2(fContext);
  101. }
  102. protected:
  103. void idleCallback() override
  104. {
  105. repaint();
  106. }
  107. void onDisplay() override
  108. {
  109. static bool init = true;
  110. if (init)
  111. {
  112. init = false;
  113. initGraph(&fPerf, GRAPH_RENDER_FPS, "Frame Time");
  114. fContext = nvgCreateGL2(512, 512, NVG_ANTIALIAS);
  115. DISTRHO_SAFE_ASSERT_RETURN(fContext != nullptr,);
  116. loadDemoData(fContext, &fData);
  117. prevt = glfwGetTime();
  118. }
  119. const int winWidth = getWidth();
  120. const int winHeight = getHeight();
  121. double t, dt;
  122. t = glfwGetTime();
  123. dt = t - prevt;
  124. prevt = t;
  125. updateGraph(&fPerf, dt);
  126. if (premult)
  127. glClearColor(0,0,0,0);
  128. else
  129. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  130. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  131. nvgBeginFrame(fContext, winWidth, winHeight, 1.0f, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA);
  132. renderDemo(fContext, mx, my, winWidth, winHeight, t, blowup, &fData);
  133. renderGraph(fContext, 5, 5, &fPerf);
  134. nvgEndFrame(fContext);
  135. if (screenshot)
  136. {
  137. screenshot = 0;
  138. saveScreenShot(winWidth, winHeight, premult, "dump.png");
  139. }
  140. }
  141. bool onKeyboard(const KeyboardEvent& ev) override
  142. {
  143. if (! ev.press)
  144. return false;
  145. switch (ev.key)
  146. {
  147. case CHAR_ESCAPE:
  148. getParentApp().quit();
  149. break;
  150. case ' ':
  151. blowup = !blowup;
  152. break;
  153. case 's':
  154. case 'S':
  155. screenshot = 1;
  156. break;
  157. case 'p':
  158. case 'P':
  159. premult = !premult;
  160. break;
  161. }
  162. return true;
  163. }
  164. bool onMotion(const MotionEvent& ev) override
  165. {
  166. mx = ev.pos.getX();
  167. my = ev.pos.getY();
  168. return false;
  169. }
  170. private:
  171. NVGcontext* fContext;
  172. DemoData fData;
  173. PerfGraph fPerf;
  174. };
  175. // ------------------------------------------------------
  176. // main entry point
  177. int main()
  178. {
  179. StandaloneWindow swin;
  180. NanoVGExampleWidget widget(swin);
  181. swin.setSize(1000, 600);
  182. swin.setTitle("NanoVG");
  183. swin.exec();
  184. return 0;
  185. }
  186. // ------------------------------------------------------
  187. extern "C" {
  188. #include "nanovg_res/demo.c"
  189. #include "nanovg_res/perf.c"
  190. }
  191. // ------------------------------------------------------