Advertisement
timeshifter

colorlife as of 8/24/17

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