Skip to content

Commit

Permalink
Shaders are now loaded via strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
thebracket committed Sep 17, 2019
1 parent 0aabd55 commit b5c6114
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 186 deletions.
12 changes: 0 additions & 12 deletions resources/backing.fs

This file was deleted.

11 changes: 0 additions & 11 deletions resources/backing.vs

This file was deleted.

17 changes: 0 additions & 17 deletions resources/console_no_bg.fs

This file was deleted.

17 changes: 0 additions & 17 deletions resources/console_no_bg.vs

This file was deleted.

16 changes: 0 additions & 16 deletions resources/console_with_bg.fs

This file was deleted.

17 changes: 0 additions & 17 deletions resources/console_with_bg.vs

This file was deleted.

26 changes: 0 additions & 26 deletions resources/scanlines.fs

This file was deleted.

11 changes: 0 additions & 11 deletions resources/scanlines.vs

This file was deleted.

1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -33,6 +33,7 @@ pub use self::simple_console::SimpleConsole;
pub use self::sparse_console::SparseConsole;
pub use self::textblock::{TextBlock, TextBuilder};
pub mod platform_specific;
pub mod shader_strings;

#[cfg(not(target_arch = "wasm32"))]
pub use glutin::event::VirtualKeyCode;
Expand Down
52 changes: 25 additions & 27 deletions src/platform_specific.rs
@@ -1,4 +1,4 @@
use super::{framebuffer::Framebuffer, quadrender, GameState, Rltk, Shader};
use super::{framebuffer::Framebuffer, quadrender, GameState, Rltk, Shader, shader_strings};
use image::GenericImageView;

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -29,8 +29,7 @@ pub struct WrappedContext {
pub fn init_raw<S: ToString>(
width_pixels: u32,
height_pixels: u32,
window_title: S,
path_to_shaders: S,
window_title: S
) -> Rltk {
let el = EventLoop::new();
let wb = WindowBuilder::new()
Expand All @@ -53,25 +52,25 @@ pub fn init_raw<S: ToString>(
// Load our basic shaders
let mut shaders: Vec<Shader> = Vec::new();

let shader_path = path_to_shaders.to_string();
shaders.push(Shader::new(
&gl,
"console_with_bg.vs",
"console_with_bg.fs",
&shader_path,
shader_strings::CONSOLE_WITH_BG_VS,
shader_strings::CONSOLE_WITH_BG_FS
));
shaders.push(Shader::new(
&gl,
"console_no_bg.vs",
"console_no_bg.fs",
&shader_path,
shader_strings::CONSOLE_NO_BG_VS,
shader_strings::CONSOLE_NO_BG_FS
));
shaders.push(Shader::new(
&gl,
shader_strings::BACKING_VS,
shader_strings::BACKING_FS
));
shaders.push(Shader::new(&gl, "backing.vs", "backing.fs", &shader_path));
shaders.push(Shader::new(
&gl,
"scanlines.vs",
"scanlines.fs",
&shader_path,
shader_strings::SCANLINES_VS,
shader_strings::SCANLINES_FS
));

// Build the backing frame-buffer
Expand Down Expand Up @@ -263,8 +262,7 @@ fn tock<GS: GameState>(
pub fn init_raw<S: ToString>(
width_pixels: u32,
height_pixels: u32,
window_title: S,
path_to_shaders: S,
window_title: S
) -> Rltk {
use wasm_bindgen::JsCast;
let canvas = web_sys::window()
Expand All @@ -287,25 +285,25 @@ pub fn init_raw<S: ToString>(
// Load our basic shaders
let mut shaders: Vec<Shader> = Vec::new();

let shader_path = path_to_shaders.to_string();
shaders.push(Shader::new(
&gl,
"console_with_bg.vs",
"console_with_bg.fs",
&shader_path,
shader_strings::CONSOLE_WITH_BG_VS,
shader_strings::CONSOLE_WITH_BG_FS
));
shaders.push(Shader::new(
&gl,
"console_no_bg.vs",
"console_no_bg.fs",
&shader_path,
shader_strings::CONSOLE_NO_BG_VS,
shader_strings::CONSOLE_NO_BG_FS
));
shaders.push(Shader::new(
&gl,
shader_strings::BACKING_VS,
shader_strings::BACKING_FS
));
shaders.push(Shader::new(&gl, "backing.vs", "backing.fs", &shader_path));
shaders.push(Shader::new(
&gl,
"scanlines.vs",
"scanlines.fs",
&shader_path,
shader_strings::SCANLINES_VS,
shader_strings::SCANLINES_FS
));

// Build the backing frame-buffer
Expand Down
11 changes: 4 additions & 7 deletions src/rltk.rs
Expand Up @@ -45,10 +45,9 @@ impl Rltk {
pub fn init_raw<S: ToString>(
width_pixels: u32,
height_pixels: u32,
window_title: S,
path_to_shaders: S,
window_title: S
) -> Rltk {
platform_specific::init_raw(width_pixels, height_pixels, window_title, path_to_shaders)
platform_specific::init_raw(width_pixels, height_pixels, window_title)
}

/// Quick initialization for when you just want an 8x8 font terminal
Expand All @@ -62,8 +61,7 @@ impl Rltk {
let mut context = Rltk::init_raw(
width_chars * 8,
height_chars * 8,
window_title,
path_to_shaders,
window_title
);
let font = context.register_font(font::Font::load(&font_path.to_string(), (8, 8)));
context.register_console(
Expand All @@ -84,8 +82,7 @@ impl Rltk {
let mut context = Rltk::init_raw(
width_chars * 8,
height_chars * 16,
window_title,
path_to_shaders,
window_title
);
let font = context.register_font(font::Font::load(&font_path.to_string(), (8, 16)));
context.register_console(
Expand Down
29 changes: 4 additions & 25 deletions src/shader.rs
@@ -1,7 +1,5 @@
use cgmath::Vector3;
use glow::HasContext;
use std::fs::File;
use std::io::Read;
use std::str;

#[allow(non_snake_case)]
Expand All @@ -16,31 +14,12 @@ pub struct Shader {
/// NOTE: mixture of `shader_s.h` and `shader_m.h` (the latter just contains
/// a few more setters for uniforms)
impl Shader {
pub fn new<S: ToString>(
pub fn new(
gl: &glow::Context,
vertex_path: S,
fragment_path: S,
path_to_shaders: S,
vertex_code: &str,
fragment_code: &str
) -> Shader {
let shader_path = path_to_shaders.to_string();
let vertex_path = format!("{}/{}", &shader_path, vertex_path.to_string());
let fragment_path = format!("{}/{}", &shader_path, fragment_path.to_string());

// 1. retrieve the vertex/fragment source code from filesystem
let mut v_shader_file =
File::open(&vertex_path).unwrap_or_else(|_| panic!("Failed to open {}", vertex_path));
let mut f_shader_file = File::open(&fragment_path)
.unwrap_or_else(|_| panic!("Failed to open {}", fragment_path));
let mut vertex_code = String::new();
let mut fragment_code = String::new();
v_shader_file
.read_to_string(&mut vertex_code)
.expect("Failed to read vertex shader");
f_shader_file
.read_to_string(&mut fragment_code)
.expect("Failed to read fragment shader");

// 2. compile shaders
// 1. compile shaders from strings
let shader;
unsafe {
// vertex shader
Expand Down

0 comments on commit b5c6114

Please sign in to comment.