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
6.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 "widgets/NanoPerfWidget.hpp"
  23. // ------------------------------------------------------
  24. // use namespace
  25. using namespace DGL;
  26. using namespace DISTRHO;
  27. // ------------------------------------------------------
  28. // NanoVG Example Widget
  29. int blowup = 0;
  30. int premult = 0;
  31. int mx = 0;
  32. int my = 0;
  33. class NanoExampleWidget : public NanoWidget,
  34. public IdleCallback
  35. {
  36. public:
  37. NanoExampleWidget(Window& parent)
  38. : NanoWidget(parent)
  39. {
  40. parent.addIdleCallback(this);
  41. for (int i = 0; i < 12; ++i)
  42. {
  43. char file[128];
  44. std::snprintf(file, 128, "./nanovg_res/images/image%d.jpg", i+1);
  45. fImages[i] = createImage(file);
  46. if (fImages[i] == nullptr)
  47. {
  48. d_stdout("Could not load %s.", file);
  49. return;
  50. }
  51. }
  52. fFontIcons = createFont("icons", "./nanovg_res/entypo.ttf");
  53. fFontNormal = createFont("sans", "./nanovg_res/Roboto-Regular.ttf");
  54. fFontBold = createFont("sans-bold", "./nanovg_res/Roboto-Bold.ttf");
  55. }
  56. protected:
  57. void idleCallback() override
  58. {
  59. repaint();
  60. }
  61. void onNanoDisplay() override
  62. {
  63. const int width = getWidth();
  64. //const int height = getHeight();
  65. const double t = gTime.getTime();
  66. if (premult)
  67. glClearColor(0, 0, 0, 0);
  68. else
  69. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  70. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  71. drawEyes(width - 250, 50, 150, 100, mx, my, t);
  72. }
  73. bool onKeyboard(const KeyboardEvent& ev) override
  74. {
  75. if (! ev.press)
  76. return false;
  77. switch (ev.key)
  78. {
  79. case CHAR_ESCAPE:
  80. getParentApp().quit();
  81. break;
  82. case ' ':
  83. blowup = !blowup;
  84. break;
  85. case 'p':
  86. case 'P':
  87. premult = !premult;
  88. break;
  89. }
  90. return true;
  91. }
  92. bool onMotion(const MotionEvent& ev) override
  93. {
  94. mx = ev.pos.getX();
  95. my = ev.pos.getY();
  96. return false;
  97. }
  98. private:
  99. FontId fFontNormal, fFontBold, fFontIcons;
  100. ScopedPointer<NanoImage> fImages[12];
  101. void drawEyes(float x, float y, float w, float h, float mx, float my, float t)
  102. {
  103. Paint gloss, bg;
  104. float ex = w *0.23f;
  105. float ey = h * 0.5f;
  106. float lx = x + ex;
  107. float ly = y + ey;
  108. float rx = x + w - ex;
  109. float ry = y + ey;
  110. float dx,dy,d;
  111. float br = (ex < ey ? ex : ey) * 0.5f;
  112. float blink = 1 - std::pow(std::sin(t*0.5f),200)*0.8f;
  113. bg = linearGradient(x,y+h*0.5f,x+w*0.1f,y+h, Color(0,0,0,32), Color(0,0,0,16));
  114. beginPath();
  115. ellipse(lx+3.0f,ly+16.0f, ex,ey);
  116. ellipse(rx+3.0f,ry+16.0f, ex,ey);
  117. fillPaint(bg);
  118. fill();
  119. bg = linearGradient(x,y+h*0.25f,x+w*0.1f,y+h, Color(220,220,220,255), Color(128,128,128,255));
  120. beginPath();
  121. ellipse(lx,ly, ex,ey);
  122. ellipse(rx,ry, ex,ey);
  123. fillPaint(bg);
  124. fill();
  125. dx = (mx - rx) / (ex * 10);
  126. dy = (my - ry) / (ey * 10);
  127. d = std::sqrt(dx*dx+dy*dy);
  128. if (d > 1.0f) {
  129. dx /= d; dy /= d;
  130. }
  131. dx *= ex*0.4f;
  132. dy *= ey*0.5f;
  133. beginPath();
  134. ellipse(lx+dx,ly+dy+ey*0.25f*(1-blink), br,br*blink);
  135. fillColor(32,32,32,255);
  136. fill();
  137. dx = (mx - rx) / (ex * 10);
  138. dy = (my - ry) / (ey * 10);
  139. d = std::sqrt(dx*dx+dy*dy);
  140. if (d > 1.0f) {
  141. dx /= d; dy /= d;
  142. }
  143. dx *= ex*0.4f;
  144. dy *= ey*0.5f;
  145. beginPath();
  146. ellipse(rx+dx,ry+dy+ey*0.25f*(1-blink), br,br*blink);
  147. fillColor(32,32,32,255);
  148. fill();
  149. gloss = radialGradient(lx-ex*0.25f,ly-ey*0.5f, ex*0.1f,ex*0.75f, Color(255,255,255,128), Color(255,255,255,0));
  150. beginPath();
  151. ellipse(lx,ly, ex,ey);
  152. fillPaint(gloss);
  153. fill();
  154. gloss = radialGradient(rx-ex*0.25f,ry-ey*0.5f, ex*0.1f,ex*0.75f, Color(255,255,255,128), Color(255,255,255,0));
  155. beginPath();
  156. ellipse(rx,ry, ex,ey);
  157. fillPaint(gloss);
  158. fill();
  159. }
  160. };
  161. // -----------------------------------------------------------------------
  162. // We need a custom window for multiple widgets
  163. class NanoExampleWindow : public Window
  164. {
  165. public:
  166. NanoExampleWindow(App& app)
  167. : Window(app),
  168. fDemo(*this),
  169. fPerf(*this, NanoPerfWidget::RENDER_FPS, "Frame Time")
  170. {
  171. fPerf.setAbsolutePos(5, 5);
  172. setSize(1000, 600);
  173. setTitle("NanoVG");
  174. }
  175. protected:
  176. void onReshape(int width, int height)
  177. {
  178. fDemo.setSize(width, height);
  179. Window::onReshape(width, height);
  180. }
  181. private:
  182. NanoExampleWidget fDemo;
  183. NanoPerfWidget fPerf;
  184. };
  185. // ------------------------------------------------------
  186. // main entry point
  187. int main()
  188. {
  189. App app;
  190. NanoExampleWindow win(app);
  191. win.show();
  192. app.exec();
  193. return 0;
  194. }
  195. // ------------------------------------------------------