-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
311 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,24 @@ | ||
[package] | ||
name = "lesson_25_x_render_gl" | ||
version = "0.1.0" | ||
authors = [] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
gl = { path = "../../lib/gl" } | ||
resources = { path = "../../lib/resources" } | ||
lesson_25_x_render_gl_derive = { path = "../render_gl_derive" } | ||
half = "1.1.1" | ||
vec-2-10-10-10 = "0.1.2" | ||
nalgebra = "0.16" | ||
ncollide3d = "0.17" | ||
failure = "0.1.3" | ||
font-kit = { version = "0.1.0" } | ||
euclid = "0.18.2" | ||
image = "0.20.1" | ||
lyon_tessellation = "0.11.0" | ||
lyon_path = "0.11.0" | ||
int_hash = "0.1.1" | ||
slotmap = "0.3" | ||
log = "0.4.6" | ||
floating-duration = "0.1.2" |
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,3 @@ | ||
#[macro_use] extern crate failure; | ||
|
||
pub mod render_target; |
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,90 @@ | ||
use nalgebra as na; | ||
use failure; | ||
use gl; | ||
use gl::types::*; | ||
|
||
#[derive(Debug, Fail)] | ||
pub enum Error { | ||
#[fail(display = "Render target creation failed. The framebuffer was not complete.")] | ||
TheFramebufferWasNotComplete, | ||
} | ||
|
||
pub struct DefaultRenderTarget { | ||
size: na::Vector2<i32>, | ||
gl: gl::Gl, | ||
} | ||
|
||
impl DefaultRenderTarget { | ||
pub fn new(gl: &gl::Gl, size: na::Vector2<i32>) -> DefaultRenderTarget { | ||
DefaultRenderTarget { | ||
size, | ||
gl: gl.clone(), | ||
} | ||
} | ||
|
||
pub fn bind(&self) { | ||
unsafe { | ||
self.gl.BindFramebuffer(gl::FRAMEBUFFER, 0); | ||
} | ||
} | ||
} | ||
|
||
pub struct FramebufferTarget { | ||
size: na::Vector2<i32>, | ||
fb: GLuint, | ||
tex: GLuint, | ||
gl: gl::Gl, | ||
} | ||
|
||
impl FramebufferTarget { | ||
pub fn new(gl: &gl::Gl, size: na::Vector2<i32>) -> Result<FramebufferTarget, Error> { | ||
let mut tex = 0; | ||
let mut fb = 0; | ||
|
||
unsafe { | ||
gl.GenFramebuffers(1, &mut fb); | ||
gl.BindFramebuffer(gl::FRAMEBUFFER, fb); | ||
|
||
gl.GenTextures(1, &mut tex); | ||
gl.BindTexture(gl::TEXTURE_2D, tex); | ||
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32); | ||
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32); | ||
|
||
gl.FramebufferTexture(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, tex, 0); | ||
|
||
gl.TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA as i32, size.x, size.y, 0, gl::RGBA, gl::UNSIGNED_BYTE, std::ptr::null()); | ||
|
||
let complete = gl.CheckFramebufferStatus(gl::FRAMEBUFFER); | ||
|
||
gl.BindFramebuffer(gl::FRAMEBUFFER, 0); | ||
gl.BindTexture(gl::TEXTURE_2D, 0); | ||
|
||
if complete != gl::FRAMEBUFFER_COMPLETE { | ||
return Err(Error::TheFramebufferWasNotComplete); | ||
} | ||
} | ||
|
||
Ok(FramebufferTarget { | ||
size, | ||
fb, | ||
tex, | ||
gl: gl.clone(), | ||
}) | ||
} | ||
|
||
pub fn bind(&self) { | ||
unsafe { | ||
self.gl.BindFramebuffer(gl::FRAMEBUFFER, self.fb); | ||
} | ||
} | ||
} | ||
|
||
impl Drop for FramebufferTarget | ||
{ | ||
fn drop(&mut self){ | ||
unsafe{ | ||
self.gl.DeleteFramebuffers(1, &self.fb); | ||
self.gl.DeleteTextures(1, &self.tex); | ||
} | ||
} | ||
} |
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,11 @@ | ||
[package] | ||
name = "lesson_25_x_render_gl_derive" | ||
version = "0.1.0" | ||
authors = [] | ||
|
||
[dependencies] | ||
quote = "0.3.15" | ||
syn = "0.11.11" | ||
|
||
[lib] | ||
proc-macro = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#![recursion_limit = "128"] | ||
|
||
extern crate proc_macro; | ||
extern crate syn; | ||
#[macro_use] | ||
extern crate quote; | ||
|
||
#[proc_macro_derive(VertexAttribPointers, attributes(location, divisor))] | ||
pub fn vertex_attrib_pointers_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let s = input.to_string(); | ||
let ast = syn::parse_derive_input(&s).unwrap(); | ||
let gen = generate_impl(&ast); | ||
gen.parse().unwrap() | ||
} | ||
|
||
fn generate_impl(ast: &syn::DeriveInput) -> quote::Tokens { | ||
let ident = &ast.ident; | ||
let generics = &ast.generics; | ||
let where_clause = &ast.generics.where_clause; | ||
let fields_vertex_attrib_pointer = generate_vertex_attrib_pointer_calls(&ast.body); | ||
|
||
quote!{ | ||
impl #ident #generics #where_clause { | ||
#[allow(unused_variables)] | ||
pub fn vertex_attrib_pointers(gl: &::gl::Gl) { | ||
let stride = ::std::mem::size_of::<Self>(); | ||
let offset = 0; | ||
|
||
#(#fields_vertex_attrib_pointer)* | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn generate_vertex_attrib_pointer_calls(body: &syn::Body) -> Vec<quote::Tokens> { | ||
match body { | ||
&syn::Body::Enum(_) => panic!("VertexAttribPointers can not be implemented for enums"), | ||
&syn::Body::Struct(syn::VariantData::Unit) => { | ||
panic!("VertexAttribPointers can not be implemented for Unit structs") | ||
} | ||
&syn::Body::Struct(syn::VariantData::Tuple(_)) => { | ||
panic!("VertexAttribPointers can not be implemented for Tuple structs") | ||
} | ||
&syn::Body::Struct(syn::VariantData::Struct(ref s)) => s | ||
.iter() | ||
.map(generate_struct_field_vertex_attrib_pointer_call) | ||
.collect(), | ||
} | ||
} | ||
|
||
fn generate_struct_field_vertex_attrib_pointer_call(field: &syn::Field) -> quote::Tokens { | ||
let field_name = match field.ident { | ||
Some(ref i) => format!("{}", i), | ||
None => String::from(""), | ||
}; | ||
let field_ty = &field.ty; | ||
|
||
if let Some(location_attr) = field | ||
.attrs | ||
.iter() | ||
.filter(|a| a.value.name() == "location") | ||
.next() | ||
{ | ||
let location_value: usize = match location_attr.value { | ||
syn::MetaItem::NameValue(_, syn::Lit::Str(ref s, _)) => { | ||
s.parse().unwrap_or_else(|_| { | ||
panic!( | ||
"Field {} location attribute value must contain an integer", | ||
field_name | ||
) | ||
}) | ||
} | ||
_ => panic!( | ||
"Field {} location attribute value must be a string literal", | ||
field_name | ||
), | ||
}; | ||
|
||
let divisor_call = match field | ||
.attrs | ||
.iter() | ||
.filter(|a| a.value.name() == "divisor") | ||
.next() | ||
{ | ||
Some(attr) => { | ||
let divisor_value: u32 = match attr.value { | ||
syn::MetaItem::NameValue(_, syn::Lit::Str(ref s, _)) => { | ||
s.parse().unwrap_or_else(|_| { | ||
panic!( | ||
"Field {} divisor attribute value must contain an integer", | ||
field_name | ||
) | ||
}) | ||
} | ||
_ => panic!( | ||
"Field {} divisor attribute value must be a string literal", | ||
field_name | ||
), | ||
}; | ||
|
||
quote! { | ||
gl.VertexAttribDivisor(#location_value as u32, #divisor_value); | ||
} | ||
} | ||
None => quote!{}, | ||
}; | ||
|
||
quote! { | ||
let location = #location_value; | ||
unsafe { | ||
#field_ty::vertex_attrib_pointer(gl, stride, location, offset); | ||
#divisor_call | ||
} | ||
let offset = offset + ::std::mem::size_of::<#field_ty>(); | ||
} | ||
} else { | ||
quote! { | ||
let offset = offset + ::std::mem::size_of::<#field_ty>(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ use resources::{ | |
Resources, | ||
backend::FileSystem | ||
}; | ||
use winput; | ||
|
||
mod debug; | ||
mod onion; | ||
|
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,7 @@ | ||
[package] | ||
name = "winput" | ||
version = "0.1.0" | ||
authors = [] | ||
edition = "2018" | ||
|
||
[dependencies] |
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,12 @@ | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct WindowDimensions { | ||
pub size: WindowSize, | ||
pub hdpi_size: WindowSize, | ||
pub high_dpi: bool, | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct WindowSize { | ||
pub width: i32, | ||
pub height: i32, | ||
} |