Example #1
0
 void DisposeReferences()
 {
     if (Device != null)
     {
         if (state_old != null)
         {
             state_old.Dispose();
         }
         if (vb_cube != null)
         {
             vb_cube.Dispose();
         }
         if (vert_decl_nocolor != null)
         {
             vert_decl_nocolor.Dispose();
         }
         foreach (var font in fontCache)
         {
             font.Value.Dispose();
         }
         Device.Dispose();
         Device            = null;
         vert_decl_nocolor = null;
         vb_cube           = null;
         state_old         = null;
         fontCache.Clear();
     }
 }
Example #2
0
        public override void ResourceUnload()
        {
            base.ResourceUnload();

            if (stateBlock != null)
            {
                stateBlock.Dispose();
                stateBlock = null;
            }

            if (lineBuffer != null)
            {
                lineBuffer.Dispose();
                lineBuffer = null;
            }
        }
Example #3
0
        /// <summary>
        /// Called when the device is lost
        /// </summary>
        public virtual void OnLostDevice()
        {
            if (state != null)
            {
                state.Dispose();
            }

            state = null;
        }
Example #4
0
        /// <summary>
        /// Clear any resources that need to be lost
        /// </summary>
        public void OnLostDevice()
        {
            foreach (FontNode fn in fontCache)
            {
                if ((fn.Font != null) && (!fn.Font.Disposed))
                {
                    fn.Font.OnLostDevice();
                }
            }

            if (dialogSprite != null)
            {
                dialogSprite.OnLostDevice();
            }

            if (dialogStateBlock != null)
            {
                dialogStateBlock.Dispose();
                dialogStateBlock = null;
            }
        }
        /// <summary>
        /// Unhooks the <see cref="OmegaGUI"/> system from the <see cref="Engine"/> and disposes its internal DirectX resources
        /// </summary>
        public void Dispose()
        {
            if (Disposed)
            {
                return;
            }

            // Unhook device events
            Engine.DeviceLost  -= OnLostDevice;
            Engine.DeviceReset -= OnResetDevice;

            // Dispose DirectX objects
            Sprite.Dispose();
            StateBlock.Dispose();

            // Clear caches
            _textureCache.ForEach(node => node.Texture.Dispose());
            _fontCache.ForEach(node => node.Font.Dispose());

            GC.SuppressFinalize(this);
            Disposed = true;
        }
        /// <summary>
        /// Called when the device has been lost.
        /// </summary>
        public void OnLostDevice()
        {
            if (!Engine.IsDisposed)
            {
                foreach (FontNode fn in _fontCache)
                {
                    if ((fn.Font != null) && (!fn.Font.Disposed))
                    {
                        fn.Font.OnLostDevice();
                    }
                }

                if (Sprite != null)
                {
                    Sprite.OnLostDevice();
                }

                if (StateBlock != null)
                {
                    StateBlock.Dispose();
                    StateBlock = null;
                }
            }
        }
 public void Dispose()
 {
     m_stateBlock.Dispose();
     m_stateBlock = null;
 }
