Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Windows.Forms;
- namespace colorlife {
- public unsafe partial class frmDavesLife : Form {
- Bitmap canvas;
- Graphics g;
- Rectangle bmpRect;
- BitmapData bmpData;
- uint* currPos;
- bool fadeColors = true,
- cycleColors = true;
- System.Windows.Forms.Timer tmrFrames = new System.Windows.Forms.Timer(); //mah frame counter
- byte[] field; //the game state array
- bool gameRunning = true, altFrame = false; //gameRunning so the Game loop terminates properly, altFrame is flipped every frame
- uint w, //width
- h, //height
- w1, //width - 1
- wp1, //width + 1
- w2,
- w3,
- l, //length
- lw, //l - w
- x, //x coord
- outerL,
- i, //index
- FPS = 0, //fps
- FPSTotal = 0, //total fps, for average calculation
- frameCount = 0, //number of frames, to determine the average
- timerCount = 0;
- byte* t, //start address of the active half of the field array
- a; //start of the inactive half
- byte n;
- //color cycling stuff
- Color[] colorList = { Color.Purple, Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Red };
- uint cycleLength = 80, //frames between each full color
- currCycleFrame = 0, currCycleItem = 0, //counters
- col, //current frame's computed color
- colDecVal = 1, //RGB value decrement each frame
- colLimit = 31; //RGB value lower limit
- byte rVal, gVal, bVal; //current color values
- public frmDavesLife() {
- Initialize();
- Random R = new Random(0);
- 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();
- }
- public frmDavesLife(string path) {
- Initialize();
- for (x = 0; x < outerL * 2; x++) field[x] = 0;
- altFrame = false;
- string[] pattern = System.IO.File.ReadAllLines(path);
- for (int x = 0; x < (pattern[0].Length <= this.ClientSize.Width ? pattern[0].Length : this.ClientSize.Width); x++) {
- for (int y = 0; y <= pattern.GetUpperBound(0); y++) {
- field[x + (y * w)] = byte.Parse(pattern[y][x].ToString());
- }
- }
- 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 frmDavesLife_Activated(object sender, EventArgs e) {
- if (!gameRunning) {
- gameRunning = true;
- Game();
- }
- }
- void tmrFrames_Tick(object sender, EventArgs e) {
- timerCount++;
- if (gameRunning) {
- FPSTotal += FPS;
- frameCount++;
- }
- this.Text = string.Format(
- "{0} ({1}) - {2}",
- FPS.ToString(),
- FPSTotal / frameCount,
- timerCount);
- FPS = 0;
- }
- void Form1_Paint(object sender, PaintEventArgs e) {
- e.Graphics.DrawImageUnscaled(canvas, 0, 0);
- }
- void Form1_Deactivate(object sender, EventArgs e) {
- gameRunning = false;
- }
- void Game() {
- while (gameRunning) {
- FPS++;
- //if (cycleColors)
- // CycleColors();
- ComputeFrame();
- this.Refresh();
- Application.DoEvents();
- altFrame = !altFrame;
- }
- }
- void CycleColors() { //this method could be optimized, but it's such a small hit on performance that it frankly doesn't matter
- Color col1 = colorList[currCycleItem];
- Color col2 = currCycleItem < (colorList.Length - 1) ? colorList[currCycleItem + 1] : colorList[0];
- rVal = (byte)((((col2.R - col1.R) / (double)cycleLength) * currCycleFrame) + col1.R);
- gVal = (byte)((((col2.G - col1.G) / (double)cycleLength) * currCycleFrame) + col1.G);
- bVal = (byte)((((col2.B - col1.B) / (double)cycleLength) * currCycleFrame) + col1.B);
- if (++currCycleFrame == cycleLength) {
- currCycleFrame = 0;
- currCycleItem = (currCycleItem + 1) % (uint)colorList.Length;
- }
- col = 0xff000000 | (uint)((rVal << 16) | (gVal << 8) | bVal);
- }
- 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 = f + w3; //starting point of the current generation
- a = t; //starting point of the next generation
- //determine which is which
- if (altFrame) t += outerL;
- else a += outerL;
- while (i++ < l) {
- //sum living neighbors
- n = (byte)(*(t - 1) + *(t + 1) + *(t - w2) + *(t + w2) + *(t - w1) + *(t + w1) + *(t - wp1) + *(t + wp1));
- if (*t == 1) { //current cell is alive
- *a = (byte)((n != 2 && n != 3) ? 0 : 1);
- *currPos = 0xffffffff; // col;
- }
- else { //he's dead, jim
- *a = (byte)(n == 3 ? 1 : 0);
- //if (fadeColors) { //fancy color cycling and fading, looks cool, but is slow
- // 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;
- }
- ++currPos;
- if (++x == w) {
- x = 0;
- a += 3;
- t += 3;
- }
- else {
- ++a;
- ++t;
- }
- }
- }
- canvas.UnlockBits(bmpData);
- }
- }
- static class Program {
- [STAThread]
- static void Main(string[] arg) {
- try {
- if (arg.Length > 0 && System.IO.File.Exists(arg[0]))
- Application.Run(new frmDavesLife(arg[0]));
- else
- Application.Run(new frmDavesLife());
- }
- catch { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement