1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! The txture module of CGL

#![allow(non_camel_case_types)]
use libc::{c_void, c_int, c_uint, size_t};

/// The internal handle used by CGL
#[repr(C)]
pub(crate) struct CGL_texture {
    _private: c_void
}

/// The texture format used by CGL
/// These are reflections of OpenGL's enum values
/// You can also use the _i version of methods to pass integers directly instead of enums
pub enum TextureFormat {
    RED     = 0x1903,
    GREEN   = 0x1904,
    BLUE    = 0x1905,
    ALPHA   = 0x1906,
    RGB     = 0x1907,
    RGBA    = 0x1908,
    BGR     = 0x80E0,
    BGRA    = 0x80E1
}

/// The texture internal format used by CGL
/// These are reflections of OpenGL's enum values
/// You can also use the _i version of methods to pass integers directly instead of enums
pub enum TextureInternalFormat {
    RGBA32F             = 0x8814,
    RGB32F              = 0x8815,
    RGBA16F             = 0x881A,
    RGB16F              = 0x881B,
    RGBA32UI            = 0x8D70,
    RGB32UI             = 0x8D71,
    RGBA16UI            = 0x8D76,
    RGB16UI             = 0x8D77,
    RGBA8UI             = 0x8D7C,
    RGB8UI              = 0x8D7D,
    RGBA32I             = 0x8D82,
    RGB32I              = 0x8D83,
    RGBA16I             = 0x8D88,
    RGB16I              = 0x8D89,
    RGBA8I              = 0x8D8E,
    RGB8I               = 0x8D8F,
    RED_INTEGER         = 0x8D94,
    GREEN_INTEGER       = 0x8D95,
    BLUE_INTEGER        = 0x8D96,
    RGB_INTEGER         = 0x8D98,
    RGBA_INTEGER        = 0x8D99,
    BGR_INTEGER         = 0x8D9A,
    BGRA_INTEGER        = 0x8D9B
}


/// The texture data type used by CGL
/// These are reflections of OpenGL's enum values
/// You can also use the _i version of methods to pass integers directly instead of enums
pub enum TextureDataType {
    BYTE              = 0x1400,
    UNSIGNED_BYTE     = 0x1401,
    SHORT             = 0x1402,
    UNSIGNED_SHORT    = 0x1403,
    INT               = 0x1404,
    UNSIGNED_INT      = 0x1405,
    FLOAT             = 0x1406
}

/// The texture scaling mode used by CGL
/// These are reflections of OpenGL's enum values
/// You can also use the _i version of methods to pass integers directly instead of enums
pub enum TextureScalingMode {
    NEAREST                 = 0x2600,
    LINEAR                  = 0x2601,
    NEAREST_MIPMAP_NEAREST  = 0x2700,
    LINEAR_MIPMAP_NEAREST   = 0x2701,
    NEAREST_MIPMAP_LINEAR   = 0x2702,
    LINEAR_MIPMAP_LINEAR    = 0x2703
}


/// The texture wrapping mode used by CGL
/// These are reflections of OpenGL's enum values
/// You can also use the _i version of methods to pass integers directly instead of enums
pub enum TextureWrappingMode {
    REPEAT              = 0x2901,
    MIRRORED_REPEAT     = 0x8370,
    CLAMP_TO_EDGE       = 0x812F,
    CLAMP_TO_BORDER     = 0x812D
}

/// The texture object
pub struct Texture {
    pub(crate) handle: *mut CGL_texture,
    pub(crate) has_been_destroyed: bool
}


#[repr(C)]
pub(crate) struct CGL_image {
    pub(crate) data: *mut c_void,
    pub(crate) height: c_int,
    pub(crate) width: c_int,
    pub(crate) bytes_per_channel: c_int,
    pub(crate) channels: c_int
}

extern {
    fn CGL_texture_create(image: *mut CGL_image) -> *mut CGL_texture;
    fn CGL_texture_create_blank(width: c_int, height: c_int, format: c_uint, internal_format: c_uint, data_type: c_uint) -> *mut CGL_texture;
    fn CGL_texture_create_array(width: c_int, height: c_int, layers: c_int, format: c_uint, internal_format: c_uint, data_type: c_uint) -> *mut CGL_texture;
    fn CGL_texture_create_3d(width: c_int, height: c_int, depth: c_int, format: c_uint, internal_format: c_uint, data_type: c_uint) -> *mut CGL_texture;
    fn CGL_texture_create_cubemap() -> *mut CGL_texture;
    fn CGL_texture_cubemap_set_face(texture: *mut CGL_texture, face: c_int, image: *mut CGL_image);
    fn CGL_texture_array_set_layer_data(texture: *mut CGL_texture, layer: c_int, data: *mut c_void);
    fn CGL_texture_destroy(texture: *mut CGL_texture);
    fn CGL_texture_bind(texture: *mut CGL_texture, unit: c_int) -> c_int;
    fn CGL_texture_set_data(texture: *mut CGL_texture, data: *mut c_void);
    fn CGL_texture_set_sub_data(texture: *mut CGL_texture, offset_x: size_t, offset_y: size_t, size_x: size_t, size_y: size_t, data: *mut c_void);
    fn CGL_texture_set_pixel_data(texture: *mut CGL_texture, x: c_int, y: c_int, data: *mut c_void);
    fn CGL_texture_set_user_data(texture: *mut CGL_texture, user_data: *mut c_void);
    fn CGL_texture_get_user_data(texture: *mut CGL_texture) -> *mut c_void;
    fn CGL_texture_get_internal_handle(texture: *mut CGL_texture) -> c_uint;
    fn CGL_texture_get_size(texture: *mut CGL_texture, width: *mut c_int, height: *mut c_int);
    fn CGL_texture_set_scaling_method(texture: *mut CGL_texture, method: c_int);
    fn CGL_texture_set_wrapping_method(texture: *mut CGL_texture, method: c_int);
}



impl Texture {
    
     /// Creates a new blank texture with the specified dimensions and format.
    /// 
    /// # Arguments
    /// 
    /// * `width` - The width of the texture in pixels.
    /// * `height` - The height of the texture in pixels.
    /// * `format` - The format of the texture data.
    /// * `internal_format` - The internal format of the texture data.
    /// * `data_type` - The data type of the texture data.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::blank_i(256, 256, cgl_rs::graphics::TextureFormat::RGBA as u32, cgl_rs::graphics::TextureInternalFormat::RGBA8UI as u32, cgl_rs::graphics::TextureDataType::UNSIGNED_BYTE as u32).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn blank_i(width: i32, height: i32, format: u32, internal_format: u32, data_type: u32) -> Result<Texture, &'static str> {
        let handle = unsafe {
            CGL_texture_create_blank(width, height, format as c_uint, internal_format as c_uint, data_type as c_uint)
        };
        if handle.is_null() {
            Err("Failed to create texture")
        } else {
            Ok(Texture {
                handle,
                has_been_destroyed: false
            })
        }
    }

    /// Creates a new blank texture with the specified dimensions and format.
    /// 
    /// # Arguments
    /// 
    /// * `width` - The width of the texture in pixels.
    /// * `height` - The height of the texture in pixels.
    /// * `format` - The format of the texture data.
    /// * `internal_format` - The internal format of the texture data.
    /// * `data_type` - The data type of the texture data.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::blank(256, 256, cgl_rs::graphics::TextureFormat::RGBA, cgl_rs::graphics::TextureInternalFormat::RGBA8UI, cgl_rs::graphics::TextureDataType::UNSIGNED_BYTE).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn blank(width: i32, height: i32, format: TextureFormat, internal_format: TextureInternalFormat, data_type: TextureDataType) -> Result<Texture, &'static str> {
        Texture::blank_i(width, height, format as u32, internal_format as u32, data_type as u32)
    }

    /// Creates a new dummy texture with a single pixel.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::dummy().unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn dummy() -> Result<Texture, &'static str> {
        Texture::blank(1, 1, TextureFormat::RGBA, TextureInternalFormat::RGBA8UI, TextureDataType::UNSIGNED_BYTE)
    }

    /// Creates a new dummy texture with the specified dimensions.
    /// 
    /// # Arguments
    /// 
    /// * `width` - The width of the texture in pixels.
    /// * `height` - The height of the texture in pixels.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn dummy2(width: i32, height: i32) -> Result<Texture, &'static str> {
        Texture::blank(width, height, TextureFormat::RGBA, TextureInternalFormat::RGBA8UI, TextureDataType::UNSIGNED_BYTE)
    }

    /// Creates a new texture array with the specified dimensions and format.
    /// 
    /// # Arguments
    /// 
    /// * `width` - The width of the texture in pixels.
    /// * `height` - The height of the texture in pixels.
    /// * `depth` - The depth of the texture array.
    /// * `format` - The format of the texture data.
    /// * `internal_format` - The internal format of the texture data.
    /// * `data_type` - The data type of the texture data.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::array_i(256, 256, 6, cgl_rs::graphics::TextureFormat::RGBA as u32, cgl_rs::graphics::TextureInternalFormat::RGBA8UI as u32, cgl_rs::graphics::TextureDataType::UNSIGNED_BYTE as u32).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn array_i(width: i32, height: i32, depth: i32, format: u32, internal_format: u32, data_type: u32) -> Result<Texture, &'static str> {
        let handle = unsafe {
            CGL_texture_create_array(width, height, depth, format as c_uint, internal_format as c_uint, data_type as c_uint)
        };
        if handle.is_null() {
            Err("Failed to create texture")
        } else {
            Ok(Texture {
                handle,
                has_been_destroyed: false
            })
        }
    }

    // Creates a new texture array with the specified dimensions and format.
    /// 
    /// # Arguments
    /// 
    /// * `width` - The width of the texture in pixels.
    /// * `height` - The height of the texture in pixels.
    /// * `depth` - The depth of the texture array.
    /// * `format` - The format of the texture data.
    /// * `internal_format` - The internal format of the texture data.
    /// * `data_type` - The data type of the texture data.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::array(256, 256, 6, cgl_rs::graphics::TextureFormat::RGBA, cgl_rs::graphics::TextureInternalFormat::RGBA8UI, cgl_rs::graphics::TextureDataType::UNSIGNED_BYTE).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn array(width: i32, height: i32, depth: i32, format: TextureFormat, internal_format: TextureInternalFormat, data_type: TextureDataType) -> Result<Texture, &'static str> {
        Texture::array_i(width, height, depth, format as u32, internal_format as u32, data_type as u32)
    }

    /// Creates a new cubemap texture.
    /// 
    /// # Returns
    /// 
    /// A `Result` containing the newly created `Texture` if successful, or an error message if creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::cubemap().unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn cubemap() -> Result<Texture, &'static str> {
        let handle = unsafe {
            CGL_texture_create_cubemap()
        };
        if handle.is_null() {
            Err("Failed to create texture")
        } else {
            Ok(Texture {
                handle,
                has_been_destroyed: false
            })
        }
    }

    // TODO : Implement cubemap_set_face

    /// Sets the data for a single layer of a texture array.
    /// 
    /// # Arguments
    /// 
    /// * `layer` - The index of the layer to set the data for.
    /// * `data` - A slice containing the data to set for the layer.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut texture = cgl_rs::graphics::Texture::array(256, 256, 6, cgl_rs::graphics::TextureFormat::RGBA, cgl_rs::graphics::TextureInternalFormat::RGBA8UI, cgl_rs::graphics::TextureDataType::UNSIGNED_BYTE).unwrap();
    ///     let data = vec![0u8; 256 * 256 * 4];
    ///     texture.array_set_layer_data(0, &data);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn array_set_layer_data(&self, layer: i32, data: &[u8]) {
        unsafe {
            CGL_texture_array_set_layer_data(self.handle, layer, data.as_ptr() as *mut c_void);
        }
    }

    /// Binds the texture to a texture unit.
    /// 
    /// # Arguments
    /// 
    /// * `unit` - The texture unit to bind the texture to.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::dummy().unwrap();
    ///     texture.bind(0);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn bind(&self, unit: u32) {
        unsafe {
            CGL_texture_bind(self.handle, unit as i32);
        }
    }

    /// Sets the data for the texture.
    /// 
    /// # Arguments
    /// 
    /// * `data` - A slice containing the data to set for the texture.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    ///     let data = vec![0u8; 256 * 256 * 4];
    ///     texture.set_data(data.as_ptr());
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn set_data(&self, data: *const u8) {
        unsafe {
            CGL_texture_set_data(self.handle, data as *mut c_void);
        }
    }

    /// Sets a rectangular subregion of the texture with new data.
    ///
    /// # Arguments
    ///
    /// * `x` - The x-coordinate of the lower left corner of the subregion.
    /// * `y` - The y-coordinate of the lower left corner of the subregion.
    /// * `width` - The width of the subregion.
    /// * `height` - The height of the subregion.
    /// * `data` - A slice containing the new data to set for the subregion.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    ///     let data = vec![255u8; 100 * 100 * 4];
    ///     texture.set_sub_data(0, 0, 100, 100, data.as_ptr());
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_sub_data(&self, x: usize, y: usize, width: usize, height: usize, data: *const u8) {
        unsafe {
            CGL_texture_set_sub_data(self.handle, x, y, width, height, data as *mut c_void);
        }
    }

    /// Returns the internal OpenGL handle of the texture.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let texture = cgl_rs::graphics::Texture::dummy().unwrap();
    ///     let handle = texture.get_internal_gl_handle();
    ///     println!("Internal OpenGL handle: {}", handle);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_internal_gl_handle(&self) -> u32 {
        unsafe {
            CGL_texture_get_internal_handle(self.handle) as u32
        }
    }

    /// Sets the scaling method of the texture.
    ///
    /// # Arguments
    ///
    /// * `method` - The scaling method to use for the texture.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    ///     texture.set_scaling_method_i(1);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_scaling_method_i(&self, method: i32) {
        unsafe {
            CGL_texture_set_scaling_method(self.handle, method as i32);
        }
    }

    // Sets the scaling method of the texture.
    ///
    /// # Arguments
    ///
    /// * `method` - The scaling method to use for the texture.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    ///     texture.set_scaling_method(cgl_rs::graphics::TextureScalingMode::LINEAR);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_scaling_method(&self, method: TextureScalingMode) {
        self.set_scaling_method_i(method as i32);
    }

    /// Sets the wrapping method of the texture.
    ///
    /// # Arguments
    ///
    /// * `method` - The wrapping method to use for the texture.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let mut texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    ///    texture.set_wrapping_method_i(1);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_wrapping_method_i(&self, method: i32) {
        unsafe {
            CGL_texture_set_wrapping_method(self.handle, method as i32);
        }
    }

    /// Sets the wrapping method of the texture.
    ///
    /// # Arguments
    ///
    /// * `method` - The wrapping method to use for the texture.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let mut texture = cgl_rs::graphics::Texture::dummy2(256, 256).unwrap();
    ///    texture.set_wrapping_method(cgl_rs::graphics::TextureWrappingMode::CLAMP_TO_EDGE);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_wrapping_method(&self, method: TextureWrappingMode) {
        self.set_wrapping_method_i(method as i32);
    }


    /// Destroys the texture and frees its resources. If the texture has already been destroyed, this method does nothing.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut texture = cgl_rs::graphics::Texture::dummy().unwrap();
    ///     texture.destroy(); // optional
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn destroy(&mut self) {
        if !self.has_been_destroyed {
            unsafe {
                CGL_texture_destroy(self.handle);
            }
            self.has_been_destroyed = true;
        }
    }

}

impl Drop for Texture {
    fn drop(&mut self) {
        self.destroy();
    }
}


impl Clone for Texture {
    /// Clones the texture.
    /// 
    /// NOTE: The new instance will have the same handle, `has_been_destroyed` flag.
    ///       This means that the new instance will not be able to receive events nor will the internal window handle be
    ///       destroyed when the new instance is dropped. The internal window handle will be destroyed when the original
    ///       instance is dropped.
    /// 
    /// 
    /// # Returns
    /// 
    /// A new instance of `Texture` with the same handle as the original texture.
    fn clone(&self) -> Self {
        Texture {
            handle: self.handle.clone(),
            has_been_destroyed: true
        }
    }
}