[cogl/wip/cogl-sharp: 27/30] cogl-sharp: Write the Texture() constructor loading files



commit f7f8bf64fe044b52796ef7b7f32f66830a41afd0
Author: Damien Lespiau <damien lespiau intel com>
Date:   Sat Oct 6 02:51:24 2012 +0100

    cogl-sharp: Write the Texture() constructor loading files
    
    To write this one, a few things have to be introduced:
      - Exception handling from CoglError
      - TextureFlags bit field
      - Marshalling C# strings to utf8 strings

 cogl-sharp/Exception.cs    |   82 ++++++++++++++++++++++++++++++++++++++++++++
 cogl-sharp/Makefile.am     |    5 ++-
 cogl-sharp/Marshaller.cs   |   72 ++++++++++++++++++++++++++++++++++++++
 cogl-sharp/Texture.cs      |   22 ++++++++++++
 cogl-sharp/TextureFlags.cs |   14 +++++++
 cogl-sharp/parse-gir.py    |    1 +
 6 files changed, 195 insertions(+), 1 deletions(-)
---
diff --git a/cogl-sharp/Exception.cs b/cogl-sharp/Exception.cs
new file mode 100644
index 0000000..ddf8e7d
--- /dev/null
+++ b/cogl-sharp/Exception.cs
@@ -0,0 +1,82 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2012 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *   Damien Lespiau <damien lespiau intel com>
+ */
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Cogl
+{
+    [StructLayout(LayoutKind.Sequential)]
+    internal struct Error
+    {
+        public uint domain;
+        public int code;
+        public IntPtr message;
+    }
+
+    public class Exception : System.Exception
+    {
+        IntPtr error;
+
+        public Exception(IntPtr error) : base()
+        {
+            this.error = error;
+        }
+
+        public int Code
+        {
+            get
+            {
+                Error err = (Error)Marshal.PtrToStructure(error, typeof(Error));
+                return err.code;
+            }
+        }
+
+        public uint Domain
+        {
+            get
+            {
+                Error err = (Error)Marshal.PtrToStructure(error, typeof(Error));
+                return err.domain;
+            }
+        }
+
+        public override string Message
+        {
+            get
+            {
+                Error err = (Error)Marshal.PtrToStructure(error, typeof(Error));
+                return Marshaller.Utf8PtrToString(err.message);
+            }
+        }
+
+        [DllImport("cogl2.dll")]
+        static extern void cogl_error_free(IntPtr error);
+        ~Exception()
+        {
+            cogl_error_free(error);
+        }
+    }
+}
diff --git a/cogl-sharp/Makefile.am b/cogl-sharp/Makefile.am
index 0a686c3..48073ba 100644
--- a/cogl-sharp/Makefile.am
+++ b/cogl-sharp/Makefile.am
@@ -13,8 +13,10 @@ sources =			\
 	Color.cs		\
 	ColorMask.cs		\
 	Context.cs		\
+	Exception.cs		\
 	FrameBuffer.cs		\
 	_FrameBuffer.cs		\
+	Marshaller.cs		\
 	Matrix.cs		\
 	Object.cs		\
 	OnScreen.cs		\
@@ -27,6 +29,7 @@ sources =			\
 	PixelFormat.cs		\
 	Texture.cs		\
 	_Texture.cs		\
+	TextureFlags.cs		\
 	TextureType.cs		\
 	VerticesMode.cs		\
 	Winding.cs		\
@@ -35,7 +38,7 @@ sources =			\
 
 cogl2-sharp.dll: $(sources)
 	@rm -f cogl2-sharp.dll.mdb
-	$(V_MCS)$(MCS) $(CSFLAGS) @$(srcdir)/cogl2-sharp.rsp -debug $(sources)
+	$(V_MCS)$(MCS) $(CSFLAGS) @$(srcdir)/cogl2-sharp.rsp -debug -unsafe $(sources)
 
 cogl2-sharp.dll.mdb: cogl2-sharp.dll
 
