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.

232 lines
6.0KB

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