Example #8
0
        private unsafe void ImGuiRenderDraw(DrawData *drawData)
        {
            if (drawData == null)
            {
                return;
            }
            var io = ImGui.GetIO();

            if (io.DisplaySize.X <= 0.0f || io.DisplaySize.Y <= 0.0f)
            {
                return;
            }
            var st = new StateBlock(device, StateBlockType.All);
            var vp = new Viewport();

            vp.X        = vp.Y = 0;
            vp.Width    = (int)io.DisplaySize.X;
            vp.Height   = (int)io.DisplaySize.Y;
            vp.MinDepth = 0.0f;
            vp.MaxDepth = 1.0f;

            device.Viewport     = vp;
            device.PixelShader  = null;
            device.VertexShader = null;
            device.SetRenderState(RenderState.CullMode, Cull.None);
            device.SetRenderState(RenderState.Lighting, false);
            device.SetRenderState(RenderState.ZEnable, false);
            device.SetRenderState(RenderState.AlphaBlendEnable, true);
            device.SetRenderState(RenderState.AlphaTestEnable, false);
            device.SetRenderState(RenderState.BlendOperation, BlendOperation.Add);
            device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
            device.SetRenderState(RenderState.DestinationBlend, Blend.BothInverseSourceAlpha);
            device.SetRenderState(RenderState.ScissorTestEnable, true);
            device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Modulate);
            device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
            device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.Diffuse);
            device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate);
            device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture);
            device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.Diffuse);
            device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
            device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear);
            // Setup orthographic projection matrix
            {
                const float L            = 0.5f;
                var         R            = io.DisplaySize.X + 0.5f;
                const float T            = 0.5f;
                var         B            = io.DisplaySize.Y + 0.5f;
                RawMatrix   mat_identity = new Matrix(1.0f, 0.0f, 0.0f, 0.0f,
                                                      0.0f, 1.0f, 0.0f, 0.0f,
                                                      0.0f, 0.0f, 1.0f, 0.0f,
                                                      0.0f, 0.0f, 0.0f, 1.0f);
                RawMatrix mat_projection = new Matrix(
                    2.0f / (R - L), 0.0f, 0.0f, 0.0f,
                    0.0f, 2.0f / (T - B), 0.0f, 0.0f,
                    0.0f, 0.0f, 0.5f, 0.0f,
                    (L + R) / (L - R), (T + B) / (B - T), 0.5f, 1.0f);
                device.SetTransform(TransformState.World, ref mat_identity);
                device.SetTransform(TransformState.View, ref mat_identity);
                device.SetTransform(TransformState.Projection, ref mat_projection);
            }
            using (device.VertexDeclaration = new VertexDeclaration(device, GuiVertex.VertexElements))
            {
                for (var n = 0; n < drawData->CmdListsCount; n++)
                {
                    var cmdList    = drawData->CmdLists[n];
                    var vtx_buffer = (DrawVert *)cmdList->VtxBuffer.Data;
                    var idx_buffer = (ushort *)cmdList->IdxBuffer.Data;

                    var myCustomVertices = new GuiVertex[cmdList->VtxBuffer.Size];

                    for (var i = 0; i < myCustomVertices.Length; i++)
                    {
                        var cl = (vtx_buffer[i].col & 0xFF00FF00) | ((vtx_buffer[i].col & 0xFF0000) >> 16) |
                                 ((vtx_buffer[i].col & 0xFF) << 16);
                        myCustomVertices[i] =
                            new GuiVertex(vtx_buffer[i].pos.X, vtx_buffer[i].pos.Y, vtx_buffer[i].uv.X,
                                          vtx_buffer[i].uv.Y, cl);
                    }

                    for (var i = 0; i < cmdList->CmdBuffer.Size; i++)
                    {
                        var pcmd = &((DrawCmd *)cmdList->CmdBuffer.Data)[i];

                        if (pcmd->UserCallback != IntPtr.Zero)
                        {
                            throw new NotImplementedException();
                        }
                        //Trace.WriteLine(pcmd->TextureId.ToString());
                        device.SetTexture(0, new Texture(pcmd->TextureId));
                        device.ScissorRect = new RectangleF((int)pcmd->ClipRect.X,
                                                            (int)pcmd->ClipRect.Y,
                                                            (int)(pcmd->ClipRect.Z - pcmd->ClipRect.X),
                                                            (int)(pcmd->ClipRect.W - pcmd->ClipRect.Y));
                        var indices = new ushort[pcmd->ElemCount];
                        for (var j = 0; j < indices.Length; j++)
                        {
                            indices[j] = idx_buffer[j];
                        }

                        device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, myCustomVertices.Length,
                                                         (int)(pcmd->ElemCount / 3), indices, Format.Index16, myCustomVertices);
                        idx_buffer += pcmd->ElemCount;
                    }
                }
            }

            st.Apply();
            st.Dispose();
        }