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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 72 by william, Thu Jul 15 14:22:00 2010 UTC revision 185 by william, Fri Jul 23 23:55:52 2010 UTC
# Line 3  using System.Collections.Generic; Line 3  using System.Collections.Generic;
3  using System.Text;  using System.Text;
4  using gr2lib.core.helpers;  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    using gr2lib.core.ui.typeeditors;
13    using System.Drawing.Design;
14    
15  namespace gr2lib.core.coretypes.implementation  namespace gr2lib.core.coretypes.implementation
16  {  {
17        /// <summary>
18      public interface ITexture      /// Represents a Granny_Texture
19      {      /// </summary>
         string FromFileName { get; set; }  
         int TextureType { get; set; }  
         int Width { get; set; }  
         int Height { get; set; }  
         int Encoding { get; set; }  
         int SubFormat { get; set; }  
         Layout Layout { get; set; }  
         List<Image> Images { get; set; }  
         IntPtr ExtendedData { get; set; }  
     }  
   
20      public class Texture : ITexture      public class Texture : ITexture
21      {      {
22          private string _FromFileName;          private GrannyRigResource _rigResource;
23          private int _TextureType;  
24            private FilePath _FromFileName;
25            private NameValuePair<string, granny_texture_type> _TextureType;
26          private int _Width;          private int _Width;
27          private int _Height;          private int _Height;
28          private int _Encoding;          private NameValuePair<string, granny_texture_encoding> _Encoding;
29          private int _SubFormat;          private NameValuePair<string, granny_pixel_layout> _SubFormat;
30          private Layout _Layout;          private Layout _Layout;
31          private List<Image> _Images;          private List<Image> _Images;
         private IntPtr _ExtendedData;  
32    
33          public IntPtr NativePointer;          /// <summary>
34            /// Gets the string representation of this instance
35            /// </summary>
36            /// <returns></returns>
37            public override string ToString()
38            {
39                return this.FromFileName.Name;
40            }
41    
42            //private string _ParentResourceName;
43            /// <summary>
44            /// Get's the parent resource name
45            /// </summary>
46            protected internal string ParentResourceName { get { return "Textures"; } }
47    
48            /// <summary>
49            /// default constructor
50            /// </summary>
51            public Texture()// : base("Textures")
52            {
53                this.FromFileName = "";
54                this.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
55                this.Width = 0;
56                this.Height = 0;
57                this.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
58                this.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.GetGrannyEnumTypeString<granny_pixel_layout>(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat);
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            }
65            /// <summary>
66            /// default constructor (specifying index)
67            /// </summary>
68            /// <param name="index">the texture's index</param>
69            public Texture(int index)
70            {
71                this.FromFileName = "";
72                this.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
73                this.Width = 0;
74                this.Height = 0;
75                this.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
76                this.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.GetGrannyEnumTypeString<granny_pixel_layout>(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat);
77                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    
85          public Texture()          internal static native.Texture AssignToStructure(Texture texture)
86          {          {
87                native.Texture _texture_struct = new gr2lib.core.coretypes.native.Texture();
88    
89                //_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    
99                return _texture_struct;
100          }          }
101    
102          internal static Texture ReadFromMemory(IntPtr pointer)          internal static Texture ReadFromMemory(IntPtr pointer)
103          {          {
104                try
105                {
106              if (pointer == IntPtr.Zero) return null;              if (pointer == IntPtr.Zero) return null;
107              native.Texture native = Helpers.ReadFromMemory<native.Texture>(pointer);              native.Texture native = Helpers.ReadFromMemory<native.Texture>(pointer);
108              Texture managed = ReadFromNative(native);              Texture managed = ReadFromNative(native);
109              managed.NativePointer = pointer;              managed.NativePointer = pointer;
110              return managed;              return managed;
111                }
112                catch (Exception ex)
113                {
114                    StackTrace st = new StackTrace(true);
115                    #if  ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
116                    Granny2ExceptionWriter.WriteToConsole(ex,st);
117    #endif
118                    return default(Texture);
119                }
120          }          }
121    
122          internal static Texture ReadFromNative(native.Texture native)          internal static Texture ReadFromNative(native.Texture native)
123          {          {
124              Texture managed = new Texture();              try
             managed.FromFileName = native.FromFileName;  
             managed.TextureType = native.TextureType;  
             managed.Width = native.Width;  
             managed.Height = native.Height;  
             managed.Encoding = native.Encoding;  
             managed.SubFormat = native.SubFormat;  
             managed.Layout = Layout.ReadFromNative(native.Layout);  
             IntPtr[] images = Helpers.ReadPtrArrayFromMemory<native.Image>(native.Images);  
             int length = images.Length;  
             managed.Images = new List<Image>(length);  
             foreach (IntPtr ptr in images)  
125              {              {
126                  managed.Images.Add(new Image());                  Texture managed = new Texture();
127                    managed.FromFileName = native.FromFileName;
128                    managed.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.GetGrannyEnumTypeString<granny_texture_type>((granny_texture_type)native.TextureType), (granny_texture_type)native.TextureType);
129                    managed.Width = native.Width;
130                    managed.Height = native.Height;
131                    managed.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.GetGrannyEnumTypeString<granny_texture_encoding>((granny_texture_encoding)native.Encoding), (granny_texture_encoding)native.Encoding);
132                    managed.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.GetGrannyEnumTypeString<granny_pixel_layout>((granny_pixel_layout)native.SubFormat), (granny_pixel_layout)native.SubFormat);
133                    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                        managed.Images[i] = Image.ReadFromMemory(images[i]);  
146                    }
147    
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                    managed.ExtendedData = native.ExtendedData;
161                    return managed;
162              }              }
163              for (int i = 0; i < length; i++)              catch (Exception ex)
164              {              {
165                  managed.Images[i] = Image.ReadFromMemory(images[i]);                  StackTrace st = new StackTrace(true);
166    #if  ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
167                    Granny2ExceptionWriter.WriteToConsole(ex,st);
168    #endif
169                    return default(Texture);
170              }              }
             managed.ExtendedData = native.ExtendedData;  
             return managed;  
171          }          }
172    
173          #region ITexture Members          #region ITexture Members
174          public string FromFileName { get { return _FromFileName; } set { _FromFileName = value; } }          /// <summary>
175          public int TextureType { get { return _TextureType; } set { _TextureType = value; } }          /// Get's the texture's name
176            /// </summary>
177            [RefreshPropertiesAttribute(RefreshProperties.All)]
178            public FilePath FromFileName { get { return _FromFileName; } set { _FromFileName = value; } }
179            /// <summary>
180            /// Get's the texture's type
181            /// </summary>
182            [RefreshPropertiesAttribute(RefreshProperties.All)]
183            [Editor(typeof(UIComboValueSelector<granny_texture_type, int>), typeof(UITypeEditor))]
184            public NameValuePair<string, granny_texture_type> TextureType { get { return _TextureType; } set { _TextureType = value; } }
185            /// <summary>
186            /// Get's the texture's widh
187            /// </summary>
188            [RefreshPropertiesAttribute(RefreshProperties.All)]
189          public int Width { get { return _Width; } set { _Width = value; } }          public int Width { get { return _Width; } set { _Width = value; } }
190            /// <summary>
191            /// Get's the texture's height
192            /// </summary>
193            [RefreshPropertiesAttribute(RefreshProperties.All)]
194          public int Height { get { return _Height; } set { _Height = value; } }          public int Height { get { return _Height; } set { _Height = value; } }
195          public int Encoding { get { return _Encoding; } set { _Encoding = value; } }          /// <summary>
196          public int SubFormat { get { return _SubFormat; } set { _SubFormat = value; } }          /// Get's the texture's encoding
197            /// </summary>
198            [RefreshPropertiesAttribute(RefreshProperties.All)]
199            [Editor(typeof(UIComboValueSelector<granny_texture_encoding, int>), typeof(UITypeEditor))]
200            public NameValuePair<string, granny_texture_encoding> Encoding { get { return _Encoding; } set { _Encoding = value; } }
201            /// <summary>
202            /// Get's the texture's subformat
203            /// </summary>
204            [RefreshPropertiesAttribute(RefreshProperties.All)]
205            [Editor(typeof(UIComboValueSelector<granny_pixel_layout, int>), typeof(UITypeEditor))]
206            public NameValuePair<string, granny_pixel_layout> SubFormat { get { return _SubFormat; } set { _SubFormat = value; } }
207            /// <summary>
208            /// Get's the texture's Pixel Layout
209            /// </summary>
210            [RefreshPropertiesAttribute(RefreshProperties.All)]
211            [Browsable(true)]
212          public Layout Layout { get { return _Layout; } set { _Layout = value; } }          public Layout Layout { get { return _Layout; } set { _Layout = value; } }
213            /// <summary>
214            /// Get a List of Images associated with this texture
215            /// </summary>
216            [RefreshPropertiesAttribute(RefreshProperties.All)]
217          public List<Image> Images { get { return _Images; } set { _Images = value; } }          public List<Image> Images { get { return _Images; } set { _Images = value; } }
218            #endregion
219    
220            #region INativePointer Members
221            private IntPtr _NativePointer;
222            /// <summary>
223            /// When used in a derived class, gets the native pointer for this instance
224            /// </summary>
225            public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
226            #endregion
227    
228            #region IExtendedData Members
229            private IntPtr _ExtendedData;
230            /// <summary>
231            /// When used in a derived class, gets the ExtendedData pointer for this instance
232            /// </summary>
233          public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }          public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }
234          #endregion          #endregion
235    
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      }      }
250  }  }

Legend:
Removed from v.72  
changed lines
  Added in v.185

  ViewVC Help
Powered by ViewVC 1.1.22