1use crate::{Bitmap, Context, Object, PixelFormat, Texture};
2
3use glib::translate::*;
4use std::{fmt, ptr};
5
6glib_wrapper! {
7 pub struct Texture2D(Object<ffi::CoglTexture2D, Texture2DClass>) @extends Object, @implements Texture;
8
9 match fn {
10 get_type => || ffi::cogl_texture_2d_get_gtype(),
11 }
12}
13
14impl Texture2D {
15 pub fn gl_new_from_foreign(
42 ctx: &Context,
43 gl_handle: u32,
44 width: i32,
45 height: i32,
46 format: PixelFormat,
47 ) -> Texture2D {
48 unsafe {
49 from_glib_full(ffi::cogl_texture_2d_gl_new_from_foreign(
50 ctx.to_glib_none().0,
51 gl_handle,
52 width,
53 height,
54 format.to_glib(),
55 ))
56 }
57 }
58
59 pub fn from_bitmap(bitmap: &Bitmap) -> Texture2D {
60 unsafe {
61 from_glib_full(ffi::cogl_texture_2d_new_from_bitmap(
62 bitmap.to_glib_none().0,
63 ))
64 }
65 }
66
67 pub fn from_data(
68 ctx: &Context,
69 width: i32,
70 height: i32,
71 format: PixelFormat,
72 rowstride: i32,
73 data: &[u8],
74 ) -> Result<Texture2D, glib::Error> {
75 unsafe {
76 let mut error = ptr::null_mut();
77 let ret = ffi::cogl_texture_2d_new_from_data(
78 ctx.to_glib_none().0,
79 width,
80 height,
81 format.to_glib(),
82 rowstride,
83 data.as_ptr(),
84 &mut error,
85 );
86 if error.is_null() {
87 Ok(from_glib_full(ret))
88 } else {
89 Err(from_glib_full(error))
90 }
91 }
92 }
93
94 pub fn from_file(ctx: &Context, filename: &str) -> Result<Texture2D, glib::Error> {
95 unsafe {
96 let mut error = ptr::null_mut();
97 let ret = ffi::cogl_texture_2d_new_from_file(
98 ctx.to_glib_none().0,
99 filename.to_glib_none().0,
100 &mut error,
101 );
102 if error.is_null() {
103 Ok(from_glib_full(ret))
104 } else {
105 Err(from_glib_full(error))
106 }
107 }
108 }
109
110 pub fn with_size(ctx: &Context, width: i32, height: i32) -> Texture2D {
111 unsafe {
112 from_glib_full(ffi::cogl_texture_2d_new_with_size(
113 ctx.to_glib_none().0,
114 width,
115 height,
116 ))
117 }
118 }
119}
120
121impl fmt::Display for Texture2D {
122 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123 write!(f, "Texture2D")
124 }
125}