/[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 191 - (hide annotations) (download)
Sat Jul 24 06:31:50 2010 UTC (9 years, 4 months ago) by william
File size: 12066 byte(s)
In coreapi:
*Add more texture methods
*Move everthing into related classes, to group related methods,etc
*Update all classes to use these grouped classes

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

  ViewVC Help
Powered by ViewVC 1.1.22