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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 142 - (show annotations) (download)
Mon Jul 19 09:17:53 2010 UTC (9 years, 4 months ago) by william
File size: 9271 byte(s)
bulk commit

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using gr2lib.core.helpers;
5
6 using gr2lib.core.interfaces;
7 using System.Diagnostics;
8 using gr2lib.core.exceptions;
9 using gr2lib.core.ui.helpers;
10 using System.ComponentModel;
11 using gr2lib.core.typedefs;
12
13 namespace gr2lib.core.coretypes.implementation
14 {
15 public class Texture : ITexture
16 {
17 private GrannyRigResource _rigResource;
18
19 private FilePath _FromFileName;
20 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
29 public override string ToString()
30 {
31 return this.FromFileName.Name;
32 }
33
34 private string _ParentResourceName;
35 protected internal string ParentResourceName { get { return "Textures"; } }
36
37 public Texture()// : base("Textures")
38 {
39 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 //this.ExtendedData = new IntPtr();
48 //this.NativePointer = IntPtr.Zero;
49 this._rigResource = new GrannyRigResource();
50 }
51 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
67 internal static native.Texture AssignToStructure(Texture texture)
68 {
69 native.Texture _texture_struct = new gr2lib.core.coretypes.native.Texture();
70
71 _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
81 return _texture_struct;
82 }
83
84 internal static Texture ReadFromMemory(IntPtr pointer)
85 {
86 try
87 {
88 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 }
94 catch (Exception ex)
95 {
96 StackTrace st = new StackTrace(true);
97 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
98 Granny2ExceptionWriter.WriteToConsole(ex,st);
99 #endif
100 return default(Texture);
101 }
102 }
103
104 internal static Texture ReadFromNative(native.Texture native)
105 {
106 try
107 {
108 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 managed.Images[i] = Image.ReadFromMemory(images[i]);
128 }
129
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 managed.ExtendedData = native.ExtendedData;
143 return managed;
144 }
145 catch (Exception ex)
146 {
147 StackTrace st = new StackTrace(true);
148 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
149 Granny2ExceptionWriter.WriteToConsole(ex,st);
150 #endif
151 return default(Texture);
152 }
153 }
154
155 #region ITexture Members
156
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 public FilePath FromFileName { get { return _FromFileName; } set { _FromFileName = value; } }
196 [RefreshPropertiesAttribute(RefreshProperties.All)]
197 public int TextureType { get { return _TextureType; } set { _TextureType = value; } }
198 [RefreshPropertiesAttribute(RefreshProperties.All)]
199 public int Width { get { return _Width; } set { _Width = value; } }
200 [RefreshPropertiesAttribute(RefreshProperties.All)]
201 public int Height { get { return _Height; } set { _Height = value; } }
202 [RefreshPropertiesAttribute(RefreshProperties.All)]
203 public int Encoding { get { return _Encoding; } set { _Encoding = value; } }
204 [RefreshPropertiesAttribute(RefreshProperties.All)]
205 public int SubFormat { get { return _SubFormat; } set { _SubFormat = value; } }
206 [RefreshPropertiesAttribute(RefreshProperties.All)]
207 [Browsable(true)]
208 public Layout Layout { get { return _Layout; } set { _Layout = value; } }
209 [RefreshPropertiesAttribute(RefreshProperties.All)]
210 public List<Image> Images { get { return _Images; } set { _Images = value; } }
211 #endregion
212
213 #region INativePointer Members
214 private IntPtr _NativePointer;
215 public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
216 #endregion
217
218 #region IExtendedData Members
219 private IntPtr _ExtendedData;
220 public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }
221 #endregion
222
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 }
237 }

  ViewVC Help
Powered by ViewVC 1.1.22