asteroids.pde 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. #include <SPI.h>
  2. #include <GD.h>
  3. // ----------------------------------------------------------------------
  4. // qrand: quick random numbers
  5. // ----------------------------------------------------------------------
  6. static uint16_t lfsr = 1;
  7. static void qrandSeed(int seed)
  8. {
  9. if (seed) {
  10. lfsr = seed;
  11. } else {
  12. lfsr = 0x947;
  13. }
  14. }
  15. static byte qrand1() // a random bit
  16. {
  17. lfsr = (lfsr >> 1) ^ (-(lfsr & 1) & 0xb400);
  18. return lfsr & 1;
  19. }
  20. static byte qrand(byte n) // n random bits
  21. {
  22. byte r = 0;
  23. while (n--)
  24. r = (r << 1) | qrand1();
  25. return r;
  26. }
  27. // ----------------------------------------------------------------------
  28. // controller: buttons on Arduino pins 3,4,5,6
  29. // ----------------------------------------------------------------------
  30. static void controller_init()
  31. {
  32. // Configure input pins with internal pullups
  33. byte i;
  34. for (i = 3; i < 7; i++) {
  35. pinMode(i, INPUT);
  36. digitalWrite(i, HIGH);
  37. }
  38. }
  39. #define CONTROL_LEFT 1
  40. #define CONTROL_RIGHT 2
  41. #define CONTROL_UP 4
  42. #define CONTROL_DOWN 8
  43. static byte controller_sense(uint16_t clock)
  44. {
  45. byte r = 0;
  46. if (!digitalRead(5))
  47. r |= CONTROL_DOWN;
  48. if (!digitalRead(4))
  49. r |= CONTROL_UP;
  50. if (!digitalRead(6))
  51. r |= CONTROL_LEFT;
  52. if (!digitalRead(3))
  53. r |= CONTROL_RIGHT;
  54. return r;
  55. }
  56. // Swap color's red and blue channels
  57. uint16_t swapRB(uint16_t color)
  58. {
  59. byte r = 31 & (color >> 10);
  60. byte g = 31 & (color >> 5);
  61. byte b = 31 & color;
  62. return (color & 0x8000) | (b << 10) | (g << 5) | r;
  63. }
  64. // Swap color's red and green channels
  65. uint16_t swapRG(uint16_t color)
  66. {
  67. byte r = 31 & (color >> 10);
  68. byte g = 31 & (color >> 5);
  69. byte b = 31 & color;
  70. return (color & 0x8000) | (g << 10) | (r << 5) | b;
  71. }
  72. #include "asteroidgraphics.h"
  73. #include "splitscreen.h"
  74. static void update_score();
  75. // [int(127 * math.sin(math.pi * 2 * i / 16)) for i in range(16)]
  76. static PROGMEM prog_uchar charsin[16] = {0, 48, 89, 117, 127, 117, 89, 48, 0, -48, -89, -117, -127, -117, -89, -48};
  77. #define qsin(a) (signed char)pgm_read_byte_near(charsin + ((a) & 15))
  78. #define qcos(a) qsin((a) + 4)
  79. static char spr2obj[256]; // Maps sprites to owning objects
  80. /*
  81. The general idea is that an object table ``objects`` has an entry for
  82. each drawn thing on screen (e.g. player, missile, rock, explosion).
  83. Each class of object has a ``handler`` function that does the necessary
  84. housekeeping and draws the actual sprites.
  85. As part of the behavior, some classes need to know if they have collided
  86. with anything. In particular the rocks need to know if they have collided
  87. with the player or a missile. The `collide` member points to the
  88. colliding sprite.
  89. */
  90. struct object {
  91. int x, y;
  92. byte handler, state;
  93. byte collide;
  94. } objects[128];
  95. #define COORD(c) ((c) << 4)
  96. static char explosions = -1;
  97. static char enemies = -1;
  98. static char missiles = -1;
  99. static void push(char *stk, byte i)
  100. {
  101. objects[i].state = *stk;
  102. *stk = i;
  103. }
  104. static char pop(char *stk)
  105. {
  106. char r = *stk;
  107. if (0 <= r) {
  108. *stk = objects[r].state;
  109. }
  110. return r;
  111. }
  112. byte rr[4] = { 0,3,6,5 };
  113. static struct {
  114. byte boing, boom, pop;
  115. byte thrust;
  116. byte bass;
  117. } sounds;
  118. static int player_vx, player_vy; // Player velocity
  119. static int player_invincible, player_dying;
  120. static byte lives;
  121. static long score;
  122. static byte level;
  123. // Move object po by velocity (vx, vy), optionally keeping in
  124. // player's frame.
  125. // Returns true if the object wrapped screen edge
  126. static bool move(struct object *po, char vx, char vy, byte playerframe = 1)
  127. {
  128. bool r = 0;
  129. if (playerframe) {
  130. po->x += (vx - player_vx);
  131. po->y += (vy - player_vy);
  132. } else {
  133. po->x += vx;
  134. po->y += vy;
  135. }
  136. if (po->x > COORD(416))
  137. r = 1, po->x -= COORD(432);
  138. else if (po->x < COORD(-16))
  139. r = 1, po->x += COORD(432);
  140. if (po->y > COORD(316))
  141. r = 1, po->y -= COORD(332);
  142. else if (po->y < COORD(-16))
  143. r = 1, po->y += COORD(332);
  144. return r;
  145. }
  146. #define HANDLE_NULL 0
  147. #define HANDLE_ROCK0 1
  148. #define HANDLE_ROCK1 2
  149. #define HANDLE_BANG0 3
  150. #define HANDLE_BANG1 4
  151. #define HANDLE_PLAYER 5
  152. #define HANDLE_MISSILE 6
  153. // Expire object i, and return it to the free stk
  154. static void expire(char *stk, byte i)
  155. {
  156. objects[i].handler = HANDLE_NULL;
  157. push(stk, i);
  158. }
  159. static void handle_null(byte i, byte state, uint16_t clock)
  160. {
  161. }
  162. static void handle_player(byte i, byte state, uint16_t clock)
  163. {
  164. struct object *po = &objects[i];
  165. byte angle = (po->state & 15);
  166. byte rot1 = (angle & 3);
  167. byte rot2 = rr[3 & (angle >> 2)];
  168. if (!player_dying && (player_invincible & 1) == 0)
  169. draw_player(200, 150, rot1, rot2);
  170. static byte prev_control;
  171. byte control = controller_sense(clock);
  172. char thrust_x, thrust_y;
  173. if (!player_dying && control & CONTROL_DOWN) { // thrust
  174. byte flame_angle = angle ^ 8;
  175. byte d;
  176. for (d = 9; d > 5; d--) {
  177. int flamex = 201 - (((d + (clock&3)) * qsin(flame_angle)) >> 5);
  178. int flamey = 150 - (((d + (clock&3)) * qcos(flame_angle)) >> 5);
  179. if ((player_invincible & 1) == 0)
  180. draw_sparkr(flamex, flamey, rot1, rot2, 1); // collision class K
  181. }
  182. thrust_x = -qsin(angle);
  183. thrust_y = -qcos(angle);
  184. sounds.thrust = 1;
  185. } else {
  186. thrust_x = thrust_y = 0;
  187. sounds.thrust = 0;
  188. }
  189. player_vx = ((31 * player_vx) + thrust_x) / 32;
  190. player_vy = ((31 * player_vy) + thrust_y) / 32;
  191. po->x += player_vx;
  192. po->y += player_vy;
  193. if (clock & 1) {
  194. char rotate = (512 - analogRead(0)) / 400;
  195. if (control & CONTROL_LEFT)
  196. rotate++;
  197. if (control & CONTROL_RIGHT)
  198. rotate--;
  199. po->state = ((angle + rotate) & 15);
  200. }
  201. if (!player_dying &&
  202. !(prev_control & CONTROL_UP) &&
  203. (control & CONTROL_UP)) { // shoot!
  204. char e = pop(&missiles);
  205. if (0 <= e) {
  206. objects[e].x = COORD(200);
  207. objects[e].y = COORD(150);
  208. objects[e].state = po->state;
  209. objects[e].handler = HANDLE_MISSILE;
  210. }
  211. sounds.boing = 1;
  212. }
  213. prev_control = control;
  214. if (player_invincible)
  215. --player_invincible;
  216. if (player_dying) {
  217. if (--player_dying == 0) {
  218. --lives;
  219. update_score();
  220. if (lives != 0) {
  221. player_invincible = 48;
  222. }
  223. }
  224. }
  225. }
  226. static void handle_missile(byte i, byte state, uint16_t clock)
  227. {
  228. struct object *po = &objects[i];
  229. byte angle = (po->state & 15);
  230. byte rot1 = (angle & 3);
  231. byte rot2 = rr[3 & (angle >> 2)];
  232. draw_sparkr(po->x >> 4, po->y >> 4, rot1, rot2);
  233. char vx = -qsin(po->state), vy = -qcos(po->state);
  234. if (move(po, vx, vy, 0)) {
  235. expire(&missiles, i);
  236. }
  237. }
  238. static void explodehere(struct object *po, byte handler, uint16_t clock)
  239. {
  240. char e = pop(&explosions);
  241. if (0 <= e) {
  242. objects[e].x = po->x;
  243. objects[e].y = po->y;
  244. objects[e].handler = handler;
  245. objects[e].state = clock;
  246. }
  247. }
  248. static void killplayer(uint16_t clock)
  249. {
  250. if (!player_invincible && !player_dying) {
  251. char e = pop(&explosions);
  252. if (0 <= e) {
  253. objects[e].x = COORD(200);
  254. objects[e].y = COORD(150);
  255. objects[e].handler = HANDLE_BANG1;
  256. objects[e].state = clock;
  257. }
  258. player_dying = 2 * 36;
  259. sounds.boom = 1;
  260. sounds.pop = 1;
  261. }
  262. }
  263. static byte commonrock(uint16_t clock, byte i, byte speed, void df(int x, int y, byte anim, byte rot, byte jk))
  264. {
  265. struct object *po = &objects[i];
  266. byte move_angle = po->state >> 4;
  267. char vx = (speed * -qsin(move_angle)) >> 6, vy = (speed * -qcos(move_angle)) >> 6;
  268. move(po, vx, vy);
  269. byte angle = (clock * speed) >> 4;
  270. if (po->state & 1)
  271. angle = ~angle;
  272. byte rot1 = (angle & 3);
  273. byte rot2 = rr[3 & (angle >> 2)];
  274. df(po->x >> 4, po->y >> 4, rot1, rot2, 1);
  275. if (po->collide != 0xff) {
  276. struct object *other = &objects[po->collide];
  277. switch (other->handler) {
  278. case HANDLE_PLAYER:
  279. killplayer(clock);
  280. break;
  281. case HANDLE_MISSILE:
  282. expire(&missiles, po->collide); // missile is dead
  283. expire(&enemies, i);
  284. return 1;
  285. }
  286. }
  287. return 0;
  288. }
  289. static void handle_rock0(byte i, byte state, uint16_t clock)
  290. {
  291. struct object *po = &objects[i];
  292. byte speed = 12 + (po->state & 7);
  293. if (commonrock(clock, i, speed, draw_rock0r)) {
  294. explodehere(po, HANDLE_BANG0, clock);
  295. score += 10;
  296. sounds.pop = 1;
  297. }
  298. }
  299. static void handle_rock1(byte i, byte state, uint16_t clock)
  300. {
  301. struct object *po = &objects[i];
  302. byte speed = 6 + (po->state & 3);
  303. if (commonrock(clock, i, speed, draw_rock1r)) {
  304. int j;
  305. for (j = 0; j < 4; j++) {
  306. char e = pop(&enemies);
  307. if (0 < e) {
  308. objects[e].x = po->x;
  309. objects[e].y = po->y;
  310. objects[e].handler = HANDLE_ROCK0;
  311. objects[e].state = (j << 6) + qrand(6); // spread fragments across 4 quadrants
  312. }
  313. }
  314. explodehere(po, HANDLE_BANG1, clock);
  315. score += 30;
  316. sounds.boom = 1;
  317. }
  318. }
  319. static void handle_bang0(byte i, byte state, uint16_t clock)
  320. {
  321. struct object *po = &objects[i];
  322. move(po, 0, 0);
  323. byte anim = ((0xff & clock) - state) >> 1;
  324. if (anim < EXPLODE16_FRAMES)
  325. draw_explode16(po->x >> 4, po->y >> 4, anim, 0);
  326. else
  327. expire(&explosions, i);
  328. }
  329. static void handle_bang1(byte i, byte state, uint16_t clock)
  330. {
  331. struct object *po = &objects[i];
  332. move(po, 0, 0);
  333. byte anim = ((0xff & clock) - state) >> 1;
  334. byte rot = 7 & i;
  335. if (anim < EXPLODE32_FRAMES)
  336. draw_explode32(po->x >> 4, po->y >> 4, anim, rot);
  337. else
  338. expire(&explosions, i);
  339. }
  340. typedef void (*handler)(byte, byte, uint16_t);
  341. static handler handlers[] = {
  342. handle_null,
  343. handle_rock0,
  344. handle_rock1,
  345. handle_bang0,
  346. handle_bang1,
  347. handle_player,
  348. handle_missile
  349. };
  350. class GDflashbits {
  351. public:
  352. void begin(prog_uchar *s) {
  353. src = s;
  354. mask = 0x01;
  355. }
  356. byte get1(void) {
  357. byte r = (pgm_read_byte_near(src) & mask) != 0;
  358. mask <<= 1;
  359. if (!mask) {
  360. mask = 1;
  361. src++;
  362. }
  363. return r;
  364. }
  365. unsigned short getn(byte n) {
  366. unsigned short r = 0;
  367. while (n--) {
  368. r <<= 1;
  369. r |= get1();
  370. }
  371. return r;
  372. }
  373. private:
  374. prog_uchar *src;
  375. byte mask;
  376. };
  377. static GDflashbits GDFB;
  378. static void GD_uncompress(unsigned int addr, PROGMEM prog_uchar *src)
  379. {
  380. GDFB.begin(src);
  381. byte b_off = GDFB.getn(4);
  382. byte b_len = GDFB.getn(4);
  383. byte minlen = GDFB.getn(2);
  384. unsigned short items = GDFB.getn(16);
  385. while (items--) {
  386. if (GDFB.get1() == 0) {
  387. GD.wr(addr++, GDFB.getn(8));
  388. } else {
  389. int offset = -GDFB.getn(b_off) - 1;
  390. int l = GDFB.getn(b_len) + minlen;
  391. while (l--) {
  392. GD.wr(addr, GD.rd(addr + offset));
  393. addr++;
  394. }
  395. }
  396. }
  397. }
  398. // uncompress one line of the title banner into buffer dst
  399. // title banner lines are run-length encoded
  400. static void titlepaint(char *dst, byte src, byte mask)
  401. {
  402. if (src != 0xff) {
  403. prog_uchar *psrc = title_runs + 2 * src;
  404. byte a, b;
  405. do {
  406. a = pgm_read_byte_near(psrc++);
  407. b = pgm_read_byte_near(psrc++);
  408. while (a < (b & 0x7f))
  409. dst[a++] |= mask;
  410. } while (!(b & 0x80));
  411. }
  412. }
  413. // draw a title banner column src (0-511) to screen column dst (0-63).
  414. static void column(byte dst, byte src)
  415. {
  416. static char scratch[76];
  417. memset(scratch, 0, sizeof(scratch));
  418. byte line = pgm_read_byte_near(title + 2 * src);
  419. titlepaint(scratch, line, 1);
  420. line = pgm_read_byte_near(title + 2 * src + 1);
  421. titlepaint(scratch, line, 2);
  422. byte j;
  423. for (j = 0; j < 38; j++) {
  424. GD.wr(dst + (j << 6),
  425. (((dst + j) & 15) << 4) +
  426. scratch[2 * j] +
  427. (scratch[2 * j + 1] << 2));
  428. }
  429. }
  430. static void setup_sprites()
  431. {
  432. GD.copy(PALETTE16A, palette16a, sizeof(palette16a));
  433. GD.copy(PALETTE4A, palette4a, sizeof(palette4a));
  434. GD.copy(PALETTE16B, palette16b, sizeof(palette16b));
  435. // Use the first two 256-color palettes as pseudo-16 color palettes
  436. int i;
  437. for (i = 0; i < 256; i++) {
  438. // palette 0 decodes low nibble, hence (i & 15)
  439. uint16_t rgb = pgm_read_word_near(palette256a + ((i & 15) << 1));
  440. GD.wr16(RAM_SPRPAL + (i << 1), rgb);
  441. // palette 1 decodes nigh nibble, hence (i >> 4)
  442. rgb = pgm_read_word_near(palette256a + ((i >> 4) << 1));
  443. GD.wr16(RAM_SPRPAL + 512 + (i << 1), rgb);
  444. }
  445. GD_uncompress(RAM_SPRIMG, asteroid_images_compressed);
  446. GD.wr(JK_MODE, 1);
  447. }
  448. // Run the object handlers, keeping track of sprite ownership in spr2obj
  449. static void runobjects(uint16_t r)
  450. {
  451. int i;
  452. GD.__wstartspr((r & 1) ? 256 : 0); // write sprites to other frame
  453. for (i = 0; i < 128; i++) {
  454. struct object *po = &objects[i];
  455. handler h = (handler)handlers[po->handler];
  456. byte loSpr = GD.spr;
  457. (*h)(i, po->state, r);
  458. while (loSpr < GD.spr) {
  459. spr2obj[loSpr++] = i;
  460. }
  461. }
  462. // Hide all the remaining sprites
  463. do
  464. GD.xhide();
  465. while (GD.spr);
  466. GD.__end();
  467. }
  468. // ----------------------------------------------------------------------
  469. // map
  470. // ----------------------------------------------------------------------
  471. static byte loaded[8];
  472. static byte scrap;
  473. // copy a (w,h) rectangle from the source image (x,y) into picture RAM
  474. static void rect(unsigned int dst, byte x, byte y, byte w, byte h)
  475. {
  476. prog_uchar *src = bg_pic + (16 * y) + x;
  477. while (h--) {
  478. GD.copy(dst, src, w);
  479. dst += 64;
  480. src += 16;
  481. }
  482. }
  483. static void map_draw(byte strip)
  484. {
  485. strip &= 63; // Universe is finite but unbounded: 64 strips
  486. byte s8 = (strip & 7); // Destination slot for this strip (0-7)
  487. if (loaded[s8] != strip) {
  488. qrandSeed(level ^ (strip * 77)); // strip number is the hash...
  489. // Random star pattern is made from characters 1-15
  490. GD.__wstart(s8 * (8 * 64));
  491. int i;
  492. for (i = 0; i < (8 * 64); i++) {
  493. byte r;
  494. if (qrand(3) == 0)
  495. r = qrand(4);
  496. else
  497. r = 0;
  498. SPI.transfer(r);
  499. }
  500. GD.__end();
  501. // Occasional planet, copied from the background char map
  502. if (qrand(2) == 0) {
  503. uint16_t dst = (qrand(3) * 8) + (s8 * (8 * 64));
  504. switch (qrand(2)) {
  505. case 0:
  506. rect(dst, 0, 1, 6, 6);
  507. break;
  508. case 1:
  509. rect(dst, 7, 1, 6, 6);
  510. break;
  511. case 2:
  512. rect(dst, 0, 7, 8, 4);
  513. break;
  514. case 3:
  515. rect(dst, 8, 7, 5, 5);
  516. break;
  517. }
  518. }
  519. loaded[s8] = strip;
  520. }
  521. }
  522. static void map_coldstart()
  523. {
  524. memset(loaded, 0xff, sizeof(loaded));
  525. scrap = 0xff;
  526. byte i;
  527. for (i = 0; i < 8; i++)
  528. map_draw(i);
  529. }
  530. static int atxy(int x, int y)
  531. {
  532. return (y << 6) + x;
  533. }
  534. static void update_score()
  535. {
  536. prog_uchar* digitcodes = bg_pic + (16 * 30);
  537. unsigned long s = score;
  538. uint16_t a = atxy(49, scrap << 3);
  539. byte i;
  540. for (i = 0; i < 6; i++) {
  541. GD.wr(a--, pgm_read_byte_near(digitcodes + (s % 10)));
  542. s /= 10;
  543. }
  544. GD.wr(atxy(0, scrap << 3), pgm_read_byte_near(digitcodes + lives));
  545. }
  546. static void map_refresh(byte strip)
  547. {
  548. byte i;
  549. byte newscrap = 7 & (strip + 7);
  550. if (scrap != newscrap) {
  551. scrap = newscrap;
  552. uint16_t scrapline = scrap << 6;
  553. GD.wr16(COMM+2, 0x8000 | scrapline); // show scrapline at line 0
  554. GD.wr16(COMM+14, 0x8000 | (0x1ff & ((scrapline + 8) - 291))); // show scrapline+8 at line 291
  555. GD.fill(atxy(0, scrap << 3), 0, 50);
  556. update_score();
  557. GD.fill(atxy(0, 1 + (scrap << 3)), 0, 64);
  558. rect(atxy(0, 1 + (scrap << 3)), 0, 31, 16, 1);
  559. rect(atxy(32, 1 + (scrap << 3)), 0, 31, 16, 1);
  560. loaded[scrap] = 0xff;
  561. }
  562. delay(1); // wait for raster to pass the top line before overwriting it
  563. for (i = 0; i < 6; i++)
  564. map_draw(strip + i);
  565. }
  566. static void start_level()
  567. {
  568. int i;
  569. for (i = 0; i < 128; i++)
  570. objects[i].handler = 0;
  571. player_vx = 0;
  572. player_vy = 0;
  573. player_invincible = 0;
  574. player_dying = 0;
  575. objects[0].x = 0;
  576. objects[0].y = 0;
  577. objects[0].state = 0;
  578. objects[0].handler = HANDLE_PLAYER;
  579. // Set up the pools of objects for missiles, enemies, explosions
  580. missiles = 0;
  581. enemies = 0;
  582. explosions = 0;
  583. for (i = 1; i < 16; i++)
  584. push(&missiles, i);
  585. for (i = 16; i < 80; i++)
  586. push(&enemies, i);
  587. for (i = 80; i < 128; i++)
  588. push(&explosions, i);
  589. // Place asteroids in a ring around the edges of the screen
  590. for (i = 0; i < min(32, 3 + level); i++) {
  591. char e = pop(&enemies);
  592. if (random(2) == 0) {
  593. objects[e].x = random(2) ? COORD(32) : COORD(400-32);
  594. objects[e].y = random(COORD(300));
  595. } else {
  596. objects[e].x = random(COORD(400));
  597. objects[e].y = random(2) ? COORD(32) : COORD(300-32);
  598. }
  599. objects[e].handler = HANDLE_ROCK1;
  600. objects[e].state = qrand(8);
  601. }
  602. GD.copy(PALETTE16B, palette16b, sizeof(palette16b));
  603. for (i = 0; i < 16; i++) {
  604. uint16_t a = PALETTE16B + 2 * i;
  605. uint16_t c = GD.rd16(a);
  606. if (level & 1)
  607. c = swapRB(c);
  608. if (level & 2)
  609. c = swapRG(c);
  610. if (level & 4)
  611. c = swapRB(c);
  612. GD.wr16(a, c);
  613. }
  614. map_coldstart();
  615. }
  616. void setup()
  617. {
  618. GD.begin();
  619. controller_init();
  620. }
  621. static void title_banner()
  622. {
  623. GD.fill(VOICES, 0, 64 * 4);
  624. GD.wr(J1_RESET, 1);
  625. GD.wr(SPR_DISABLE, 1);
  626. GD.wr16(SCROLL_X, 0);
  627. GD.wr16(SCROLL_Y, 0);
  628. GD.fill(RAM_PIC, 0, 4096);
  629. setup_sprites();
  630. uint16_t i;
  631. uint16_t j;
  632. GD.__wstart(RAM_CHR);
  633. for (i = 0; i < 256; i++) {
  634. // bits control lit segments like this:
  635. // 0 1
  636. // 2 3
  637. byte a = (i & 1) ? 0x3f : 0;
  638. byte b = (i & 2) ? 0x3f : 0;
  639. byte c = (i & 4) ? 0x3f : 0;
  640. byte d = (i & 8) ? 0x3f : 0;
  641. for (j = 0; j < 3; j++) {
  642. SPI.transfer(a);
  643. SPI.transfer(b);
  644. }
  645. SPI.transfer(0);
  646. SPI.transfer(0);
  647. for (j = 0; j < 3; j++) {
  648. SPI.transfer(c);
  649. SPI.transfer(d);
  650. }
  651. SPI.transfer(0);
  652. SPI.transfer(0);
  653. }
  654. GD.__end();
  655. for (i = 0; i < 256; i++) {
  656. GD.setpal(4 * i + 0, RGB(0,0,0));
  657. uint16_t color = pgm_read_word_near(title_ramp + 2 * (i >> 4));
  658. GD.setpal(4 * i + 3, color);
  659. }
  660. for (i = 0; i < 64; i++) {
  661. column(i, i);
  662. }
  663. for (i = 0; i < 128; i++) {
  664. objects[i].handler = 0;
  665. objects[i].collide = 0xff;
  666. }
  667. for (i = 0; i < 128; i++)
  668. push(&enemies, i);
  669. for (i = 0; i < 40; i++) {
  670. char e = pop(&enemies);
  671. objects[e].x = COORD(random(400));
  672. objects[e].y = COORD(random(300));
  673. objects[e].handler = qrand1() ? HANDLE_ROCK1 : HANDLE_ROCK0;
  674. objects[e].state = qrand(8);
  675. }
  676. byte startgame = 0;
  677. for (i = 0; startgame < 50; i++) {
  678. for (j = 0; j < 256; j++) {
  679. byte index = 15 & ((-i >> 2) + (j >> 4));
  680. uint16_t color = pgm_read_word_near(title_ramp + 2 * index);
  681. GD.setpal(4 * j + 3, color);
  682. }
  683. if (!startgame &&
  684. (controller_sense(i) || (i == (2048 - 400)))) {
  685. // explode all rocks!
  686. for (j = 0; j < 128; j++) {
  687. byte h = objects[j].handler;
  688. if ((h == HANDLE_ROCK0) || (h == HANDLE_ROCK1))
  689. objects[j].handler = HANDLE_BANG1;
  690. objects[j].state = i;
  691. }
  692. startgame = 1;
  693. }
  694. if (startgame)
  695. startgame++;
  696. runobjects(i);
  697. GD.waitvblank();
  698. GD.wr(SPR_PAGE, (i & 1));
  699. GD.wr(SPR_DISABLE, 0);
  700. GD.wr16(SCROLL_X, i);
  701. if ((i & 7) == 0) {
  702. byte x = ((i >> 3) + 56);
  703. column(63 & x, 255 & x);
  704. }
  705. }
  706. for (i = 0; i < 32; i++) {
  707. for (j = 0; j < 256; j++) {
  708. uint16_t a = RAM_PAL + (8 * j) + 6;
  709. uint16_t pal = GD.rd16(a);
  710. byte r = 31 & (pal >> 10);
  711. byte g = 31 & (pal >> 5);
  712. byte b = 31 & pal;
  713. if (r) r--;
  714. if (g) g--;
  715. if (b) b--;
  716. pal = (r << 10) | (g << 5) | b;
  717. GD.wr16(a, pal);
  718. }
  719. GD.waitvblank();
  720. GD.waitvblank();
  721. }
  722. GD.fill(RAM_PIC, 0, 4096);
  723. }
  724. #define SOUNDCYCLE(state) ((state) = v ? ((state) + 1) : 0)
  725. void loop()
  726. {
  727. title_banner();
  728. GD_uncompress(RAM_CHR, bg_chr_compressed);
  729. GD_uncompress(RAM_PAL, bg_pal_compressed);
  730. GD.wr16(COMM+0, 0);
  731. GD.wr16(COMM+2, 0x8000);
  732. GD.wr16(COMM+4, 8); // split at line 8
  733. GD.wr16(COMM+6, 177);
  734. GD.wr16(COMM+8, 166);
  735. GD.wr16(COMM+10, 291); // split at line 291
  736. GD.wr16(COMM+12, 0);
  737. GD.wr16(COMM+14, 0x8000 | (0x1ff & (8 - 291))); // show line 8 at line 292
  738. GD.microcode(splitscreen_code, sizeof(splitscreen_code));
  739. setup_sprites();
  740. memset(&sounds, 0, sizeof(sounds));
  741. level = 0;
  742. score = 0;
  743. lives = 3;
  744. unsigned int r = 0;
  745. start_level();
  746. while (lives) {
  747. int i, j;
  748. runobjects(r);
  749. for (i = 0; i < 128; i++)
  750. objects[i].collide = 0xff;
  751. GD.waitvblank();
  752. // swap frames
  753. GD.wr(SPR_PAGE, (r & 1));
  754. int scrollx = objects[0].x >> 4;
  755. int scrolly = objects[0].y >> 4;
  756. GD.wr16(COMM+6, scrollx & 0x1ff);
  757. GD.wr16(COMM+8, scrolly & 0x1ff);
  758. map_refresh(scrolly >> 6);
  759. update_score();
  760. GD.wr16(COMM+12, r); // horizontal scroll the bottom banner
  761. GD.waitvblank();
  762. GD.__start(COLLISION);
  763. for (i = 0; i < 256; i++) {
  764. byte c = SPI.transfer(0); // c is the colliding sprite number
  765. if (c != 0xff) {
  766. objects[spr2obj[i]].collide = spr2obj[c];
  767. }
  768. }
  769. GD.__end();
  770. if (sounds.boing) {
  771. byte v = max(0, 16 - (sounds.boing - 1) * 2);
  772. GD.voice(0, 0, 4 * 4000 - 700 * sounds.boing, v/2, v/2);
  773. GD.voice(1, 1, 1000 - 100 * sounds.boing, v, v);
  774. SOUNDCYCLE(sounds.boing);
  775. }
  776. if (sounds.boom) {
  777. byte v = max(0, 96 - (sounds.boom - 1) * 6);
  778. GD.voice(2, 0, 220, v, v);
  779. GD.voice(3, 1, 220/8, v/2, v/2);
  780. SOUNDCYCLE(sounds.boom);
  781. }
  782. if (sounds.pop) {
  783. byte v = max(0, 32 - (sounds.pop - 1) * 3);
  784. GD.voice(4, 0, 440, v, v);
  785. GD.voice(5, 1, 440/8, v/2, v/2);
  786. SOUNDCYCLE(sounds.pop);
  787. }
  788. GD.voice(6, 1, 40, sounds.thrust ? 10 : 0, sounds.thrust ? 10 : 0);
  789. static byte tune;
  790. if (sounds.bass) {
  791. byte v = sounds.bass < 9 ? 63 : 0;
  792. int f0 = tune ? 130: 163 ;
  793. byte partials[] = { 71, 32, 14, 75, 20, 40 };
  794. byte i;
  795. for (i = 0; i < 6; i++) {
  796. byte a = (v * partials[i]) >> 8;
  797. GD.voice(7 + i, 0, f0 * (i + 1), a, a);
  798. }
  799. SOUNDCYCLE(sounds.bass);
  800. }
  801. static byte rhythm;
  802. if (++rhythm >= max(24 - level, 10)) {
  803. sounds.bass = 1;
  804. rhythm = 0;
  805. tune = !tune;
  806. }
  807. byte nenemies = 64;
  808. byte pe = enemies;
  809. while (pe) {
  810. pe = objects[pe].state;
  811. nenemies--;
  812. }
  813. if (nenemies == 0) {
  814. level++;
  815. start_level();
  816. }
  817. r++;
  818. }
  819. }