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.

216 lines
5.3KB

  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. #ifndef EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "Image.hpp"
  21. #include "Widget.hpp"
  22. #include "Window.hpp"
  23. // ------------------------------------------------------
  24. // Images
  25. #include "../images_res/CatPics.hpp"
  26. // ------------------------------------------------------
  27. // use namespace
  28. using DGL::IdleCallback;
  29. using DGL::Image;
  30. using DGL::Rectangle;
  31. using DGL::Widget;
  32. using DGL::Window;
  33. // ------------------------------------------------------
  34. // our widget
  35. class ExampleImagesWidget : public Widget,
  36. public IdleCallback
  37. {
  38. public:
  39. static const int kImg1y = 0;
  40. static const int kImg2y = 500/2-CatPics::cat2Height/2;
  41. static const int kImg3x = 400/3-CatPics::cat3Width/3;
  42. static const int kImg1max = 500-CatPics::cat1Width;
  43. static const int kImg2max = 500-CatPics::cat2Width;
  44. static const int kImg3max = 400-CatPics::cat3Height;
  45. ExampleImagesWidget(Window& parent, const bool setParentSize = false)
  46. : Widget(parent),
  47. fImgTop1st(1),
  48. fImgTop2nd(2),
  49. fImgTop3rd(3),
  50. fImg1x(0),
  51. fImg2x(kImg2max),
  52. fImg3y(kImg3max),
  53. fImg1rev(false),
  54. fImg2rev(true),
  55. fImg3rev(true),
  56. fImg1(CatPics::cat1Data, CatPics::cat1Width, CatPics::cat1Height, GL_BGR),
  57. fImg2(CatPics::cat2Data, CatPics::cat2Width, CatPics::cat2Height, GL_BGR),
  58. fImg3(CatPics::cat3Data, CatPics::cat3Width, CatPics::cat3Height, GL_BGR)
  59. {
  60. setSize(500, 400);
  61. parent.addIdleCallback(this);
  62. if (setParentSize)
  63. {
  64. parent.setSize(500, 400);
  65. parent.setResizable(false);
  66. }
  67. }
  68. private:
  69. void idleCallback() noexcept override
  70. {
  71. if (fImg1rev)
  72. {
  73. fImg1x -= 2;
  74. if (fImg1x <= -50)
  75. {
  76. fImg1rev = false;
  77. setNewTopImg(1);
  78. }
  79. }
  80. else
  81. {
  82. fImg1x += 2;
  83. if (fImg1x >= kImg1max+50)
  84. {
  85. fImg1rev = true;
  86. setNewTopImg(1);
  87. }
  88. }
  89. if (fImg2rev)
  90. {
  91. fImg2x -= 1;
  92. if (fImg2x <= -50)
  93. {
  94. fImg2rev = false;
  95. setNewTopImg(2);
  96. }
  97. }
  98. else
  99. {
  100. fImg2x += 4;
  101. if (fImg2x >= kImg2max+50)
  102. {
  103. fImg2rev = true;
  104. setNewTopImg(2);
  105. }
  106. }
  107. if (fImg3rev)
  108. {
  109. fImg3y -= 3;
  110. if (fImg3y <= -50)
  111. {
  112. fImg3rev = false;
  113. setNewTopImg(3);
  114. }
  115. }
  116. else
  117. {
  118. fImg3y += 3;
  119. if (fImg3y >= kImg3max+50)
  120. {
  121. fImg3rev = true;
  122. setNewTopImg(3);
  123. }
  124. }
  125. repaint();
  126. }
  127. void onDisplay() override
  128. {
  129. const int cx = getX();
  130. const int cy = getY();
  131. switch (fImgTop3rd)
  132. {
  133. case 1:
  134. fImg1.drawAt(fImg1x+cx, kImg1y+cy);
  135. break;
  136. case 2:
  137. fImg2.drawAt(fImg2x+cx, kImg2y+cy);
  138. break;
  139. case 3:
  140. fImg3.drawAt(kImg3x+cx, fImg3y+cy);
  141. break;
  142. };
  143. switch (fImgTop2nd)
  144. {
  145. case 1:
  146. fImg1.drawAt(fImg1x+cx, kImg1y+cy);
  147. break;
  148. case 2:
  149. fImg2.drawAt(fImg2x+cx, kImg2y+cy);
  150. break;
  151. case 3:
  152. fImg3.drawAt(kImg3x+cx, fImg3y+cy);
  153. break;
  154. };
  155. switch (fImgTop1st)
  156. {
  157. case 1:
  158. fImg1.drawAt(fImg1x+cx, kImg1y+cy);
  159. break;
  160. case 2:
  161. fImg2.drawAt(fImg2x+cx, kImg2y+cy);
  162. break;
  163. case 3:
  164. fImg3.drawAt(kImg3x+cx, fImg3y+cy);
  165. break;
  166. };
  167. }
  168. void setNewTopImg(const int imgId) noexcept
  169. {
  170. if (fImgTop1st == imgId)
  171. return;
  172. if (fImgTop2nd == imgId)
  173. {
  174. fImgTop2nd = fImgTop1st;
  175. fImgTop1st = imgId;
  176. return;
  177. }
  178. fImgTop3rd = fImgTop2nd;
  179. fImgTop2nd = fImgTop1st;
  180. fImgTop1st = imgId;
  181. }
  182. int fImgTop1st, fImgTop2nd, fImgTop3rd;
  183. int fImg1x, fImg2x, fImg3y;
  184. bool fImg1rev, fImg2rev, fImg3rev;
  185. Image fImg1, fImg2, fImg3;
  186. };
  187. // ------------------------------------------------------
  188. #endif // EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED