diff --git a/src/font_build.rs b/src/font_build.rs index 2d2e9fb..66ce765 100644 --- a/src/font_build.rs +++ b/src/font_build.rs @@ -1,4 +1,4 @@ -//! A redneck font renderer +//! A redneck bitmap font renderer //! //! This takes a janky ass raw image file with width info inserted //! and turns it into a bunch of points for SDL2 to read. @@ -41,10 +41,10 @@ pub fn generate_glyph_atlas() -> Vec { let width = u16::from_be_bytes(number); println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}"); - let gtable_prune = &contents[2..]; + let gtable_prune = &contents[762..]; + // Generate the glyph atlas let mut glyph_atlas: Vec = vec![]; - for glyph in 0..96 { let mut new_glyph: Glyph = vec![]; @@ -72,11 +72,33 @@ pub fn generate_glyph_atlas() -> Vec { } glyph_atlas.push(new_glyph); } - glyph_atlas } -pub fn generate_text(content: &str, glyph_atlas: Vec) -> Vec { +pub fn draw_text(content: &str, glyph_atlas: Vec) -> Vec { + let mut points: Vec = vec![]; + + let lines = content.split('\n'); + for (y, chars) in lines.enumerate() { + for (x, chara) in chars.chars().enumerate() { + let index; + if chara.is_lowercase() { + index = chara.to_ascii_lowercase() as usize; + } else { + index = chara.to_ascii_uppercase() as usize; + } - unimplemented!() + for pixel in &glyph_atlas[index - 32] { + let x_offset = x * GLYPH_WIDTH; + let y_offset = y * GLYPH_HEIGHT; + + let positioned_pixel = Point::new( + pixel.x + x_offset as i32, + pixel.y + y_offset as i32, + ); + points.push(positioned_pixel); + } + } + } + points } diff --git a/src/main.rs b/src/main.rs index 3bd3fca..efa7c18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,12 @@ extern crate sdl2; use std::path::Path; +use font_build::draw_text; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; -use sdl2::rect::Rect; -use sdl2::render::TextureQuery; - -//mod _catlas; -//use _catlas::*; +//use sdl2::rect::Rect; +//use sdl2::render::TextureQuery; mod font_build; @@ -22,6 +20,7 @@ struct ModifierKeys { shift: bool, } +/* // handle the annoying Rect i32 macro_rules! rect( ($x:expr, $y:expr, $w:expr, $h:expr) => ( @@ -35,10 +34,9 @@ fn get_corner_rect(rect_width: u32, rect_height: u32) -> Rect { let cy = 15; rect!(cx, cy, w, h) } - - +*/ pub fn main() -> Result<(), String> { - font_build::generate_glyph_atlas(); + let glyph_atlas = font_build::generate_glyph_atlas(); let font_path: &Path = Path::new("./fonts/Monoid-Regular.ttf"); @@ -54,7 +52,6 @@ pub fn main() -> Result<(), String> { .map_err(|e| e.to_string())?; let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?; - let texture_creator = canvas.texture_creator(); // Load a font let mut font = ttf_context.load_font(font_path, 12)?; @@ -64,28 +61,13 @@ pub fn main() -> Result<(), String> { let mut cursor_position = 0; let mut draw_text = |text: &str| -> Result<(), String> { - // render a surface, and convert it to a texture bound to the canvas - let surface = font - .render(text) - .blended(Color::RGBA(255, 255, 255, 255)) - .map_err(|e| e.to_string())?; - let texture = texture_creator - .create_texture_from_surface(&surface) - .map_err(|e| e.to_string())?; - canvas.set_draw_color(Color::RGB(32, 32, 32)); canvas.clear(); - let TextureQuery { width, height, .. } = texture.query(); + canvas.set_draw_color(Color::RGB(240, 240, 240)); + let fb_text = draw_text(text, glyph_atlas.clone()); + canvas.draw_points(&fb_text[..])?; - // If the example text is too big for the screen, downscale it (and center regardless) - // let padding = 64; - let target = get_corner_rect( - width, - height, - ); - - canvas.copy(&texture, None, Some(target))?; canvas.present(); Ok(()) @@ -185,11 +167,15 @@ pub fn main() -> Result<(), String> { cursor_position = (cursor_position + 1).min(buffer.len()) }, + Event::TextInput { text, .. } => { + println!("{text}") + }, + Event::KeyDown { keycode, .. } => { let key = keycode.unwrap().to_string(); // Ignore multi-char keycodes - if key.len() > 1 { break } + if key.len() != 1 { break } let key_case = match modifier_keys.shift { true => key.to_uppercase(), @@ -199,9 +185,10 @@ pub fn main() -> Result<(), String> { buffer.insert(cursor_position, key_case); cursor_position += 1; draw_text(&buffer)?; - println!("{key}"); + //println!("{key}"); }, + _ => {} } }