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.

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