/* INITIALIZATION PROCEDURES */ /// <summary> /// Create a projection matrix based on the current window size. /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> public void CreateProjection(Tesseract gameEngine) { Size windowSize = gameEngine.WindowManager.Window.ClientSize; Projection = Matrix.PerspectiveFovRH((float)Math.PI / 3F, windowSize.Width / (float)windowSize.Height, 0.5F, 1000F); }
/// <summary> /// Stores a reference to the map manager and initializes the projection matrix. /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> public void PostInitialize(Tesseract gameEngine) { // Initialize some data fields MapManager = gameEngine.MapManager; Clock = gameEngine.Clock; LastTime = (float)Clock.Elapsed.TotalSeconds; // Initialize the chunk position & start constructing chunks around the starting position ChunkPosition = Vector3.Zero; ThreadManager.NewMapTask(MapManager.TransitionChunk, ChunkPosition); // Setup the initial camera position & target parameters Position.X = SettingsManager.ChunkSize[1] / 2; Position.Z = -SettingsManager.ChunkSize[0] / 2; Position.Y = MapManager.GetHeight(Position.X, Position.Z) + 3; Target = new Vector3(Position.X, Position.Y, Position.Z - 1); // Create the projection matrix CreateProjection(gameEngine); }
/* CONSTRUCTOR METHOD */ /// <summary> /// /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> /// <param name="dpi">The dots per inch (DPI) of the monitor that the window is displayed on.</param> public DeviceManager(Tesseract gameEngine, float dpi = 96.0f) { gameEngine.OnInitialize += Initialize; Dpi = dpi; }
/* CONSTRUCTOR METHOD */ /// <summary> /// Constructs a camera to view the world being rendered. /// <para>Also registers callback functions for the size change, post-rendering, and post-initialization events.</para> /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> public Camera(Tesseract gameEngine) { // Store a reference to the CPU thread manager ThreadManager = gameEngine.ThreadManager; // Register event handlers gameEngine.OnSizeChange += CreateProjection; gameEngine.OnInitialized += PostInitialize; gameEngine.OnRendered += Translate; }
/// <summary> /// Create resources that depend on the size of the render target. /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> protected virtual void CreateSizeResources(Tesseract gameEngine) { }
/* DEVICE MANAGER METHODS */ /// <summary> /// Initialize resources and trigger an initialization event for all registered listeners /// </summary> public void Initialize(Tesseract gameEngine) { // Release any pre-exisitng references ReleaseResources(); // Retrieve the Direct3D 11.1 device using (var device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel)) { Device3D = ToDispose(device.QueryInterface<Device1>()); } // Get the Direct3D 11.1 context Context3D = ToDispose(Device3D.ImmediateContext.QueryInterface<DeviceContext1>()); // Create the remaining references Factory2D = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded)); FactoryDW = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)); FactoryWIC = ToDispose(new SharpDX.WIC.ImagingFactory2()); // Create the Direct2D device using (var device = Device3D.QueryInterface<SharpDX.DXGI.Device>()) { Device2D = ToDispose(new SharpDX.Direct2D1.Device(Factory2D, device)); } // Create the Direct2D context Context2D = ToDispose(new SharpDX.Direct2D1.DeviceContext(Device2D, SharpDX.Direct2D1.DeviceContextOptions.None)); }
/* RESOURCE MANIPUTLATION PROCEDURES */ /// <summary> /// Binds the device-dependent resources to their respective buffers once the game engine initialization procedure is complete. /// <para>This superclass implementation automatically binds the texture sampler state and the vertex buffer.</para> /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> protected virtual void BindDeviceResources(Tesseract gameEngine) { // Store references to the now-initialized Direct3D device & device context Context3D = gameEngine.DeviceManager.Context3D; Device3D = gameEngine.DeviceManager.Device3D; // Bind the texture sampler state description TextureSamplerState = ToDispose(new SamplerState(Device3D, TextureSamplerStateDescription)); // Create and bind the vertex buffer VertexBuffer = ToDispose(Buffer.Create(Device3D, BindFlags.VertexBuffer, VertexArray)); VertexBufferBinding = new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<Vertex>(), 0); }
/// <summary> /// Initialize resources that depend on the device or device context. /// <para>This superclass implementation automatically creates the texture sampler state description.</para> /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> protected virtual void CreateDeviceResources(Tesseract gameEngine) { ReleaseResources(); TextureSamplerStateDescription = new SamplerStateDescription() { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, BorderColor = new Color4(0, 0, 0, 0), ComparisonFunction = Comparison.Never, Filter = Filter.MinMagMipLinear, MaximumAnisotropy = 16, MaximumLod = float.MaxValue, MinimumLod = 0, MipLodBias = 0f }; }
/* ABSTRACT RENDERING METHOD */ /// <summary> /// /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> public abstract void Render(Tesseract gameEngine);
/* CONSTRUCTOR METHOD */ /// <summary> /// Construct a basic renderer object and subscribe to initialization and window size change events. /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> public Renderer(Tesseract gameEngine) { gameEngine.OnInitialize += CreateDeviceResources; gameEngine.OnInitialized += BindDeviceResources; gameEngine.OnSizeChange += CreateSizeResources; gameEngine.OnRender += Render; }
/// <summary> /// Renders the frame rate data as text to the game window against a colored panel backdrop. /// </summary> /// <param name="gameEngine">A reference to the <see cref="Tesseract"/> game engine.</param> public void Render(Tesseract gameEngine) { // Set up the text rendering for the frame rate display var context = gameEngine.DeviceManager.Context2D; Camera camera = gameEngine.Camera; Rectangle textLocation = new Rectangle(Location.X, Location.Y, Location.X + LineLength, Location.Y + 5*Size); // Calculate the time elapsed since the last call float elapsedTime = (float)gameEngine.Clock.Elapsed.TotalSeconds - LastTime; float elapsedTimeMs = elapsedTime * 1000f; LastTime = (float)gameEngine.Clock.Elapsed.TotalSeconds; // Fill in values for the formatted string being displayed Text = String.Format(FormatStr, 1 / (elapsedTime), elapsedTimeMs, camera.Position.X, camera.Position.Y, camera.Position.Z, camera.Facing.X, camera.Facing.Y, camera.Facing.Z, camera.Yaw, camera.Pitch); // Create a colored panel backdrop for the text RoundedRectangle textPanel = new RoundedRectangle() { Rect = new RectangleF(Location.X - 10, Location.Y - 5, LineLength + 10, Location.Y + 5*Size + 10), RadiusX = 10f, RadiusY = 10f, }; // Render the text context.BeginDraw(); context.Transform = Matrix.Identity; context.DrawRoundedRectangle(textPanel, PanelColorBrush); context.FillRoundedRectangle(textPanel, PanelColorBrush); context.DrawText(Text, Format, textLocation, FontColorBrush); context.EndDraw(); }
public void BindDeviceResources(Tesseract gameEngine) { // Release pre-existing resources RemoveAndDispose(ref FontColorBrush); RemoveAndDispose(ref Format); RemoveAndDispose(ref PanelColorBrush); // Set up the font & debug panel visual properties PanelColor.Alpha = 0.5f; FontColorBrush = ToDispose(new SolidColorBrush(gameEngine.DeviceManager.Context2D, FontColor)); PanelColorBrush = ToDispose(new SolidColorBrush(gameEngine.DeviceManager.Context2D, PanelColor)); Format = ToDispose(new TextFormat(gameEngine.DeviceManager.FactoryDW, Font, Size) { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Center, }); gameEngine.DeviceManager.Context2D.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale; // Start the clock needed to time frame intervals LastTime = (float)gameEngine.Clock.Elapsed.TotalSeconds; }
/* CONSTRUCTOR METHOD */ /// <summary> /// Construct a 2D text renderer for displaying debug information within a rendering window. /// </summary> /// <param name="gameEngine">An instance of the <see cref="Tesseract"/> game engine.</param> public DebugPanel(Tesseract gameEngine) { gameEngine.OnInitialized += BindDeviceResources; gameEngine.OnRender += Render; }