DISTRHO Plugin Framework
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.

229 lines
6.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 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. #include "tests.hpp"
  17. #include "dgl/NanoVG.hpp"
  18. START_NAMESPACE_DGL
  19. // --------------------------------------------------------------------------------------------------------------------
  20. // Images
  21. #include "images_res/CatPics.cpp"
  22. // --------------------------------------------------------------------------------------------------------------------
  23. class NanoImageExample : public NanoStandaloneWindow,
  24. public IdleCallback
  25. {
  26. static const int kImg1y = 0;
  27. static const int kImg2y = 500/2-CatPics::cat2Height/2;
  28. static const int kImg3x = 400/3-CatPics::cat3Width/3;
  29. static const int kImg1max = 500-CatPics::cat1Width;
  30. static const int kImg2max = 500-CatPics::cat2Width;
  31. static const int kImg3max = 400-CatPics::cat3Height;
  32. int imgTop1st, imgTop2nd, imgTop3rd;
  33. int img1x, img2x, img3y;
  34. bool img1rev, img2rev, img3rev;
  35. NanoImage img1, img2, img3;
  36. public:
  37. NanoImageExample(Application& app)
  38. : NanoStandaloneWindow(app),
  39. imgTop1st(1),
  40. imgTop2nd(2),
  41. imgTop3rd(3),
  42. img1x(0),
  43. img2x(kImg2max),
  44. img3y(kImg3max),
  45. img1rev(false),
  46. img2rev(true),
  47. img3rev(true),
  48. img1(createImageFromRawMemory(CatPics::cat1Width, CatPics::cat1Height, (uchar*)CatPics::cat1Data, 0, kImageFormatBGR)),
  49. img2(createImageFromRawMemory(CatPics::cat2Width, CatPics::cat2Height, (uchar*)CatPics::cat2Data, 0, kImageFormatBGR)),
  50. img3(createImageFromRawMemory(CatPics::cat3Width, CatPics::cat3Height, (uchar*)CatPics::cat3Data, 0, kImageFormatBGR))
  51. {
  52. DISTRHO_SAFE_ASSERT(img1.isValid());
  53. DISTRHO_SAFE_ASSERT(img2.isValid());
  54. DISTRHO_SAFE_ASSERT(img3.isValid());
  55. DISTRHO_SAFE_ASSERT_UINT2(img1.getSize().getWidth() == CatPics::cat1Width,
  56. img1.getSize().getWidth(), CatPics::cat1Width);
  57. DISTRHO_SAFE_ASSERT_UINT2(img1.getSize().getHeight() == CatPics::cat1Height,
  58. img1.getSize().getHeight(), CatPics::cat1Height);
  59. DISTRHO_SAFE_ASSERT_UINT2(img2.getSize().getWidth() == CatPics::cat2Width,
  60. img2.getSize().getWidth(), CatPics::cat2Width);
  61. DISTRHO_SAFE_ASSERT_UINT2(img2.getSize().getHeight() == CatPics::cat2Height,
  62. img2.getSize().getHeight(), CatPics::cat2Height);
  63. DISTRHO_SAFE_ASSERT_UINT2(img3.getSize().getWidth() == CatPics::cat3Width,
  64. img3.getSize().getWidth(), CatPics::cat3Width);
  65. DISTRHO_SAFE_ASSERT_UINT2(img3.getSize().getHeight() == CatPics::cat3Height,
  66. img3.getSize().getHeight(), CatPics::cat3Height);
  67. setResizable(true);
  68. setSize(500, 500);
  69. setGeometryConstraints(500, 500, false, true);
  70. setTitle("NanoImage");
  71. done();
  72. addIdleCallback(this);
  73. }
  74. protected:
  75. void onNanoDisplay() override
  76. {
  77. // bottom image
  78. beginPath();
  79. fillPaint(setupImagePaint(imgTop3rd));
  80. fill();
  81. // middle image
  82. beginPath();
  83. fillPaint(setupImagePaint(imgTop2nd));
  84. fill();
  85. // top image
  86. beginPath();
  87. fillPaint(setupImagePaint(imgTop1st));
  88. fill();
  89. }
  90. void idleCallback() noexcept override
  91. {
  92. if (img1rev)
  93. {
  94. img1x -= 2;
  95. if (img1x <= -50)
  96. {
  97. img1rev = false;
  98. setNewTopImg(1);
  99. }
  100. }
  101. else
  102. {
  103. img1x += 2;
  104. if (img1x >= kImg1max+50)
  105. {
  106. img1rev = true;
  107. setNewTopImg(1);
  108. }
  109. }
  110. if (img2rev)
  111. {
  112. img2x -= 1;
  113. if (img2x <= -50)
  114. {
  115. img2rev = false;
  116. setNewTopImg(2);
  117. }
  118. }
  119. else
  120. {
  121. img2x += 4;
  122. if (img2x >= kImg2max+50)
  123. {
  124. img2rev = true;
  125. setNewTopImg(2);
  126. }
  127. }
  128. if (img3rev)
  129. {
  130. img3y -= 3;
  131. if (img3y <= -50)
  132. {
  133. img3rev = false;
  134. setNewTopImg(3);
  135. }
  136. }
  137. else
  138. {
  139. img3y += 3;
  140. if (img3y >= kImg3max+50)
  141. {
  142. img3rev = true;
  143. setNewTopImg(3);
  144. }
  145. }
  146. repaint();
  147. }
  148. private:
  149. Paint setupImagePaint(const int imgId) noexcept
  150. {
  151. switch (imgId)
  152. {
  153. case 1:
  154. rect(img1x, kImg1y, CatPics::cat1Width, CatPics::cat1Height);
  155. return imagePattern(img1x, kImg1y, CatPics::cat1Width, CatPics::cat1Height, 0, img1, 1.0f);
  156. case 2:
  157. rect(img2x, kImg2y, CatPics::cat2Width, CatPics::cat2Height);
  158. return imagePattern(img2x, kImg2y, CatPics::cat2Width, CatPics::cat2Height, 0, img2, 1.0f);
  159. case 3:
  160. rect(kImg3x, img3y, CatPics::cat3Width, CatPics::cat3Height);
  161. return imagePattern(kImg3x, img3y, CatPics::cat3Width, CatPics::cat3Height, 0, img3, 1.0f);
  162. };
  163. return Paint();
  164. }
  165. void setNewTopImg(const int imgId) noexcept
  166. {
  167. if (imgTop1st == imgId)
  168. return;
  169. if (imgTop2nd == imgId)
  170. {
  171. imgTop2nd = imgTop1st;
  172. imgTop1st = imgId;
  173. return;
  174. }
  175. imgTop3rd = imgTop2nd;
  176. imgTop2nd = imgTop1st;
  177. imgTop1st = imgId;
  178. }
  179. };
  180. // --------------------------------------------------------------------------------------------------------------------
  181. END_NAMESPACE_DGL
  182. int main()
  183. {
  184. using DGL_NAMESPACE::Application;
  185. using DGL_NAMESPACE::NanoImageExample;
  186. Application app(true);
  187. NanoImageExample win(app);
  188. win.show();
  189. app.exec();
  190. return 0;
  191. }
  192. // --------------------------------------------------------------------------------------------------------------------