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
#![no_std]
#![allow(bad_style)]
#![allow(unused_macros)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::let_unit_value)]
#![allow(clippy::let_and_return)]

//! Global GL loader and bindings for OpenGL 3.3 Core.
//!
//! It was generated using `gl_generator`, so it works basically like the `gl`
//! crate does.
//!
//! ```ignore
//! use ogl33 as gl;
//!
//! gl::load_with(|ptr| SDL_GL_GetProcAddress(ptr));
//!
//! gl::ClearColor(0.5, 0.5, 0.5, 1.0);
//! ```
//!
//! The main difference from the `gl` crate is that this crate _only_ loads
//! OpenGL 3.3 Core, not any other version of OpenGL. It might sound silly, but
//! not even having the other functions present in the crate while developing on
//! Windows or Linux helps to avoid relying on something that won't be there
//! when you go to make the Mac version.
//!
//! ## Features
//!
//! There's two features you can turn on. Both will print stuff to the console
//! if `debug_assertions` are enabled.
//!
//! * `debug_trace_messages`: If enabled, immediately _before_ a call to a GL
//!   function it'll print the function's name. I expect that you'd keep this
//!   off most of the time, but if you're getting segfaults it might help to get
//!   a message before each GL call and hopefully you can spot the problem.
//! * `debug_error_checks`: If enabled, immediately _after_ all calls to GL
//!   there's an additional call to `glGetError`. If the error value is
//!   something other than `NO_ERROR` then you'll immediately get an error
//!   message printed showing the name of the function, the arguments you
//!   passed, and the error code.
//!
//! Unfortunately, the `glDebugMessageCallback` function didn't become part of
//! Core until well after 3.3. However, even in 3.3 you can try the
//! `ARB_debug_output` extension if you want to have additional debug messaging.
//!
//! ## `no_std` Support
//!
//! This library is `no_std` friendly! It's just bindings and loader callbacks
//! after all.
//!
//! However, having either of the above features for message printing enabled
//! **will** cause the crate to link to the `std` crate whenever
//! `debug_assertions` are on. It's the price we pay for debugging messages.

#[cfg(all(
  debug_assertions,
  any(feature = "debug_error_checks", feature = "trace_messages")
))]
extern crate std;

macro_rules! error {
  ($($tokens:tt)*) => {
    #[cfg(all(debug_assertions, feature = "debug_error_checks"))]
    {
      std::println!($($tokens)*);
    }
  }
}

macro_rules! trace {
  ($($tokens:tt)*) => {
    #[cfg(all(debug_assertions, feature = "trace_messages"))]
    {
      std::println!($($tokens)*);
    }
  }
}

mod global_loader;
pub use global_loader::*;

use core::{
  mem::transmute,
  ptr::{null_mut, NonNull},
  sync::atomic::{AtomicPtr, Ordering},
};
type OptVoidPtr = Option<NonNull<c_void>>;

pub use core::ffi::c_void;

#[cfg(not(windows))]
pub use libc::{
  c_char, c_double, c_float, c_int, c_long, c_longlong, c_short, c_uchar,
  c_uint, c_ulong, c_ulonglong, c_ushort,
};
#[cfg(windows)]
pub type c_char = i8;
#[cfg(windows)]
pub type c_schar = i8;
#[cfg(windows)]
pub type c_uchar = u8;
#[cfg(windows)]
pub type c_short = i16;
#[cfg(windows)]
pub type c_ushort = u16;
#[cfg(windows)]
pub type c_int = i32;
#[cfg(windows)]
pub type c_uint = u32;
#[cfg(windows)]
pub type c_long = i32;
#[cfg(windows)]
pub type c_ulong = u32;
#[cfg(windows)]
pub type c_longlong = i64;
#[cfg(windows)]
pub type c_ulonglong = u64;
#[cfg(windows)]
pub type c_float = f32;
#[cfg(windows)]
pub type c_double = f64;