Audio plugin host https://kx.studio/carla
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.

NekoWidget.hpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "dgl/Image.hpp"
  18. #include "dgl/Widget.hpp"
  19. #include <cstdlib> // rand
  20. #include "DistrhoArtworkNekobi.hpp"
  21. USE_NAMESPACE_DGL;
  22. class NekoWidget
  23. {
  24. public:
  25. NekoWidget()
  26. : fPos(0),
  27. fTimer(0),
  28. fTimerSpeed(20),
  29. fCurAction(kActionNone),
  30. fCurImage(&fImages.sit)
  31. {
  32. // load images
  33. {
  34. using namespace DistrhoArtworkNekobi;
  35. #define JOIN(a, b) a ## b
  36. #define LOAD_IMAGE(NAME) fImages.NAME.loadFromMemory(JOIN(NAME, Data), JOIN(NAME, Width), JOIN(NAME, Height));
  37. LOAD_IMAGE(sit)
  38. LOAD_IMAGE(tail)
  39. LOAD_IMAGE(claw1)
  40. LOAD_IMAGE(claw2)
  41. LOAD_IMAGE(scratch1)
  42. LOAD_IMAGE(scratch2)
  43. LOAD_IMAGE(run1)
  44. LOAD_IMAGE(run2)
  45. LOAD_IMAGE(run3)
  46. LOAD_IMAGE(run4)
  47. #undef JOIN
  48. #undef LOAD_IMAGE
  49. }
  50. }
  51. void draw()
  52. {
  53. int x = fPos+108;
  54. int y = -2;
  55. if (fCurImage == &fImages.claw1 || fCurImage == &fImages.claw2)
  56. {
  57. x += 2;
  58. y += 12;
  59. }
  60. fCurImage->draw(x, y);
  61. }
  62. // returns true if needs repaint
  63. bool idle()
  64. {
  65. if (++fTimer % fTimerSpeed != 0) // target is 20ms
  66. return false;
  67. if (fTimer == fTimerSpeed*9)
  68. {
  69. if (fCurAction == kActionNone)
  70. fCurAction = static_cast<Action>(rand() % kActionCount);
  71. else
  72. fCurAction = kActionNone;
  73. fTimer = 0;
  74. }
  75. switch (fCurAction)
  76. {
  77. case kActionNone:
  78. if (fCurImage == &fImages.sit)
  79. fCurImage = &fImages.tail;
  80. else
  81. fCurImage = &fImages.sit;
  82. break;
  83. case kActionClaw:
  84. if (fCurImage == &fImages.claw1)
  85. fCurImage = &fImages.claw2;
  86. else
  87. fCurImage = &fImages.claw1;
  88. break;
  89. case kActionScratch:
  90. if (fCurImage == &fImages.scratch1)
  91. fCurImage = &fImages.scratch2;
  92. else
  93. fCurImage = &fImages.scratch1;
  94. break;
  95. case kActionRunRight:
  96. if (fTimer == 0 && fPos > 20*9)
  97. {
  98. // run the other way
  99. --fTimer;
  100. fCurAction = kActionRunLeft;
  101. idle();
  102. break;
  103. }
  104. fPos += 20;
  105. if (fCurImage == &fImages.run1)
  106. fCurImage = &fImages.run2;
  107. else
  108. fCurImage = &fImages.run1;
  109. break;
  110. case kActionRunLeft:
  111. if (fTimer == 0 && fPos < 20*9)
  112. {
  113. // run the other way
  114. --fTimer;
  115. fCurAction = kActionRunRight;
  116. idle();
  117. break;
  118. }
  119. fPos -= 20;
  120. if (fCurImage == &fImages.run3)
  121. fCurImage = &fImages.run4;
  122. else
  123. fCurImage = &fImages.run3;
  124. break;
  125. case kActionCount:
  126. break;
  127. }
  128. return true;
  129. }
  130. void setTimerSpeed(int speed)
  131. {
  132. fTimer = 0;
  133. fTimerSpeed = speed;
  134. }
  135. private:
  136. enum Action {
  137. kActionNone, // bounce tail
  138. kActionClaw,
  139. kActionScratch,
  140. kActionRunRight,
  141. kActionRunLeft,
  142. kActionCount
  143. };
  144. struct Images {
  145. Image sit;
  146. Image tail;
  147. Image claw1;
  148. Image claw2;
  149. Image scratch1;
  150. Image scratch2;
  151. Image run1;
  152. Image run2;
  153. Image run3;
  154. Image run4;
  155. } fImages;
  156. int fPos;
  157. int fTimer;
  158. int fTimerSpeed;
  159. Action fCurAction;
  160. Image* fCurImage;
  161. };