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.

239 lines
5.3KB

  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. double getTime()
  68. {
  69. return (double)(getRawTime() - base) * resolution;
  70. }
  71. };
  72. static TimePOSIX gTime;
  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. // init = false;
  95. initGraph(&fPerf, GRAPH_RENDER_FPS, "Frame Time");
  96. fContext = nvgCreateGL2(512, 512, NVG_ANTIALIAS);
  97. DISTRHO_SAFE_ASSERT_RETURN(fContext != nullptr,);
  98. loadDemoData(fContext, &fData);
  99. prevt = gTime.getTime();
  100. }
  101. ~NanoVGExampleWidget() override
  102. {
  103. if (fContext == nullptr)
  104. return;
  105. freeDemoData(fContext, &fData);
  106. nvgDeleteGL2(fContext);
  107. }
  108. protected:
  109. void idleCallback() override
  110. {
  111. repaint();
  112. }
  113. void onDisplay() override
  114. {
  115. const int winWidth = getWidth();
  116. const int winHeight = getHeight();
  117. double t, dt;
  118. t = gTime.getTime();
  119. dt = t - prevt;
  120. prevt = t;
  121. updateGraph(&fPerf, dt);
  122. if (premult)
  123. glClearColor(0,0,0,0);
  124. else
  125. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  126. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  127. nvgBeginFrame(fContext, winWidth, winHeight, 1.0f, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA);
  128. renderDemo(fContext, mx, my, winWidth, winHeight, t, blowup, &fData);
  129. renderGraph(fContext, 5, 5, &fPerf);
  130. nvgEndFrame(fContext);
  131. if (screenshot)
  132. {
  133. screenshot = 0;
  134. saveScreenShot(winWidth, winHeight, premult, "dump.png");
  135. }
  136. }
  137. bool onKeyboard(const KeyboardEvent& ev) override
  138. {
  139. if (! ev.press)
  140. return false;
  141. switch (ev.key)
  142. {
  143. case CHAR_ESCAPE:
  144. getParentApp().quit();
  145. break;
  146. case ' ':
  147. blowup = !blowup;
  148. break;
  149. case 's':
  150. case 'S':
  151. screenshot = 1;
  152. break;
  153. case 'p':
  154. case 'P':
  155. premult = !premult;
  156. break;
  157. }
  158. return true;
  159. }
  160. bool onMotion(const MotionEvent& ev) override
  161. {
  162. mx = ev.pos.getX();
  163. my = ev.pos.getY();
  164. return false;
  165. }
  166. private:
  167. NVGcontext* fContext;
  168. DemoData fData;
  169. PerfGraph fPerf;
  170. };
  171. // ------------------------------------------------------
  172. // main entry point
  173. int main()
  174. {
  175. StandaloneWindow swin;
  176. NanoVGExampleWidget widget(swin);
  177. swin.setSize(1000, 600);
  178. swin.setTitle("NanoVG");
  179. swin.exec();
  180. return 0;
  181. }
  182. // ------------------------------------------------------
  183. extern "C" {
  184. #include "nanovg_res/demo.c"
  185. #include "nanovg_res/perf.c"
  186. }
  187. // ------------------------------------------------------