private void Render() { if (deviceLost) { // This should only become true if we're using D3D9, so we can assume the // D3D9 context is valid at this point. if (Device.TestCooperativeLevel() == global::SlimDX.Direct3D9.ResultCode.DeviceNotReset) { Device.Reset(Context9.PresentParameters); deviceLost = false; OnResetDevice(); } else { Thread.Sleep(100); return; } } try { Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightGray, 1.0f, 0); Device.BeginScene(); foreach (CollisionObject colObj in Demo.Simulation.World.CollisionObjectArray) { if (colObj is SoftBody) { if (Demo.IsDebugDrawEnabled) { continue; } Device.Material = SoftBodyMaterial; Device.SetTransform(TransformState.World, Matrix.Identity); _meshFactory.RenderSoftBody(colObj as SoftBody); } else { if ("Ground".Equals(colObj.UserObject)) { Device.Material = GroundMaterial; } else if (colObj.ActivationState == ActivationState.ActiveTag) { Device.Material = ActiveMaterial; } else { Device.Material = PassiveMaterial; } _meshFactory.Render(colObj); } } if (Demo.IsDebugDrawEnabled) { (Demo.Simulation.World.DebugDrawer as PhysicsDebugDraw).DrawDebugWorld((DynamicsWorld)Demo.Simulation.World); } Info.OnRender((float)Demo.FramesPerSecond); Device.EndScene(); Device.Present(); } catch (global::SlimDX.Direct3D9.Direct3D9Exception e) { if (e.ResultCode == global::SlimDX.Direct3D9.ResultCode.DeviceLost) { OnLostDevice(); deviceLost = true; } else { throw; } } }
public void Run() { if (_loaded) { Window.Show(); ViewerSetup viewer = new ViewerSetup(); _lastTime = Stopwatch.GetTimestamp(); while (_running) { Application.DoEvents(); if (!_deactive) { long currentTime = Stopwatch.GetTimestamp(); long elapsed = (currentTime - _lastTime) / Stopwatch.Frequency; _lastTime = currentTime; Input.Update(); if (Input.GetKeyPress(Key.Escape)) { Quit(); } if (CurrentState != null) { CurrentState.RequestViewer(out viewer); } _stateChanged = false; if (CurrentState != null) { CurrentState.Update(elapsed); } if (_stateChanged) { continue; } try { _device.Clear(viewer.ClearFlags, Color.White, 1.0f, 0); } catch (Direct3D9Exception dex) { } if (_device.BeginScene().IsSuccess) { if (CurrentState != null) { CurrentState.Render(); } _device.EndScene(); _device.Present(); if (++_currentBackBuffer == _setup.TotalBackBuffers + 1) { _currentBackBuffer = 0; } } } } Release(); Application.Exit(); } }