Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public frmDavesLife() {
- Initialize();
- Random R = new Random();
- for (x = 1; x <=w; x++) {
- for (int y = 1; y <= h; y++) {
- field[(y*(w+2)) + x] = (byte)(R.Next(100) <= 15 ? 1 : 0);
- }
- }
- altFrame = false;
- tmrFrames.Enabled = true;
- Game();
- }
- void Initialize() {
- this.ClientSize = new Size(800, 600);
- this.FormBorderStyle = FormBorderStyle.Fixed3D;
- this.MinimizeBox = false;
- this.MaximizeBox = false;
- this.DoubleBuffered = true;
- this.Text = "Dave's Game of Life";
- this.Deactivate += new EventHandler(Form1_Deactivate);
- this.Activated += new EventHandler(frmDavesLife_Activated);
- this.Paint += new PaintEventHandler(Form1_Paint);
- tmrFrames.Tick += new EventHandler(tmrFrames_Tick);
- canvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
- w = (uint)canvas.Width;
- h = (uint)canvas.Height;
- bmpRect = new Rectangle(0, 0, (int)w, (int)h);
- l = w * h;
- w1 = (w+2) - 1;
- wp1 = (w+2) + 1;
- //lw = l - w;
- w2 = w + 2;
- w3 = w + 3;
- outerL = (w + 2) * (h + 2);
- g = Graphics.FromImage(canvas);
- g.Clear(Color.Black);
- field = new byte[outerL * 2];
- tmrFrames.Interval = 1000;
- if (cycleColors) {
- rVal = colorList[0].R;
- gVal = colorList[0].G;
- bVal = colorList[0].B;
- }
- else
- col = 0xffffffff;
- this.Show();
- Application.DoEvents();
- }
- void ComputeFrame() {
- i = 0;
- x = 0;
- //lock the canvas, because i haven't delved into actual bitmap pointer drawing to the screen itself
- bmpData = canvas.LockBits(bmpRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
- currPos = (uint*)bmpData.Scan0;
- fixed (byte* f = field) { //field contains the entire grid plus swap
- t = (uint*)f + w3; //starting point of the current generation
- a = (uint*)f + w3; //starting point of the next generation
- //determine which is which
- if (altFrame) t += outerL;
- else a += outerL;
- while (i++ <= l) {
- n =
- (byte)(*(t - 1) +
- *(t + 1) +
- *(t - w2) +
- *(t + w2) +
- *(t - w1) +
- *(t + w1) +
- *(t - wp1) +
- *(t + wp1));
- if (*t == 1) {
- *a = ((n != 2 && n != 3) ? 0U : 1U);
- *currPos = 0xffffffff; // col;
- }
- else {
- *a = (n == 3 ? 1U : 0U);
- //if (fadeColors) { //fancy color cycling and fading, looks cool, but is slow(er)
- // if ((*currPos & 0x00ff0000) > (uint)(colLimit << 16)) *currPos -= (uint)(colDecVal << 16);
- // if ((*currPos & 0x0000ff00) > (uint)(colLimit << 8)) *currPos -= (uint)(colDecVal << 8);
- // if ((*currPos & 0x000000ff) > (uint)(colLimit)) *currPos -= (uint)(colDecVal);
- //}
- //else
- *currPos = 0xff000000;
- }
- ++a;
- ++t;
- ++currPos;
- if (++x == w) {
- x = 0;
- a += 2;
- t += 2;
- }
- }
- }
- canvas.UnlockBits(bmpData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement