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
//! The framebuffer module for CGL

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

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

extern {
    fn CGL_framebuffer_create_from_default(window: *mut crate::window::CGL_window) -> *mut CGL_framebuffer;
    fn CGL_framebuffer_create(width: c_int, height: c_int) -> *mut CGL_framebuffer;
    fn CGL_framebuffer_create_basic(width: c_int, height: c_int) -> *mut CGL_framebuffer;
    fn CGL_framebuffer_add_color_attachment(framebuffer: *mut CGL_framebuffer, texture: *mut super::texture::CGL_texture) -> ();
    fn CGL_framebuffer_destroy(framebuffer: *mut CGL_framebuffer) -> ();
    fn CGL_framebuffer_get_color_attachment(framebuffer: *mut CGL_framebuffer, index: c_int) -> *mut super::texture::CGL_texture;
    fn CGL_framebuffer_bind(framebuffer: *mut CGL_framebuffer) -> ();
    fn CGL_framebuffer_get_size(framebuffer: *mut CGL_framebuffer, width: *mut c_int, height: *mut c_int) -> ();
    fn CGL_framebuffer_set_user_data(framebuffer: *mut CGL_framebuffer, user_data: *mut c_void) -> ();
    fn CGL_framebuffer_get_user_data(framebuffer: *mut CGL_framebuffer) -> *mut c_void;
    fn CGL_framebuffer_read_pixels(framebuffer: *mut CGL_framebuffer, x: c_int, y: c_int, width: c_int, height: c_int, pixels: *mut c_void) -> ();
    fn CGL_framebuffer_get_mouse_pick_id(framebuffer: *mut CGL_framebuffer, x: c_int, y: c_int, index: c_int) -> c_int;
    fn CGL_framebuffer_get_color_texture(framebuffer: *mut CGL_framebuffer) -> *mut super::texture::CGL_texture;
    fn CGL_framebuffer_get_depth_texture(framebuffer: *mut CGL_framebuffer) -> *mut super::texture::CGL_texture;
}


/// A framebuffer object that can be used for offscreen rendering.
#[derive(Debug)]
pub struct Framebuffer {
    pub(crate) handle: *mut CGL_framebuffer,
    pub(crate) has_been_destroyed: bool
}


impl Framebuffer {

