kenney.ino 6.3 KB

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