fna-workbench

fna-workbench Git Source Tree


Root/src/Graphics/GraphicsResource.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
using System.Collections.Generic;
#endregion
 
namespace Microsoft.Xna.Framework.Graphics
{  
    public abstract class GraphicsResource : IDisposable
    {
        #region Public Properties
 
        public GraphicsDevice GraphicsDevice
        {
            get
            {
                return graphicsDevice;
            }
            internal set
            {
                if (graphicsDevice == value)
                {
                    return;
                }
 
                /* VertexDeclaration objects can be bound to
                 * multiple GraphicsDevice objects during their
                 * lifetime. But only one GraphicsDevice should
                 * retain ownership.
                 */
                if (graphicsDevice != null)
                {
                    graphicsDevice.RemoveResourceReference(selfReference);
                    selfReference = null;
                }
 
                graphicsDevice = value;
 
                selfReference = new WeakReference(this);
                graphicsDevice.AddResourceReference(selfReference);
            }
        }
 
        public bool IsDisposed
        {
            get;
            private set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        public Object Tag
        {
            get;
            set;
        }
 
        #endregion
 
        #region Private Variables
 
        private WeakReference selfReference;
 
        private GraphicsDevice graphicsDevice;
 
        #endregion
 
        #region Disposing Event
 
        public event EventHandler<EventArgs> Disposing;
 
        #endregion
 
        #region Internal Constructor and Deconstructor
 
        internal GraphicsResource()
        {
        }
 
        ~GraphicsResource()
        {
            // Pass false so the managed objects are not released
            // FIXME: This can lock up your game from the GC! -flibit
            // Dispose(false);
        }
 
        #endregion
 
        #region Public Dispose Method
 
        public void Dispose()
        {
            // Dispose of managed objects as well
            Dispose(true);
            // Since we have been manually disposed, do not call the finalizer on this object
            GC.SuppressFinalize(this);
        }
 
        #endregion
 
        #region Public Methods
 
        public override string ToString()
        {
            return string.IsNullOrEmpty(Name) ? base.ToString() : Name;
        }
 
        #endregion
 
        #region Internal Methods
 
        /// <summary>
        /// Called before the device is reset. Allows graphics resources to
        /// invalidate their state so they can be recreated after the device reset.
        /// Warning: This may be called after a call to Dispose() up until
        /// the resource is garbage collected.
        /// </summary>
        internal protected virtual void GraphicsDeviceResetting()
        {
        }
 
        #endregion
 
        #region Protected Dispose Method
 
        /// <summary>
        /// The method that derived classes should override to implement disposing of
        /// managed and native resources.
        /// </summary>
        /// <param name="disposing">True if managed objects should be disposed.</param>
        /// <remarks>
        /// Native resources should always be released regardless of the value of the
        /// disposing parameter.
        /// </remarks>
        protected virtual void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                // Do not trigger the event if called from the finalizer
                if (disposing && Disposing != null)
                {
                    Disposing(this, EventArgs.Empty);
                }
 
                // Remove from the list of graphics resources
                if (graphicsDevice != null)
                {
                    graphicsDevice.RemoveResourceReference(selfReference);
                }
 
                selfReference = null;
                graphicsDevice = null;
                IsDisposed = true;
            }
        }
 
        #endregion
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.13761s using 11 queries.