![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Conway's game of life applied to the mbed and an RA8875 LCD.
Dependencies: LifeRules mbed RA8875
Inspired by a forum discussion on the mbed site, this version was scaled to support up to a 480x272 display - in a monochrome mode, or at a lower resolution in color (the color shows simple animation for birthing and dying cells).
Leveraging the LifeRules class, the game can be easily adapted to other displays - whether monochrome or color.
By default, this version allocates memory from the Ethernet ram banks, so avoids the memory limitations of some designs.
It should be simple to adapt it to any display - color or b&w, high or low resolution.
main.cpp@8:d0e048eef5db, 2016-08-06 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Aug 06 15:12:41 2016 +0000
- Revision:
- 8:d0e048eef5db
- Parent:
- 7:f6d73bba9c4c
- Child:
- 9:159df2630ad0
Update to latest libraries and test with both 480x272 and 800x480 screens.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 2:f4aece10ba62 | 1 | // game of life implementation inspired by this forum thread |
WiredHome | 2:f4aece10ba62 | 2 | // http://mbed.org/forum/helloworld/topic/4822/ |
WiredHome | 2:f4aece10ba62 | 3 | // |
WiredHome | 8:d0e048eef5db | 4 | #include "mbed.h" // v122 |
WiredHome | 8:d0e048eef5db | 5 | #include "RA8875.h" // v126 |
WiredHome | 7:f6d73bba9c4c | 6 | #include "LifeRules.h" // v3 |
WiredHome | 0:d268818ad088 | 7 | |
WiredHome | 8:d0e048eef5db | 8 | // These two defines can be enabled, or commented out |
WiredHome | 8:d0e048eef5db | 9 | //#define BIG_SCREEN |
WiredHome | 8:d0e048eef5db | 10 | //#define CAP_TOUCH |
WiredHome | 8:d0e048eef5db | 11 | #define LCD_C 16 // color - bits per pixel |
WiredHome | 8:d0e048eef5db | 12 | |
WiredHome | 8:d0e048eef5db | 13 | #ifdef CAP_TOUCH |
WiredHome | 8:d0e048eef5db | 14 | RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // MOSI,MISO,SCK,/ChipSelect,/reset, SDA,SCL,/IRQ, name |
WiredHome | 8:d0e048eef5db | 15 | #else |
WiredHome | 8:d0e048eef5db | 16 | RA8875 lcd(p5, p6, p7, p12, NC, "tft"); //MOSI, MISO, SCK, /ChipSelect, /reset, name |
WiredHome | 8:d0e048eef5db | 17 | LocalFileSystem local("local"); // access to calibration file for resistive touch. |
WiredHome | 8:d0e048eef5db | 18 | #endif |
WiredHome | 8:d0e048eef5db | 19 | |
WiredHome | 8:d0e048eef5db | 20 | #ifdef BIG_SCREEN |
WiredHome | 8:d0e048eef5db | 21 | #define LCD_W 800 |
WiredHome | 8:d0e048eef5db | 22 | #define LCD_H 480 |
WiredHome | 8:d0e048eef5db | 23 | #else |
WiredHome | 8:d0e048eef5db | 24 | #define LCD_W 480 |
WiredHome | 8:d0e048eef5db | 25 | #define LCD_H 272 |
WiredHome | 8:d0e048eef5db | 26 | #endif |
WiredHome | 8:d0e048eef5db | 27 | |
WiredHome | 8:d0e048eef5db | 28 | |
WiredHome | 0:d268818ad088 | 29 | // Define the life-map size |
WiredHome | 3:1139b132f983 | 30 | #define LIFE_W 150 |
WiredHome | 3:1139b132f983 | 31 | #define LIFE_H 100 |
WiredHome | 3:1139b132f983 | 32 | #define LIFE_C 2 /* 1 = monochrome, 2 = color */ |
WiredHome | 3:1139b132f983 | 33 | #define LIFE_Z 2 /* Zoom factor */ |
WiredHome | 0:d268818ad088 | 34 | |
WiredHome | 3:1139b132f983 | 35 | // Try to check if there is enough memory (for LPC1768) |
WiredHome | 3:1139b132f983 | 36 | #if LIFE_W * LIFE_H * LIFE_C * 2 / 8 > 0x8000 |
WiredHome | 3:1139b132f983 | 37 | #error "Sorry, but there isn't this much memory on an LPC1768" |
WiredHome | 3:1139b132f983 | 38 | #endif |
WiredHome | 0:d268818ad088 | 39 | |
WiredHome | 0:d268818ad088 | 40 | extern "C" void mbed_reset(); |
WiredHome | 0:d268818ad088 | 41 | |
WiredHome | 0:d268818ad088 | 42 | //#define DEBUG "main" |
WiredHome | 0:d268818ad088 | 43 | // ... |
WiredHome | 0:d268818ad088 | 44 | // INFO("Stuff to show %d", var); // new-line is automatically appended |
WiredHome | 0:d268818ad088 | 45 | // |
WiredHome | 0:d268818ad088 | 46 | #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) |
WiredHome | 0:d268818ad088 | 47 | #define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 0:d268818ad088 | 48 | #define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 0:d268818ad088 | 49 | #define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 0:d268818ad088 | 50 | #else |
WiredHome | 0:d268818ad088 | 51 | #define INFO(x, ...) |
WiredHome | 0:d268818ad088 | 52 | #define WARN(x, ...) |
WiredHome | 0:d268818ad088 | 53 | #define ERR(x, ...) |
WiredHome | 0:d268818ad088 | 54 | #endif |
WiredHome | 0:d268818ad088 | 55 | |
WiredHome | 0:d268818ad088 | 56 | |
WiredHome | 3:1139b132f983 | 57 | #if LIFE_C == 2 |
WiredHome | 3:1139b132f983 | 58 | #define LIFE_CLR Life::color |
WiredHome | 3:1139b132f983 | 59 | #else |
WiredHome | 3:1139b132f983 | 60 | #define LIFE_CLR Life::monochrome |
WiredHome | 3:1139b132f983 | 61 | #endif |
WiredHome | 3:1139b132f983 | 62 | |
WiredHome | 3:1139b132f983 | 63 | Life life(LIFE_W, LIFE_H, LIFE_CLR); |
WiredHome | 0:d268818ad088 | 64 | |
WiredHome | 0:d268818ad088 | 65 | // Where on screen do we locate it? |
WiredHome | 8:d0e048eef5db | 66 | #if LIFE_W * LIFE_Z < LCD_W |
WiredHome | 3:1139b132f983 | 67 | #define H_OFFSET -1 |
WiredHome | 3:1139b132f983 | 68 | #else |
WiredHome | 3:1139b132f983 | 69 | #define H_OFFSET 0 |
WiredHome | 3:1139b132f983 | 70 | #endif |
WiredHome | 0:d268818ad088 | 71 | |
WiredHome | 8:d0e048eef5db | 72 | #if LIFE_H * LIFE_Z < LCD_H |
WiredHome | 3:1139b132f983 | 73 | #define V_OFFSET -1 |
WiredHome | 3:1139b132f983 | 74 | #else |
WiredHome | 3:1139b132f983 | 75 | #define V_OFFSET 0 |
WiredHome | 3:1139b132f983 | 76 | #endif |
WiredHome | 0:d268818ad088 | 77 | |
WiredHome | 8:d0e048eef5db | 78 | #define LIFE_OFFSET_X (LCD_W - (LIFE_W * LIFE_Z) + H_OFFSET) |
WiredHome | 8:d0e048eef5db | 79 | #define LIFE_OFFSET_Y (LCD_H - (LIFE_H * LIFE_Z) + V_OFFSET) |
WiredHome | 3:1139b132f983 | 80 | |
WiredHome | 3:1139b132f983 | 81 | unsigned char imgbuffer[3*LIFE_W]; // from forum thread... |
WiredHome | 0:d268818ad088 | 82 | FILE *img; |
WiredHome | 0:d268818ad088 | 83 | |
WiredHome | 0:d268818ad088 | 84 | Serial pc(USBTX,USBRX); // for debugging |
WiredHome | 0:d268818ad088 | 85 | |
WiredHome | 0:d268818ad088 | 86 | int msDelay = 1000; //delay between frames |
WiredHome | 0:d268818ad088 | 87 | |
WiredHome | 0:d268818ad088 | 88 | void CheckForUserInteraction(void); |
WiredHome | 0:d268818ad088 | 89 | void genrand(); //random start |
WiredHome | 0:d268818ad088 | 90 | void genglidergun(); //glider gun |
WiredHome | 0:d268818ad088 | 91 | void genglider(); //glider |
WiredHome | 0:d268818ad088 | 92 | void genship1(); //light weight ship |
WiredHome | 0:d268818ad088 | 93 | void genBMP(); //from image |
WiredHome | 0:d268818ad088 | 94 | void gentest(); |
WiredHome | 0:d268818ad088 | 95 | void genBlinker(); |
WiredHome | 0:d268818ad088 | 96 | |
WiredHome | 2:f4aece10ba62 | 97 | void ScreenUpdate() |
WiredHome | 0:d268818ad088 | 98 | { |
WiredHome | 3:1139b132f983 | 99 | lcd.window(LIFE_OFFSET_X, LIFE_OFFSET_Y, LIFE_W * LIFE_Z, LIFE_H * LIFE_Z); |
WiredHome | 8:d0e048eef5db | 100 | lcd.SetGraphicsCursor(LIFE_OFFSET_X+1, LIFE_OFFSET_Y+1); |
WiredHome | 8:d0e048eef5db | 101 | pc.printf("window(%d,%d, %d,%d)\r\n", LIFE_OFFSET_X, LIFE_OFFSET_Y, LIFE_W * LIFE_Z, LIFE_H * LIFE_Z); |
WiredHome | 0:d268818ad088 | 102 | lcd._StartGraphicsStream(); |
WiredHome | 0:d268818ad088 | 103 | for (int j = 0; j < LIFE_H; j++) { |
WiredHome | 3:1139b132f983 | 104 | for (int Zx = 0; Zx < LIFE_Z; Zx++) { |
WiredHome | 3:1139b132f983 | 105 | for (int i = 0; i < LIFE_W; i++) { |
WiredHome | 3:1139b132f983 | 106 | Life::ValueOfLife lifeState = life.getbit(i,j); |
WiredHome | 3:1139b132f983 | 107 | color_t p; |
WiredHome | 3:1139b132f983 | 108 | switch (lifeState) { |
WiredHome | 3:1139b132f983 | 109 | case Life::dead: |
WiredHome | 7:f6d73bba9c4c | 110 | p = RGB(0,0,0); |
WiredHome | 3:1139b132f983 | 111 | break; |
WiredHome | 3:1139b132f983 | 112 | case Life::dying: |
WiredHome | 7:f6d73bba9c4c | 113 | p = RGB(192,0,0); |
WiredHome | 3:1139b132f983 | 114 | break; |
WiredHome | 3:1139b132f983 | 115 | case Life::living: |
WiredHome | 7:f6d73bba9c4c | 116 | p = RGB(64,255,64); |
WiredHome | 3:1139b132f983 | 117 | break; |
WiredHome | 3:1139b132f983 | 118 | case Life::birthing: |
WiredHome | 7:f6d73bba9c4c | 119 | p = RGB(0,0,192); |
WiredHome | 3:1139b132f983 | 120 | break; |
WiredHome | 3:1139b132f983 | 121 | default: |
WiredHome | 7:f6d73bba9c4c | 122 | p = RGB(192,192,0); // Should never see this one... |
WiredHome | 3:1139b132f983 | 123 | ERR(" lifeState = %d\r\n", lifeState); |
WiredHome | 3:1139b132f983 | 124 | break; |
WiredHome | 3:1139b132f983 | 125 | } |
WiredHome | 3:1139b132f983 | 126 | for (int Zy = 0; Zy < LIFE_Z; Zy++) { |
WiredHome | 3:1139b132f983 | 127 | lcd._putp(p); |
WiredHome | 3:1139b132f983 | 128 | } |
WiredHome | 0:d268818ad088 | 129 | } |
WiredHome | 0:d268818ad088 | 130 | } |
WiredHome | 0:d268818ad088 | 131 | } |
WiredHome | 0:d268818ad088 | 132 | lcd._EndGraphicsStream(); |
WiredHome | 0:d268818ad088 | 133 | lcd.WindowMax(); |
WiredHome | 0:d268818ad088 | 134 | } |
WiredHome | 0:d268818ad088 | 135 | |
WiredHome | 4:6edc72b38ec2 | 136 | int GetScreenCapture(void) |
WiredHome | 4:6edc72b38ec2 | 137 | { |
WiredHome | 4:6edc72b38ec2 | 138 | char fqfn[50]; |
WiredHome | 4:6edc72b38ec2 | 139 | int i = 0; |
WiredHome | 4:6edc72b38ec2 | 140 | |
WiredHome | 4:6edc72b38ec2 | 141 | pc.printf("Screen Capture... "); |
WiredHome | 4:6edc72b38ec2 | 142 | for (i=1; i< 100; i++) { |
WiredHome | 4:6edc72b38ec2 | 143 | snprintf(fqfn, sizeof(fqfn), "/local/Screen%02d.bmp", i); |
WiredHome | 4:6edc72b38ec2 | 144 | FILE * fh = fopen(fqfn, "rb"); |
WiredHome | 4:6edc72b38ec2 | 145 | if (!fh) { |
WiredHome | 8:d0e048eef5db | 146 | lcd.PrintScreen(0,0,LCD_W,LCD_H,fqfn); |
WiredHome | 4:6edc72b38ec2 | 147 | pc.printf(" as /local/Screen%02d.bmp\r\n", i); |
WiredHome | 4:6edc72b38ec2 | 148 | return i; |
WiredHome | 4:6edc72b38ec2 | 149 | } else { |
WiredHome | 4:6edc72b38ec2 | 150 | fclose(fh); // close this and try the next |
WiredHome | 4:6edc72b38ec2 | 151 | } |
WiredHome | 4:6edc72b38ec2 | 152 | } |
WiredHome | 4:6edc72b38ec2 | 153 | return 0; |
WiredHome | 4:6edc72b38ec2 | 154 | } |
WiredHome | 4:6edc72b38ec2 | 155 | |
WiredHome | 4:6edc72b38ec2 | 156 | |
WiredHome | 0:d268818ad088 | 157 | int main() |
WiredHome | 0:d268818ad088 | 158 | { |
WiredHome | 0:d268818ad088 | 159 | pc.baud(460800); // I like a snappy terminal, so crank it up! |
WiredHome | 0:d268818ad088 | 160 | pc.printf("\r\nConway's Game of Life - Build " __DATE__ " " __TIME__ "\r\n"); |
WiredHome | 0:d268818ad088 | 161 | |
WiredHome | 5:1c9223c17976 | 162 | lcd.init(); |
WiredHome | 8:d0e048eef5db | 163 | lcd.Backlight(0.5f); |
WiredHome | 8:d0e048eef5db | 164 | |
WiredHome | 8:d0e048eef5db | 165 | //lcd.frequency(5000000); |
WiredHome | 0:d268818ad088 | 166 | lcd.puts("Welcome to Conway's Game of Life\r\n\r\n"); |
WiredHome | 0:d268818ad088 | 167 | INFO("Destroy all life"); |
WiredHome | 0:d268818ad088 | 168 | life.DestroyAllLife(); |
WiredHome | 0:d268818ad088 | 169 | INFO("Life destroyed"); |
WiredHome | 0:d268818ad088 | 170 | |
WiredHome | 7:f6d73bba9c4c | 171 | lcd.foreground(RGB(255,255,255)); |
WiredHome | 7:f6d73bba9c4c | 172 | |
WiredHome | 0:d268818ad088 | 173 | //pc.printf("choice value = %d\n\r",choice); //for debugging |
WiredHome | 0:d268818ad088 | 174 | lcd.puts("Please Select Starting Configuration:\r\n"); |
WiredHome | 0:d268818ad088 | 175 | lcd.puts(" 0 = Glider 1 = Random\r\n" |
WiredHome | 0:d268818ad088 | 176 | " 2 = Ship 3 = Glider Gun\r\n" |
WiredHome | 0:d268818ad088 | 177 | " 4 = BMP image 5 = Blinker\r\n" |
WiredHome | 4:6edc72b38ec2 | 178 | " t = test \r\n" |
WiredHome | 0:d268818ad088 | 179 | " r = reset\r\n" |
WiredHome | 0:d268818ad088 | 180 | ); |
WiredHome | 0:d268818ad088 | 181 | |
WiredHome | 0:d268818ad088 | 182 | // Frame the life map |
WiredHome | 3:1139b132f983 | 183 | lcd.rect(LIFE_OFFSET_X-1,LIFE_OFFSET_Y-1,LIFE_OFFSET_X+LIFE_W*LIFE_Z,LIFE_OFFSET_Y+LIFE_H*LIFE_Z,Red); |
WiredHome | 0:d268818ad088 | 184 | lcd.foreground(Blue); |
WiredHome | 0:d268818ad088 | 185 | int choice = pc.getc(); |
WiredHome | 0:d268818ad088 | 186 | |
WiredHome | 0:d268818ad088 | 187 | switch ( choice ) { |
WiredHome | 0:d268818ad088 | 188 | case '0': |
WiredHome | 0:d268818ad088 | 189 | lcd.puts(">Glider!\r\n"); |
WiredHome | 0:d268818ad088 | 190 | genglider(); |
WiredHome | 0:d268818ad088 | 191 | break; |
WiredHome | 0:d268818ad088 | 192 | case '1': |
WiredHome | 0:d268818ad088 | 193 | lcd.puts(">Random!\r\n"); |
WiredHome | 0:d268818ad088 | 194 | genrand(); |
WiredHome | 0:d268818ad088 | 195 | break; |
WiredHome | 0:d268818ad088 | 196 | case '2': |
WiredHome | 0:d268818ad088 | 197 | lcd.puts(">Ship!\r\n"); |
WiredHome | 0:d268818ad088 | 198 | genship1(); |
WiredHome | 0:d268818ad088 | 199 | break; |
WiredHome | 0:d268818ad088 | 200 | case '3': |
WiredHome | 0:d268818ad088 | 201 | lcd.puts(">Glider Gun!\r\n"); |
WiredHome | 0:d268818ad088 | 202 | genglidergun(); |
WiredHome | 0:d268818ad088 | 203 | break; |
WiredHome | 0:d268818ad088 | 204 | case '4': |
WiredHome | 0:d268818ad088 | 205 | lcd.puts(">BMP image!\r\n"); |
WiredHome | 0:d268818ad088 | 206 | genBMP(); |
WiredHome | 0:d268818ad088 | 207 | break; |
WiredHome | 0:d268818ad088 | 208 | case '5': |
WiredHome | 0:d268818ad088 | 209 | lcd.puts(">Blinker!\r\n"); |
WiredHome | 0:d268818ad088 | 210 | genBlinker(); |
WiredHome | 0:d268818ad088 | 211 | break; |
WiredHome | 0:d268818ad088 | 212 | case 't': |
WiredHome | 0:d268818ad088 | 213 | lcd.puts(">test!\r\n"); |
WiredHome | 0:d268818ad088 | 214 | gentest(); |
WiredHome | 0:d268818ad088 | 215 | break; |
WiredHome | 0:d268818ad088 | 216 | } |
WiredHome | 4:6edc72b38ec2 | 217 | lcd.puts("\r\nRuntime Menu:\r\n" |
WiredHome | 4:6edc72b38ec2 | 218 | " p = PrintScreen\r\n" |
WiredHome | 0:d268818ad088 | 219 | " + = faster\r\n" |
WiredHome | 0:d268818ad088 | 220 | " - = slower\r\n" |
WiredHome | 0:d268818ad088 | 221 | " 0-9 = updates/sec\r\n" |
WiredHome | 0:d268818ad088 | 222 | ); |
WiredHome | 0:d268818ad088 | 223 | |
WiredHome | 0:d268818ad088 | 224 | ScreenUpdate(); |
WiredHome | 2:f4aece10ba62 | 225 | wait(1); |
WiredHome | 0:d268818ad088 | 226 | |
WiredHome | 0:d268818ad088 | 227 | while(1) { |
WiredHome | 0:d268818ad088 | 228 | CheckForUserInteraction(); |
WiredHome | 0:d268818ad088 | 229 | if (msDelay >= 0) { |
WiredHome | 0:d268818ad088 | 230 | static uint16_t toggle = 0; |
WiredHome | 0:d268818ad088 | 231 | |
WiredHome | 0:d268818ad088 | 232 | if ((++toggle & 1) == 0) { |
WiredHome | 0:d268818ad088 | 233 | life.GenerationStep(); |
WiredHome | 0:d268818ad088 | 234 | } else { |
WiredHome | 0:d268818ad088 | 235 | life.UpdateLifeCycle(); |
WiredHome | 0:d268818ad088 | 236 | } |
WiredHome | 0:d268818ad088 | 237 | ScreenUpdate(); |
WiredHome | 0:d268818ad088 | 238 | wait_ms(msDelay); |
WiredHome | 0:d268818ad088 | 239 | } |
WiredHome | 0:d268818ad088 | 240 | } |
WiredHome | 0:d268818ad088 | 241 | } |
WiredHome | 0:d268818ad088 | 242 | |
WiredHome | 0:d268818ad088 | 243 | void CheckForUserInteraction(void) |
WiredHome | 0:d268818ad088 | 244 | { |
WiredHome | 0:d268818ad088 | 245 | while (pc.readable()) { |
WiredHome | 0:d268818ad088 | 246 | int c = pc.getc(); |
WiredHome | 0:d268818ad088 | 247 | if (c == '+' && msDelay >= 0) |
WiredHome | 0:d268818ad088 | 248 | msDelay -= 10; |
WiredHome | 0:d268818ad088 | 249 | else if (c == '-' && msDelay < 1000) |
WiredHome | 0:d268818ad088 | 250 | msDelay += 10; |
WiredHome | 0:d268818ad088 | 251 | else if (c >= '0' && c <= '9') { |
WiredHome | 0:d268818ad088 | 252 | if (c == '0') |
WiredHome | 0:d268818ad088 | 253 | msDelay = -1; |
WiredHome | 0:d268818ad088 | 254 | else |
WiredHome | 0:d268818ad088 | 255 | msDelay = 10 * ((1000 / (c - '0'))/10); |
WiredHome | 0:d268818ad088 | 256 | } else if (c == 'r') |
WiredHome | 0:d268818ad088 | 257 | mbed_reset(); |
WiredHome | 4:6edc72b38ec2 | 258 | else if (c == 'p') |
WiredHome | 4:6edc72b38ec2 | 259 | GetScreenCapture(); |
WiredHome | 0:d268818ad088 | 260 | lcd.locate(49, 0); |
WiredHome | 0:d268818ad088 | 261 | if (msDelay < 0) { |
WiredHome | 0:d268818ad088 | 262 | msDelay = -10; |
WiredHome | 0:d268818ad088 | 263 | lcd.printf("Paused "); |
WiredHome | 0:d268818ad088 | 264 | } else { |
WiredHome | 0:d268818ad088 | 265 | lcd.printf("Delay %4d", msDelay); |
WiredHome | 0:d268818ad088 | 266 | } |
WiredHome | 0:d268818ad088 | 267 | } |
WiredHome | 0:d268818ad088 | 268 | } |
WiredHome | 0:d268818ad088 | 269 | |
WiredHome | 0:d268818ad088 | 270 | |
WiredHome | 0:d268818ad088 | 271 | void genBlinker() |
WiredHome | 0:d268818ad088 | 272 | { |
WiredHome | 0:d268818ad088 | 273 | life.setbit(1,1, Life::living); |
WiredHome | 0:d268818ad088 | 274 | life.setbit(1,2, Life::living); |
WiredHome | 0:d268818ad088 | 275 | life.setbit(1,3, Life::living); |
WiredHome | 0:d268818ad088 | 276 | } |
WiredHome | 0:d268818ad088 | 277 | |
WiredHome | 0:d268818ad088 | 278 | void gentest() |
WiredHome | 0:d268818ad088 | 279 | { |
WiredHome | 0:d268818ad088 | 280 | // Point |
WiredHome | 0:d268818ad088 | 281 | life.setbit(6,3, Life::living); |
WiredHome | 0:d268818ad088 | 282 | // Block |
WiredHome | 0:d268818ad088 | 283 | life.setbit(1,1, Life::living); |
WiredHome | 0:d268818ad088 | 284 | life.setbit(1,2, Life::living); |
WiredHome | 0:d268818ad088 | 285 | life.setbit(2,1, Life::living); |
WiredHome | 0:d268818ad088 | 286 | life.setbit(2,2, Life::living); |
WiredHome | 0:d268818ad088 | 287 | // Beehive |
WiredHome | 0:d268818ad088 | 288 | life.setbit(6,1, Life::living); |
WiredHome | 0:d268818ad088 | 289 | life.setbit(7,1, Life::living); |
WiredHome | 0:d268818ad088 | 290 | life.setbit(5,2, Life::living); |
WiredHome | 0:d268818ad088 | 291 | life.setbit(8,2, Life::living); |
WiredHome | 0:d268818ad088 | 292 | life.setbit(6,3, Life::living); |
WiredHome | 0:d268818ad088 | 293 | life.setbit(7,3, Life::living); |
WiredHome | 0:d268818ad088 | 294 | // Blinker |
WiredHome | 0:d268818ad088 | 295 | life.setbit(11,2, Life::living); |
WiredHome | 0:d268818ad088 | 296 | life.setbit(12,2, Life::living); |
WiredHome | 0:d268818ad088 | 297 | life.setbit(13,2, Life::living); |
WiredHome | 0:d268818ad088 | 298 | // Glider |
WiredHome | 0:d268818ad088 | 299 | // x |
WiredHome | 0:d268818ad088 | 300 | // x |
WiredHome | 0:d268818ad088 | 301 | // xxx |
WiredHome | 0:d268818ad088 | 302 | life.setbit(2,10, Life::living); |
WiredHome | 0:d268818ad088 | 303 | life.setbit(3,11, Life::living); |
WiredHome | 0:d268818ad088 | 304 | life.setbit(1,12, Life::living); |
WiredHome | 0:d268818ad088 | 305 | life.setbit(2,12, Life::living); |
WiredHome | 0:d268818ad088 | 306 | life.setbit(3,12, Life::living); |
WiredHome | 0:d268818ad088 | 307 | } |
WiredHome | 0:d268818ad088 | 308 | |
WiredHome | 0:d268818ad088 | 309 | |
WiredHome | 0:d268818ad088 | 310 | void genrand() |
WiredHome | 0:d268818ad088 | 311 | { |
WiredHome | 0:d268818ad088 | 312 | for (int i = 0; i < LIFE_W; i++) { //loop through each cell |
WiredHome | 0:d268818ad088 | 313 | for (int j = 0; j < LIFE_H; j++) { |
WiredHome | 0:d268818ad088 | 314 | if (rand() & 1) { //50% chance |
WiredHome | 0:d268818ad088 | 315 | life.setbit(i,j, Life::living); |
WiredHome | 0:d268818ad088 | 316 | } |
WiredHome | 0:d268818ad088 | 317 | } |
WiredHome | 0:d268818ad088 | 318 | } |
WiredHome | 0:d268818ad088 | 319 | } |
WiredHome | 0:d268818ad088 | 320 | |
WiredHome | 0:d268818ad088 | 321 | void genBMP() |
WiredHome | 0:d268818ad088 | 322 | { |
WiredHome | 0:d268818ad088 | 323 | for (int i = 0; i < LIFE_W; i++) { |
WiredHome | 0:d268818ad088 | 324 | img = fopen("/local/TESTIM~1.BMP", "rb"); |
WiredHome | 0:d268818ad088 | 325 | fseek (img , 54 + (LIFE_W*i*3) , SEEK_SET); |
WiredHome | 0:d268818ad088 | 326 | fread(imgbuffer, (LIFE_W*3), 1, img); |
WiredHome | 0:d268818ad088 | 327 | fclose(img); |
WiredHome | 0:d268818ad088 | 328 | for (int j = 0; j < LIFE_H; j++) { |
WiredHome | 0:d268818ad088 | 329 | int red = imgbuffer[j*3]; |
WiredHome | 0:d268818ad088 | 330 | if (red == 0) { |
WiredHome | 0:d268818ad088 | 331 | life.setbit(j, LIFE_H - 1 - i, Life::living); |
WiredHome | 0:d268818ad088 | 332 | } |
WiredHome | 0:d268818ad088 | 333 | } |
WiredHome | 0:d268818ad088 | 334 | } |
WiredHome | 0:d268818ad088 | 335 | } |
WiredHome | 0:d268818ad088 | 336 | |
WiredHome | 0:d268818ad088 | 337 | |
WiredHome | 0:d268818ad088 | 338 | void genglider() //set certain pixels |
WiredHome | 0:d268818ad088 | 339 | { |
WiredHome | 0:d268818ad088 | 340 | life.setbit(0,0, Life::living); |
WiredHome | 0:d268818ad088 | 341 | life.setbit(1,1, Life::living); |
WiredHome | 0:d268818ad088 | 342 | life.setbit(1,2, Life::living); |
WiredHome | 0:d268818ad088 | 343 | life.setbit(2,0, Life::living); |
WiredHome | 0:d268818ad088 | 344 | life.setbit(2,1, Life::living); |
WiredHome | 0:d268818ad088 | 345 | } |
WiredHome | 0:d268818ad088 | 346 | |
WiredHome | 0:d268818ad088 | 347 | void genglidergun() //set certain pixels |
WiredHome | 0:d268818ad088 | 348 | { |
WiredHome | 0:d268818ad088 | 349 | life.setbit(0,7, Life::living); //gun |
WiredHome | 0:d268818ad088 | 350 | life.setbit(0,8, Life::living); |
WiredHome | 0:d268818ad088 | 351 | life.setbit(1,7, Life::living); |
WiredHome | 0:d268818ad088 | 352 | life.setbit(1,8, Life::living); |
WiredHome | 0:d268818ad088 | 353 | life.setbit(8,8, Life::living); |
WiredHome | 0:d268818ad088 | 354 | life.setbit(8,9, Life::living); |
WiredHome | 0:d268818ad088 | 355 | life.setbit(9,7, Life::living); |
WiredHome | 0:d268818ad088 | 356 | life.setbit(9,9, Life::living); |
WiredHome | 0:d268818ad088 | 357 | life.setbit(10,7, Life::living); |
WiredHome | 0:d268818ad088 | 358 | life.setbit(10,8, Life::living); |
WiredHome | 0:d268818ad088 | 359 | life.setbit(16,9, Life::living); |
WiredHome | 0:d268818ad088 | 360 | life.setbit(16,10, Life::living); |
WiredHome | 0:d268818ad088 | 361 | life.setbit(16,11, Life::living); |
WiredHome | 0:d268818ad088 | 362 | life.setbit(17,9, Life::living); |
WiredHome | 0:d268818ad088 | 363 | life.setbit(18,10, Life::living); |
WiredHome | 0:d268818ad088 | 364 | life.setbit(22,6, Life::living); |
WiredHome | 0:d268818ad088 | 365 | life.setbit(22,7, Life::living); |
WiredHome | 0:d268818ad088 | 366 | life.setbit(23,5, Life::living); |
WiredHome | 0:d268818ad088 | 367 | life.setbit(23,7, Life::living); |
WiredHome | 0:d268818ad088 | 368 | life.setbit(24,5, Life::living); |
WiredHome | 0:d268818ad088 | 369 | life.setbit(24,6, Life::living); |
WiredHome | 0:d268818ad088 | 370 | life.setbit(24,17, Life::living); |
WiredHome | 0:d268818ad088 | 371 | life.setbit(24,18, Life::living); |
WiredHome | 0:d268818ad088 | 372 | life.setbit(25,17, Life::living); |
WiredHome | 0:d268818ad088 | 373 | life.setbit(25,19, Life::living); |
WiredHome | 0:d268818ad088 | 374 | life.setbit(26,17, Life::living); |
WiredHome | 0:d268818ad088 | 375 | life.setbit(34,5, Life::living); |
WiredHome | 0:d268818ad088 | 376 | life.setbit(34,6, Life::living); |
WiredHome | 0:d268818ad088 | 377 | life.setbit(35,5, Life::living); |
WiredHome | 0:d268818ad088 | 378 | life.setbit(35,6, Life::living); |
WiredHome | 0:d268818ad088 | 379 | life.setbit(35,12, Life::living); |
WiredHome | 0:d268818ad088 | 380 | life.setbit(35,13, Life::living); |
WiredHome | 0:d268818ad088 | 381 | life.setbit(35,14, Life::living); |
WiredHome | 0:d268818ad088 | 382 | life.setbit(36,12, Life::living); |
WiredHome | 0:d268818ad088 | 383 | life.setbit(37,13, Life::living); |
WiredHome | 0:d268818ad088 | 384 | |
WiredHome | 0:d268818ad088 | 385 | life.setbit(50,38, Life::living); //eater |
WiredHome | 0:d268818ad088 | 386 | life.setbit(51,38, Life::living); |
WiredHome | 0:d268818ad088 | 387 | life.setbit(50,39, Life::living); |
WiredHome | 0:d268818ad088 | 388 | life.setbit(52,39, Life::living); |
WiredHome | 0:d268818ad088 | 389 | life.setbit(52,40, Life::living); |
WiredHome | 0:d268818ad088 | 390 | life.setbit(52,41, Life::living); |
WiredHome | 0:d268818ad088 | 391 | life.setbit(53,41, Life::living); |
WiredHome | 0:d268818ad088 | 392 | } |
WiredHome | 0:d268818ad088 | 393 | |
WiredHome | 0:d268818ad088 | 394 | void genship1() //set certain pixels |
WiredHome | 0:d268818ad088 | 395 | { |
WiredHome | 0:d268818ad088 | 396 | life.setbit(10,10, Life::living); |
WiredHome | 0:d268818ad088 | 397 | life.setbit(13,10, Life::living); |
WiredHome | 0:d268818ad088 | 398 | life.setbit(14,11, Life::living); |
WiredHome | 0:d268818ad088 | 399 | life.setbit(10,12, Life::living); |
WiredHome | 0:d268818ad088 | 400 | life.setbit(14,12, Life::living); |
WiredHome | 0:d268818ad088 | 401 | life.setbit(11,13, Life::living); |
WiredHome | 0:d268818ad088 | 402 | life.setbit(12,13, Life::living); |
WiredHome | 0:d268818ad088 | 403 | life.setbit(13,13, Life::living); |
WiredHome | 0:d268818ad088 | 404 | life.setbit(14,13, Life::living); |
WiredHome | 0:d268818ad088 | 405 | } |
WiredHome | 0:d268818ad088 | 406 | |
WiredHome | 0:d268818ad088 | 407 | |
WiredHome | 0:d268818ad088 | 408 |