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.

238 lines
5.1KB

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