public MainForm() { InitializeComponent(); this.DoubleBuffered = true; this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.ContainerControl | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor , true); FloorPlanController.Boot(); HeatTransferController.ArrayHeight = 90; HeatTransferController.ArrayWidth = 150; HeatTransferController.BlockHeight = 5; HeatTransferController.BlockWidth = 5; HeatTransferController.Boot(FloorPlanController.Walls); HumanController.Boot(FloorPlanController.Walls, FloorPlanController.Doors, 5); HumanController.Humans.ForEach(i => i.SetEmergencyRoute(FloorPlanController.EmergencyRoute)); Task.Run(() => { var screenWidth = pictureBox.Width; var screenHeight = pictureBox.Height; Random rnd = new Random(); int randomCounter = 0; Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { if (sw.ElapsedMilliseconds > 20) { Bitmap buffer = new Bitmap(screenWidth, screenHeight); System.Drawing.Graphics gfx = Graphics.FromImage(buffer);//set the graphics to draw on the image HumanController.Calculate( FloorPlanController.Walls, FloorPlanController.Doors, FloorPlanController.Rooms ); HumanController.Humans.ForEach(h => h.Temperature = HeatTransferController.GetNearestBlock(h.Position).Temperature ); HeatTransferController.Calculate(); HumanController.Draw(gfx); FloorPlanController.Draw(gfx); HeatTransferController.Draw(gfx); ChangeScreen(buffer);//set the PictureBox's image to be the buffer if (randomCounter == 0) { randomCounter = rnd.Next(0, 30); HumanController.RandomEvent(); } else { randomCounter--; } sw.Restart(); } } }); }
/** SIMULATIONS **/ private void SimulateWithoutAny() { Simulate = true; CreateLogInvoke("Simple Simulation Starting"); ResetControllers(); BootControllers(); Task.Run(() => { Fps = 0; var screenWidth = pictureBox.Width; var screenHeight = pictureBox.Height; Stopwatch stopwatchFps = new Stopwatch(); Stopwatch stopwatchDrawTime = new Stopwatch(); stopwatchFps.Start(); WriteStats(0, 0); while (Simulate) { Bitmap buffer = new Bitmap(screenWidth, screenHeight); Graphics gfx = Graphics.FromImage(buffer);//set the graphics to draw on the image // Calculations HumanController.Calculate( FloorPlanController.Walls, FloorPlanController.Doors, FloorPlanController.Rooms ); // Human --> HeatBlock HumanController.Humans.ForEach(h => h.Temperature = HeatTransferController.GetNearestBlock(h.Position).Temperature ); // Heat Temperature Calculation HeatTransferController.Calculate(); // Drawing section stopwatchDrawTime.Restart(); HumanController.Draw(gfx); FloorPlanController.Draw(gfx); HeatTransferController.Draw(gfx); stopwatchDrawTime.Stop(); // Screen Change ChangeScreen(buffer); HumanController.RandomEvent(); Fps++; // Check Logs CheckLogger(); if (stopwatchFps.ElapsedMilliseconds > ONE_SEC) { FpsNumbers.Add(Fps); float fpsAverage = FpsNumbers.Sum() / FpsNumbers.Count; WriteStats(stopwatchDrawTime.ElapsedMilliseconds, fpsAverage); Fps = 0; stopwatchFps.Restart(); if (FpsNumbers.Count > SimulationLength) { Simulate = false; CreateLogInvoke("Simple Simulation Ended"); stopwatchFps.Stop(); return; } } } }); }