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.

182 lines
4.1KB

  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 "widgets/NanoPerfWidget.hpp"
  22. #include "nanovg_res/demo.h"
  23. // ------------------------------------------------------
  24. // NanoVG Example Widget
  25. int blowup = 0;
  26. int screenshot = 0;
  27. int premult = 0;
  28. int mx = 0;
  29. int my = 0;
  30. class NanoExampleWidget : public NanoWidget,
  31. public IdleCallback
  32. {
  33. public:
  34. NanoExampleWidget(Window& parent)
  35. : NanoWidget(parent),
  36. fContext(getContext())
  37. {
  38. parent.addIdleCallback(this);
  39. loadDemoData(fContext, &fData);
  40. }
  41. ~NanoExampleWidget() override
  42. {
  43. if (fContext == nullptr)
  44. return;
  45. freeDemoData(fContext, &fData);
  46. }
  47. protected:
  48. void idleCallback() override
  49. {
  50. repaint();
  51. }
  52. void onNanoDisplay() override
  53. {
  54. if (fContext == nullptr)
  55. return;
  56. const int winWidth = getWidth();
  57. const int winHeight = getHeight();
  58. if (premult)
  59. glClearColor(0, 0, 0, 0);
  60. else
  61. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  62. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  63. renderDemo(fContext, mx, my, winWidth, winHeight, gTime.getTime(), blowup, &fData);
  64. if (screenshot)
  65. {
  66. screenshot = 0;
  67. saveScreenShot(winWidth, winHeight, premult, "dump.png");
  68. }
  69. }
  70. bool onKeyboard(const KeyboardEvent& ev) override
  71. {
  72. if (! ev.press)
  73. return false;
  74. switch (ev.key)
  75. {
  76. case kCharEscape:
  77. getParentApp().quit();
  78. break;
  79. case ' ':
  80. blowup = !blowup;
  81. break;
  82. case 's':
  83. case 'S':
  84. screenshot = 1;
  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. NVGcontext* fContext;
  101. DemoData fData;
  102. };
  103. // -----------------------------------------------------------------------
  104. // We need a custom window for multiple widgets
  105. class NanoExampleWindow : public Window
  106. {
  107. public:
  108. NanoExampleWindow(Application& app)
  109. : Window(app),
  110. fDemo(*this),
  111. fPerf(*this, NanoPerfWidget::RENDER_FPS, "Frame Time")
  112. {
  113. fPerf.setAbsolutePos(5, 5);
  114. //fPerf.hide();
  115. setSize(1000, 600);
  116. setTitle("NanoVG");
  117. }
  118. protected:
  119. void onReshape(uint width, uint height) override
  120. {
  121. fDemo.setSize(width, height);
  122. //fDemo.setAbsolutePos(10, height-fDemo.getHeight()-50);
  123. Window::onReshape(width, height);
  124. }
  125. private:
  126. NanoExampleWidget fDemo;
  127. NanoPerfWidget fPerf;
  128. };
  129. // ------------------------------------------------------
  130. // main entry point
  131. int main()
  132. {
  133. Application app;
  134. NanoExampleWindow win(app);
  135. win.show();
  136. app.exec();
  137. return 0;
  138. }
  139. // ------------------------------------------------------
  140. extern "C" {
  141. #include "nanovg_res/demo.c"
  142. }
  143. // ------------------------------------------------------