![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Scroll demo for the Gameduino
main.cpp@1:e67cae7d6015, 2012-12-21 (annotated)
- Committer:
- TheChrisyd
- Date:
- Fri Dec 21 14:05:08 2012 +0000
- Revision:
- 1:e67cae7d6015
- Parent:
- 0:714c22289f04
updated Gameduino library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TheChrisyd | 0:714c22289f04 | 1 | #include "mbed.h" |
TheChrisyd | 0:714c22289f04 | 2 | #include "GD.h" |
TheChrisyd | 0:714c22289f04 | 3 | |
TheChrisyd | 0:714c22289f04 | 4 | #include "platformer.h" |
TheChrisyd | 0:714c22289f04 | 5 | #include "shield.h" |
TheChrisyd | 0:714c22289f04 | 6 | |
TheChrisyd | 0:714c22289f04 | 7 | GDClass GD(ARD_MOSI, ARD_MISO, ARD_SCK, ARD_D9, USBTX, USBRX) ; |
TheChrisyd | 0:714c22289f04 | 8 | int atxy(int x, int y) { |
TheChrisyd | 0:714c22289f04 | 9 | return (y << 6) + x; |
TheChrisyd | 0:714c22289f04 | 10 | } |
TheChrisyd | 0:714c22289f04 | 11 | |
TheChrisyd | 0:714c22289f04 | 12 | // copy a (w,h) rectangle from the source image (x,y) into picture RAM |
TheChrisyd | 0:714c22289f04 | 13 | static void rect(unsigned int dst, byte x, byte y, byte w, byte h) { |
TheChrisyd | 0:714c22289f04 | 14 | PROGMEM prog_uchar *src = platformer_pic + (16 * y) + x; |
TheChrisyd | 0:714c22289f04 | 15 | while (h--) { |
TheChrisyd | 0:714c22289f04 | 16 | GD.copy(dst, src, w); |
TheChrisyd | 0:714c22289f04 | 17 | dst += 64; |
TheChrisyd | 0:714c22289f04 | 18 | src += 16; |
TheChrisyd | 0:714c22289f04 | 19 | } |
TheChrisyd | 0:714c22289f04 | 20 | } |
TheChrisyd | 0:714c22289f04 | 21 | |
TheChrisyd | 0:714c22289f04 | 22 | //#define SINGLE(x, y) (platformer_pic[(y) * 16 + (x)])) |
TheChrisyd | 0:714c22289f04 | 23 | |
TheChrisyd | 0:714c22289f04 | 24 | byte SINGLE(int x, int y) { |
TheChrisyd | 0:714c22289f04 | 25 | y = (y * 16) + x; |
TheChrisyd | 0:714c22289f04 | 26 | return platformer_pic[y]; |
TheChrisyd | 0:714c22289f04 | 27 | } |
TheChrisyd | 0:714c22289f04 | 28 | // Draw a random 8-character wide background column at picture RAM dst |
TheChrisyd | 0:714c22289f04 | 29 | |
TheChrisyd | 0:714c22289f04 | 30 | void draw_column(unsigned int dst) { |
TheChrisyd | 0:714c22289f04 | 31 | byte y; |
TheChrisyd | 0:714c22289f04 | 32 | byte x; |
TheChrisyd | 0:714c22289f04 | 33 | byte ch; |
TheChrisyd | 0:714c22289f04 | 34 | |
TheChrisyd | 0:714c22289f04 | 35 | // Clouds and sky, 11 lines |
TheChrisyd | 0:714c22289f04 | 36 | rect(dst, 0, 0, 8, 11); |
TheChrisyd | 0:714c22289f04 | 37 | |
TheChrisyd | 0:714c22289f04 | 38 | // bottom plain sky, lines 11-28 |
TheChrisyd | 0:714c22289f04 | 39 | ch = SINGLE(0,11); |
TheChrisyd | 0:714c22289f04 | 40 | for (y = 11; y < 28; y++) |
TheChrisyd | 0:714c22289f04 | 41 | GD.fill(dst + (y << 6), ch, 8); |
TheChrisyd | 0:714c22289f04 | 42 | |
TheChrisyd | 0:714c22289f04 | 43 | // randomly choose between background elements |
TheChrisyd | 0:714c22289f04 | 44 | byte what = rand()%256; |
TheChrisyd | 0:714c22289f04 | 45 | if (what < 10) { |
TheChrisyd | 0:714c22289f04 | 46 | // big mushroom thing |
TheChrisyd | 0:714c22289f04 | 47 | y = (rand()%7) + 11; |
TheChrisyd | 0:714c22289f04 | 48 | rect(dst + atxy(0, y), 8, 18, 8, 9); |
TheChrisyd | 0:714c22289f04 | 49 | y += 9; |
TheChrisyd | 0:714c22289f04 | 50 | byte i = 0; |
TheChrisyd | 0:714c22289f04 | 51 | while (y < 28) { |
TheChrisyd | 0:714c22289f04 | 52 | rect(dst + atxy(0, y), 8, 23 + (i & 3), 8, 1); |
TheChrisyd | 0:714c22289f04 | 53 | i++, y++; |
TheChrisyd | 0:714c22289f04 | 54 | } |
TheChrisyd | 0:714c22289f04 | 55 | } else if (what < 32) { |
TheChrisyd | 0:714c22289f04 | 56 | // pair of green bollards |
TheChrisyd | 0:714c22289f04 | 57 | for (x = 0; x < 8; x += 4) { |
TheChrisyd | 0:714c22289f04 | 58 | y = (rand()%5) + 20; |
TheChrisyd | 0:714c22289f04 | 59 | rect(dst + atxy(x, y), 6, 11, 4, 3); |
TheChrisyd | 0:714c22289f04 | 60 | y += 3; |
TheChrisyd | 0:714c22289f04 | 61 | while (y < 28) { |
TheChrisyd | 0:714c22289f04 | 62 | rect(dst + atxy(x, y), 6, 13, 4, 1); |
TheChrisyd | 0:714c22289f04 | 63 | y++; |
TheChrisyd | 0:714c22289f04 | 64 | } |
TheChrisyd | 0:714c22289f04 | 65 | } |
TheChrisyd | 0:714c22289f04 | 66 | } else { |
TheChrisyd | 0:714c22289f04 | 67 | // hills |
TheChrisyd | 0:714c22289f04 | 68 | for (x = 0; x < 8; x += 2) { |
TheChrisyd | 0:714c22289f04 | 69 | y = (rand()%5) + 20; |
TheChrisyd | 0:714c22289f04 | 70 | rect(dst + atxy(x, y), 4, 11, 2, 3); |
TheChrisyd | 0:714c22289f04 | 71 | y += 3; |
TheChrisyd | 0:714c22289f04 | 72 | while (y < 28) { |
TheChrisyd | 0:714c22289f04 | 73 | rect(dst + atxy(x, y), 4, 13, 2, 1); |
TheChrisyd | 0:714c22289f04 | 74 | y++; |
TheChrisyd | 0:714c22289f04 | 75 | } |
TheChrisyd | 0:714c22289f04 | 76 | } |
TheChrisyd | 0:714c22289f04 | 77 | // foreground blocks |
TheChrisyd | 0:714c22289f04 | 78 | x = rand()%5; |
TheChrisyd | 0:714c22289f04 | 79 | y = (rand()%13) + 11; |
TheChrisyd | 0:714c22289f04 | 80 | byte blk = rand()%4; |
TheChrisyd | 0:714c22289f04 | 81 | rect(dst + atxy(x, y), blk * 4, 14, 4, 3); |
TheChrisyd | 0:714c22289f04 | 82 | y += 3; |
TheChrisyd | 0:714c22289f04 | 83 | while (y < 28) { |
TheChrisyd | 0:714c22289f04 | 84 | rect(dst + atxy(x, y), blk * 4, 17, 4, 1); |
TheChrisyd | 0:714c22289f04 | 85 | y++; |
TheChrisyd | 0:714c22289f04 | 86 | } |
TheChrisyd | 0:714c22289f04 | 87 | } |
TheChrisyd | 0:714c22289f04 | 88 | |
TheChrisyd | 0:714c22289f04 | 89 | // Ground, line 28 |
TheChrisyd | 0:714c22289f04 | 90 | ch = SINGLE(0,18); |
TheChrisyd | 0:714c22289f04 | 91 | GD.fill(dst + atxy(0,28), ch, 8); |
TheChrisyd | 0:714c22289f04 | 92 | // Underground, line 29 |
TheChrisyd | 0:714c22289f04 | 93 | ch = SINGLE(0,19); |
TheChrisyd | 0:714c22289f04 | 94 | GD.fill(dst + atxy(0,29), ch, 8); |
TheChrisyd | 0:714c22289f04 | 95 | } |
TheChrisyd | 0:714c22289f04 | 96 | |
TheChrisyd | 0:714c22289f04 | 97 | unsigned long xscroll; |
TheChrisyd | 0:714c22289f04 | 98 | |
TheChrisyd | 0:714c22289f04 | 99 | void setup() { |
TheChrisyd | 0:714c22289f04 | 100 | GD.begin(); |
TheChrisyd | 0:714c22289f04 | 101 | GD.copy(RAM_CHR, platformer_chr, sizeof(platformer_chr)); |
TheChrisyd | 0:714c22289f04 | 102 | GD.copy(RAM_PAL, platformer_pal, sizeof(platformer_pal)); |
TheChrisyd | 0:714c22289f04 | 103 | |
TheChrisyd | 0:714c22289f04 | 104 | int i; |
TheChrisyd | 0:714c22289f04 | 105 | for (i = 0; i < 256; i++) |
TheChrisyd | 0:714c22289f04 | 106 | GD.sprite(i, 400, 400, 0, 0, 0); |
TheChrisyd | 0:714c22289f04 | 107 | |
TheChrisyd | 0:714c22289f04 | 108 | for (i = 0; i < 64; i += 8) { |
TheChrisyd | 0:714c22289f04 | 109 | draw_column(atxy(i, 0)); |
TheChrisyd | 0:714c22289f04 | 110 | } |
TheChrisyd | 0:714c22289f04 | 111 | // Serial.begin(1000000); |
TheChrisyd | 0:714c22289f04 | 112 | } |
TheChrisyd | 0:714c22289f04 | 113 | |
TheChrisyd | 0:714c22289f04 | 114 | int main() { |
TheChrisyd | 0:714c22289f04 | 115 | setup(); |
TheChrisyd | 0:714c22289f04 | 116 | while (1) { |
TheChrisyd | 0:714c22289f04 | 117 | xscroll++; |
TheChrisyd | 0:714c22289f04 | 118 | if ((xscroll & 63) == 0) { |
TheChrisyd | 0:714c22289f04 | 119 | // figure out where to draw the 64-pixel draw_column |
TheChrisyd | 0:714c22289f04 | 120 | // offscreen_pixel is the pixel x draw_column that is offscreen... |
TheChrisyd | 0:714c22289f04 | 121 | int offscreen_pixel = ((xscroll + (7 * 64)) & 511); |
TheChrisyd | 0:714c22289f04 | 122 | // offscreen_ch is the character address |
TheChrisyd | 0:714c22289f04 | 123 | byte offscreen_ch = (offscreen_pixel >> 3); |
TheChrisyd | 0:714c22289f04 | 124 | draw_column(atxy(offscreen_ch, 0)); |
TheChrisyd | 0:714c22289f04 | 125 | } |
TheChrisyd | 0:714c22289f04 | 126 | GD.waitvblank(); |
TheChrisyd | 0:714c22289f04 | 127 | GD.wr16(SCROLL_X, xscroll); |
TheChrisyd | 0:714c22289f04 | 128 | } |
TheChrisyd | 0:714c22289f04 | 129 | } |