kenney.ino 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include <EEPROM.h>
  2. #include <SPI.h>
  3. #include <GD2.h>
  4. #include "kenney_assets.h"
  5. #undef PLAYER1_SIZE
  6. #define PLAYER1_SIZE (PLAYER1_HEIGHT + 8)
  7. #define HEART_SZ (HEART_HEIGHT + 14)
  8. static void rotate_player(int a)
  9. {
  10. GD.cmd_loadidentity();
  11. GD.cmd_translate(F16(PLAYER1_SIZE / 2),F16(PLAYER1_SIZE / 2));
  12. GD.cmd_rotate(a);
  13. GD.cmd_translate(F16(-PLAYER1_WIDTH / 2),F16(-PLAYER1_HEIGHT / 2));
  14. GD.cmd_setmatrix();
  15. }
  16. static void rotate_heart(int a)
  17. {
  18. GD.cmd_loadidentity();
  19. GD.cmd_translate(F16(HEART_SZ / 2),F16(HEART_SZ / 2));
  20. GD.cmd_rotate(a);
  21. GD.cmd_translate(F16(-HEART_WIDTH / 2),F16(-HEART_HEIGHT / 2));
  22. GD.cmd_setmatrix();
  23. }
  24. #define BLOBS 32
  25. class Trail {
  26. public:
  27. int idx;
  28. int x[BLOBS];
  29. int y[BLOBS];
  30. int sz[BLOBS];
  31. void add(int nx, int ny) {
  32. x[idx] = nx;
  33. y[idx] = ny;
  34. sz[idx] = 192;
  35. idx = (idx + 1) % BLOBS;
  36. }
  37. void draw(int sy) {
  38. for (int i = 0; i < BLOBS; i++) {
  39. if (sz[i]) {
  40. GD.PointSize(sz[i]);
  41. GD.Vertex2f(x[i], -(sy << 4) + y[i]);
  42. sz[i] -= 1;
  43. y[i] += 12;
  44. }
  45. }
  46. }
  47. };
  48. struct state_t {
  49. xy p[3];
  50. Trail trail[3];
  51. xy hearts[20];
  52. xy clouds[20];
  53. } state;
  54. static void polar(uint16_t th, int r)
  55. {
  56. int x, y;
  57. GD.polar(x, y, r, th);
  58. GD.Vertex2f(16 * 240 + x, 16 * 136 + y);
  59. }
  60. #define RAYS 7
  61. #define RAYSIZE (65536 / RAYS)
  62. #define RAYEDGE (4 * 16)
  63. static void ray(uint16_t th, int r0, int r1)
  64. {
  65. polar(th, r0);
  66. polar(th, r1);
  67. polar(th + RAYSIZE / 2, r1);
  68. polar(th + RAYSIZE / 2, r0);
  69. polar(th, r0);
  70. }
  71. // c1 is edge color, c2 is interior color
  72. static void burst(uint16_t th, int r, uint32_t c1, uint32_t c2)
  73. {
  74. GD.Clear(0,1,0);
  75. GD.ColorMask(0,0,0,0);
  76. GD.StencilOp(KEEP, INVERT);
  77. GD.StencilFunc(ALWAYS, 255, 255);
  78. for (int i = 0; i < RAYS; i++) {
  79. GD.Begin(EDGE_STRIP_A);
  80. ray(th + (i * RAYSIZE), r / 4, r + (r >> 4));
  81. }
  82. GD.ColorMask(1,1,1,1);
  83. GD.StencilFunc(EQUAL, 255, 255);
  84. GD.StencilOp(KEEP, KEEP);
  85. GD.Begin(POINTS);
  86. GD.ColorRGB(c1);
  87. GD.PointSize(r);
  88. GD.Vertex2ii(240, 136);
  89. GD.ColorRGB(c2);
  90. GD.PointSize(r - RAYEDGE);
  91. GD.Vertex2ii(240, 136);
  92. GD.ColorRGB(c1);
  93. GD.PointSize((r / 4) + RAYEDGE);
  94. GD.Vertex2ii(240, 136);
  95. GD.StencilFunc(ALWAYS, 255, 255);
  96. GD.LineWidth(RAYEDGE / 2);
  97. for (int i = 0; i < RAYS; i++) {
  98. GD.Begin(LINES);
  99. ray(th + (i * RAYSIZE), r / 4 + (RAYEDGE / 2), r - (RAYEDGE / 2));
  100. }
  101. }
  102. static void sunrise(int t)
  103. {
  104. static int r = 0, v = 0;
  105. // yellow 0xffcc00
  106. // reddish 0xe86a17
  107. // blue 0x1ea7e1
  108. // green 0x73cd4b
  109. GD.ColorRGB(C1B);
  110. GD.PointSize((r * 3) >> 4);
  111. GD.Begin(POINTS);
  112. GD.Vertex2f(16 * 240, 16 * 136);
  113. // GD.ColorA(min(255, t * 6));
  114. if (t == 0) {
  115. r = 0;
  116. v = 0;
  117. }
  118. burst(t * -261, r + (r >> 1), C2B, C2);
  119. burst(t * 335, r, C1B, C1);
  120. int f = ((16L * 130) - r) / 29;
  121. v = ((v + f) * 243L) >> 8;
  122. r += v;
  123. }
  124. static void draw(int t)
  125. {
  126. int y = 1648 - t;
  127. // GD.finish(); long t0 = micros();
  128. // GD.ClearColorRGB(0xd0f4f7); GD.Clear();
  129. GD.cmd_gradient(0, 0, 0xa0a4f7, //' gradient{
  130. 0, 272, 0xd0f4f7); //' }gradient
  131. if (360 <= t) {
  132. GD.RestoreContext();
  133. sunrise(t - 360);
  134. }
  135. GD.Begin(BITMAPS); //' clouds{
  136. GD.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA);
  137. GD.BitmapHandle(CLOUD_HANDLE);
  138. GD.Cell(0);
  139. for (int i = 0; i < 20; i++) {
  140. byte lum = 128 + 5 * i;
  141. GD.ColorA(lum);
  142. GD.ColorRGB(lum, lum, lum);
  143. GD.Vertex2f(state.clouds[i].x, state.clouds[i].y);
  144. state.clouds[i].y += (4 + (i >> 3));
  145. if (state.clouds[i].y > (16 * 272))
  146. state.clouds[i].y -= 16 * (272 + CLOUD_HEIGHT);
  147. } //' }clouds
  148. GD.RestoreContext();
  149. GD.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA);
  150. GD.Begin(BITMAPS); //' tiles{
  151. GD.BitmapHandle(TILES_HANDLE);
  152. const PROGMEM uint8_t *src = layer1_map + (y >> 5) * 15;
  153. byte yo = y & 31;
  154. for (byte j = 0; j < 10; j++)
  155. for (byte i = 0; i < 15; i++) {
  156. byte t = pgm_read_byte_near(src++);
  157. if (t != 0) {
  158. GD.Cell(t - 1);
  159. GD.Vertex2f(16 * 32 * i, 16 * ((32 * j) - yo));
  160. }
  161. } //' }tiles
  162. // GD.BlendFunc(SRC_ALPHA, ZERO);
  163. uint16_t a = t * 100;
  164. uint16_t a2 = a << 1;
  165. uint16_t a3 = a2 << 1;
  166. state.p[0].x = 16 * 240 + GD.rsin(16 * 120, a) + GD.rsin(16 * 120, a2);
  167. state.p[0].y = 16 * 136 + GD.rsin(16 * 70, a3);
  168. state.p[1].x = 16 * 240 + GD.rsin(16 * 240, a);
  169. state.p[1].y = 16 * 100 + GD.rsin(16 * 36, a2);
  170. state.p[2].x = 16 * 240 + GD.rsin(16 * 240, a2);
  171. state.p[2].y = 16 * 135 + GD.rsin(16 * 10, a) + GD.rsin(16 * 18, a3);
  172. for (int i = 0; i < 3; i++) {
  173. if ((t & 3) == 0) {
  174. state.trail[i].add(state.p[i].x, (y << 4) + state.p[i].y);
  175. }
  176. GD.BlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
  177. GD.Begin(POINTS);
  178. GD.ColorA(0x90);
  179. uint32_t colors[3] = {0x8bcfba, 0x8db5e7, 0xf19cb7};
  180. GD.ColorRGB(colors[i]);
  181. state.trail[i].draw(y);
  182. }
  183. GD.ColorRGB(0xffffff);
  184. GD.ColorA(0xff);
  185. GD.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA);
  186. GD.BitmapHandle(PLAYER1_HANDLE); //' player{
  187. GD.Begin(BITMAPS);
  188. for (int i = 0; i < 3; i++) {
  189. rotate_player(a + i * 0x7000);
  190. GD.Cell(i);
  191. GD.Vertex2f(state.p[i].x - (16 * PLAYER1_SIZE / 2),
  192. state.p[i].y - (16 * PLAYER1_SIZE / 2));
  193. } //' }player
  194. if (t > 480) {
  195. GD.BlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
  196. GD.ColorA(min((t - 480) * 4, 255));
  197. GD.Begin(BITMAPS);
  198. GD.BitmapHandle(HEART_HANDLE);
  199. GD.Cell(0);
  200. for (int i = 0; i < 20; i++) {
  201. if ((i & 3) == 0)
  202. rotate_heart(a + (i << 12));
  203. GD.Vertex2f(state.hearts[i].x - (16 * HEART_SZ / 2), state.hearts[i].y);
  204. state.hearts[i].y += 30 + (i << 2);
  205. if (state.hearts[i].y > (16 * 272))
  206. state.hearts[i].y -= 16 * (272 + HEART_SZ);
  207. }
  208. }
  209. // GD.RestoreContext(); GD.cmd_number(0, 0, 26, 6, micros() - t0);
  210. GD.swap();
  211. }
  212. void setup()
  213. {
  214. GD.begin(~GD_STORAGE);
  215. Serial.begin(1000000);
  216. // GD.wr32(REG_PWM_HZ, 18000); GD.wr(REG_PWM_DUTY, 64);
  217. GD.Clear();
  218. LOAD_ASSETS();
  219. // Handle Graphic
  220. // 0 Tiles
  221. byte hy[] = {36, 12, 144, 204, 216, 120, 48, 168, 192, 84, 72, 60, 24, 180, 0, 108, 228, 132, 156, 96};
  222. for (int i = 0; i < 20; i++) {
  223. state.hearts[i].x = random(16 * 480);
  224. state.hearts[i].y = 16 * hy[i];
  225. state.clouds[i].x = 16 * (random(480) - (CLOUD_WIDTH / 2));
  226. state.clouds[i].y = 16 * hy[i];
  227. }
  228. }
  229. void loop()
  230. {
  231. for (int t = 0; t < 720; t++) {
  232. draw(t);
  233. }
  234. }