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.

187 lines
4.2KB

  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 "widgets/NanoPerfWidget.hpp"
  22. #include "nanovg_res/demo.h"
  23. // ------------------------------------------------------
  24. // use namespace
  25. using namespace DGL;
  26. // ------------------------------------------------------
  27. // NanoVG Example Widget
  28. int blowup = 0;
  29. int screenshot = 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. fContext(getContext())
  40. {
  41. parent.addIdleCallback(this);
  42. loadDemoData(fContext, &fData);
  43. }
  44. ~NanoExampleWidget() override
  45. {
  46. if (fContext == nullptr)
  47. return;
  48. freeDemoData(fContext, &fData);
  49. }
  50. protected:
  51. void idleCallback() override
  52. {
  53. repaint();
  54. }
  55. void onNanoDisplay() override
  56. {
  57. if (fContext == nullptr)
  58. return;
  59. const int winWidth = getWidth();
  60. const int winHeight = getHeight();
  61. if (premult)
  62. glClearColor(0, 0, 0, 0);
  63. else
  64. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  65. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  66. renderDemo(fContext, mx, my, winWidth, winHeight, gTime.getTime(), blowup, &fData);
  67. if (screenshot)
  68. {
  69. screenshot = 0;
  70. saveScreenShot(winWidth, winHeight, premult, "dump.png");
  71. }
  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 's':
  86. case 'S':
  87. screenshot = 1;
  88. break;
  89. case 'p':
  90. case 'P':
  91. premult = !premult;
  92. break;
  93. }
  94. return true;
  95. }
  96. bool onMotion(const MotionEvent& ev) override
  97. {
  98. mx = ev.pos.getX();
  99. my = ev.pos.getY();
  100. return false;
  101. }
  102. private:
  103. NVGcontext* fContext;
  104. DemoData fData;
  105. };
  106. // -----------------------------------------------------------------------
  107. // We need a custom window for multiple widgets
  108. class NanoExampleWindow : public Window
  109. {
  110. public:
  111. NanoExampleWindow(App& app)
  112. : Window(app),
  113. fDemo(*this),
  114. fPerf(*this, NanoPerfWidget::RENDER_FPS, "Frame Time")
  115. {
  116. fPerf.setAbsolutePos(5, 5);
  117. //fPerf.hide();
  118. setSize(1000, 600);
  119. setTitle("NanoVG");
  120. }
  121. protected:
  122. void onReshape(int width, int height)
  123. {
  124. fDemo.setSize(width, height);
  125. //fDemo.setAbsolutePos(10, height-fDemo.getHeight()-50);
  126. Window::onReshape(width, height);
  127. }
  128. private:
  129. NanoExampleWidget fDemo;
  130. NanoPerfWidget fPerf;
  131. };
  132. // ------------------------------------------------------
  133. // main entry point
  134. int main()
  135. {
  136. App app;
  137. NanoExampleWindow win(app);
  138. win.show();
  139. app.exec();
  140. return 0;
  141. }
  142. // ------------------------------------------------------
  143. extern "C" {
  144. #include "nanovg_res/demo.c"
  145. }
  146. // ------------------------------------------------------