/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/Texture.cs
ViewVC logotype

Annotation of /trunk/gr2lib/core/coretypes/implementation/Texture.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 149 - (hide annotations) (download)
Tue Jul 20 08:11:51 2010 UTC (9 years, 4 months ago) by william
File size: 9291 byte(s)
bulk commit

1 william 72 using System;
2     using System.Collections.Generic;
3     using System.Text;
4     using gr2lib.core.helpers;
5    
6 william 79 using gr2lib.core.interfaces;
7 william 95 using System.Diagnostics;
8 william 106 using gr2lib.core.exceptions;
9 william 141 using gr2lib.core.ui.helpers;
10     using System.ComponentModel;
11     using gr2lib.core.typedefs;
12 william 79
13 william 72 namespace gr2lib.core.coretypes.implementation
14     {
15 william 142 public class Texture : ITexture
16 william 72 {
17 william 141 private GrannyRigResource _rigResource;
18    
19 william 75 private FilePath _FromFileName;
20 william 72 private int _TextureType;
21     private int _Width;
22     private int _Height;
23     private int _Encoding;
24     private int _SubFormat;
25     private Layout _Layout;
26     private List<Image> _Images;
27    
28 william 82
29 william 141 public override string ToString()
30     {
31     return this.FromFileName.Name;
32     }
33 william 72
34 william 149 //private string _ParentResourceName;
35 william 142 protected internal string ParentResourceName { get { return "Textures"; } }
36    
37     public Texture()// : base("Textures")
38 william 72 {
39 william 73 this.FromFileName = "";
40     this.TextureType = 0;
41     this.Width = 0;
42     this.Height = 0;
43     this.Encoding = 0;
44     this.SubFormat = 0;
45     this.Layout = new Layout();
46     this.Images = new List<Image>();
47 william 83 //this.ExtendedData = new IntPtr();
48     //this.NativePointer = IntPtr.Zero;
49 william 141 this._rigResource = new GrannyRigResource();
50 william 72 }
51 william 141 public Texture(int index)
52     {
53     this.FromFileName = "";
54     this.TextureType = 0;
55     this.Width = 0;
56     this.Height = 0;
57     this.Encoding = 0;
58     this.SubFormat = 0;
59     this.Layout = new Layout();
60     this.Images = new List<Image>();
61     //this.ExtendedData = new IntPtr();
62     //this.NativePointer = IntPtr.Zero;
63     this._rigResource = new GrannyRigResource();
64     this.Index = index;
65     }
66 william 142
67     internal static native.Texture AssignToStructure(Texture texture)
68     {
69     native.Texture _texture_struct = new gr2lib.core.coretypes.native.Texture();
70    
71 william 149 //_texture_struct.Encoding = texture.Encoding;
72     //_texture_struct.ExtendedData = texture.ExtendedData;
73     //_texture_struct.FromFileName = texture.FromFileName.FullPath;
74     //_texture_struct.Height = texture.Height;
75     ////_texture_struct.Images = texture.Images;
76     //_texture_struct.Layout = Layout.AssignToStructure(texture.Layout);
77     //_texture_struct.SubFormat = texture.SubFormat;
78     //_texture_struct.TextureType = texture.TextureType;
79     //_texture_struct.Width = texture.Width;
80 william 142
81     return _texture_struct;
82     }
83    
84 william 72 internal static Texture ReadFromMemory(IntPtr pointer)
85     {
86 william 86 try
87     {
88 william 72 if (pointer == IntPtr.Zero) return null;
89     native.Texture native = Helpers.ReadFromMemory<native.Texture>(pointer);
90     Texture managed = ReadFromNative(native);
91     managed.NativePointer = pointer;
92     return managed;
93 william 86 }
94 william 106 catch (Exception ex)
95 william 86 {
96 william 95 StackTrace st = new StackTrace(true);
97 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
98 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
99 william 102 #endif
100 william 86 return default(Texture);
101     }
102 william 72 }
103    
104     internal static Texture ReadFromNative(native.Texture native)
105     {
106 william 86 try
107 william 72 {
108 william 86 Texture managed = new Texture();
109     managed.FromFileName = native.FromFileName;
110     managed.TextureType = native.TextureType;
111     managed.Width = native.Width;
112     managed.Height = native.Height;
113     managed.Encoding = native.Encoding;
114     managed.SubFormat = native.SubFormat;
115     managed.Layout = Layout.ReadFromNative(native.Layout);
116     IntPtr[] images = Helpers.ReadPtrArrayFromMemory<native.Image>(native.Images);
117     int length = images.Length;
118     managed.Images = new List<Image>(length);
119     foreach (IntPtr ptr in images)
120     {
121     Image _tmp = new Image();
122     _tmp.NativePointer = ptr;
123     managed.Images.Add(_tmp);
124     }
125     for (int i = 0; i < length; i++)
126     {
127 william 142 managed.Images[i] = Image.ReadFromMemory(images[i]);
128 william 86 }
129 william 142
130     for (int i = 0; i < managed.Images.Count; i++)
131     {
132     for (int j = 0; j < managed.Images[i].MIPLevels.Count; j++)
133     {
134     for (int k = 0; k < managed.Images[i].MIPLevels[j].Pixels.Count; k++)
135     {
136     managed.Images[i].MIPLevels[j].Pixels[k].ReferenceTexture = managed;
137     }
138     }
139     }
140    
141    
142 william 86 managed.ExtendedData = native.ExtendedData;
143     return managed;
144 william 72 }
145 william 106 catch (Exception ex)
146 william 72 {
147 william 95 StackTrace st = new StackTrace(true);
148 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
149 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
150 william 102 #endif
151 william 86 return default(Texture);
152 william 72 }
153     }
154    
155     #region ITexture Members
156 william 141
157     [RefreshPropertiesAttribute(RefreshProperties.All)]
158     public string TextureTypeFormat
159     {
160     get
161     {
162     string texture_type = "Unknown Texture Type";
163    
164     foreach (int val in Enum.GetValues(typeof(granny_texture_type)))
165     {
166     if (val == this.TextureType)
167     {
168     texture_type = Enum.GetName(typeof(granny_texture_type),val);
169     break;
170     }
171     }
172     return texture_type;
173     }
174     }
175     [RefreshPropertiesAttribute(RefreshProperties.All)]
176     public string EncodingFormat
177     {
178     get
179     {
180     string encodingformat = "Unknown Encoding Format";
181    
182     foreach (int val in Enum.GetValues(typeof(granny_texture_encoding)))
183     {
184     if (val == this.Encoding)
185     {
186     encodingformat = Enum.GetName(typeof(granny_texture_encoding), val);
187     break;
188     }
189     }
190     return encodingformat;
191     }
192     }
193    
194     [RefreshPropertiesAttribute(RefreshProperties.All)]
195 william 75 public FilePath FromFileName { get { return _FromFileName; } set { _FromFileName = value; } }
196 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
197 william 72 public int TextureType { get { return _TextureType; } set { _TextureType = value; } }
198 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
199 william 72 public int Width { get { return _Width; } set { _Width = value; } }
200 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
201 william 72 public int Height { get { return _Height; } set { _Height = value; } }
202 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
203 william 72 public int Encoding { get { return _Encoding; } set { _Encoding = value; } }
204 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
205 william 72 public int SubFormat { get { return _SubFormat; } set { _SubFormat = value; } }
206 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
207     [Browsable(true)]
208 william 72 public Layout Layout { get { return _Layout; } set { _Layout = value; } }
209 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
210 william 72 public List<Image> Images { get { return _Images; } set { _Images = value; } }
211     #endregion
212 william 79
213     #region INativePointer Members
214     private IntPtr _NativePointer;
215     public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
216     #endregion
217 william 82
218     #region IExtendedData Members
219     private IntPtr _ExtendedData;
220     public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }
221     #endregion
222 william 141
223     #region IResource Members
224     private int _index;
225     [RefreshPropertiesAttribute(RefreshProperties.All)]
226     internal int Index { get { return _index; } set { _index = value; } }
227     #endregion
228     #region IEmbeddedResource Members
229     [RefreshPropertiesAttribute(RefreshProperties.All)]
230     internal string IndexName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockIndexFormat; } }
231     [RefreshPropertiesAttribute(RefreshProperties.All)]
232     internal string FullName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockFormat; } }
233     [RefreshPropertiesAttribute(RefreshProperties.All)]
234     internal string Name { get { return this.FromFileName; } set { this.FromFileName = value; } }
235     #endregion
236 william 72 }
237     }

  ViewVC Help
Powered by ViewVC 1.1.22