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.

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