Advertisement
timeshifter

fast life

Sep 16th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.84 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Windows.Forms;
  5.  
  6. namespace colorlife {
  7.     public unsafe partial class frmDavesLife : Form {
  8.         Bitmap canvas;
  9.         Graphics g;
  10.         Rectangle bmpRect;
  11.         BitmapData bmpData;
  12.         uint* currPos;
  13.         bool fadeColors = true,
  14.             cycleColors = true;
  15.  
  16.         System.Windows.Forms.Timer tmrFrames = new System.Windows.Forms.Timer(); //mah frame counter
  17.         uint[] field; //the game state array
  18.         bool gameRunning = true, altFrame = false; //gameRunning so the Game loop terminates properly, altFrame is flipped every frame
  19.         uint w, //width
  20.              h, //height
  21.              w1, //width - 1
  22.              wp1, //width + 1
  23.              l,  //length
  24.              lw, //l - w
  25.              x, //x coord
  26.              i, //index
  27.              FPS = 0, //fps
  28.              FPSTotal = 0, //total fps, for average calculation
  29.              frameCount = 0, //number of frames, to determine the average
  30.              timerCount = 0;
  31.         uint* t, //start address of the active half of the field array
  32.               a; //start of the inactive half
  33.         byte n;
  34.         //color cycling stuff
  35.         Color[] colorList = { Color.Purple, Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Red };
  36.         uint cycleLength = 80, //frames between each full color
  37.             currCycleFrame = 0, currCycleItem = 0, //counters
  38.             col, //current frame's computed color
  39.             colDecVal = 1, //RGB value decrement each frame
  40.             colLimit = 31; //RGB value lower limit
  41.  
  42.         byte rVal, gVal, bVal; //current color values
  43.  
  44.         public frmDavesLife() {
  45.             Initialize();
  46.             Random R = new Random();
  47.             for (x = 0; x < l; x++) field[x] = (uint)(R.Next(100) <= 15 ? 1 : 0);
  48.             altFrame = false;
  49.             Game();
  50.         }
  51.  
  52.         public frmDavesLife(string path) {
  53.             Initialize();
  54.             for (x = 0; x < l; x++) field[x] = 0;
  55.             altFrame = false;
  56.             string[] pattern = System.IO.File.ReadAllLines(path);
  57.             for (int x = 0; x < (pattern[0].Length <= this.ClientSize.Width ? pattern[0].Length : this.ClientSize.Width); x++) {
  58.                 for (int y = 0; y <= pattern.GetUpperBound(0); y++) {
  59.                     field[x + (y * w)] = uint.Parse(pattern[y][x].ToString());
  60.                 }
  61.             }
  62.             Game();
  63.         }
  64.  
  65.         void Initialize() {
  66.             this.ClientSize = new Size(800, 600);
  67.             this.FormBorderStyle = FormBorderStyle.Fixed3D;
  68.             this.MinimizeBox = false;
  69.             this.MaximizeBox = false;
  70.             this.DoubleBuffered = true;
  71.             this.Text = "Dave's Game of Life";
  72.             this.Deactivate += new EventHandler(Form1_Deactivate);
  73.             this.Activated += new EventHandler(frmDavesLife_Activated);
  74.             this.Paint += new PaintEventHandler(Form1_Paint);
  75.             tmrFrames.Tick += new EventHandler(tmrFrames_Tick);
  76.             canvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
  77.             w = (uint)canvas.Width;
  78.             h = (uint)canvas.Height;
  79.             bmpRect = new Rectangle(0, 0, (int)w, (int)h);
  80.             l = w * h;
  81.             w1 = w - 1;
  82.             wp1 = w + 1;
  83.             lw = l - w;
  84.             g = Graphics.FromImage(canvas);
  85.             g.Clear(Color.Black);
  86.             field = new uint[l * 2];
  87.             tmrFrames.Interval = 1000;
  88.             tmrFrames.Enabled = true;
  89.             if (cycleColors) {
  90.                 rVal = colorList[0].R;
  91.                 gVal = colorList[0].G;
  92.                 bVal = colorList[0].B;
  93.             }
  94.             else
  95.                 col = 0xffffffff;
  96.             this.Show();
  97.             Application.DoEvents();
  98.         }
  99.  
  100.         void frmDavesLife_Activated(object sender, EventArgs e) {
  101.             if (!gameRunning) {
  102.                 gameRunning = true;
  103.                 Game();
  104.             }
  105.         }
  106.  
  107.         void tmrFrames_Tick(object sender, EventArgs e) {
  108.             timerCount++;
  109.             if (gameRunning) {
  110.                 FPSTotal += FPS;
  111.                 frameCount++;
  112.             }
  113.             this.Text = string.Format(
  114.                 "{0} ({1}) - {2}",
  115.                 FPS.ToString(),
  116.                 FPSTotal / frameCount,
  117.                 timerCount);
  118.             FPS = 0;
  119.         }
  120.  
  121.         void Form1_Paint(object sender, PaintEventArgs e) {
  122.             e.Graphics.DrawImageUnscaled(canvas, 0, 0);
  123.         }
  124.  
  125.         void Form1_Deactivate(object sender, EventArgs e) {
  126.             gameRunning = false;
  127.         }
  128.  
  129.         void Game() {
  130.             while (gameRunning) {
  131.                 FPS++;
  132.                 //if (cycleColors)
  133.                 //    CycleColors();
  134.                 ComputeFrame();
  135.                 this.Refresh();
  136.                 Application.DoEvents();
  137.                 altFrame = !altFrame;
  138.             }
  139.         }
  140.  
  141.         void CycleColors() { //this method could be optimized, but it's such a small hit on performance that it frankly doesn't matter
  142.             Color col1 = colorList[currCycleItem];
  143.             Color col2 = currCycleItem < (colorList.Length - 1) ? colorList[currCycleItem + 1] : colorList[0];
  144.             rVal = (byte)((((col2.R - col1.R) / (double)cycleLength) * currCycleFrame) + col1.R);
  145.             gVal = (byte)((((col2.G - col1.G) / (double)cycleLength) * currCycleFrame) + col1.G);
  146.             bVal = (byte)((((col2.B - col1.B) / (double)cycleLength) * currCycleFrame) + col1.B);
  147.             if (++currCycleFrame == cycleLength) {
  148.                 currCycleFrame = 0;
  149.                 currCycleItem = (currCycleItem + 1) % (uint)colorList.Length;
  150.             }
  151.             col = 0xff000000 | (uint)((rVal << 16) | (gVal << 8) | bVal);
  152.         }
  153.  
  154.         void ComputeFrame() {
  155.             i = 1;
  156.             x = 0;
  157.  
  158.             //lock the canvas, because i haven't delved into actual bitmap pointer drawing to the screen itself
  159.             bmpData = canvas.LockBits(bmpRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
  160.             currPos = (uint*)bmpData.Scan0;
  161.  
  162.             fixed (uint* f = field) { //field contains the entire grid plus swap
  163.                 t = f; //starting point of the current generation
  164.                 a = f; //starting point of the next generation
  165.  
  166.                 //determine which is which
  167.                 if (altFrame) t += l;
  168.                 else a += l;
  169.  
  170.                 while (i++ <= l) {
  171.                     n = 0;
  172.                    
  173.                     // w1 = width - 1
  174.                     // wp1 = width + 1
  175.                     // lw = (width * height) - width
  176.                     if (x != 0) if (*(t - 1) == 1) n++; //left
  177.                     if (x != w1) if (*(t + 1) == 1) n++; //right
  178.                     if (i > w) if (*(t - w) == 1) n++; //top
  179.                     if (i < lw) if (*(t + w) == 1) n++; //bottom
  180.                     if (x != 0 && i > w) if (*(t - w1) == 1) n++; //left top
  181.                     if (x != 0 && i < lw) if (*(t + w1) == 1) n++; //left bottom
  182.                     if (x != w1 && i > w) if (*(t - wp1) == 1) n++; //right top
  183.                     if (x != w1 && i < lw) if (*(t + wp1) == 1) n++; //right bottom
  184.  
  185.                     if (*t == 1) {
  186.                         *a = ((n != 2 && n != 3) ? 0U : 1U);
  187.                         *currPos = col;
  188.                     }
  189.                     else {
  190.                         *a = (n == 3 ? 1U : 0U);
  191.                         //if (fadeColors) { //fancy color cycling and fading, looks cool, but is slow(er)
  192.                         //    if ((*currPos & 0x00ff0000) > (uint)(colLimit << 16)) *currPos -= (uint)(colDecVal << 16);
  193.                         //    if ((*currPos & 0x0000ff00) > (uint)(colLimit << 8)) *currPos -= (uint)(colDecVal << 8);
  194.                         //    if ((*currPos & 0x000000ff) > (uint)(colLimit)) *currPos -= (uint)(colDecVal);
  195.                         //}
  196.                         //else
  197.                         *currPos = 0xff000000;
  198.                     }
  199.  
  200.                     ++a;
  201.                     ++t;
  202.                     ++currPos;
  203.                     if (++x == w) x = 0;
  204.                 }
  205.             }
  206.             canvas.UnlockBits(bmpData);
  207.         }
  208.     }
  209.  
  210.     static class Program {
  211.         [STAThread]
  212.         static void Main(string[] arg) {
  213.             try {
  214.                 if (arg.Length > 0 && System.IO.File.Exists(arg[0]))
  215.                     Application.Run(new frmDavesLife(arg[0]));
  216.                 else
  217.                     Application.Run(new frmDavesLife());
  218.             }
  219.             catch { }
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement