cat_engine_basement 0.0.0-alpha7

The CatEnigne's basement
Documentation
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
use crate::graphics::Colour;

#[cfg(any(windows))]
use crate::windows::OpenGraphicsLibrary;

use core::mem::transmute;

// Blend functions
const ZERO:u32=0;
const ONE:u32=1;
const SRC_COLOR:u32=0x0300;
const ONE_MINUS_SRC_COLOR:u32=0x0301;
const SRC_ALPHA:u32=0x0302;
const ONE_MINUS_SRC_ALPHA:u32=0x0303;
const DST_ALPHA:u32=0x0304;
const ONE_MINUS_DST_ALPHA:u32=0x0305;
const DST_COLOR:u32=0x0306;
const ONE_MINUS_DST_COLOR:u32=0x0307;
const SRC_ALPHA_SATURATE:u32=0x0308;
const CONSTANT_COLOR:u32=0x8001;
const ONE_MINUS_CONSTANT_COLOR:u32=0x8002;
const CONSTANT_ALPHA:u32=0x8003;
const ONE_MINUS_CONSTANT_ALPHA:u32=0x8004;
const SRC1_ALPHA:u32=0x8589;
const SRC1_COLOR:u32=0x88F9;
const ONE_MINUS_SRC1_COLOR:u32=0x88FA;
const ONE_MINUS_SRC1_ALPHA:u32=0x88FB;

// Blend equation
const FUNC_ADD:u32=0x8006;
const FUNC_SUBTRACT:u32=0x800A;
const FUNC_REVERSE_SUBTRACT:u32=0x800B;
const MIN:u32=0x8007;
const MAX:u32=0x8008;

// Blend parameters
const BLEND_COLOR:u32=0x8005;
const BLEND_DST_ALPHA:u32=0x80CA;
const BLEND_DST_RGB:u32=0x80C8;
const BLEND_EQUATION:u32=0x8009;
const BLEND_EQUATION_ALPHA:u32=0x883D;
const BLEND_EQUATION_RGB:u32=0x8009;
const BLEND_SRC_ALPHA:u32=0x80CB;
const BLEND_SRC_RGB:u32=0x80C9;

/// The `BlendingEquation::MIN` and `BlendingEquation::MAX` equations
/// do not use the source or destination factors,
/// only the source and destination colors.
#[repr(u32)]
pub enum BlendingEquation{
    /// The default, adds both colours to each other.
    /// 
    /// Result = source + destination.
    Addition=FUNC_ADD,

    /// Subtracts both colours from each other.
    /// 
    /// Result = source - destination.
    Subtraction=FUNC_SUBTRACT,

    /// Subtracts both colours, but reverses order.
    /// 
    /// Result = destination - source.
    ReverseSubtraction=FUNC_REVERSE_SUBTRACT,

    /// Takes the component-wise minimum of both colours.
    /// 
    /// Result = min(source, destination).
    Minimum=MIN,

    /// Takes the component-wise maximum of both colours.
    /// 
    /// Result = max(source, destination).
    Maximum=MAX
}

#[repr(u32)]
#[derive(Clone,Copy,Debug)]
pub enum BlendingFunction{
    /// Multiply the component by zero.
    /// 
    /// Result = Colour * 0.
    Zero=ZERO,

    /// Multiply the component by one.
    /// 
    /// Result = Colour * 1.
    One=ONE,

    /// Multiply the component by its corresponding value in the source.
    /// 
    /// Result = Colour * SourceColour.
    SourceColour=SRC_COLOR,

    /// Equivalent to `1 - SourceColour`.
    /// 
    /// Result = Result = Colour * (1 - SourceColour).
    OneMinusSourceColour=ONE_MINUS_SRC_COLOR,

    /// Multiply the component by its corresponding value in the destination.
    /// 
    /// Result = Colour * DestinationColour
    DestinationColour=DST_COLOR,

    /// Equivalent to `1 - DestinationColour`.
    /// 
    /// Result = Result = Colour * (1 - DestinationColour).
    OneMinusDestinationColour=ONE_MINUS_DST_COLOR,

    /// Multiply the component by the alpha value of the source.
    /// 
    /// Result = Colour * SourceAlpha.
    SourceAlpha=SRC_ALPHA,

    /// Equivalent to `1 - SourceAlpha`.
    /// 
    /// Result = Colour * (1 - SourceAlpha).
    OneMinusSourceAlpha=ONE_MINUS_SRC_ALPHA,

    /// Multiply the component by the alpha value of the destination.
    /// 
    /// Result = Colour * DestinationAlpha.
    DestinationAlpha=DST_ALPHA,

    /// Equivalent to `1 - DestinationAlpha`.
    /// 
    /// Result = Colour * (1 - DestinationAlpha).
    OneMinusDestinationAlpha=ONE_MINUS_DST_ALPHA,

