From 8eeb02b6368562b3dd8e3d5ca0e6e0330dee7b73 Mon Sep 17 00:00:00 2001 From: korin Date: Mon, 10 Apr 2023 08:03:48 -0400 Subject: [PATCH] some cleanup --- src/main.rs | 47 ++++++++--------------------- src/{editor_render.rs => render.rs} | 15 +++++---- 2 files changed, 20 insertions(+), 42 deletions(-) rename src/{editor_render.rs => render.rs} (89%) diff --git a/src/main.rs b/src/main.rs index b136a11..fe2941c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ extern crate sdl2; use clipboard::{ClipboardProvider, ClipboardContext}; -use editor_render::GlyphAtlas; +use render::GlyphAtlas; use num_format::{Locale, ToFormattedString}; use sdl2::{ event::{Event, WindowEvent}, @@ -12,7 +12,7 @@ use sdl2::{ video::Window, }; -mod editor_render; +mod render; static SCREEN_WIDTH: u32 = 1280; static SCREEN_HEIGHT: u32 = 720; @@ -32,29 +32,22 @@ struct ModifierKeys { // struct EditorGraphics { // canvas: Canvas, -// window_size: (u32, u32), // glyph_atlas: GlyphAtlas, // } /// Draw all contents to the window -fn draw(canvas: &mut Canvas, - window_size: (u32, u32), - glyph_atlas: &GlyphAtlas, - buffer: &str, - cursor_position: usize) -> Result<(), String> { +fn draw(canvas: &mut Canvas, glyph_atlas: &GlyphAtlas, buffer: &str, cursor_position: usize) -> Result<(), String> { + // Quick initialization + let window_size = canvas.output_size()?; let text_offset = Point::new(10, 10); + // Draw background canvas.set_draw_color(Color::RGB(32, 32, 32)); canvas.clear(); // Draw text canvas.set_draw_color(Color::RGB(240, 240, 240)); - let fb_text = editor_render::draw_text( - &glyph_atlas.glyphs, - &glyph_atlas.metrics, - buffer, - text_offset - ); + let fb_text = render::draw_text(&glyph_atlas, buffer, text_offset); canvas.draw_points(&fb_text[..])?; // Draw info @@ -72,22 +65,12 @@ fn draw(canvas: &mut Canvas, ))?; canvas.set_draw_color(Color::RGB(127, 240, 240)); - let status_bar = editor_render::draw_text( - &glyph_atlas.glyphs, - &glyph_atlas.metrics, - &status, - status_position - ); + let status_bar = render::draw_text(&glyph_atlas, &status, status_position); canvas.draw_points(&status_bar[..])?; // Draw cursor canvas.set_draw_color(Color::RGB(64, 240, 240)); - let fb_cursor = editor_render::draw_cursor( - &glyph_atlas.metrics, - cursor_position, - buffer, - text_offset - ); + let fb_cursor = render::draw_cursor(&glyph_atlas, cursor_position, buffer, text_offset); canvas.draw_line(fb_cursor.0, fb_cursor.1)?; canvas.present(); @@ -113,8 +96,6 @@ pub fn main() -> Result<(), String> { let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?; - let mut window_size = canvas.output_size()?; - // Initalize buffer let mut buffer = String::new(); @@ -129,12 +110,12 @@ pub fn main() -> Result<(), String> { let mut selection_anchor: Option = None; // Initialize graphics data and values - let glyph_atlas = editor_render::generate_glyph_data(); + let glyph_atlas = render::generate_glyph_data(); // Easier way to please the borrow checker macro_rules! draw { () => { - draw(&mut canvas, window_size, &glyph_atlas, &buffer, cursor_position)? + draw(&mut canvas, &glyph_atlas, &buffer, cursor_position)? }; } @@ -145,9 +126,7 @@ pub fn main() -> Result<(), String> { for event in sdl_context.event_pump()?.poll_iter() { match event { Event::Window { win_event, .. } => { - if let WindowEvent::Resized(w, h) = win_event { - window_size = (w as u32, h as u32); - + if let WindowEvent::Resized(_w, _h) = win_event { draw!(); } } @@ -398,7 +377,7 @@ pub fn main() -> Result<(), String> { undo_position += 1; } - ::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)); } println!("{buffer}"); Ok(()) diff --git a/src/editor_render.rs b/src/render.rs similarity index 89% rename from src/editor_render.rs rename to src/render.rs index 34c1c2a..32c99c5 100644 --- a/src/editor_render.rs +++ b/src/render.rs @@ -82,11 +82,11 @@ pub fn generate_glyph_data() -> GlyphAtlas { } /// Method for generating points to render, using given string -pub fn draw_text(glyph_atlas: &Vec, glyph_metrics: &GlyphMetrics, content: &str, offset: Point) -> Vec { +pub fn draw_text(glyph_atlas: &GlyphAtlas, content: &str, offset: Point) -> Vec { let mut points: Vec = vec![]; - let glyph_width = glyph_metrics.width; - let glyph_height = glyph_metrics.height; + let glyph_width = glyph_atlas.metrics.width; + let glyph_height = glyph_atlas.metrics.height; let lines = content.split('\n'); for (y, chars) in lines.enumerate() { @@ -98,7 +98,7 @@ pub fn draw_text(glyph_atlas: &Vec, glyph_metrics: &GlyphMetrics, content index = chara.to_ascii_uppercase() as usize; } - for pixel in &glyph_atlas[index - 32] { + for pixel in &glyph_atlas.glyphs[index - 32] { let x_glyph = x * glyph_width; let y_glyph = y * glyph_height; @@ -113,9 +113,9 @@ pub fn draw_text(glyph_atlas: &Vec, glyph_metrics: &GlyphMetrics, content points } -pub fn draw_cursor(glyph_metrics: &GlyphMetrics, mut cursor_position: usize, content: &str, offset: Point) -> (Point, Point) { - let glyph_width = glyph_metrics.width; - let glyph_height = glyph_metrics.height; +pub fn draw_cursor(glyph_atlas: &GlyphAtlas, mut cursor_position: usize, content: &str, offset: Point) -> (Point, Point) { + let glyph_width = glyph_atlas.metrics.width; + let glyph_height = glyph_atlas.metrics.height; let mut x = 0; let mut y = 0; @@ -123,7 +123,6 @@ pub fn draw_cursor(glyph_metrics: &GlyphMetrics, mut cursor_position: usize, con cursor_position = cursor_position.checked_sub(1).unwrap_or(0); for (idx, chara) in content.chars().enumerate() { x += 1; - if chara == '\n' { x = 0; y += 1;