diff --git a/cogl-sharp/Marshaller.cs b/cogl-sharp/Marshaller.cs
new file mode 100644
index 0000000..f6074af
--- /dev/null
+++ b/cogl-sharp/Marshaller.cs
@@ -0,0 +1,72 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2012 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;Vjg
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *   Damien Lespiau <damien lespiau intel com>
+ */
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace Cogl {
+
+    internal sealed class Marshaller
+    {
+        private Marshaller () {}
+
+        private static unsafe int strlen(IntPtr s)
+        {
+            int len = 0;
+            byte *b = (byte *)s;
+
+            while (*b != 0)
+            {
+                b++;
+                len++;
+            }
+
+            return len;
+        }
+
+        public static string Utf8PtrToString(IntPtr ptr)
+        {
+            if (ptr == IntPtr.Zero)
+                return null;
+
+            int len = strlen(ptr);
+            byte[] bytes = new byte[len];
+            Marshal.Copy(ptr, bytes, 0, len);
+            return System.Text.Encoding.UTF8.GetString(bytes);
+        }
+
+        /* Caller needs to free the newly allocated buffer with
+         * System.Marshal.FreeHGlobal() */
+        public static IntPtr StringToUtf8Ptr(string str)
+        {
+            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
+            IntPtr utf8 = Marshal.AllocHGlobal(bytes.Length + 1 /* '\0 */);
+            Marshal.Copy(bytes, 0, utf8, bytes.Length);
+            Marshal.WriteByte(utf8, bytes.Length, 0);
+            return utf8;
+        }
+
+    }
+}
diff --git a/cogl-sharp/Texture.cs b/cogl-sharp/Texture.cs
index c491ec7..f792c75 100644
--- a/cogl-sharp/Texture.cs
+++ b/cogl-sharp/Texture.cs
@@ -32,5 +32,27 @@ namespace Cogl
     {
         public Texture(IntPtr h) : base(h) {}
         public Texture() {}
+
+        [DllImport("cogl2.dll")]
+        public static extern IntPtr
+        cogl_texture_new_from_file(IntPtr s,
+                                   TextureFlags flags,
+                                   PixelFormat internal_format,
+                                   out IntPtr error);
+
+        public Texture(string filename,
+                       TextureFlags flags = TextureFlags.None,
+                       PixelFormat internal_format = PixelFormat.Any)
+        {
+            IntPtr filename_utf8, error;
+
+            filename_utf8 = Marshaller.StringToUtf8Ptr(filename);
+            handle = cogl_texture_new_from_file(filename_utf8, flags,
+                                           internal_format, out error);
+            Marshal.FreeHGlobal(filename_utf8);
+
+            if (error != IntPtr.Zero)
+                throw new Cogl.Exception(error);
+        }
     }
 }
diff --git a/cogl-sharp/TextureFlags.cs b/cogl-sharp/TextureFlags.cs
new file mode 100644
index 0000000..5980120
--- /dev/null
+++ b/cogl-sharp/TextureFlags.cs
@@ -0,0 +1,14 @@
+/* This file has been generated by parse-gir.py, do not hand edit */
+using System;
+
+namespace Cogl
+{
+    [Flags]
+    public enum TextureFlags
+    {
+        None = 0,
+        NoAutoMipmap = 1,
+        NoSlicing = 2,
+        NoAtlas = 4
+    }
+}
diff --git a/cogl-sharp/parse-gir.py b/cogl-sharp/parse-gir.py
index 07581c7..e325610 100755
--- a/cogl-sharp/parse-gir.py
+++ b/cogl-sharp/parse-gir.py
@@ -14,6 +14,7 @@ enum_types = (
     "PipelineFilter",
     "PipelineWrapMode",
     "PixelFormat",
+    "TextureFlags",
     "VerticesMode",
     "Winding"
 )



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]