    /// Multiply the component by the corresponding value in the blending constant colour.
    /// 
    /// Result = Colour * ConstantColour.
    ConstantColour=CONSTANT_COLOR,

    /// Equivalent to `1 - ConstantColour`.
    /// 
    /// Result = Colour * (1 - ConstantColour).
    OneMinusConstantColour=ONE_MINUS_CONSTANT_COLOR,

    /// Multiply the component by the alpha value of the blending constant colour.
    /// 
    /// Result = Colour * ConstantAlpha.
    ConstantAlpha=CONSTANT_ALPHA,

    /// Equivalent to `1 - ConstantAlpha`.
    /// 
    /// Result = Colour * (1 - ConstantAlpha).
    OneMinusConstantAlpha=ONE_MINUS_CONSTANT_ALPHA,

    /// Multiply the component by the smallest value of `SourceAlpha` and `1 - DestinationAlpha`.
    SourceAlphaSaturate=SRC_ALPHA_SATURATE,

    // not supported
    // Source1Colour=SRC1_COLOR,
    // 
    // OneMinusSource1Colour=ONE_MINUS_SRC1_COLOR,
    // 
    // Source1Alpha=SRC1_ALPHA,
    // 
    // OneMinusSourse1Alpha=ONE_MINUS_SRC1_ALPHA,
}

/// A wrapper for blending functions.
/// 
/// Blend is disabled by default.
/// 
/// The default blending constant colour is `[0f32;4]`.
/// 
/// The default blending functions for `Source` are `BlendingFunction::One`.
/// 
/// The default blending functions for `Destination` are `BlendingFunction::Zero`.
/// 
/// The default blending equations are `BlendingEquation::Addition`.
pub struct Blend{
    glBlendColor:usize,
    glBlendFunc:usize,
    glBlendFuncSeparate:usize,
    glBlendEquation:usize,
    glBlendEquationSeparate:usize,
}

impl Blend{
    pub const fn new()->Blend{
        Self{
            glBlendColor:0,
            glBlendFunc:0,
            glBlendFuncSeparate:0,
            glBlendEquation:0,
            glBlendEquationSeparate:0,
        }
    }

    #[cfg(any(windows))]
    pub fn load(&mut self,library:&OpenGraphicsLibrary){
        unsafe{
            self.glBlendColor=transmute(library.get_proc_address("glBlendColor\0"));
            self.glBlendFunc=transmute(library.get_proc_address("glBlendFunc\0"));
            self.glBlendFuncSeparate=transmute(library.get_proc_address("glBlendFuncSeparate\0"));
            self.glBlendEquation=transmute(library.get_proc_address("glBlendEquation\0"));
            self.glBlendEquationSeparate=transmute(library.get_proc_address("glBlendEquationSeparate\0"));
        }
    }
}

impl Blend{
    /// Enables blending.
    #[inline(always)]
    pub fn enable(&self){
        unsafe{
            GLCore.enable(GLCapability::Blend)
        }
    }

    /// Disables blending.
    #[inline(always)]
    pub fn disable(&self){
        unsafe{
            GLCore.disable(GLCapability::Blend)
        }
    }

    /// Checks whether blending is enabled.
    #[inline(always)]
    pub fn is_enabled(&self)->bool{
        unsafe{
            GLCore.is_enabled(GLCapability::Blend)
        }
    }
}

impl Blend{
    /// Sets the blending constant colour.
    #[inline(always)]
    pub fn set_blending_colour(&self,[red,greed,blue,alpha]:Colour){
        unsafe{
            transmute::<usize,fn(f32,f32,f32,f32)>(self.glBlendColor)(red,greed,blue,alpha)
        }
    }

    /// Returns the blending constant colour.
    #[inline(always)]
    pub fn get_blending_colour(&self)->Colour{
        unsafe{
            let mut colour=[0f32;4];
            GLCore.get_float_v(BLEND_COLOR,colour.get_unchecked_mut(0));
            colour
        }
    }

    /// Writes the blending constant colour to `colour`.
    #[inline(always)]
    pub fn write_blending_colour(&self,colour:&mut Colour){
        unsafe{
            GLCore.get_float_v(BLEND_COLOR,colour.get_unchecked_mut(0))
        }
    }
}

impl Blend{
    /// Sets the blending functions.
    #[inline(always)]
    pub fn set_function(&self,sourse_factor:BlendingFunction,destination_factor:BlendingFunction){
        unsafe{
            transmute::<usize,fn(BlendingFunction,BlendingFunction)>(self.glBlendFunc)(sourse_factor,destination_factor)
        }
    }

