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
//! This provices CGL's Bloom post-processing effect (based on Unity's Bloom effect)

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

use crate::graphics::texture::{Texture, CGL_texture};

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

extern {
    fn CGL_bloom_create(width: c_int, height: c_int, iterations: c_int) -> *mut CGL_bloom;
    fn CGL_bloom_destroy(bloom: *mut CGL_bloom) -> c_void;
    fn CGL_bloom_set_threshold(bloom: *mut CGL_bloom, val: c_float) -> c_void;
    fn CGL_bloom_get_threshold(bloom: *mut CGL_bloom) -> c_float;
    fn CGL_bloom_set_knee(bloom: *mut CGL_bloom, val: c_float) -> c_void;
    fn CGL_bloom_get_knee(bloom: *mut CGL_bloom) -> c_float;
    fn CGL_bloom_set_offset(bloom: *mut CGL_bloom, x: c_float, y: c_float) -> c_void;
    fn CGL_bloom_apply(bloom: *mut CGL_bloom, tex: *mut CGL_texture) -> c_void;
    fn CGL_bloom_apply2(bloom: *mut CGL_bloom, tex_src: *mut CGL_texture, tex_dst: *mut CGL_texture) -> c_void;
    fn CGL_bloom_get_iterations(bloom: *mut CGL_bloom) -> c_int;
    fn CGL_bloom_get_lod_texture(bloom: *mut CGL_bloom, index: c_int) -> *mut CGL_texture;
    fn CGL_bloom_get_prefiltered_texture(bloom: *mut CGL_bloom) -> *mut CGL_texture;
}

/// The Bloom Object
#[repr(C)]
pub struct Bloom {
    pub(crate) handle: *mut CGL_bloom,
    pub(crate) has_been_destroyed: bool
}

impl Bloom {
    /// Creates a new Bloom object with the specified width, height, and number of iterations.
    /// 
    /// # Arguments
    /// 
    /// * `width` - The width of the Bloom effect.
    /// * `height` - The height of the Bloom effect.
    /// * `iterations` - The number of iterations to apply the Bloom effect.
    /// 
    /// # Returns
    /// 
    /// Returns a new Bloom object if successful, otherwise returns an error message.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn new(width: i32, height: i32, iterations: i32) -> Result<Bloom, &'static str> {
        unsafe {
            let handle = CGL_bloom_create(width, height, iterations);
            if handle.is_null() {
                Err("Failed to create Bloom")
            } else {
                Ok(Bloom {
                    handle,
                    has_been_destroyed: false
                })
            }
        }
    }

    

    /// 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 bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///     bloom.destroy(); // optional
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    pub fn destroy(&mut self) {
        if !self.has_been_destroyed {
            unsafe {
                CGL_bloom_destroy(self.handle);
            }
            self.has_been_destroyed = true;
        }
    }


    /// Sets the threshold value for the Bloom effect.
    /// 
    /// # Arguments
    /// 
    /// * `val` - The threshold value to set.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    bloom.set_threshold(0.5);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_threshold(&self, val: f32) {
        unsafe {
            CGL_bloom_set_threshold(self.handle, val);
        }
    }

    /// Gets the threshold value for the Bloom effect.
    /// 
    /// # Returns
    /// 
    /// Returns the current threshold value.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let threshold = bloom.get_threshold();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_threshold(&self) -> f32 {
        unsafe {
            CGL_bloom_get_threshold(self.handle)
        }
    }

    /// Sets the knee value for the Bloom effect.
    /// 
    /// # Arguments
    /// 
    /// * `val` - The knee value to set.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    bloom.set_knee(0.5);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_knee(&self, val: f32) {
        unsafe {
            CGL_bloom_set_knee(self.handle, val);
        }
    }

    /// Gets the knee value for the Bloom effect.
    /// 
    /// # Returns
    /// 
    /// Returns the current knee value.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let knee = bloom.get_knee();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_knee(&self) -> f32 {
        unsafe {
            CGL_bloom_get_knee(self.handle)
        }
    }

    /// Sets the offset values for the Bloom effect.
    /// 
    /// # Arguments
    /// 
    /// * `x` - The x offset value to set.
    /// * `y` - The y offset value to set.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    bloom.set_offset(0.5, 0.5);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_offset(&self, x: f32, y: f32) {
        unsafe {
            CGL_bloom_set_offset(self.handle, x, y);
        }
    }


    /// Applies the Bloom effect to a texture.
    /// 
    /// # Arguments
    /// 
    /// * `tex` - The texture to apply the Bloom effect to.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let texture = cgl_rs::graphics::Texture::dummy2(600, 600).unwrap();
    ///    bloom.apply(&texture);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn apply(&self, tex: &Texture) {
        unsafe {
            CGL_bloom_apply(self.handle, tex.handle);
        }
    }

    /// Applies the Bloom effect to a source texture and writes the result to a destination texture.
    /// 
    /// # Arguments
    /// 
    /// * `tex_src` - The source texture to apply the Bloom effect to.
    /// * `tex_dst` - The destination texture to write the result to.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let texture_src = cgl_rs::graphics::Texture::dummy2(600, 600).unwrap();
    ///    let texture_dst = cgl_rs::graphics::Texture::dummy2(600, 600).unwrap();
    ///    bloom.apply2(&texture_src, &texture_dst);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn apply2(&self, tex_src: &Texture, tex_dst: &Texture) {
        unsafe {
            CGL_bloom_apply2(self.handle, tex_src.handle, tex_dst.handle);
        }
    }

    /// Gets the value of the number of iterations for the Bloom effect.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let iterations = bloom.get_iterations();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_iterations(&self) -> i32 {
        unsafe {
            CGL_bloom_get_iterations(self.handle)
        }
    }

    /// Gets the texture for a specific level of detail (LOD) for the Bloom effect.
    /// 
    /// This texture is owned and managed by the Bloom effect and should not be destroyed manually.
    /// 
    /// # Arguments
    /// 
    /// * `index` - The index of the LOD texture to retrieve.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let lod_texture = bloom.get_lod_texture(0);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_lod_texture(&self, index: i32) -> Result<Texture, &'static str> {
        unsafe {
            let handle = CGL_bloom_get_lod_texture(self.handle, index);
            if handle.is_null() {
                Err("Failed to get LOD texture")
            } else {
                Ok(Texture {
                    handle,
                    has_been_destroyed: true
                })
            }
        }
    }

    /// Gets the prefiltered texture for the Bloom effect.
    /// 
    /// This texture is owned and managed by the Bloom effect and should not be destroyed manually.
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///    let bloom = cgl_rs::graphics::Bloom::new(800, 600, 3).unwrap();
    ///    let prefiltered_texture = bloom.get_prefiltered_texture();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_prefiltered_texture(&self) -> Result<Texture, &'static str> {
        unsafe {
            let handle = CGL_bloom_get_prefiltered_texture(self.handle);
            if handle.is_null() {
                Err("Failed to get prefiltered texture")
            } else {
                Ok(Texture {
                    handle,
                    has_been_destroyed: true
                })
            }
        }
    }
}


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

impl Clone for Bloom {
    fn clone(&self) -> Self {
        Bloom {
            handle: self.handle.clone(),
            has_been_destroyed: true
        }
    }
}