jkcollision.pde 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <SPI.h>
  2. #include <GD.h>
  3. void readn(byte *dst, unsigned int addr, int c)
  4. {
  5. GD.__start(addr);
  6. while (c--)
  7. *dst++ = SPI.transfer(0);
  8. GD.__end();
  9. }
  10. static byte coll[256];
  11. static void load_coll()
  12. {
  13. while (GD.rd(VBLANK) == 0) // Wait until vblank
  14. ;
  15. while (GD.rd(VBLANK) == 1) // Wait until display
  16. ;
  17. while (GD.rd(VBLANK) == 0) // Wait until vblank
  18. ;
  19. readn(coll, COLLISION, sizeof(coll));
  20. }
  21. void setup()
  22. {
  23. int i;
  24. GD.begin();
  25. GD.wr(JK_MODE, 1);
  26. GD.wr16(RAM_PAL, RGB(255,255,255));
  27. // Use the 4 palettes:
  28. // 0 pink, for J sprites
  29. // 1 green, for K sprites
  30. // 2 dark pink, J collisions
  31. // 3 dark green, K collisions
  32. for (i = 0; i < 256; i++) {
  33. GD.wr16(RAM_SPRPAL + (0 * 512) + (i << 1), RGB(255, 0, 255));
  34. GD.wr16(RAM_SPRPAL + (1 * 512) + (i << 1), RGB(0, 255, 0));
  35. GD.wr16(RAM_SPRPAL + (2 * 512) + (i << 1), RGB(100, 0, 100));
  36. GD.wr16(RAM_SPRPAL + (3 * 512) + (i << 1), RGB(0, 100, 0));
  37. }
  38. }
  39. byte spr;
  40. static void polar(float th, int r, byte jk)
  41. {
  42. // add 2 to the palette if this sprite is colliding
  43. byte colliding = coll[spr] != 0xff;
  44. GD.sprite(spr, 200 + int(r * sin(th)), 142 + int(r * cos(th)), 0, jk + (colliding ? 2 : 0), 0, jk);
  45. spr++;
  46. }
  47. void loop()
  48. {
  49. byte i;
  50. float th;
  51. spr = 0;
  52. // draw the J sprites (pink)
  53. for (i = 0; i < 5; i++) {
  54. th = (millis() / 3000.) + 2 * PI * i / 5;
  55. polar(th, 134, 0);
  56. }
  57. // draw the K sprites (green)
  58. randomSeed(4);
  59. for (i = 0; i < 17; i++) {
  60. th = (millis() / float(random(1000,3000))) + 2 * PI * i / 17;
  61. polar(th, 134, 1);
  62. }
  63. load_coll();
  64. }