Advertisement
timeshifter

Untitled

Sep 16th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1.         void Initialize() {
  2.             this.ClientSize = new Size(800, 600);
  3.             this.FormBorderStyle = FormBorderStyle.Fixed3D;
  4.             this.MinimizeBox = false;
  5.             this.MaximizeBox = false;
  6.             this.DoubleBuffered = true;
  7.             this.Text = "Dave's Game of Life";
  8.             this.Deactivate += new EventHandler(Form1_Deactivate);
  9.             this.Activated += new EventHandler(frmDavesLife_Activated);
  10.             this.Paint += new PaintEventHandler(Form1_Paint);
  11.             tmrFrames.Tick += new EventHandler(tmrFrames_Tick);
  12.             canvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
  13.             w = (uint)canvas.Width;
  14.             h = (uint)canvas.Height;
  15.             bmpRect = new Rectangle(0, 0, (int)w, (int)h);
  16.             l = w * h;
  17.             w1 = (w+2) - 1;
  18.             wp1 = (w+2) + 1;
  19.             lw = l - w;
  20.             outerL = (w + 2) * (h + 2);
  21.             g = Graphics.FromImage(canvas);
  22.             g.Clear(Color.Black);
  23.             field = new uint[outerL * 2];
  24.             tmrFrames.Interval = 1000;
  25.             if (cycleColors) {
  26.                 rVal = colorList[0].R;
  27.                 gVal = colorList[0].G;
  28.                 bVal = colorList[0].B;
  29.             }
  30.             else
  31.                 col = 0xffffffff;
  32.             this.Show();
  33.             Application.DoEvents();
  34.         }
  35.  
  36.  
  37.         void ComputeFrame() {
  38.             i = 0;
  39.             x = 0;
  40.  
  41.             //lock the canvas, because i haven't delved into actual bitmap pointer drawing to the screen itself
  42.             bmpData = canvas.LockBits(bmpRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
  43.             currPos = (uint*)bmpData.Scan0;
  44.  
  45.             fixed (uint* f = field) { //field contains the entire grid plus swap
  46.                 t = f + (w+3); //starting point of the current generation
  47.                 a = f + (w+3); //starting point of the next generation
  48.  
  49.                 //determine which is which
  50.                 if (altFrame) t += outerL;
  51.                 else a += outerL;
  52.  
  53.                 while (i++ <= l) {
  54.                     //n = 0;
  55.  
  56.                     // w1 = width - 1
  57.                     // wp1 = width + 1
  58.                     // lw = (width * height) - width
  59.                     //if (x != 0 && *(t - 1) == 1) n++; //left
  60.                     //if (x != w1 && *(t + 1) == 1) n++; //right
  61.                     //if (i > w && *(t - w) == 1) n++; //top
  62.                     //if (i < lw && *(t + w) == 1) n++; //bottom
  63.                     //if (x != 0 && i > w && *(t - w1) == 1) n++; //left top
  64.                     //if (x != 0 && i < lw && *(t + w1) == 1) n++; //left bottom
  65.                     //if (x != w1 && i > w && *(t - wp1) == 1) n++; //right top
  66.                     //if (x != w1 && i < lw && *(t + wp1) == 1) n++; //right bottom
  67.  
  68.                     //if ( *(t - 1) == 1) n++; //left
  69.                     //if ( *(t + 1) == 1) n++; //right
  70.                     //if (*(t - w) == 1) n++; //top
  71.                     //if (*(t + w) == 1) n++; //bottom
  72.                     //if (*(t - w1) == 1) n++; //left top
  73.                     //if (*(t + w1) == 1) n++; //left bottom
  74.                     //if (*(t - wp1) == 1) n++; //right top
  75.                     //if (*(t + wp1) == 1) n++; //right bottom
  76.  
  77.                     n =
  78.                          (byte)(*(t - 1) +
  79.                          *(t + 1) +
  80.                         *(t - (w+2)) +
  81.                         *(t + (w+2)) +
  82.                         *(t - w1) +
  83.                         *(t + w1) +
  84.                         *(t - wp1) +
  85.                         *(t + wp1));
  86.  
  87.                     if (*t == 1) {
  88.                         *a = ((n != 2 && n != 3) ? 0U : 1U);
  89.                         *currPos = 0xffffffff; // col;
  90.                     }
  91.                     else {
  92.                         *a = (n == 3 ? 1U : 0U);
  93.                         //if (fadeColors) { //fancy color cycling and fading, looks cool, but is slow(er)
  94.                         //    if ((*currPos & 0x00ff0000) > (uint)(colLimit << 16)) *currPos -= (uint)(colDecVal << 16);
  95.                         //    if ((*currPos & 0x0000ff00) > (uint)(colLimit << 8)) *currPos -= (uint)(colDecVal << 8);
  96.                         //    if ((*currPos & 0x000000ff) > (uint)(colLimit)) *currPos -= (uint)(colDecVal);
  97.                         //}
  98.                         //else
  99.                         *currPos = 0xff000000;
  100.                     }
  101.  
  102.                     ++a;
  103.                     ++t;
  104.                     ++currPos;
  105.                     if (++x == w) {
  106.                         x = 0;
  107.                         ++a;
  108.                         ++a;
  109.                         ++t;
  110.                         ++t;
  111.                     }
  112.                 }
  113.             }
  114.             canvas.UnlockBits(bmpData);
  115.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement