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.

231 lines
6.8KB

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