/// <summary> /// Called when the application is idle. This function is used to render the canvas. /// </summary> /// <param name="sender">Object that caused this event to be triggered.</param> /// <param name="e">Arguments explaining why this event occured.</param> void Application_Idle(object sender, EventArgs e) { if (Visible == false) { return; } while (NativeMethods.IsApplicationIdle == true) { GraphicsCanvas.RenderAll(); } }
/// <summary> /// Called by the base engine class when its safe to update and render the scene. /// </summary> protected override void Update() { // Ignore everything else if we are not running a game. if (_gameName == "") { return; } //System.Console.WriteLine("Collective Time("+EntityNode.CollectiveCalls+" calls): "+EntityNode.CollectiveTime); //EntityNode.CollectiveTime = 0.0f; //EntityNode.CollectiveCalls = 0; //EntityNode.CollectiveTimer.Restart(); // Time to load a map? if (_mapLoadPending == true) { // Pump out a MapFinish event. EventManager.FireEvent(new Event("map_finish", this, null)); // Allow the processes some time to be notified of closing events. //EventManager.ProcessEvents(); //ProcessManager.RunProcesses(1); // Tell the scripts that we are now loading the new map (so they can show a loadings screen). _gameScriptProcess.Process.InvokeFunction("OnLoadingBegin", true, true); // Don't want to render the scene graph as its currently being loaded >.<. bool priorSceneGraphRender = true; if (_isServer == false) { priorSceneGraphRender = _window.RenderSceneGraph; _window.RenderLoadingScreen = true; _window.RenderSceneGraph = false; } // Keep track of time :). HighPreformanceTimer loadTimer = new HighPreformanceTimer(); // Get starting memory. long startingMemory = GC.GetTotalMemory(true); if (GraphicsManager.ThreadSafe == false) { // Load the new map. DebugLogger.WriteLog("Loading map from " + _mapLoadFile + " " + (_mapLoadPassword != "" ? " with password " + _mapLoadPassword : "") + "."); LoadMapThread(); } else { // Load the new map. DebugLogger.WriteLog("Loading map from " + _mapLoadFile + " " + (_mapLoadPassword != "" ? " with password " + _mapLoadPassword : "") + "."); Thread thread = new Thread(LoadMapThread); thread.Priority = ThreadPriority.Highest; // Thread.CurrentThread.Priority = ThreadPriority.Lowest; thread.IsBackground = true; thread.Start(); // Ech, there has to be a better way than this. I hate thread safety >.>. HighPreformanceTimer timer = new HighPreformanceTimer(); while (thread != null && thread.IsAlive == true) { // Track frame stats. TrackFrameStatsBegin(); // Update the process. //timer = new HighPreformanceTimer(); //_gameScriptProcess.Run(_deltaTime); //_processProcessingDuration = (float)timer.DurationMillisecond; // Update the graphical console. if (_isServer == false) { GraphicalConsole.Update(1.0f); } // Tell the window to render if (_isServer == false) { timer.Restart(); GraphicsCanvas.RenderAll(); _window.Render(); _renderingDuration = (float)timer.DurationMillisecond; } // Update network. NetworkManager.Poll(); // Process application level events. timer.Restart(); Application.DoEvents(); _applicationProcessingDuration = (float)timer.DurationMillisecond; // Track frame stats. TrackFrameStatsFinish(); } } // Invoke OnCreate events of scripted entities. foreach (SceneNode node in _map.SceneGraph.EnumerateNodes()) { if (node != null && node is ScriptedEntityNode && node.IsPersistent == false) { ((ScriptedEntityNode)node).ScriptProcess[0].InvokeFunction("OnCreate", true, false); } } // Run the collision manager quickly so that we can sort out any unplesent problems. CollisionManager.ProcessCollisions(); //Thread.CurrentThread.Priority = ThreadPriority.Normal; // We can render again! Oh holy days! if (_isServer == false) { _window.RenderLoadingScreen = false; _window.RenderSceneGraph = priorSceneGraphRender; } // Remove any old resource from the cache that haven't been used for 5 maps :P. ResourceManager.CollectGarbage(3); // Free up some space. GC.Collect(); // And we are done! DebugLogger.WriteLog("Map loaded successfully in " + loadTimer.DurationMillisecond + "ms, " + (((GC.GetTotalMemory(false) - startingMemory) / 1024.0f) / 1024.0f) + "mb allocated during loading."); // Yay, loaded! _mapLoadPending = false; // Reset the delta time! _forcedDeltaTimeThisFrame = 1.0f; } }