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.

209 lines
4.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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 "Application.hpp"
  19. #include "CairoWidget.hpp"
  20. #include "Window.hpp"
  21. #include <cstdio>
  22. // ------------------------------------------------------
  23. // Background widget (cairo will be painted on top)
  24. class BackgroundWidget : public Widget
  25. {
  26. public:
  27. BackgroundWidget(Window& parent)
  28. : Widget(parent)
  29. {
  30. }
  31. private:
  32. void onDisplay() override
  33. {
  34. int x = 0;
  35. int y = 0;
  36. int width = getWidth();
  37. int height = getHeight();
  38. // paint bg color (in full size)
  39. glColor3b(20, 80, 20);
  40. glBegin(GL_QUADS);
  41. glTexCoord2f(0.0f, 0.0f);
  42. glVertex2i(x, y);
  43. glTexCoord2f(1.0f, 0.0f);
  44. glVertex2i(x+width, y);
  45. glTexCoord2f(1.0f, 1.0f);
  46. glVertex2i(x+width, y+height);
  47. glTexCoord2f(0.0f, 1.0f);
  48. glVertex2i(x, y+height);
  49. glEnd();
  50. }
  51. void onReshape(int width, int height) override
  52. {
  53. // make this widget same size as window
  54. setSize(width, height);
  55. Widget::onReshape(width, height);
  56. }
  57. };
  58. // ------------------------------------------------------
  59. // Custom Cairo Widget
  60. class CustomCairoWidget : public CairoWidget,
  61. public IdleCallback
  62. {
  63. public:
  64. CustomCairoWidget(Window& parent)
  65. : CairoWidget(parent),
  66. value(0.0f),
  67. pressed(false)
  68. {
  69. setSize(100, 100);
  70. }
  71. private:
  72. void idleCallback() override
  73. {
  74. value += 0.001f;
  75. if (value > 1.0f)
  76. value = 0;
  77. repaint();
  78. }
  79. void cairoDisplay(cairo_t* const context) override
  80. {
  81. const int w = getWidth();
  82. const int h = getHeight();
  83. float radius = 40.0f;
  84. // * 0.9 for line width to remain inside redraw area
  85. if (w > h)
  86. radius = (h / 2.0f)*0.9f;
  87. else
  88. radius = (w / 2.0f)*0.9f;
  89. cairo_save(context);
  90. cairo_rectangle(context, 0, 0, w, h );
  91. cairo_set_source_rgba(context, 1.1, 0.1, 0.1, 0 );
  92. cairo_fill(context);
  93. cairo_set_line_join(context, CAIRO_LINE_JOIN_ROUND);
  94. cairo_set_line_cap(context, CAIRO_LINE_CAP_ROUND);
  95. cairo_set_line_width(context, 5-0.2);
  96. cairo_move_to(context, w/2, h/2);
  97. cairo_line_to(context, w/2, h/2);
  98. cairo_set_source_rgba(context, 0.1, 0.1, 0.1, 0 );
  99. cairo_stroke(context);
  100. cairo_arc(context, w/2, h/2, radius, 2.46, 0.75 );
  101. cairo_set_source_rgb(context, 0.1, 0.1, 0.1 );
  102. cairo_stroke(context);
  103. float angle = 2.46 + ( 4.54 * value );
  104. cairo_set_line_width(context, 5);
  105. cairo_arc(context, w/2, h/2, radius, 2.46, angle );
  106. cairo_line_to(context, w/2, h/2);
  107. cairo_set_source_rgba(context, 1.0, 0.48, 0, 0.8);
  108. cairo_stroke(context);
  109. cairo_restore(context);
  110. }
  111. bool onMouse(int button, bool press, int x, int y)
  112. {
  113. if (button == 1)
  114. {
  115. pressed = press;
  116. if (press)
  117. {
  118. setX(x-100/2);
  119. setY(y-100/2);
  120. }
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool onMotion(int x, int y)
  126. {
  127. if (pressed)
  128. {
  129. setX(x-100/2);
  130. setY(y-100/2);
  131. return true;
  132. }
  133. return false;
  134. }
  135. float value;
  136. bool pressed;
  137. };
  138. // ------------------------------------------------------
  139. // Custom window, with bg + cairo + image
  140. class CustomWindow : public Window
  141. {
  142. public:
  143. CustomWindow(App& app)
  144. : Window(app),
  145. bg(*this),
  146. cairo(*this)
  147. {
  148. addIdleCallback(&cairo);
  149. }
  150. private:
  151. BackgroundWidget bg;
  152. CustomCairoWidget cairo;
  153. };
  154. // ------------------------------------------------------
  155. // main entry point
  156. int main()
  157. {
  158. Application app;
  159. CustomWindow win(app);
  160. win.setSize(300, 300);
  161. win.setTitle("Cairo");
  162. win.show();
  163. app.exec();
  164. return 0;
  165. }
  166. // ------------------------------------------------------