/[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 196 - (hide annotations) (download)
Thu Jul 29 07:47:26 2010 UTC (9 years, 4 months ago) by william
File size: 12446 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 160 using gr2lib.core.ui.typeeditors;
13     using System.Drawing.Design;
14 william 79
15 william 72 namespace gr2lib.core.coretypes.implementation
16     {
17 william 185 /// <summary>
18     /// Represents a Granny_Texture
19     /// </summary>
20 william 196 [Editor(typeof(UITextureEditor), typeof(UITypeEditor))]
21 william 142 public class Texture : ITexture
22 william 72 {
23 william 141 private GrannyRigResource _rigResource;
24    
25 william 75 private FilePath _FromFileName;
26 william 160 private NameValuePair<string, granny_texture_type> _TextureType;
27 william 72 private int _Width;
28     private int _Height;
29 william 160 private NameValuePair<string, granny_texture_encoding> _Encoding;
30 william 166 private NameValuePair<string, granny_pixel_layout> _SubFormat;
31 william 72 private Layout _Layout;
32     private List<Image> _Images;
33 william 196 //private bool _IsTexture;
34 william 185 /// <summary>
35     /// Gets the string representation of this instance
36     /// </summary>
37     /// <returns></returns>
38 william 141 public override string ToString()
39     {
40     return this.FromFileName.Name;
41     }
42 william 72
43 william 149 //private string _ParentResourceName;
44 william 185 /// <summary>
45     /// Get's the parent resource name
46     /// </summary>
47 william 142 protected internal string ParentResourceName { get { return "Textures"; } }
48    
49 william 185 /// <summary>
50     /// default constructor
51     /// </summary>
52 william 142 public Texture()// : base("Textures")
53 william 72 {
54 william 73 this.FromFileName = "";
55 william 191 this.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
56 william 73 this.Width = 0;
57     this.Height = 0;
58 william 191 this.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
59     this.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat);
60 william 73 this.Layout = new Layout();
61     this.Images = new List<Image>();
62 william 196 //this.IsTexture = true;
63 william 83 //this.ExtendedData = new IntPtr();
64     //this.NativePointer = IntPtr.Zero;
65 william 141 this._rigResource = new GrannyRigResource();
66 william 72 }
67 william 185 /// <summary>
68     /// default constructor (specifying index)
69     /// </summary>
70     /// <param name="index">the texture's index</param>
71 william 141 public Texture(int index)
72     {
73     this.FromFileName = "";
74 william 191 this.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
75 william 141 this.Width = 0;
76     this.Height = 0;
77 william 191 this.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
78     this.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat);
79 william 141 this.Layout = new Layout();
80     this.Images = new List<Image>();
81 william 196 //this.IsTexture = true;
82 william 141 //this.ExtendedData = new IntPtr();
83     //this.NativePointer = IntPtr.Zero;
84     this._rigResource = new GrannyRigResource();
85     this.Index = index;
86     }
87 william 142
88     internal static native.Texture AssignToStructure(Texture texture)
89     {
90     native.Texture _texture_struct = new gr2lib.core.coretypes.native.Texture();
91    
92 william 149 //_texture_struct.Encoding = texture.Encoding;
93     //_texture_struct.ExtendedData = texture.ExtendedData;
94     //_texture_struct.FromFileName = texture.FromFileName.FullPath;
95     //_texture_struct.Height = texture.Height;
96     ////_texture_struct.Images = texture.Images;
97     //_texture_struct.Layout = Layout.AssignToStructure(texture.Layout);
98     //_texture_struct.SubFormat = texture.SubFormat;
99     //_texture_struct.TextureType = texture.TextureType;
100     //_texture_struct.Width = texture.Width;
101 william 142
102     return _texture_struct;
103     }
104    
105 william 72 internal static Texture ReadFromMemory(IntPtr pointer)
106     {
107 william 86 try
108     {
109 william 72 if (pointer == IntPtr.Zero) return null;
110     native.Texture native = Helpers.ReadFromMemory<native.Texture>(pointer);
111     Texture managed = ReadFromNative(native);
112     managed.NativePointer = pointer;
113     return managed;
114 william 86 }
115 william 106 catch (Exception ex)
116 william 86 {
117 william 95 StackTrace st = new StackTrace(true);
118 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
119 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
120 william 102 #endif
121 william 86 return default(Texture);
122     }
123 william 72 }
124    
125     internal static Texture ReadFromNative(native.Texture native)
126     {
127 william 86 try
128 william 72 {
129 william 86 Texture managed = new Texture();
130     managed.FromFileName = native.FromFileName;
131 william 191 managed.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>((granny_texture_type)native.TextureType), (granny_texture_type)native.TextureType);
132 william 86 managed.Width = native.Width;
133     managed.Height = native.Height;
134 william 191 managed.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>((granny_texture_encoding)native.Encoding), (granny_texture_encoding)native.Encoding);
135     managed.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>((granny_pixel_layout)native.SubFormat), (granny_pixel_layout)native.SubFormat);
136 william 86 managed.Layout = Layout.ReadFromNative(native.Layout);
137     IntPtr[] images = Helpers.ReadPtrArrayFromMemory<native.Image>(native.Images);
138     int length = images.Length;
139     managed.Images = new List<Image>(length);
140     foreach (IntPtr ptr in images)
141     {
142     Image _tmp = new Image();
143     _tmp.NativePointer = ptr;
144     managed.Images.Add(_tmp);
145     }
146     for (int i = 0; i < length; i++)
147     {
148 william 142 managed.Images[i] = Image.ReadFromMemory(images[i]);
149 william 86 }
150 william 142
151     for (int i = 0; i < managed.Images.Count; i++)
152     {
153     for (int j = 0; j < managed.Images[i].MIPLevels.Count; j++)
154     {
155     for (int k = 0; k < managed.Images[i].MIPLevels[j].Pixels.Count; k++)
156     {
157     managed.Images[i].MIPLevels[j].Pixels[k].ReferenceTexture = managed;
158     }
159     }
160     }
161    
162    
163 william 86 managed.ExtendedData = native.ExtendedData;
164     return managed;
165 william 72 }
166 william 106 catch (Exception ex)
167 william 72 {
168 william 95 StackTrace st = new StackTrace(true);
169 william 102 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
170 william 106 Granny2ExceptionWriter.WriteToConsole(ex,st);
171 william 102 #endif
172 william 86 return default(Texture);
173 william 72 }
174     }
175    
176     #region ITexture Members
177 william 185 /// <summary>
178     /// Get's the texture's name
179     /// </summary>
180 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
181 william 75 public FilePath FromFileName { get { return _FromFileName; } set { _FromFileName = value; } }
182 william 185 /// <summary>
183     /// Get's the texture's type
184     /// </summary>
185 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
186 william 160 [Editor(typeof(UIComboValueSelector<granny_texture_type, int>), typeof(UITypeEditor))]
187     public NameValuePair<string, granny_texture_type> TextureType { get { return _TextureType; } set { _TextureType = value; } }
188 william 185 /// <summary>
189     /// Get's the texture's widh
190     /// </summary>
191 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
192 william 72 public int Width { get { return _Width; } set { _Width = value; } }
193 william 185 /// <summary>
194     /// Get's the texture's height
195     /// </summary>
196 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
197 william 72 public int Height { get { return _Height; } set { _Height = value; } }
198 william 185 /// <summary>
199     /// Get's the texture's encoding
200     /// </summary>
201 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
202 william 160 [Editor(typeof(UIComboValueSelector<granny_texture_encoding, int>), typeof(UITypeEditor))]
203     public NameValuePair<string, granny_texture_encoding> Encoding { get { return _Encoding; } set { _Encoding = value; } }
204 william 185 /// <summary>
205     /// Get's the texture's subformat
206     /// </summary>
207 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
208 william 166 [Editor(typeof(UIComboValueSelector<granny_pixel_layout, int>), typeof(UITypeEditor))]
209     public NameValuePair<string, granny_pixel_layout> SubFormat { get { return _SubFormat; } set { _SubFormat = value; } }
210 william 185 /// <summary>
211     /// Get's the texture's Pixel Layout
212     /// </summary>
213 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
214     [Browsable(true)]
215 william 72 public Layout Layout { get { return _Layout; } set { _Layout = value; } }
216 william 185 /// <summary>
217     /// Get a List of Images associated with this texture
218     /// </summary>
219 william 141 [RefreshPropertiesAttribute(RefreshProperties.All)]
220 william 72 public List<Image> Images { get { return _Images; } set { _Images = value; } }
221 william 196
222    
223     ///// <summary>
224     ///// Indicates if this instance is a texture or not
225     ///// </summary>
226     //public bool IsTexture { get { return _IsTexture; } set { _IsTexture = value; } }
227 william 72 #endregion
228 william 79
229     #region INativePointer Members
230     private IntPtr _NativePointer;
231 william 181 /// <summary>
232     /// When used in a derived class, gets the native pointer for this instance
233     /// </summary>
234 william 79 public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
235     #endregion
236 william 82
237     #region IExtendedData Members
238     private IntPtr _ExtendedData;
239 william 181 /// <summary>
240     /// When used in a derived class, gets the ExtendedData pointer for this instance
241     /// </summary>
242 william 82 public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }
243     #endregion
244 william 141
245     #region IResource Members
246     private int _index;
247     [RefreshPropertiesAttribute(RefreshProperties.All)]
248     internal int Index { get { return _index; } set { _index = value; } }
249     #endregion
250     #region IEmbeddedResource Members
251     [RefreshPropertiesAttribute(RefreshProperties.All)]
252     internal string IndexName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockIndexFormat; } }
253     [RefreshPropertiesAttribute(RefreshProperties.All)]
254     internal string FullName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockFormat; } }
255     [RefreshPropertiesAttribute(RefreshProperties.All)]
256     internal string Name { get { return this.FromFileName; } set { this.FromFileName = value; } }
257     #endregion
258 william 72 }
259     }

  ViewVC Help
Powered by ViewVC 1.1.22