jkcollision demo for the Gameduino

Committer:
TheChrisyd
Date:
Fri Dec 21 13:55:55 2012 +0000
Revision:
1:d6cb4e7c1518
Parent:
0:31e2d78f22ef
updated Gameduino library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 0:31e2d78f22ef 1 #include "mbed.h"
TheChrisyd 0:31e2d78f22ef 2 #include "GD.h"
TheChrisyd 0:31e2d78f22ef 3 #include "arduino.h"
TheChrisyd 0:31e2d78f22ef 4 #include "shield.h"
TheChrisyd 0:31e2d78f22ef 5
TheChrisyd 0:31e2d78f22ef 6 GDClass GD(ARD_MOSI, ARD_MISO, ARD_SCK, ARD_D9, USBTX, USBRX) ;
TheChrisyd 0:31e2d78f22ef 7 SPI spimain(ARD_MOSI, ARD_MISO, ARD_SCK); // mosi, miso, sclk
TheChrisyd 0:31e2d78f22ef 8 #define PI 3.1415926535
TheChrisyd 0:31e2d78f22ef 9
TheChrisyd 0:31e2d78f22ef 10 void readn(byte *dst, unsigned int addr, int c) {
TheChrisyd 0:31e2d78f22ef 11 GD.__start(addr);
TheChrisyd 0:31e2d78f22ef 12 while (c--)
TheChrisyd 0:31e2d78f22ef 13 *dst++ = spimain.write(0);
TheChrisyd 0:31e2d78f22ef 14 GD.__end();
TheChrisyd 0:31e2d78f22ef 15 }
TheChrisyd 0:31e2d78f22ef 16
TheChrisyd 0:31e2d78f22ef 17 static byte coll[256];
TheChrisyd 0:31e2d78f22ef 18 static void load_coll() {
TheChrisyd 0:31e2d78f22ef 19 while (GD.rd(VBLANK) == 0) // Wait until vblank
TheChrisyd 0:31e2d78f22ef 20 ;
TheChrisyd 0:31e2d78f22ef 21 while (GD.rd(VBLANK) == 1) // Wait until display
TheChrisyd 0:31e2d78f22ef 22 ;
TheChrisyd 0:31e2d78f22ef 23 while (GD.rd(VBLANK) == 0) // Wait until vblank
TheChrisyd 0:31e2d78f22ef 24 ;
TheChrisyd 0:31e2d78f22ef 25 readn(coll, COLLISION, sizeof(coll));
TheChrisyd 0:31e2d78f22ef 26 }
TheChrisyd 0:31e2d78f22ef 27
TheChrisyd 0:31e2d78f22ef 28 void setup() {
TheChrisyd 0:31e2d78f22ef 29 int i;
TheChrisyd 0:31e2d78f22ef 30
TheChrisyd 0:31e2d78f22ef 31 GD.begin();
TheChrisyd 0:31e2d78f22ef 32
TheChrisyd 0:31e2d78f22ef 33 GD.wr(JK_MODE, 1);
TheChrisyd 0:31e2d78f22ef 34 GD.wr16(RAM_PAL, RGB(255,255,255));
TheChrisyd 0:31e2d78f22ef 35
TheChrisyd 0:31e2d78f22ef 36 // Use the 4 palettes:
TheChrisyd 0:31e2d78f22ef 37 // 0 pink, for J sprites
TheChrisyd 0:31e2d78f22ef 38 // 1 green, for K sprites
TheChrisyd 0:31e2d78f22ef 39 // 2 dark pink, J collisions
TheChrisyd 0:31e2d78f22ef 40 // 3 dark green, K collisions
TheChrisyd 0:31e2d78f22ef 41 for (i = 0; i < 256; i++) {
TheChrisyd 0:31e2d78f22ef 42 GD.wr16(RAM_SPRPAL + (0 * 512) + (i << 1), RGB(255, 0, 255));
TheChrisyd 0:31e2d78f22ef 43 GD.wr16(RAM_SPRPAL + (1 * 512) + (i << 1), RGB(0, 255, 0));
TheChrisyd 0:31e2d78f22ef 44 GD.wr16(RAM_SPRPAL + (2 * 512) + (i << 1), RGB(100, 0, 100));
TheChrisyd 0:31e2d78f22ef 45 GD.wr16(RAM_SPRPAL + (3 * 512) + (i << 1), RGB(0, 100, 0));
TheChrisyd 0:31e2d78f22ef 46 }
TheChrisyd 0:31e2d78f22ef 47 }
TheChrisyd 0:31e2d78f22ef 48
TheChrisyd 0:31e2d78f22ef 49 byte spr;
TheChrisyd 0:31e2d78f22ef 50 static void polar(float th, int r, byte jk) {
TheChrisyd 0:31e2d78f22ef 51 // add 2 to the palette if this sprite is colliding
TheChrisyd 0:31e2d78f22ef 52 byte colliding = coll[spr] != 0xff;
TheChrisyd 0:31e2d78f22ef 53 GD.sprite(spr, 200 + int(r * sin(th)), 142 + int(r * cos(th)), 0, jk + (colliding ? 2 : 0), 0, jk);
TheChrisyd 0:31e2d78f22ef 54 spr++;
TheChrisyd 0:31e2d78f22ef 55 }
TheChrisyd 0:31e2d78f22ef 56 Timer timer;
TheChrisyd 0:31e2d78f22ef 57 int main() {
TheChrisyd 0:31e2d78f22ef 58
TheChrisyd 0:31e2d78f22ef 59 timer.start();
TheChrisyd 0:31e2d78f22ef 60 setup();
TheChrisyd 0:31e2d78f22ef 61 byte i;
TheChrisyd 0:31e2d78f22ef 62 float th;
TheChrisyd 0:31e2d78f22ef 63 while (1) {
TheChrisyd 0:31e2d78f22ef 64 spr = 0;
TheChrisyd 0:31e2d78f22ef 65 // draw the J sprites (pink)
TheChrisyd 0:31e2d78f22ef 66 for (i = 0; i < 5; i++) {
TheChrisyd 0:31e2d78f22ef 67 th = (((timer.read_us()+2147)/500) / 3000.) + 2 * PI * i / 5;
TheChrisyd 0:31e2d78f22ef 68 polar(th, 134, 0);
TheChrisyd 0:31e2d78f22ef 69 }
TheChrisyd 0:31e2d78f22ef 70 // draw the K sprites (green)
TheChrisyd 0:31e2d78f22ef 71 randomSeed(4);
TheChrisyd 0:31e2d78f22ef 72 for (i = 0; i < 17; i++) {
TheChrisyd 0:31e2d78f22ef 73 th = (((timer.read_us()+2147)/500) / float((rand()%2000)+1000)) + 2 * PI * i / 17;
TheChrisyd 0:31e2d78f22ef 74 polar(th, 134, 1);
TheChrisyd 0:31e2d78f22ef 75 }
TheChrisyd 0:31e2d78f22ef 76 load_coll();
TheChrisyd 0:31e2d78f22ef 77 }
TheChrisyd 0:31e2d78f22ef 78 }
TheChrisyd 0:31e2d78f22ef 79