    /// Sets the blending functions for the RBG and Alpha colour components separately.
    #[inline(always)]
    pub fn set_function_separate(
        &self,
        sourse_factor_rgb:BlendingFunction,
        destination_factor_rgb:BlendingFunction,
        sourse_factor_alpha:BlendingFunction,
        destination_factor_alpha:BlendingFunction,
    ){
        unsafe{
            transmute::<usize,fn(BlendingFunction,BlendingFunction,BlendingFunction,BlendingFunction)>(self.glBlendFuncSeparate)(
                sourse_factor_rgb,
                destination_factor_rgb,
                sourse_factor_alpha,
                destination_factor_alpha
            )
        }
    }

    /// Returns the source blending function for the RBG colour components.
    #[inline(always)]
    pub fn get_function_src_rgb(&self)->BlendingFunction{
        unsafe{
            let mut function=BlendingFunction::One;
            GLCore.get_integer_v(BLEND_SRC_RGB,transmute(&mut function));
            function
        }
    }

    /// Writes the source blending function for the RBG colour components to `function`.
    #[inline(always)]
    pub fn write_function_src_rgb(&self,function:&mut BlendingFunction){
        unsafe{
            GLCore.get_integer_v(BLEND_SRC_RGB,transmute(function))
        }
    }

    /// Returns the source blending function for the Alpha colour component.
    #[inline(always)]
    pub fn get_function_src_alpha(&self)->BlendingFunction{
        unsafe{
            let mut function=BlendingFunction::One;
            GLCore.get_integer_v(BLEND_SRC_ALPHA,transmute(&mut function));
            function
        }
    }

    /// Writes the souse blending function for the Alpha colour component to the `function`.
    #[inline(always)]
    pub fn write_function_src_alpha(&self,function:&mut BlendingFunction){
        unsafe{
            GLCore.get_integer_v(BLEND_SRC_ALPHA,transmute(function))
        }
    }

    /// Returns the destination blending function for the RBG colour components.
    #[inline(always)]
    pub fn get_function_dst_rgb(&self)->BlendingFunction{
        unsafe{
            let mut function=BlendingFunction::Zero;
            GLCore.get_integer_v(BLEND_DST_RGB,transmute(&mut function));
            function
        }
    }

    /// Writes the destination blending function for the RBG colour components to `function`.
    #[inline(always)]
    pub fn write_function_dst_rgb(&self,function:&mut BlendingFunction){
        unsafe{
            GLCore.get_integer_v(BLEND_DST_RGB,transmute(function))
        }
    }

    /// Returns the destination blending function for the Alpha colour component.
    #[inline(always)]
    pub fn get_function_dst_alpha(&self)->BlendingFunction{
        unsafe{
            let mut function=BlendingFunction::Zero;
            GLCore.get_integer_v(BLEND_DST_ALPHA,transmute(&mut function));
            function
        }
    }

    /// Writes the blending function for the Alpha colour component to `function`.
    #[inline(always)]
    pub fn write_function_dst_alpha(&self,function:&mut BlendingFunction){
        unsafe{
            GLCore.get_integer_v(BLEND_DST_ALPHA,transmute(function))
        }
    }
}

impl Blend{
    /// Sets the equation used for both the RGB blending equation and the Alpha blend equation.
    #[inline(always)]
    pub fn set_equation(&self,equation:BlendingEquation){
        unsafe{
            transmute::<usize,fn(BlendingEquation)>(self.glBlendEquation)(equation)
        }
    }

    /// Sets the equation for the RGB blending equation and the Alpha blend equation sepatately.
    #[inline(always)]
    pub fn set_equation_separate(
        &self,
        equation_rgb:BlendingEquation,
        equation_alpha:BlendingEquation
    ){
        unsafe{
            transmute::<usize,fn(BlendingEquation,BlendingEquation)>(self.glBlendEquationSeparate)(equation_rgb,equation_alpha)
        }
    }

    /// Returns the RGB blending equation.
    #[inline(always)]
    pub fn get_equation_rbg(&self)->BlendingEquation{
        unsafe{
            let mut equation=BlendingEquation::Addition;
            GLCore.get_integer_v(BLEND_EQUATION_RGB,transmute(&mut equation));
            equation
        }
    }

    /// Writes the RGB blending equation to `equation`.
    #[inline(always)]
    pub fn write_equation_rbg(&self,equation:&mut BlendingEquation){
        unsafe{
            GLCore.get_integer_v(BLEND_EQUATION_RGB,transmute(equation))
        }
    }

    /// Returns the Alpha blending equation.
    #[inline(always)]
    pub fn get_equation_alpha(&self)->BlendingEquation{
        unsafe{
            let mut equation=BlendingEquation::Addition;
            GLCore.get_integer_v(BLEND_EQUATION_ALPHA,transmute(&mut equation));
            equation
        }
    }

    /// Writes the Alpha blending equation to `equation`.
    #[inline(always)]
    pub fn write_equation_alpha(&self,equation:&mut BlendingEquation){
        unsafe{
            GLCore.get_integer_v(BLEND_EQUATION_ALPHA,transmute(equation));
        }
    }
}