public void UpdateLoop(ulong iterations) { while (iterations-- > 0) { fishManager.Update(); foodManager.Update(); if (iterations % (60 * 60) == 0) { Console.WriteLine("[" + ID + "] Time: " + (iterations / 60 / 60)); } } fishManager.TopPerformer.Brain.WriteWeights(ID + "_" + fishManager.TopPerformer.Score + ".brain"); }
private void RenderFrame() { Input.XInputInput.Update(); if ((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.Back) == SharpDX.XInput.GamepadButtonFlags.Back) { renderForm.Close(); } if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.Start) == SharpDX.XInput.GamepadButtonFlags.Start) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.Start) != SharpDX.XInput.GamepadButtonFlags.Start)) { swapChain.GetFullscreenState(out SharpDX.Mathematics.Interop.RawBool fs, out Output o); o?.Dispose(); swapChain.SetFullscreenState(!fs, null); } fishManager.Update(); fishRenderer.Update(); foodManager.Update(); //theta = 0.0f; if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.X) == SharpDX.XInput.GamepadButtonFlags.X) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.X) != SharpDX.XInput.GamepadButtonFlags.X)) { camState = !camState; } if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadUp) == SharpDX.XInput.GamepadButtonFlags.DPadUp) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadUp) != SharpDX.XInput.GamepadButtonFlags.DPadUp)) { Wireframe = !Wireframe; } if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadLeft) == SharpDX.XInput.GamepadButtonFlags.DPadLeft) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadLeft) != SharpDX.XInput.GamepadButtonFlags.DPadLeft)) { frameSkip = frameSkip > 1 ? frameSkip - 1 : 1; } if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadRight) == SharpDX.XInput.GamepadButtonFlags.DPadRight) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadRight) != SharpDX.XInput.GamepadButtonFlags.DPadRight)) { frameSkip++; } if (!camState) { SetCamera(Matrix.LookAtLH(new Vector3((float)Math.Sin(Math.Sin(theta) * 0.1f), 0, (float)Math.Cos(Math.Sin(theta) * 0.1f)) * -1.3333f, Vector3.Zero, Vector3.Up), //Matrix.LookAtLH(Vector3.BackwardLH * 5, Vector3.Zero, Vector3.Up), //Matrix.OrthoLH(16.0f / 9.0f * 2.0f, 2.0f, 0, 10.0f)); Matrix.PerspectiveFovLH(MathUtil.DegreesToRadians(75.0f), RenderWidth / (float)RenderHeight, 0.1f, 20.0f)); } else { SetCamera(Matrix.LookAtLH(new Vector3(fishManager.FishList[0].Position * 0.75f, -1.33333f), new Vector3(fishManager.FishList[0].Position * 0.75f, 0.0f), Vector3.Up), Matrix.PerspectiveFovLH(MathUtil.DegreesToRadians(45.0f), RenderWidth / (float)RenderHeight, 0.1f, 20.0f)); } theta += (float)Math.PI / 30.0f / 60.0f * (Input.XInputInput.State[0].LeftThumbY / (float)short.MaxValue + 0.25f) * 4.0f; if (frameNumber % (ulong)frameSkip == 0 && (Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.LeftShoulder) != SharpDX.XInput.GamepadButtonFlags.LeftShoulder) { DeviceContext.Rasterizer.State = Wireframe ? RasterizerStates.Wire : RasterizerStates.Back; //DeviceContext.OutputMerger.SetRenderTargets(renderTargetView); DeviceContext.PixelShader.SetShaderResource(0, null); DeviceContext.OutputMerger.SetRenderTargets(colorTarget.DSV, colorTarget.RTV); DeviceContext.OutputMerger.DepthStencilState = DepthStencilStates.Enabled; DeviceContext.ClearRenderTargetView(renderTargetView, new Color(0.0f, 0.0f, 0.0f)); colorTarget.Clear(Color4.Lerp(new Color4(0, 0, 0.04f, 0.0f), new Color4(0.08f, 0.08f, 0.2f, 0.0f), Math.Max(0, -(float)Math.Cos(theta)))); //RenderDebugTri(); var intensity = Math.Max(0, -(float)Math.Cos(theta)); var sunDir = new Vector3((float)Math.Sin(theta), (float)Math.Cos(theta), 1.0f); Sun.Set(sunDir, new Vector3(1.0f, 1.0f, 1.0f), intensity); fishGlow.Intensity = Math.Max(0, ((float)Math.Cos(intensity * MathUtil.Pi) + 0.2f) / 1.2f); raysRenderer.Intensity = 0.02f * (1 - fishGlow.Intensity); fishGlow.Intensity *= 0.75f; decorRenderer.Render(); fishRenderer.Render(); DeviceContext.OutputMerger.SetRenderTargets(colorTarget.DSV, renderTargetView); //var pos = new Vector3(fishManager.FishList[0].Position, 0); //var np = Vector3.Transform(pos, Projection * View); var np = Vector3.Transform(sunDir * new Vector3(1, 1, 0) * -10, Projection * View); DeviceContext.OutputMerger.DepthStencilState = DepthStencilStates.Disabled; DeviceContext.Rasterizer.State = RasterizerStates.Back; raysRenderer.CentrePoint = new Vector2(-sunDir.X * RenderWidth * 10.0f, sunDir.Y * RenderHeight * 10.0f);//new Vector2((np.X / np.W / 2.0f + 0.5f) * RenderWidth, (-np.Y / np.W / 2.0f + 0.5f) * RenderHeight); raysRenderer.Render(colorTarget.SRV, 1.0f / 60.0f); fishGlow.Render(); DeviceContext.Rasterizer.State = Wireframe ? RasterizerStates.Wire : RasterizerStates.Back; DeviceContext.OutputMerger.DepthStencilState = DepthStencilStates.Enabled; foodRenderer.Render(); decorRenderer.Render2(); try { if ((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.RightShoulder) != SharpDX.XInput.GamepadButtonFlags.RightShoulder) { swapChain.Present(1, PresentFlags.None); } else { swapChain.Present(0, PresentFlags.None); } } catch (Exception e) { Console.WriteLine(Device.DeviceRemovedReason.ToString()); Console.ReadLine(); renderForm.Close(); } } frameNumber++; }