forked from Dav1dde/glad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust: added a loader, a Cargo.toml and an example.
- Loading branch information
Showing
7 changed files
with
249 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
extern crate glfw; | ||
extern crate glad_gl; | ||
use glfw::{Action, Context, Key}; | ||
use glad_gl::gl; | ||
|
||
|
||
fn main() { | ||
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); | ||
|
||
let (mut window, events) = glfw.create_window(300, 300, "[glad] Rust - OpenGL with GLFW", glfw::WindowMode::Windowed) | ||
.expect("Failed to create GLFW window."); | ||
|
||
window.set_key_polling(true); | ||
window.make_current(); | ||
|
||
gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void); | ||
|
||
while !window.should_close() { | ||
glfw.poll_events(); | ||
for (_, event) in glfw::flush_messages(&events) { | ||
handle_window_event(&mut window, event); | ||
} | ||
|
||
unsafe { | ||
gl::ClearColor(0.7, 0.9, 0.1, 1.0); | ||
gl::Clear(gl::GL_COLOR_BUFFER_BIT); | ||
} | ||
|
||
window.swap_buffers(); | ||
} | ||
} | ||
|
||
fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) { | ||
match event { | ||
glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { | ||
window.set_should_close(true) | ||
} | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "glad-{{ feature_set.name }}" | ||
version = "{{ version }}" | ||
authors = ["David Herberth <[email protected]>"] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
pub use self::types::*; | ||
pub use self::enumerations::*; | ||
pub use self::functions::*; | ||
|
||
use std::os::raw; | ||
|
||
pub struct FnPtr { | ||
ptr: *const raw::c_void, | ||
} | ||
|
||
impl FnPtr { | ||
pub fn empty() -> FnPtr { | ||
FnPtr { ptr: FnPtr::not_initialized as *const raw::c_void } | ||
} | ||
|
||
pub fn load<F>(&mut self, loadfn: &mut F, name: &'static str) where F: FnMut(&'static str) -> *const raw::c_void { | ||
let loaded = loadfn(name); | ||
if !loaded.is_null() { | ||
self.ptr = loaded; | ||
}; | ||
} | ||
|
||
#[inline(never)] | ||
fn not_initialized() -> ! { panic!("{{ feature_set.name }}: function not initialized") } | ||
} | ||
|
||
pub mod types { | ||
{% include 'types/' + spec.name + '.rs' %} | ||
} | ||
|
||
pub mod enumerations { | ||
#![allow(dead_code, non_upper_case_globals)] | ||
|
||
use super::types::*; | ||
|
||
{% for enum in feature_set.enums %} | ||
pub const {{ enum.name }}: {{ enum | enum_type }} = {{ enum.value }}; | ||
{% endfor %} | ||
} | ||
|
||
pub mod functions { | ||
#![allow(non_snake_case, unused_variables, dead_code)] | ||
|
||
use std::mem; | ||
use super::storage; | ||
use super::types::*; | ||
|
||
{% for command in feature_set.commands %} | ||
#[inline] pub unsafe fn {{ command.name|no_prefix }}({{ command|params }}) -> {{ command.proto.ret|type }} { mem::transmute::<_, extern "system" fn({{ command|params('types') }}) -> {{ command.proto.ret|type }}>(storage::{{ command.name|no_prefix }}.ptr)({{ command|params('names') }}) } | ||
{% endfor %} | ||
} | ||
|
||
mod storage { | ||
#![allow(non_snake_case, non_upper_case_globals)] | ||
|
||
use super::FnPtr; | ||
use std::os::raw; | ||
|
||
{% for command in feature_set.commands %} | ||
pub static mut {{ command.name|no_prefix }}: FnPtr = FnPtr { ptr: FnPtr::not_initialized as *const raw::c_void }; | ||
{% endfor %} | ||
} | ||
|
||
pub fn load<F>(mut loadfn: F) where F: FnMut(&'static str) -> *const raw::c_void { | ||
unsafe { | ||
{% for command in feature_set.commands %} | ||
storage::{{ command.name | no_prefix }}.load(&mut loadfn, "{{ command.name }}"); | ||
{% endfor %} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub mod gl; | ||
pub mod {{ feature_set.name }}; |
Oops, something went wrong.