public bool Initialize() { // Initialize the system configuration. if (Configuration == null) { Configuration = new SystemConfiguration(); } // Initialize windows api. InitializeWindows(); if (Input == null) { Input = new InputClass(); if (!Input.Initialize(Configuration, MainForm.Handle)) { return(false); } } if (Graphics == null) { Graphics = new GraphicsClass(); if (!Graphics.Initialize(Configuration, MainForm.Handle)) { return(false); } } // Create the sound object Sound = new WaveSound("sound01.wav"); // Initialize the sound object. if (!Sound.Initialize(MainForm.Handle)) { MessageBox.Show("Could not initialize Direct Sound", "Error", MessageBoxButtons.OK); return(false); } // Create and initialize the FPS object. FPS = new FPS(); FPS.Initialize(); // Create and initialize the CPU. CPU = new CPU(); CPU.Initialize(); // Create and initialize Timer. Timer = new Timer(); if (!Timer.Initialize()) { MessageBox.Show("Could not initialize Timer object", "Error", MessageBoxButtons.OK); return(false); } // Create the position object. Position = new Position(); return(true); }
public bool Initialize(SystemConfiguration configuration, IntPtr windowHandle) { try { #region Environment Configuration // Store the vsync setting. VerticalSyncEnabled = SystemConfiguration.VerticalSyncEnabled; // Create a DirectX graphics interface factory. var factory = new Factory(); // Use the factory to create an adapter for the primary graphics interface (video card). var adapter = factory.GetAdapter(0); // Get the primary adapter output (monitor). var monitor = adapter.GetOutput(0); // Get modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor). var modes = monitor.GetDisplayModeList(Format.R8G8B8A8_UNorm, DisplayModeEnumerationFlags.Interlaced); // Now go through all the display modes and find the one that matches the screen width and height. // When a match is found store the the refresh rate for that monitor, if vertical sync is enabled. // Otherwise we use maximum refresh rate. var rational = new Rational(0, 1); if (VerticalSyncEnabled) { foreach (var mode in modes) { if (mode.Width == configuration.Width && mode.Height == configuration.Height) { rational = new Rational(mode.RefreshRate.Numerator, mode.RefreshRate.Denominator); break; } } } // Get the adapter (video card) description. var adapterDescription = adapter.Description; // Store the dedicated video card memory in megabytes. VideoCardMemory = adapterDescription.DedicatedVideoMemory >> 10 >> 10; // Convert the name of the video card to a character array and store it. VideoCardDescription = adapterDescription.Description; // Release the adapter output. monitor.Dispose(); // Release the adapter. adapter.Dispose(); // Release the factory. factory.Dispose(); #endregion #region Initialize swap chain and d3d device // Initialize the swap chain description. var swapChainDesc = new SwapChainDescription() { // Set to a single back buffer. BufferCount = 1, // Set the width and height of the back buffer. ModeDescription = new ModeDescription(configuration.Width, configuration.Height, rational, Format.R8G8B8A8_UNorm), // Set the usage of the back buffer. Usage = Usage.RenderTargetOutput, // Set the handle for the window to render to. OutputHandle = windowHandle, // Turn multisampling off. SampleDescription = new SampleDescription(1, 0), // Set to full screen or windowed mode. IsWindowed = !SystemConfiguration.FullScreen, // Don't set the advanced flags. Flags = SwapChainFlags.None, // Discard the back buffer content after presenting. SwapEffect = SwapEffect.Discard }; // Create the swap chain, Direct3D device, and Direct3D device context. Device device; SwapChain swapChain; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain); Device = device; SwapChain = swapChain; DeviceContext = device.ImmediateContext; #endregion #region Initialize buffers // Get the pointer to the back buffer. var backBuffer = Texture2D.FromSwapChain<Texture2D>(SwapChain, 0); // Create the render target view with the back buffer pointer. RenderTargetView = new RenderTargetView(device, backBuffer); // Release pointer to the back buffer as we no longer need it. backBuffer.Dispose(); // Initialize and set up the description of the depth buffer. var depthBufferDesc = new Texture2DDescription() { Width = configuration.Width, Height = configuration.Height, MipLevels = 1, ArraySize = 1, Format = Format.D24_UNorm_S8_UInt, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; // Create the texture for the depth buffer using the filled out description. DepthStencilBuffer = new Texture2D(device, depthBufferDesc); #endregion #region Initialize Depth Enabled Stencil // Initialize and set up the description of the stencil state. var depthStencilDesc = new DepthStencilStateDescription() { IsDepthEnabled = true, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less, IsStencilEnabled = true, StencilReadMask = 0xFF, StencilWriteMask = 0xFF, // Stencil operation if pixel front-facing. FrontFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Increment, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always }, // Stencil operation if pixel is back-facing. BackFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Decrement, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always } }; // Create the depth stencil state. DepthStencilState = new DepthStencilState(Device, depthStencilDesc); #endregion #region Initialize Output Merger // Set the depth stencil state. DeviceContext.OutputMerger.SetDepthStencilState(DepthStencilState, 1); // Initialize and set up the depth stencil view. var depthStencilViewDesc = new DepthStencilViewDescription() { Format = Format.D24_UNorm_S8_UInt, Dimension = DepthStencilViewDimension.Texture2D, Texture2D = new DepthStencilViewDescription.Texture2DResource() { MipSlice = 0 } }; // Create the depth stencil view. DepthStencilView = new DepthStencilView(Device, DepthStencilBuffer, depthStencilViewDesc); // Bind the render target view and depth stencil buffer to the output render pipeline. DeviceContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView); #endregion #region Initialize Raster State // Setup the raster description which will determine how and what polygon will be drawn. var rasterDesc = new RasterizerStateDescription() { IsAntialiasedLineEnabled = false, CullMode = CullMode.Back, DepthBias = 0, DepthBiasClamp = .0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsFrontCounterClockwise = false, IsMultisampleEnabled = false, IsScissorEnabled = false, SlopeScaledDepthBias = .0f }; // Create the rasterizer state from the description we just filled out. RasterState = new RasterizerState(Device, rasterDesc); #endregion #region Initialize Rasterizer // Now set the rasterizer state. DeviceContext.Rasterizer.State = RasterState; // Setup and create the viewport for rendering. DeviceContext.Rasterizer.SetViewport(0, 0, configuration.Width, configuration.Height, 0, 1); #endregion #region Initialize matrices // Setup and create the projection matrix. ProjectionMatrix = Matrix.PerspectiveFovLH((float)(Math.PI / 4f), ((float)configuration.Width / configuration.Height), SystemConfiguration.ScreenNear, SystemConfiguration.ScreenDepth); // Initialize the world matrix to the identity matrix. WorldMatrix = Matrix.Identity; // Create an orthographic projection matrix for 2D rendering. OrthoMatrix = Matrix.OrthoLH(configuration.Width, configuration.Height, SystemConfiguration.ScreenNear, SystemConfiguration.ScreenDepth); #endregion #region Initialize Depth Disabled Stencil // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. // The difference is that DepthEnable is set to false. // All other parameters are the same as the other depth stencil state. var depthDisabledStencilDesc = new DepthStencilStateDescription() { IsDepthEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less, IsStencilEnabled = true, StencilReadMask = 0xFF, StencilWriteMask = 0xFF, // Stencil operation if pixel front-facing. FrontFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Increment, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always }, // Stencil operation if pixel is back-facing. BackFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Decrement, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always } }; // Create the depth stencil state. DepthDisabledStencilState = new DepthStencilState(Device, depthDisabledStencilDesc); #endregion #region Initialize Blend States // Create an alpha enabled blend state description. var blendStateDesc = new BlendStateDescription(); blendStateDesc.RenderTarget[0].IsBlendEnabled = true; blendStateDesc.RenderTarget[0].SourceBlend = BlendOption.One; blendStateDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; blendStateDesc.RenderTarget[0].BlendOperation = BlendOperation.Add; blendStateDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One; blendStateDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero; blendStateDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add; blendStateDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; // Create the blend state using the description. AlphaEnableBlendingState = new BlendState(device, blendStateDesc); // Modify the description to create an disabled blend state description. blendStateDesc.RenderTarget[0].IsBlendEnabled = false; // Create the blend state using the description. AlphaDisableBlendingState = new BlendState(device, blendStateDesc); #endregion return true; } catch (Exception) { return false; } }
internal bool Initialize(SystemConfiguration configuration, IntPtr windowsHandle) { // Screen the screen size which will be used for positioning the mouse cursor. _ScreenWidth = configuration.Width; _ScreenHeight = configuration.Height; // Initialize the location of the mouse on the screen. _MouseX = 0; _MouseY = 0; // Initialize the main direct input interface. _DirectInput = new DirectInput(); // Initialize the direct interface for the keyboard. _Keyboard = new Keyboard(_DirectInput); _Keyboard.Properties.BufferSize = 256; // Set the cooperative level of the keyboard to not share with other programs. _Keyboard.SetCooperativeLevel(windowsHandle, CooperativeLevel.Foreground | CooperativeLevel.Exclusive); // Now acquire the keyboard. if (_Keyboard.Acquire().Failure) return false; // Initialize the direct interface for the mouse. _Mouse = new Mouse(_DirectInput); _Mouse.Properties.AxisMode = DeviceAxisMode.Relative; // Set the cooperative level of the mouse to share with other programs. _Mouse.SetCooperativeLevel(windowsHandle, CooperativeLevel.Foreground | CooperativeLevel.NonExclusive); // Now acquire the mouse. if (_Mouse.Acquire().Failure) return false; return true; }
public bool Initialize(SystemConfiguration configuration, IntPtr windowHandle) { try { // Create the Direct3D object. D3D = new DX11(); // Initialize the Direct3D object. if (!D3D.Initialize(configuration, windowHandle)) { MessageBox.Show("Could not initialize Direct3D", "Error", MessageBoxButtons.OK); return false; } // Create the camera object Camera = new Camera(); // Initialize a base view matrix the camera for 2D user interface rendering. Camera.SetPosition(0, 0, -1); Camera.Render(); var baseViewMatrix = Camera.ViewMatrix; // Create the text object. Text = new Text(); if (!Text.Initialize(D3D.Device, D3D.DeviceContext, windowHandle, configuration.Width, configuration.Height, baseViewMatrix)) { MessageBox.Show("Could not initialize the text object", "Error", MessageBoxButtons.OK); return false; } // Create the model class. Model = new Model(); // Initialize the model object. if (!Model.Initialize(D3D.Device, "sphere.txt", new[] { "stone01.dds", "dirt01.dds" })) { MessageBox.Show("Could not initialize the model object", "Error", MessageBoxButtons.OK); return false; } // Create the light shader object. MultiTextureLightShader = new MultiTextureLightShader(); // Initialize the light shader object. if (!MultiTextureLightShader.Initialize(D3D.Device, windowHandle)) { MessageBox.Show("Could not initialize the light shader", "Error", MessageBoxButtons.OK); return false; } // Create the light object. Light = new Light(); // Initialize the light object. Light.SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f); Light.SetDiffuseColor(1, 0, 0, 1f); Light.SetDirection(1, 0, 1); Light.SetSpecularColor(0, 1, 1, 1); Light.SetSpecularPower(32); // Create the model list object. ModelList = new ModelList(); // Initialize the model list object. if (!ModelList.Initialize(25)) { MessageBox.Show("Could not initialize the model list object", "Error", MessageBoxButtons.OK); return false; } // Create the frustum object. Frustum = new Frustum(); return true; } catch (Exception ex) { MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'"); return false; } }
public bool Initialize() { // Initialize the system configuration. if(Configuration == null) Configuration = new SystemConfiguration(); // Initialize windows api. InitializeWindows(); if (Input == null) { Input = new InputClass(); if (!Input.Initialize(Configuration, MainForm.Handle)) return false; } if (Graphics == null) { Graphics = new GraphicsClass(); if (!Graphics.Initialize(Configuration, MainForm.Handle)) return false; } // Create the sound object Sound = new WaveSound("sound01.wav"); // Initialize the sound object. if (!Sound.Initialize(MainForm.Handle)) { MessageBox.Show("Could not initialize Direct Sound", "Error", MessageBoxButtons.OK); return false; } // Create and initialize the FPS object. FPS = new FPS(); FPS.Initialize(); // Create and initialize the CPU. CPU = new CPU(); CPU.Initialize(); // Create and initialize Timer. Timer = new Timer(); if (!Timer.Initialize()) { MessageBox.Show("Could not initialize Timer object", "Error", MessageBoxButtons.OK); return false; } // Create the position object. Position = new Position(); return true; }