/// <summary> /// Creates a canvas that can be rendered to by the current driver. /// </summary> /// <param name="control">Control to render this canvases graphics to.</param> /// <returns>New renderable canvas.</returns> public static GraphicsCanvas CreateCanvas(Control control, GraphicsFlags flags, CanvasRenderHandler handler) { GraphicsCanvas canvas = new GraphicsCanvas(control, flags, handler); ClearRenderState(); return(canvas); }
/// <summary> /// Initializes a new instance of this class. /// </summary> /// <param name="width">Width of the resolution of this window.</param> /// <param name="height">Height of the resolution of this window.</param> /// <param name="flags">Decides how the window is created.</param> public GameWindow(int width, int height, GraphicsFlags flags) { InitializeComponent(); _canvas = new GraphicsCanvas(this, 0, new CanvasRenderHandler(Render)); GraphicsManager.RenderTarget = _canvas; Reset(width, height, flags); }
/// <summary> /// Setups up a new DirectX9Canvas instance with the given specifications. /// </summary> /// <param name="driver">DirectX9 driver to associated with this canvas.</param> /// <param name="control">Control to render this canvases graphics to.</param> /// <param name="flags">BitMask of GraphicsFlags flags, specifies how the canvas is setup.</param> public Direct3D9Canvas(Direct3D9Driver driver, Control control, GraphicsFlags flags) { // Store the driver for future use _control = control; _dx9Driver = driver; _flags = flags; // Setup windows components and show it. _control.Resize += new System.EventHandler(ResizeEvent); _control.Disposed += new EventHandler(Disposed); }
/// <summary> /// Sets up a new instance of this class with the given values. /// </summary> /// <param name="renderControl">Control to render to.</param> /// <param name="flags">Flags to setup this control with.</param> public GraphicsCanvas(Control renderControl, GraphicsFlags flags, CanvasRenderHandler handler) { _renderControl = renderControl; _graphicsCanvas = GraphicsManager.Driver.CreateCanvas(renderControl, flags); if (handler != null) { _renderDelegate += handler; // Fix this so its removed when canvas is destroyed!. } _driver = GraphicsManager.Driver; GraphicsManager.RenderTarget = this; }
/// <summary> /// Resets this window to take the give effect to the given settings. /// </summary> /// <param name="width">Width of the resolution of this window.</param> /// <param name="height">Height of the resolution of this window.</param> /// <param name="flags">Decides how the window is created.</param> public void Reset(int width, int height, GraphicsFlags flags) { _flags = flags; bool fullScreen = (flags & GraphicsFlags.FullScreen) != 0; FormBorderStyle = fullScreen ? FormBorderStyle.None : FormBorderStyle.FixedDialog; ClientSize = new Size(fullScreen ? NativeMethods.GetSystemMetrics(0) : width, fullScreen ? NativeMethods.GetSystemMetrics(1) : height); Location = new Point(fullScreen ? 0 : (NativeMethods.GetSystemMetrics(0) - ClientSize.Width) / 2, fullScreen ? 0 : (NativeMethods.GetSystemMetrics(1) - ClientSize.Height) / 2); GraphicsManager.SetResolution(width, height, Engine.GlobalInstance.KeepAspectRatio); //_renderTarget = new Graphics.Image((int)(GraphicsManager.Resolution[0] * GraphicsManager.ResolutionScale[0]), (int)(GraphicsManager.Resolution[1] * GraphicsManager.ResolutionScale[1]), ImageFlags.Dynamic); }
/// <summary> /// Sets up the DirectX9 devices used for rendering. /// </summary> /// <param name="renderControl">Control that default swap chain should render to.</param> /// <param name="width">Width of control to render on, this is used to create the correct backbuffer size.</param> /// <param name="height">Height of control to render on, this is used to create the correct backbuffer size.</param> /// <param name="flags">A bitmask of GraphicsFlags, specifying how the device should be setup.</param> /// <returns>True if device was setup successfull, else false.</returns> public bool InitializeDevice(Control renderControl, int width, int height, GraphicsFlags flags) { return true; }
/// <summary> /// Creates a new canvas capable of being rendered to by this driver.. /// </summary> /// <param name="control">Control this canvas should render to.</param> /// <param name="flags">Flags describing how canvas should be created.</param> /// <returns>New canvas being capable of being rendered to.</returns> IRenderTarget IGraphicsDriver.CreateCanvas(Control control, GraphicsFlags flags) { return new OpenGLCanvas(); }
/// <summary> /// Sets up the DirectX9 devices used for rendering. /// </summary> /// <param name="renderControl">Control that default swap chain should render to.</param> /// <param name="width">Width of control to render on, this is used to create the correct backbuffer size.</param> /// <param name="height">Height of control to render on, this is used to create the correct backbuffer size.</param> /// <param name="flags">A bitmask of GraphicsFlags, specifying how the device should be setup.</param> /// <returns>True if device was setup successfull, else false.</returns> public bool InitializeDevice(Control renderControl, int width, int height, GraphicsFlags flags) { if (width == 0 || height == 0) return false; CreateFlags createFlags; Caps caps; // Catch any odd errors DirectX may give off. try { // Get device capabilitys caps = Manager.GetDeviceCaps(0, DeviceType.Hardware); // Try and get the fastest game experiance possible createFlags = (caps.DeviceCaps.SupportsHardwareTransformAndLight == true) ? CreateFlags.HardwareVertexProcessing : CreateFlags.SoftwareVertexProcessing; if (caps.DeviceCaps.SupportsPureDevice == true) createFlags |= CreateFlags.PureDevice; // Decide on window setup parameters _presentParameters = CreatePresentParameters(renderControl, width, height, flags); // Create device if it dosent exist, else reset it if (_dx9Device == null) { // Last but not least create out new DirectX9 device with all the settings // we just worked out. _dx9Device = new Device(0, DeviceType.Hardware, renderControl, createFlags, _presentParameters); // Add lost and resizing handlers _dx9Device.DeviceLost += new System.EventHandler(DeviceLost); _dx9Device.DeviceReset += new System.EventHandler(DeviceReset); _dx9Device.DeviceResizing += new System.ComponentModel.CancelEventHandler(DeviceResizing); } else { int result; _dx9Device.CheckCooperativeLevel(out result); int count = 0; while (true) { if (result == (int)ResultCode.DeviceLost) Thread.Sleep(500); else if (result == (int)ResultCode.DeviceNotReset) break; else break; if (count++ >= 10) break; } // Reset the device. DebugLogger.WriteLog("Attempting to reset Direct3D9 device..."); _dx9Device.Reset(_presentParameters); } } catch (DirectXException) { _dx9Device = null; return false; } return true; }
/// <summary> /// Creates a new canvas capable of being rendered to by this driver.. /// </summary> /// <param name="control">Control this canvas should render to.</param> /// <param name="flags">Flags describing how canvas should be created.</param> /// <returns>New canvas being capable of being rendered to.</returns> IRenderTarget IGraphicsDriver.CreateCanvas(Control control, GraphicsFlags flags) { // Create a new canvas capable o being rendered to. Direct3D9Canvas canvas = new Direct3D9Canvas(this, control, flags); // Gets the rendering device and attach to the current window if ((_dx9Device == null || _dx9Device.Disposed == true) && InitializeDevice(control, ((IRenderTarget)canvas).Width, ((IRenderTarget)canvas).Height, flags) == false) throw new Exception("An error occured while attempting to initialize the Direct3D9 device."); // Set the render target to this canvas. SetRenderTarget(canvas); return canvas; }
/// <summary> /// Creates a canvas that can be rendered to by the current driver. /// </summary> /// <param name="control">Control to render this canvases graphics to.</param> /// <returns>New renderable canvas.</returns> public static GraphicsCanvas CreateCanvas(Control control, GraphicsFlags flags, CanvasRenderHandler handler) { GraphicsCanvas canvas = new GraphicsCanvas(control, flags, handler); ClearRenderState(); return canvas; }
/// <summary> /// Sets up the DirectX9 devices used for rendering. /// </summary> /// <param name="renderControl">Control that default swap chain should render to.</param> /// <param name="width">Width of control to render on, this is used to create the correct backbuffer size.</param> /// <param name="height">Height of control to render on, this is used to create the correct backbuffer size.</param> /// <param name="flags">A bitmask of GraphicsFlags, specifying how the device should be setup.</param> /// <returns>True if device was setup successfull, else false.</returns> public bool InitializeDevice(Control renderControl, int width, int height, GraphicsFlags flags) { return(true); }
/// <summary> /// Creates a new canvas capable of being rendered to by this driver.. /// </summary> /// <param name="control">Control this canvas should render to.</param> /// <param name="flags">Flags describing how canvas should be created.</param> /// <returns>New canvas being capable of being rendered to.</returns> IRenderTarget IGraphicsDriver.CreateCanvas(Control control, GraphicsFlags flags) { return(new OpenGLCanvas()); }
/// <summary> /// Sets up a new instance of this class with the given values. /// </summary> /// <param name="renderControl">Control to render to.</param> /// <param name="flags">Flags to setup this control with.</param> public GraphicsCanvas(Control renderControl, GraphicsFlags flags, CanvasRenderHandler handler) { _renderControl = renderControl; _graphicsCanvas = GraphicsManager.Driver.CreateCanvas(renderControl, flags); if (handler != null) _renderDelegate += handler; // Fix this so its removed when canvas is destroyed!. _driver = GraphicsManager.Driver; GraphicsManager.RenderTarget = this; }
/// <summary> /// Handy macro function that works out the appropriate present parameters for the given values. /// </summary> /// <param name="renderControl">Control to render to.</param> /// <param name="width">Width of rendering target.</param> /// <param name="height">Height of rendering target.</param> /// <param name="flags">Flags specifying how to create parameters.</param> /// <returns>New present parameters created using the values given.</returns> public static PresentParameters CreatePresentParameters(Control renderControl, int width, int height, GraphicsFlags flags) { PresentParameters presentParameters = new PresentParameters(); presentParameters.DeviceWindow = renderControl; presentParameters.Windowed = (flags & GraphicsFlags.FullScreen) == 0; presentParameters.AutoDepthStencilFormat = DepthFormat.D16; presentParameters.EnableAutoDepthStencil = true; // Speeds it up a bit in debug mode at the expense of a few graphical effects. //#if DEBUG presentParameters.SwapEffect = SwapEffect.Discard; presentParameters.PresentationInterval = PresentInterval.Immediate; //#else // _presentParameters.SwapEffect = SwapEffect.Flip; // _presentParameters.PresentationInterval = PresentInterval.One; //#endif // If we are in fullscreen the setup the screen to the current resolution if ((flags & GraphicsFlags.FullScreen) != 0) { presentParameters.BackBufferWidth = width; presentParameters.BackBufferHeight = height; presentParameters.BackBufferFormat = Manager.Adapters[0].CurrentDisplayMode.Format; presentParameters.BackBufferCount = 1; presentParameters.FullScreenRefreshRateInHz = Manager.Adapters[0].CurrentDisplayMode.RefreshRate; } return presentParameters; }