summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 9568b6c958de0e290acc587c00057bddae6a9db6 (plain)
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
//! OpenGL bindings.
#![deny(clippy::all, clippy::pedantic, missing_docs)]

use opengl_registry_macros::for_each_opengl_command;

for_each_opengl_command! {
    #[doc = stringify!(_gl_command_)]
    #[allow(non_snake_case, clippy::too_many_arguments)]
    pub unsafe fn _gl_command_(_gl_command_args_) -> _gl_command_ret_type_ {
        type GLFunc =
            unsafe extern "C" fn(_gl_command_arg_types_) -> _gl_command_ret_type_;

        let gl_func = std::mem::transmute::<_, GLFunc>(functions::_gl_command_);

        gl_func(_gl_command_arg_names_)
    }
}

mod ffi
{
    #![allow(
        non_upper_case_globals,
        non_camel_case_types,
        non_snake_case,
        unused,
        missing_docs,
        clippy::unreadable_literal
    )]

    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

pub use ffi::*;

/// Loads OpenGL function pointers using the given function address retriever function.
pub fn load_with<GetProcAddrFn>(get_proc_addr: GetProcAddrFn)
where
    GetProcAddrFn: Fn(&str) -> unsafe extern "C" fn(),
{
    for_each_opengl_command! {
        unsafe {
            functions::_gl_command_ = get_proc_addr(stringify!(_gl_command_));
        }
    };
}

mod functions
{
    use opengl_registry_macros::for_each_opengl_command;

    for_each_opengl_command!(
        #[doc = stringify!(_gl_command_)]
        #[allow(non_upper_case_globals)]
        pub static mut _gl_command_: unsafe extern "C" fn() = function_not_loaded;
    );

    extern "C" fn function_not_loaded()
    {
        panic!("OpenGL function not loaded");
    }
}