file handling relocated to its own file, some cleanup

type-render
korin 3 years ago
parent f267c12e5c
commit 67cd6bb02f
  1. 12
      src/file.rs
  2. 2
      src/input.rs
  3. 9
      src/main.rs
  4. 48
      src/render.rs

@ -0,0 +1,12 @@
//! File management
use std::{fs::File, /* path::Path ,*/ io::Read};
/// Reads the file and turns it into a Vec of u8s
pub fn read_file(file_name: String) -> Result<Vec<u8>, String> {
let mut file_content = Vec::new();
let mut file = File::open(&file_name).expect("Unable to open file");
file.read_to_end(&mut file_content).expect("Unable to read");
Ok(file_content)
}

@ -43,6 +43,6 @@ mod keybinds {
} }
} }
fn input(buffer: &str, modifiers: crate::ModifierKeys, key: Keycode) { fn process_input(buffer: &str, modifiers: crate::ModifierKeys, key: Keycode) {
} }

@ -8,6 +8,7 @@ use sdl2::{
mod render; mod render;
mod input; mod input;
mod file;
static SCREEN_WIDTH: u32 = 1280; static SCREEN_WIDTH: u32 = 1280;
static SCREEN_HEIGHT: u32 = 720; static SCREEN_HEIGHT: u32 = 720;
@ -25,11 +26,6 @@ struct ModifierKeys {
shift: bool, shift: bool,
} }
// struct EditorGraphics {
// canvas: Canvas<Window>,
// glyph_atlas: GlyphAtlas,
// }
pub fn main() -> Result<(), String> { pub fn main() -> Result<(), String> {
// Initialize clipboard // Initialize clipboard
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap(); let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap();
@ -62,7 +58,7 @@ pub fn main() -> Result<(), String> {
let mut selection_anchor: Option<usize> = None; let mut selection_anchor: Option<usize> = None;
// Initialize graphics data and values // Initialize graphics data and values
let glyph_atlas = render::generate_glyph_data(); let glyph_atlas = render::generate_glyph_data()?;
// Easier way to please the borrow checker // Easier way to please the borrow checker
macro_rules! draw { macro_rules! draw {
@ -331,6 +327,7 @@ pub fn main() -> Result<(), String> {
std::thread::sleep(std::time::Duration::new(0, 1_000_000_000 / REFRESH_RATE)); std::thread::sleep(std::time::Duration::new(0, 1_000_000_000 / REFRESH_RATE));
} }
format!("{selection_anchor:?}");
println!("{buffer}"); println!("{buffer}");
Ok(()) Ok(())
} }

@ -11,7 +11,7 @@ use sdl2::{
video::Window, video::Window,
}; };
use std::{fs::File, path::Path, io::Read}; use crate::file;
type Glyph = Vec<Point>; type Glyph = Vec<Point>;
@ -31,25 +31,11 @@ pub struct GlyphAtlas {
metrics: GlyphMetrics, metrics: GlyphMetrics,
} }
/// Reads the file and turns it into a Vec of u8s pub fn generate_glyph_data() -> Result<GlyphAtlas, String> {
fn read_file(file_name: String) -> Vec<u8> {
let path = Path::new(&file_name);
if !path.exists() {
return String::from("Not Found!").into();
}
let mut file_content = Vec::new();
let mut file = File::open(&file_name).expect("Unable to open file");
file.read_to_end(&mut file_content).expect("Unable to read");
file_content
}
pub fn generate_glyph_data() -> GlyphAtlas {
// Retrieve font data from file // Retrieve font data from file
// TODO: Get crate path instead of working directory path
let file_path = String::from("./fonts/Terminus14x8.data"); let file_path = String::from("./fonts/Terminus14x8.data");
let contents = read_file(file_path); let contents = file::read_file(file_path)?;
// Get glyph metrics // Get glyph metrics
let glyph_metrics = GlyphMetrics { width: 8, height: 16 }; let glyph_metrics = GlyphMetrics { width: 8, height: 16 };
@ -60,9 +46,9 @@ pub fn generate_glyph_data() -> GlyphAtlas {
let width_right_byte = contents[1]; let width_right_byte = contents[1];
let width_bytes = [width_left_byte, width_right_byte]; let width_bytes = [width_left_byte, width_right_byte];
let width = u16::from_be_bytes(width_bytes); let width = u16::from_be_bytes(width_bytes);
// println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}"); println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}");
let gtable_prune = &contents[width as usize + 2 ..]; let pruned_glyph_table = &contents[width as usize + 2 ..];
// Generate the glyph atlas // Generate the glyph atlas
let mut glyph_atlas: Vec<Glyph> = vec![]; let mut glyph_atlas: Vec<Glyph> = vec![];
@ -78,13 +64,13 @@ pub fn generate_glyph_data() -> GlyphAtlas {
let offset = glyph * glyph_width as u16; let offset = glyph * glyph_width as u16;
let position = (x + multiplier + offset) as usize; let position = (x + multiplier + offset) as usize;
if gtable_prune[position] == 1 { if pruned_glyph_table[position] == 1 {
new_glyph.push(Point::new(x as i32, y as i32)); new_glyph.push(Point::new(x as i32, y as i32));
} }
} }
glyph_atlas.push(new_glyph); glyph_atlas.push(new_glyph);
} }
GlyphAtlas { glyphs: glyph_atlas, metrics: glyph_metrics } Ok(GlyphAtlas { glyphs: glyph_atlas, metrics: glyph_metrics })
} }
/// Method for generating points to render, using given string /// Method for generating points to render, using given string
@ -94,14 +80,14 @@ fn draw_text(glyph_atlas: &GlyphAtlas, content: &str, offset: Point) -> Vec<Poin
let glyph_width = glyph_atlas.metrics.width; let glyph_width = glyph_atlas.metrics.width;
let glyph_height = glyph_atlas.metrics.height; let glyph_height = glyph_atlas.metrics.height;
let lines = content.split('\n'); let content_lines = content.split('\n');
for (y, chars) in lines.enumerate() { for (y, line) in content_lines.enumerate() {
for (x, chara) in chars.chars().enumerate() { for (x, character) in line.chars().enumerate() {
let index; let index;
if chara.is_lowercase() { if character.is_lowercase() {
index = chara.to_ascii_lowercase() as usize; index = character.to_ascii_lowercase() as usize;
} else { } else {
index = chara.to_ascii_uppercase() as usize; index = character.to_ascii_uppercase() as usize;
} }
for pixel in &glyph_atlas.glyphs[index - 32] { for pixel in &glyph_atlas.glyphs[index - 32] {
@ -114,7 +100,9 @@ fn draw_text(glyph_atlas: &GlyphAtlas, content: &str, offset: Point) -> Vec<Poin
); );
points.push(positioned_pixel); points.push(positioned_pixel);
} }
// TODO: Limit X drawing
} }
// TODO: Limit Y drawing
} }
points points
} }
@ -127,9 +115,9 @@ pub fn draw_cursor(glyph_atlas: &GlyphAtlas, mut cursor_position: usize, content
let mut y = 0; let mut y = 0;
if cursor_position > 0 { if cursor_position > 0 {
cursor_position = cursor_position.checked_sub(1).unwrap_or(0); cursor_position = cursor_position.checked_sub(1).unwrap_or(0);
for (idx, chara) in content.chars().enumerate() { for (idx, character) in content.chars().enumerate() {
x += 1; x += 1;
if chara == '\n' { if character == '\n' {
x = 0; x = 0;
y += 1; y += 1;
} }

Loading…
Cancel
Save