Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeReV committed Dec 2, 2020
1 parent 45fb732 commit 2d1fd91
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 336 deletions.
2 changes: 0 additions & 2 deletions src/background.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::quad;
use crate::render_gl::{Error, FrameBuffer, Program, Shader, Texture};
use failure;
use gl;
use nalgebra as na;
use std::rc::Rc;

Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Config {
match ini {
Ok(ini) => ini
.get_from(None::<&str>, BACKGROUND_KEY)
.map(|s| PathBuf::from(s)),
.map(PathBuf::from),
Err(_) => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let _ = write!(&mut result, "{}", cause);
if let Some(backtrace) = cause.backtrace() {
let backtrace_str = format!("{}", backtrace);
if backtrace_str.len() > 0 {
if !backtrace_str.is_empty() {
let _ = writeln!(&mut result, " This happened at {}", backtrace);
} else {
let _ = writeln!(&mut result);
Expand Down
6 changes: 6 additions & 0 deletions src/droplet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ impl Droplet {
}
}
}

impl Default for Droplet {
fn default() -> Self {
Self::new()
}
}
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ mod droplets;
mod quad;
mod rain;
pub mod render_gl;
pub mod resources;
mod vertex;

use crate::config::Config;
Expand Down Expand Up @@ -201,7 +200,6 @@ fn run(
}
Event::LoopDestroyed => {
context.take(); // Make sure it drops first
return;
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::CursorMoved { position, .. } => match initial_mouse_position {
Expand Down
1 change: 0 additions & 1 deletion src/quad.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::render_gl::buffer;
use crate::vertex::Vertex;
use gl;

pub struct Quad {
_vbo: buffer::ArrayBuffer,
Expand Down
8 changes: 4 additions & 4 deletions src/rain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ impl Rain {

let path = config
.cached_background()
.unwrap_or(fallback_background.clone());
.unwrap_or_else(|| fallback_background.clone());

let mut options = TextureLoadOptions::from_res_rgb(path.to_str().unwrap());
let mut options = TextureLoadOptions::rgb();
options.gen_mipmaps = true;

let image = image::open(&path)
Expand Down Expand Up @@ -484,7 +484,7 @@ impl Rain {
gl::FLOAT, // data type
gl::FALSE,
std::mem::size_of::<na::Vector3<f32>>() as gl::types::GLint,
0 as *const gl::types::GLvoid,
std::ptr::null(),
);
}
instance_vbo.unbind();
Expand Down Expand Up @@ -533,7 +533,7 @@ impl Rain {
if droplet.seed <= 0 {
droplet.seed =
(droplet.size * 0.5 * rng.gen_range(0.0, 1.0) * fps).floor() as i32;
droplet.skipping = droplet.skipping == false;
droplet.skipping = !droplet.skipping;
droplet.slowing = true;
}

Expand Down
15 changes: 7 additions & 8 deletions src/render_gl/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use gl;

pub trait BufferType {
pub trait BufferType {
const BUFFER_TYPE: gl::types::GLuint;
}

Expand Down Expand Up @@ -96,14 +94,15 @@ where
(size * ::std::mem::size_of::<T>()) as gl::types::GLsizeiptr, // length
gl::MAP_WRITE_BIT | gl::MAP_INVALIDATE_RANGE_BIT, // usage
);
if ptr == ::std::ptr::null_mut() {
if ptr.is_null() {
return None;
}
return Some(MappedBuffer {

Some(MappedBuffer {
gl: self.gl.clone(),
data: ::std::slice::from_raw_parts_mut(ptr as *mut T, size),
_marker: ::std::marker::PhantomData,
});
})
}
}

Expand All @@ -113,7 +112,7 @@ where
{
fn drop(&mut self) {
unsafe {
self.gl.DeleteBuffers(1, &mut self.vbo);
self.gl.DeleteBuffers(1, &self.vbo);
}
}
}
Expand Down Expand Up @@ -195,7 +194,7 @@ impl VertexArray {
impl Drop for VertexArray {
fn drop(&mut self) {
unsafe {
self.gl.DeleteVertexArrays(1, &mut self.vao);
self.gl.DeleteVertexArrays(1, &self.vao);
}
}
}
3 changes: 1 addition & 2 deletions src/render_gl/color_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gl;
use nalgebra as na;
use nalgebra as na;

pub struct ColorBuffer {
pub color: na::Vector4<f32>,
Expand Down
2 changes: 0 additions & 2 deletions src/render_gl/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![allow(non_camel_case_types)]

use gl;

#[derive(Copy, Clone, Debug)]
#[repr(C, packed)]
pub struct i8_ {
Expand Down
50 changes: 1 addition & 49 deletions src/render_gl/shader.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
use gl;
use nalgebra as na;
use std;
use nalgebra as na;
use std::ffi::{CStr, CString};
use std::iter::Iterator;

#[derive(Debug, Fail)] // derive Fail, in addition to Debug
pub enum Error {
#[fail(display = "Failed to load resource {}", name)]
ResourceLoad {
name: String,
#[cause]
inner: resources::Error,
},
#[fail(display = "Can not determine shader type for resource {}", name)]
CanNotDetermineShaderTypeForResource { name: String },
#[fail(display = "Failed to compile shader {}: {}", name, message)]
CompileError { name: String, message: String },
#[fail(display = "Failed to link program {}: {}", name, message)]
Expand Down Expand Up @@ -76,20 +66,6 @@ impl Program {
})
}

pub fn from_res(gl: &gl::Gl, res: &Resources, name: &str) -> Result<Program, Error> {
const POSSIBLE_EXT: [&str; 2] = [".vert", ".frag"];

let shaders = POSSIBLE_EXT
.iter()
.map(|file_extension| Shader::from_res(gl, res, &format!("{}{}", name, file_extension)))
.collect::<Result<Vec<Shader>, Error>>()?;

Program::from_shaders(gl, &shaders[..]).map_err(|msg| Error::LinkError {
message: msg,
name: name.into(),
})
}

pub fn id(&self) -> gl::types::GLuint {
self.id
}
Expand Down Expand Up @@ -178,9 +154,6 @@ pub struct Shader {
gl: gl::Gl,
}

use crate::resources;
use crate::resources::Resources;

impl Shader {
pub fn from_source(
gl: &gl::Gl,
Expand Down Expand Up @@ -214,27 +187,6 @@ impl Shader {
Shader::from_source(gl, source, gl::FRAGMENT_SHADER)
}

pub fn from_res(gl: &gl::Gl, res: &Resources, name: &str) -> Result<Shader, Error> {
const POSSIBLE_EXT: [(&str, gl::types::GLenum); 2] =
[(".vert", gl::VERTEX_SHADER), (".frag", gl::FRAGMENT_SHADER)];

let shader_kind = POSSIBLE_EXT
.iter()
.find(|&&(file_extension, _)| name.ends_with(file_extension))
.map(|&(_, kind)| kind)
.ok_or_else(|| Error::CanNotDetermineShaderTypeForResource { name: name.into() })?;

let source = res.load_cstring(name).map_err(|e| Error::ResourceLoad {
name: name.into(),
inner: e,
})?;

Shader::from_source(gl, &source, shader_kind).map_err(|msg| Error::CompileError {
message: msg,
name: name.into(),
})
}

pub fn id(&self) -> gl::types::GLuint {
self.id
}
Expand Down
Loading

0 comments on commit 2d1fd91

Please sign in to comment.