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.

134 lines
3.4KB

  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 "App.hpp"
  19. #include "Window.hpp"
  20. #include "widgets/ExampleRectanglesWidget.hpp"
  21. // ------------------------------------------------------
  22. // Qt Stuff
  23. #include <QtGui/QApplication>
  24. #include <QtGui/QIcon>
  25. #include <QtGui/QResizeEvent>
  26. #if DISTRHO_OS_LINUX
  27. # include <QtGui/QX11EmbedContainer>
  28. typedef QX11EmbedContainer QWidgetContainer;
  29. #elif DISTRHO_OS_MAC
  30. # include <QtGui/QMacCocoaViewContainer>
  31. typedef QMacCocoaViewContainer QWidgetContainer;
  32. #else
  33. # define DISTRHO_NEEDS_WINDOW_SIZE
  34. # include <QtGui/QWidget>
  35. typedef QWidget QWidgetContainer;
  36. #endif
  37. // ------------------------------------------------------
  38. // use namespace
  39. using DGL::App;
  40. using DGL::Window;
  41. // ------------------------------------------------------
  42. // Qt widget
  43. template<class DGLWidget>
  44. class DGLtoQtWidget : public QWidgetContainer
  45. {
  46. public:
  47. DGLtoQtWidget(QWidget* const qParent = nullptr)
  48. #if DISTRHO_OS_MAC
  49. : QWidgetContainer(nullptr, qParent),
  50. glApp(),
  51. glWindow(glApp, 0),
  52. #else
  53. : QWidgetContainer(qParent),
  54. glApp(),
  55. glWindow(glApp, winId()),
  56. #endif
  57. glWidget(glWindow),
  58. qTimerId(startTimer(30))
  59. {
  60. #if DISTRHO_OS_MAC
  61. //NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  62. //[pool release];
  63. setCocoaView((void*)glWindow.getWindowId());
  64. #endif
  65. }
  66. ~DGLtoQtWidget() override
  67. {
  68. killTimer(qTimerId);
  69. glWindow.close();
  70. }
  71. protected:
  72. void resizeEvent(QResizeEvent* const event) override
  73. {
  74. const QSize& size(event->size());
  75. glWidget.setSize(size.width(), size.height());
  76. #ifdef DISTRHO_NEEDS_WINDOW_SIZE
  77. // only needed if not handled by qt
  78. glWindow.setSize(size.width(), size.height());
  79. #endif
  80. QWidgetContainer::resizeEvent(event);
  81. }
  82. void timerEvent(QTimerEvent* const event) override
  83. {
  84. if (event->timerId() == qTimerId)
  85. glApp.idle();
  86. QWidgetContainer::timerEvent(event);
  87. }
  88. private:
  89. App glApp;
  90. Window glWindow;
  91. DGLWidget glWidget;
  92. const int qTimerId;
  93. };
  94. #ifdef DISTRHO_NEEDS_WINDOW_SIZE
  95. # undef DISTRHO_NEEDS_WINDOW_SIZE
  96. #endif
  97. // ------------------------------------------------------
  98. // main entry point
  99. int main(int argc, char* argv[])
  100. {
  101. QApplication app(argc, argv);
  102. app.setWindowIcon(QIcon::fromTheme("firefox"));
  103. DGLtoQtWidget<ExampleRectanglesWidget> widget;
  104. widget.setWindowIcon(QIcon::fromTheme("firefox"));
  105. widget.resize(300, 300);
  106. widget.show();
  107. return app.exec();
  108. }
  109. // ------------------------------------------------------