desert.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <SPI.h>
  2. #include <GD.h>
  3. #include "desert.h"
  4. #include "bgstripes.h"
  5. void setup()
  6. {
  7. GD.begin();
  8. // Load sprite graphics
  9. GD.copy(PALETTE16A, palette16a, sizeof(palette16a));
  10. GD.copy(PALETTE16B, palette16b, sizeof(palette16b));
  11. GD.uncompress(RAM_SPRIMG, sprimg_cc);
  12. // Load background tile graphics
  13. GD.uncompress(RAM_CHR, dchr);
  14. GD.uncompress(RAM_PAL, dpal);
  15. // Load 'sunset' as a coprocessor program and the 'desert' color ramp
  16. GD.microcode(bgstripes_code, sizeof(bgstripes_code));
  17. GD.copy(0x3e80, desert, sizeof(desert));
  18. }
  19. void loop()
  20. {
  21. int r = 0;
  22. int ypos;
  23. int delta = 512;
  24. int speedup = 0;
  25. byte dirty = 32; // redraw whole screen first time through
  26. for (ypos = 2048 - 299; ypos > 0; ypos -= (delta >> 9)) {
  27. delta++;
  28. int yd = ypos >> 4;
  29. // Draw background tiles, each tile is 2x2 chars
  30. byte x, y;
  31. for (y = 0; y < dirty; y++) {
  32. uint16_t ty = (((y + yd) & 31) << 7);
  33. flash_uint8_t *lp = level + ((yd + y) << 5);
  34. for (x = 0; x < 25; x++) {
  35. byte t = pgm_read_byte_near(lp++);
  36. if (t == 4)
  37. t = 0;
  38. flash_uint8_t *pt = tiles + (t << 2);
  39. int da = (x << 1) + ty;
  40. GD.copy(da, pt, 2);
  41. GD.copy(da + 64, pt + 2, 2);
  42. }
  43. }
  44. dirty = 1;
  45. // Sprites: first draw the player sprite
  46. GD.__wstartspr(r << 8);
  47. int jy = (2048-44) - ypos;
  48. if (jy < 400)
  49. draw_walk(100, jy, 0, 0);
  50. // draw all the fruit, in every tile '4'
  51. for (y = 0; y < 24; y++) {
  52. flash_uint8_t *lp = level + ((yd + y) << 5);
  53. for (x = 0; x < 25; x++) {
  54. byte t = pgm_read_byte_near(lp++);
  55. if (t == 4)
  56. draw_fruit(16 * x + 8, 16 * y - (ypos & 15) + 8, ((x + y + yd) % 6), 0);
  57. }
  58. }
  59. // blank unused sprite slots
  60. while (GD.spr)
  61. GD.xhide();
  62. GD.__end();
  63. GD.waitvblank();
  64. GD.wr16(SCROLL_Y, ypos); // main background scroll
  65. GD.wr(COMM+0, 236 - yd); // move the 'sky' down
  66. GD.wr(SPR_PAGE, r & 1); // show sprites
  67. r ^= 1;
  68. }
  69. delay(2000);
  70. }