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.

233 lines
5.0KB

  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. #include "NanoVG.hpp"
  22. #include "nanovg_res/demo.h"
  23. #include "nanovg_res/perf.h"
  24. // ------------------------------------------------------
  25. // get time
  26. #include <sys/time.h>
  27. #include <time.h>
  28. #ifdef DISTRHO_OS_WINDOWS
  29. #else
  30. struct TimePOSIX {
  31. bool monotonic;
  32. double resolution;
  33. uint64_t base;
  34. TimePOSIX()
  35. : monotonic(false),
  36. resolution(1e-6),
  37. base(0)
  38. {
  39. #if defined(CLOCK_MONOTONIC)
  40. struct timespec ts;
  41. if (::clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  42. {
  43. monotonic = true;
  44. resolution = 1e-9;
  45. }
  46. #endif
  47. base = getRawTime();
  48. }
  49. uint64_t getRawTime()
  50. {
  51. #if defined(CLOCK_MONOTONIC)
  52. if (monotonic)
  53. {
  54. struct timespec ts;
  55. ::clock_gettime(CLOCK_MONOTONIC, &ts);
  56. return (uint64_t) ts.tv_sec * (uint64_t) 1000000000 + (uint64_t) ts.tv_nsec;
  57. }
  58. else
  59. #endif
  60. {
  61. struct timeval tv;
  62. ::gettimeofday(&tv, NULL);
  63. return (uint64_t) tv.tv_sec * (uint64_t) 1000000 + (uint64_t) tv.tv_usec;
  64. }
  65. }
  66. double getTime()
  67. {
  68. return (double)(getRawTime() - base) * resolution;
  69. }
  70. };
  71. static TimePOSIX gTime;
  72. #endif
  73. // ------------------------------------------------------
  74. // use namespace
  75. using namespace DGL;
  76. // ------------------------------------------------------
  77. // NanoVG Example Widget
  78. int blowup = 0;
  79. int screenshot = 0;
  80. int premult = 0;
  81. int mx = 0;
  82. int my = 0;
  83. double prevt = 0;
  84. class NanoExampleWidget : public NanoWidget,
  85. public IdleCallback
  86. {
  87. public:
  88. NanoExampleWidget(Window& parent)
  89. : NanoWidget(parent),
  90. fContext(getContext())
  91. {
  92. parent.addIdleCallback(this);
  93. initGraph(&fPerf, GRAPH_RENDER_FPS, "Frame Time");
  94. loadDemoData(fContext, &fData);
  95. prevt = gTime.getTime();
  96. }
  97. ~NanoExampleWidget() override
  98. {
  99. if (fContext == nullptr)
  100. return;
  101. freeDemoData(fContext, &fData);
  102. }
  103. protected:
  104. void idleCallback() override
  105. {
  106. repaint();
  107. }
  108. void onNanoDisplay() override
  109. {
  110. if (fContext == nullptr)
  111. return;
  112. const int winWidth = getWidth();
  113. const int winHeight = getHeight();
  114. double t, dt;
  115. t = gTime.getTime();
  116. dt = t - prevt;
  117. prevt = t;
  118. updateGraph(&fPerf, dt);
  119. if (premult)
  120. glClearColor(0, 0, 0, 0);
  121. else
  122. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  123. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  124. renderDemo(fContext, mx, my, winWidth, winHeight, t, blowup, &fData);
  125. renderGraph(fContext, 5, 5, &fPerf);
  126. if (screenshot)
  127. {
  128. screenshot = 0;
  129. saveScreenShot(winWidth, winHeight, premult, "dump.png");
  130. }
  131. }
  132. bool onKeyboard(const KeyboardEvent& ev) override
  133. {
  134. if (! ev.press)
  135. return false;
  136. switch (ev.key)
  137. {
  138. case CHAR_ESCAPE:
  139. getParentApp().quit();
  140. break;
  141. case ' ':
  142. blowup = !blowup;
  143. break;
  144. case 's':
  145. case 'S':
  146. screenshot = 1;
  147. break;
  148. case 'p':
  149. case 'P':
  150. premult = !premult;
  151. break;
  152. }
  153. return true;
  154. }
  155. bool onMotion(const MotionEvent& ev) override
  156. {
  157. mx = ev.pos.getX();
  158. my = ev.pos.getY();
  159. return false;
  160. }
  161. private:
  162. NVGcontext* fContext;
  163. DemoData fData;
  164. PerfGraph fPerf;
  165. };
  166. // ------------------------------------------------------
  167. // main entry point
  168. int main()
  169. {
  170. StandaloneWindow swin;
  171. NanoExampleWidget widget(swin);
  172. swin.setSize(1000, 600);
  173. swin.setTitle("NanoVG");
  174. swin.exec();
  175. return 0;
  176. }
  177. // ------------------------------------------------------
  178. extern "C" {
  179. #include "nanovg_res/demo.c"
  180. #include "nanovg_res/perf.c"
  181. }
  182. // ------------------------------------------------------