/// <summary> /// Updates the board. /// </summary> /// <param name="form">Current GameForm</param> void UpdateBoard(GameForm form) { if (form.InvokeRequired) { form.Invoke(new MethodInvoker(() => UpdateBoard(form))); return; } form.UpdateBoard(); }
static void Main() { LogLevel = LogLevel.Error; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); gf = new GameForm(); StartGame(new TimeSpan(0), false, false, 0, 0, new TimeSpan(0)); gf.ShowMenu(); Application.Run(gf); }
/// <summary> /// Main loop of the game. /// </summary> /// <param name="gf">GameForm object</param> public void GameLoop(object gf) { gf1 = (GameForm)gf; Program.game.TargetsMutex.WaitOne(); for (int i = 0; i < StartingTargets; i++) { AddTarget(); } Program.game.TargetsMutex.ReleaseMutex(); sw.Start(); var last_time = sw.Elapsed; SecondCount = sw.Elapsed; TargetAddDelta = sw.Elapsed; var update_time = new TimeSpan(0); while (true) { var delta = sw.Elapsed - last_time; last_time += delta; update_time += delta; if ((sw.Elapsed - SecondCount).Seconds >= 1) { if ((sw.Elapsed - SecondCount).Seconds < 2) // if more than 2 seconds elapsed, something gone wrong with time measurement - do not update then { SecondTimer(); // execute method every one second } SecondCount = sw.Elapsed; } if (type == GameType.ScoreTrial) { if ((sw.Elapsed - TargetAddDelta) >= TargetAddTime) { AddTarget(); TargetAddDelta = sw.Elapsed; } } while (update_time > FrameTime) { var tickstart = sw.Elapsed; update_time -= FrameTime; max_x = gf1.GetBoardSize().Width; max_y = gf1.GetBoardSize().Height; UpdateTargets(); // do some per frame target operations UpdateBoard(gf1); // flush the board Program.Debug(LogLevel.Verbose, "UpdateTime: {0}", update_time); spin_counter = 0; TickCount++; ticktime = sw.Elapsed - tickstart; ticktimes.Add(ticktime.Milliseconds); if (ticktime > FrameTime) { Program.Debug(LogLevel.Error, "Ticktime {0} ms exceeded frame time!", ticktime.Milliseconds); } } Thread.Sleep(1); spin_counter++; Program.Debug(LogLevel.Verbose, "SpinCounter: {0}", spin_counter); // how many waiting loops passed until new frame while (!state) // if paused, wait { last_time = sw.Elapsed; Thread.Sleep(10); } } }