ball.ino 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <SPI.h>
  2. #include <GD.h>
  3. #include "ball.h"
  4. void setup()
  5. {
  6. Serial.begin(1000000); // JCB
  7. GD.begin();
  8. // Background image
  9. GD.copy(RAM_PIC, bg_pic, sizeof(bg_pic));
  10. GD.copy(RAM_CHR, bg_chr, sizeof(bg_chr));
  11. GD.copy(RAM_PAL, bg_pal, sizeof(bg_pal));
  12. // Sprite graphics
  13. GD.uncompress(RAM_SPRIMG, ball);
  14. // Palettes 0 and 1 are for the ball itself,
  15. // and palette 2 is the shadow. Set it to
  16. // all gray.
  17. int i;
  18. for (i = 0; i < 256; i++)
  19. GD.wr16(RAM_SPRPAL + (2 * (512 + i)), RGB(64, 64, 64));
  20. // Set color 255 to transparent in all three palettes
  21. GD.wr16(RAM_SPRPAL + 2 * 0xff, TRANSPARENT);
  22. GD.wr16(RAM_SPRPAL + 2 * 0x1ff, TRANSPARENT);
  23. GD.wr16(RAM_SPRPAL + 2 * 0x2ff, TRANSPARENT);
  24. }
  25. #define RADIUS (112 / 2) // radius of the ball, in pixels
  26. #define YBASE (300 - RADIUS)
  27. void loop()
  28. {
  29. int x = 200, y = RADIUS; // ball position
  30. int xv = 2, yv = 0; // ball velocity
  31. int r; // frame counter
  32. for (r = 0; ; r++) {
  33. GD.__wstartspr((r & 1) ? 256 : 0); // write sprites to other frame
  34. draw_ball(x + 15, y + 15, 2); // draw shadow using palette 2
  35. draw_ball(x, y, r & 1); // draw ball using palette 0 or 1
  36. GD.__end();
  37. // paint the new palette
  38. uint16_t palette = RAM_SPRPAL + 512 * (r & 1);
  39. byte li;
  40. for (li = 0; li < 7; li++) {
  41. byte liv = 0x90 + 0x10 * li; // brightness goes 0x90, 0xa0, etc
  42. uint16_t red = RGB(liv, 0, 0);
  43. uint16_t white = RGB(liv, liv, liv);
  44. byte i;
  45. for (i = 0; i < 32; i++) { // palette cycling using 'r'
  46. GD.wr16(palette, ((i + r) & 16) ? red : white);
  47. palette += 2;
  48. }
  49. }
  50. // bounce the ball around
  51. x += xv;
  52. if ((x < RADIUS) || (x > (400 - RADIUS)))
  53. xv = -xv;
  54. y += yv;
  55. if ((yv > 0) && (y > YBASE)) {
  56. y = YBASE - (y - YBASE); // reflect in YBASE
  57. yv = -yv; // reverse Y velocity
  58. }
  59. if (0 == (r & 3))
  60. yv++; // gravity
  61. // swap frames
  62. GD.waitvblank();
  63. GD.wr(SPR_PAGE, (r & 1));
  64. // GD.screenshot(r); // JCB
  65. }
  66. }