    /// Creates a new framebuffer object from the default window.
    ///
    /// # Arguments
    ///
    /// * `window` - A reference to the window object to create the framebuffer from.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the newly created `Framebuffer` object or an error message if the creation failed.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::from_default(&window).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn from_default(window: &crate::window::Window) -> Result<Framebuffer, &'static str> {
        unsafe {
            let result = CGL_framebuffer_create_from_default(window.get_cgl_handle());
            if result.is_null() {
                Err("Failed to create framebuffer from default")
            } else {
                Ok(Framebuffer {
                    handle: result,
                    has_been_destroyed: false
                })
            }
        }
    }

    /// Creates a new framebuffer object with the specified width and height.
    ///
    /// # Arguments
    ///
    /// * `width` - The width of the framebuffer.
    /// * `height` - The height of the framebuffer.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the newly created `Framebuffer` object or an error message if the creation failed.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn new(width: i32, height: i32) -> Result<Framebuffer, &'static str> {
        unsafe {
            let result = CGL_framebuffer_create(width, height);
            if result.is_null() {
                Err("Failed to create framebuffer")
            } else {
                Ok(Framebuffer {
                    handle: result,
                    has_been_destroyed: false
                })
            }
        }
    }

    /// Creates a new basic framebuffer object with the specified width and height.
    ///
    /// # Arguments
    ///
    /// * `width` - The width of the framebuffer.
    /// * `height` - The height of the framebuffer.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the newly created `Framebuffer` object or an error message if the creation failed.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new_basic(800, 600).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn new_basic(width: i32, height: i32) -> Result<Framebuffer, &'static str> {
        unsafe {
            let result = CGL_framebuffer_create_basic(width, height);
            if result.is_null() {
                Err("Failed to create framebuffer")
            } else {
                Ok(Framebuffer {
                    handle: result,
                    has_been_destroyed: false
                })
            }
        }
    }

    /// Destroys the framebuffer object.
    /// 
    /// Note: This function is called automatically when the framebuffer object goes out of scope.
    ///       But this can also be called manually to destroy the framebuffer object before it goes out of scope.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     framebuffer.destroy(); // This is not necessary, but can be called manually.
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn destroy(&mut self) {
        if !self.has_been_destroyed {
            unsafe {
                CGL_framebuffer_destroy(self.handle);
            }
            self.has_been_destroyed = true;
        }
    }

    /// Adds a color attachment to the framebuffer object.
    ///
    /// Note: The texture object passed in will be destroyed automatically after being added to the framebuffer object.
    ///       So even if you clone the parent texture 
    ///
    /// # Arguments
    ///
    /// * `texture` - The texture object to be added as a color attachment to the framebuffer object.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     let texture = cgl_rs::graphics::Texture::dummy().unwrap();
    ///     framebuffer.add_color_attachment(texture);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_color_attachment(&mut self, mut texture: crate::graphics::texture::Texture) {
        texture.has_been_destroyed = true;
        unsafe {
            CGL_framebuffer_add_color_attachment(self.handle, texture.handle);
        }
    }

    /// Gets the color attachment at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the color attachment to retrieve.
    ///
    /// # Returns
    ///
    /// Returns `Some(Texture)` if the color attachment exists, otherwise returns `None`.
    /// This returned texture object is managed by the framebuffer object and will be 
    /// destroyed automatically when the framebuffer object is destroyed.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut framebuffer = cgl_rs::graphics::Framebuffer::new_basic(800, 600).unwrap();
    ///     let texture = cgl_rs::graphics::Texture::dummy().unwrap();
    ///     framebuffer.add_color_attachment(texture);
    ///     let color_attachment = framebuffer.get_color_atttachment(0);
    ///     assert!(color_attachment.is_some());
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_color_atttachment(&self, index: i32) -> Option<crate::graphics::texture::Texture> {
        unsafe {
            let result = CGL_framebuffer_get_color_attachment(self.handle, index);
            if result.is_null() {
                None
            } else {
                Some(crate::graphics::texture::Texture {
                    handle: result,
                    has_been_destroyed: true
                })
            }
        }
    }


    /// Binds the framebuffer object for rendering.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     framebuffer.bind();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn bind(&self) {
        unsafe {
            CGL_framebuffer_bind(self.handle);
        }
    }


    // Gets the size of the framebuffer object.
    ///
    /// # Returns
    ///
    /// Returns a tuple containing the width and height of the framebuffer object.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     let (width, height) = framebuffer.get_size();
    ///     assert_eq!(width, 800);
    ///     assert_eq!(height, 600);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_size(&self) -> (i32, i32) {
        unsafe {
            let mut width = 0;
            let mut height = 0;
            CGL_framebuffer_get_size(self.handle, &mut width, &mut height);
            (width, height)
        }
    }

    /// Reads the pixels from the framebuffer object.
    ///
    /// Note: This function currently has some issues and may crash the program.
    /// 
    /// # Arguments
    ///
    /// * `x` - The x coordinate of the lower left corner of the rectangle of pixels to read.
    /// * `y` - The y coordinate of the lower left corner of the rectangle of pixels to read.
    /// * `width` - The width of the rectangle of pixels to read.
    /// * `height` - The height of the rectangle of pixels to read.
    /// 
    ///
    /// # Returns
    ///
    /// Returns a vector of bytes containing the pixel data.
    ///
    /// # Example
    ///
    /// ```no_run
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     let pixels = framebuffer.read_pixels(0, 0, 100, 100);
    ///     assert_eq!(pixels.len(), 100 * 100 * 4 * 4);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn read_pixels(&self, x: i32, y: i32, width: i32, height: i32) -> Vec<u8> {
        unsafe {
            let mut buffer = Vec::with_capacity((width * height * 4) as usize);
            buffer.set_len((width * height * 4 * 4) as usize);
            CGL_framebuffer_read_pixels(self.handle, x, y, width, height, buffer.as_mut_ptr() as *mut std::ffi::c_void);
            buffer          
        }
    }


    /// Gets the mouse pick ID at the specified screen coordinates and index.
    ///
    /// # Arguments
    ///
    /// * `x` - The x coordinate of the screen position to check.
    /// * `y` - The y coordinate of the screen position to check.
    /// * `index` - The index of the mouse pick ID to retrieve.
    ///
    /// # Returns
    ///
    /// Returns the mouse pick ID at the specified screen coordinates and index.
    ///
    /// # Safety
    ///
    /// This function is marked as unsafe because it directly calls an unsafe C function.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     let mouse_pick_id = framebuffer.get_mouse_pick_id(400, 300, 0);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_mouse_pick_id(&self, x: i32, y: i32, index: i32) -> i32 {
        unsafe {
            CGL_framebuffer_get_mouse_pick_id(self.handle, x, y, index)
        }
    }

    /// Gets the color texture of the framebuffer.
    ///
    /// # Returns
    ///
    /// Returns a `Texture` object representing the color texture of the framebuffer.
    ///
    /// # Safety
    ///
    /// This function is marked as unsafe because it directly calls an unsafe C function.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///    let texture = framebuffer.get_color_texture();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_color_texture(&self) -> crate::graphics::Texture {
        unsafe {
            let handle = CGL_framebuffer_get_color_texture(self.handle);
            crate::graphics::Texture {
                handle,
                has_been_destroyed: true
            }
        }
    }

    /// Gets the depth texture of the framebuffer.
    ///
    /// # Returns
    ///
    /// Returns a `Texture` object representing the depth texture of the framebuffer.
    ///
    /// # Safety
    ///
    /// This function is marked as unsafe because it directly calls an unsafe C function.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     let depth_texture = framebuffer.get_depth_texture();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_depth_texture(&self) -> crate::graphics::Texture {
        unsafe {
            let handle = CGL_framebuffer_get_depth_texture(self.handle);
            crate::graphics::Texture {
                handle,
                has_been_destroyed: true
            }
        }
    }
}

impl std::ops::Drop for Framebuffer {
    fn drop(&mut self) {
        self.destroy();
    }
}


impl Clone for Framebuffer {
    /// Clones the framebuffer object.
    ///
    /// 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.
    /// 
    /// 
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let framebuffer = cgl_rs::graphics::Framebuffer::new(800, 600).unwrap();
    ///     let cloned_framebuffer = framebuffer.clone();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    fn clone(&self) -> Self {
        Framebuffer {
            handle: self.handle.clone(),
            has_been_destroyed: true
        }
    }
}