Advertisement
timeshifter

Untitled

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