/// <summary> /// Load graphics resources content. /// </summary> public override void LoadContent() { int block1 = -1; MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyMinerGame::LoadContent", ref block1); base.LoadContent(); DefaultSurface = MyMinerGame.Static.GraphicsDevice.GetRenderTarget(0); DefaultSurface.DebugName = "DefaultSurface"; DefaultDepth = MyMinerGame.Static.GraphicsDevice.DepthStencilSurface; DefaultDepth.DebugName = "DefaultDepth"; MyMwcLog.WriteLine("MyMinerGame.LoadContent() - START"); MyMwcLog.IncreaseIndent(); MyVideoModeManager.UpdateScreenSize(); System.Drawing.Font systemfont = new System.Drawing.Font("Tahoma", 12f, FontStyle.Regular); m_debugFont = new SharpDX.Direct3D9.Font(GraphicsDevice, systemfont); // GUI MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyGuiManager.LoadContent()"); MyGuiManager.LoadContent(); // Models MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("MyModels.LoadContent()"); MyModels.LoadContent(); // Render MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("MyRender.LoadContent();"); MyRender.LoadContent(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("MyDebugDraw.LoadContent();"); MyDebugDraw.LoadContent(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("MyDebugDrawCachedLines.LoadContent()"); MyDebugDrawCachedLines.LoadContent(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("MySunGlare.LoadContent()"); MySunGlare.LoadContent(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); MyMwcLog.DecreaseIndent(); MyMwcLog.WriteLine("MyMinerGame.LoadContent() - END"); GC.Collect(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(block1); }
/// <summary> /// Called when graphics resources need to be unloaded. Override this method to unload any game-specific graphics resources. /// </summary> public override void UnloadContent() { MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyMinerGame::UnloadContent"); MyMwcLog.WriteLine("MyMinerGame.UnloadContent() - START"); MyMwcLog.IncreaseIndent(); if (m_debugFont != null) { m_debugFont.Dispose(); m_debugFont = null; } if (DefaultSurface != null) { DefaultSurface.Dispose(); DefaultSurface = null; } if (DefaultDepth != null) { DefaultDepth.Dispose(); DefaultDepth = null; } // GUI MyGuiManager.UnloadContent(); MyRender.UnloadContent(); MyTextureManager.UnloadContent(); MyModels.UnloadContent(); // Global content //Content.Unload(); // Render MySunGlare.UnloadContent(); MyDebugDrawCachedLines.UnloadContent(); MyDebugDraw.UnloadContent(); //MyRender.UnloadContent(); //it is unloaded in gui gameplay screen MyMwcLog.DecreaseIndent(); MyMwcLog.WriteLine("MyMinerGame.UnloadContent() - END"); GraphicsDevice.SetStreamSource(0, null, 0, 0); GraphicsDevice.Indices = null; GraphicsDevice.VertexDeclaration = null; GraphicsDevice.PixelShader = null; GraphicsDevice.VertexShader = null; for (int i = 0; i < 16; i++) { GraphicsDevice.SetTexture(i, null); } base.UnloadContent(); GC.Collect(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); }
/// <summary> /// precise collision detection based on data from broadphase. Collision detection runs multithreaded. /// </summary> protected void NearPhaseUpdate(float timeStep) { m_InteractionsList = m_BroadPhase.GetRBElementInteractionList(); if (MyFakes.DEBUG_DRAW_COLLIDING_ENTITIES) { bool logToConsole = false; bool debugDraw = true; bool drawText = true; float textSize = 0.7f; Vector2 screenPos = new Vector2(100, 100); if (logToConsole) { Console.WriteLine("-----------------------------"); } Vector4 color = new Vector4(1, 0, 0, 1); foreach (MyRBElementInteraction i in m_InteractionsList) { object obj = i.GetRigidBody1().m_UserData; if (obj is MyPhysicsBody) { MyPhysicsBody rb = (MyPhysicsBody)obj; string name = rb.Entity.ToString() + " " + i.GetRigidBody1().GUID; if (debugDraw) { MyDebugDrawCachedLines.AddAABB(rb.Entity.WorldAABB, Color.Red); } if (drawText) { System.Text.StringBuilder sb0 = new System.Text.StringBuilder(100); sb0.Append(ParseLastElement(i.ToString())); MyDebugDraw.TextBatch.AddText(screenPos, sb0, Color.Red, textSize); screenPos.Y += 20 * textSize; System.Text.StringBuilder sb = new System.Text.StringBuilder(100); sb.Append(ParseLastElement(name)); MyDebugDraw.TextBatch.AddText(screenPos, sb, Color.YellowGreen, textSize); screenPos.Y += 20 * textSize; } if (logToConsole) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(name); Console.ForegroundColor = ConsoleColor.Gray; } } object obj2 = i.GetRigidBody2().m_UserData; if (obj2 is MyPhysicsBody) { MyPhysicsBody rb2 = (MyPhysicsBody)obj2; string name2 = rb2.Entity.ToString() + " " + i.GetRigidBody2().GUID; if (debugDraw) { MyDebugDrawCachedLines.AddAABB(rb2.Entity.WorldAABB, Color.Red); } if (drawText) { System.Text.StringBuilder sb2 = new System.Text.StringBuilder(100); sb2.Append(ParseLastElement(name2)); MyDebugDraw.TextBatch.AddText(screenPos, sb2, Color.LightBlue, textSize); screenPos.Y += 20 * textSize; } if (logToConsole) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(name2); Console.ForegroundColor = ConsoleColor.Gray; } } if (drawText) { screenPos.Y += 10 * textSize; } } } MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("Processing interactions"); #if RENDER_PROFILING foreach (MyRBElementInteraction i in m_InteractionsList) { i.DoWork(); } //Parallel.For(0, m_InteractionsList.Count, m_InteractionAction, 1); #else /*foreach (MyRBElementInteraction i in m_InteractionsList) * { * i.DoWork(); * } */ //Peaks //System.Threading.Tasks.Parallel.For(0, m_InteractionsList.Count, m_InteractionAction); Parallel.For(0, m_InteractionsList.Count, m_InteractionAction, 1); #endif /* * Dictionary<MinerWars.AppCode.Game.Entities.MyEntity, HashSet<MinerWars.AppCode.Game.Entities.MyEntity>> test = new Dictionary<Game.Managers.EntityManager.Entities.MyEntity, HashSet<Game.Managers.EntityManager.Entities.MyEntity>>(); * * foreach (MyRBElementInteraction i in m_InteractionsList) * { * MinerWars.AppCode.Game.Entities.MyEntity e1 = ((MyPhysicsBody)i.GetRigidBody1().m_UserData).Entity; * MinerWars.AppCode.Game.Entities.MyEntity e2 = ((MyPhysicsBody)i.GetRigidBody2().m_UserData).Entity; * * if (test.ContainsKey(e1)) * { * if (!test[e1].Add(e2)) * { * } * } * else * { * HashSet<Game.Managers.EntityManager.Entities.MyEntity> hs = new HashSet<Game.Managers.EntityManager.Entities.MyEntity>(); * hs.Add(e2); * test.Add(e1, hs); * } * if (test.ContainsKey(e2)) * { * if (!test[e2].Add(e1)) * { * } * } * else * { * HashSet<Game.Managers.EntityManager.Entities.MyEntity> hs = new HashSet<Game.Managers.EntityManager.Entities.MyEntity>(); * hs.Add(e1); * test.Add(e2, hs); * * } * } */ MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); m_InteractionsList.Clear(); }
/// <summary> /// Debug draw of this physics object. /// </summary> public void DebugDraw() { const float alpha = 0.3f; if (!Enabled) { return; } foreach (var primitive in this.rigidBody.GetRBElementList()) { MyRBElementType type = primitive.GetElementType(); switch (type) { case MyRBElementType.ET_BOX: { var box = (MyRBBoxElement)primitive; MyDebugDraw.DrawHiresBoxWireframe( Matrix.CreateScale(box.Size) * box.GetGlobalTransformation(), Color.Green.ToVector3(), alpha); } break; case MyRBElementType.ET_SPHERE: { var sphere = (MyRBSphereElement)primitive; MyDebugDraw.DrawSphereWireframe( Matrix.CreateScale(sphere.Radius) * sphere.GetGlobalTransformation(), Color.Green.ToVector3(), alpha); } break; case MyRBElementType.ET_TRIANGLEMESH: { var triMesh = (MyRBTriangleMeshElement)primitive; var model = triMesh.Model; Matrix transformMatrix = this.rigidBody.Matrix; MyDebugDrawCachedLines.Clear(); // This is just a reserve const int numberOfAddTrianglesInLoop = 3; int triangleIndex = 0; while (true) { //bool finished = triangleIndex >= mesh.GetNumTriangles(); bool finished = triangleIndex >= model.GetTrianglesCount(); if ((MyDebugDrawCachedLines.IsFull(-numberOfAddTrianglesInLoop)) || (finished)) { MyDebugDrawCachedLines.DrawLines(); MyDebugDrawCachedLines.Clear(); } if (finished) { break; } MyTriangleVertexIndices triangle = model.Triangles[triangleIndex]; // We now transform the triangleVertexes into world space (we could keep leave the mesh alone // but at this point 3 vector transforms is probably not a major slow down) Vector3 triVec0 = model.GetVertex(triangle.I0); Vector3 triVec1 = model.GetVertex(triangle.I2); Vector3 triVec2 = model.GetVertex(triangle.I1); // Move triangle into world space Vector3.Transform(ref triVec0, ref transformMatrix, out triVec0); Vector3.Transform(ref triVec1, ref transformMatrix, out triVec1); Vector3.Transform(ref triVec2, ref transformMatrix, out triVec2); MyDebugDrawCachedLines.AddLine(triVec0, triVec1, Color.Green, Color.Green); MyDebugDrawCachedLines.AddLine(triVec1, triVec2, Color.Green, Color.Green); MyDebugDrawCachedLines.AddLine(triVec2, triVec0, Color.Green, Color.Green); /* * MyTriangle_Vertexes tv = new MyTriangle_Vertexes(); * tv.Vertex0 = triVec0; * tv.Vertex1 = triVec1; * tv.Vertex2 = triVec2; * Vector3 calculatedTriangleNormal = MyUtils.GetNormalVectorFromTriangle(ref tv); * Vector3 center = (triVec0 + triVec1 + triVec2)/3.0f; * MyDebugDrawCachedLines.AddLine(center, center + calculatedTriangleNormal * 5, Color.Red, Color.Red); */ //MyDebugDraw.AddDrawTriangle(triVec0, triVec1, triVec2, new Color(0,0.8f, 0, 0.1f)); triangleIndex++; } } break; } } }