new GlyphAtlas struct, draw routine is no longer a closure, but now a function.

type-render
korin 3 years ago
parent 8d16591b17
commit 1728e4e878
  1. 11
      src/editor_render.rs
  2. 137
      src/main.rs

@ -7,6 +7,8 @@ use std::{fs::File, path::Path, io::Read};
use sdl2::rect::Point;
type Glyph = Vec<Point>;
pub struct GlyphMetrics {
pub width: usize,
pub height: usize,
@ -18,7 +20,10 @@ impl GlyphMetrics {
}
}
type Glyph = Vec<Point>;
pub struct GlyphAtlas {
pub glyphs: Vec<Glyph>,
pub metrics: GlyphMetrics,
}
/// Reads the file and turns it into a Vec of u8s
fn read_file(file_name: String) -> Vec<u8> {
@ -35,7 +40,7 @@ fn read_file(file_name: String) -> Vec<u8> {
file_content
}
pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) {
pub fn generate_glyph_data() -> GlyphAtlas {
// Retrieve font data from file
let file_path = String::from("./fonts/Terminus14x8.data");
let contents = read_file(file_path);
@ -73,7 +78,7 @@ pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) {
}
glyph_atlas.push(new_glyph);
}
(glyph_atlas, glyph_metrics)
GlyphAtlas { glyphs: glyph_atlas, metrics: glyph_metrics }
}
/// Method for generating points to render, using given string

@ -1,11 +1,16 @@
extern crate sdl2;
use clipboard::{ClipboardProvider, ClipboardContext};
use editor_render::GlyphAtlas;
use num_format::{Locale, ToFormattedString};
use sdl2::event::{Event, WindowEvent};
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect};
use sdl2::{
event::{Event, WindowEvent},
keyboard::Keycode,
pixels::Color,
rect::{Point, Rect},
render::Canvas,
video::Window,
};
mod editor_render;
@ -25,6 +30,71 @@ struct ModifierKeys {
shift: bool,
}
// struct EditorGraphics {
// canvas: Canvas<Window>,
// window_size: (u32, u32),
// glyph_atlas: GlyphAtlas,
// }
/// Draw all contents to the window
fn draw(canvas: &mut Canvas<Window>,
window_size: (u32, u32),
glyph_atlas: &GlyphAtlas,
text: &str,
pos: usize) -> Result<(), String> {
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,
text,
text_offset
);
canvas.draw_points(&fb_text[..])?;
// Draw info
let status = text.len().to_formatted_string(&Locale::en);
let status_position = Point::new(
text_offset.x,
window_size.1 as i32 - glyph_atlas.metrics.height as i32 * 2
);
canvas.set_draw_color(Color::RGB(16, 64, 64));
canvas.fill_rect(Rect::new(0,
status_position.y - 5,
window_size.0,
glyph_atlas.metrics.height as u32 + 10
))?;
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
);
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,
pos,
text,
text_offset
);
canvas.draw_line(fb_cursor.0, fb_cursor.1)?;
canvas.present();
Ok(())
}
pub fn main() -> Result<(), String> {
// Initialize clipboard
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap();
@ -59,67 +129,12 @@ pub fn main() -> Result<(), String> {
let mut selection_anchor: Option<usize> = None;
// Initialize graphics data and values
let (glyph_atlas, glyph_metrics) = editor_render::generate_glyph_data();
let pad_offset = Point::new(10, 10);
// The drawing routine
let mut draw = |window_size: (u32, u32), text: &str, pos: usize| -> Result<(), String> {
// 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,
&glyph_metrics,
text,
pad_offset
);
canvas.draw_points(&fb_text[..])?;
// Draw info
let status = text.len().to_formatted_string(&Locale::en);
let status_position = Point::new(
pad_offset.x,
window_size.1 as i32 - glyph_metrics.height as i32 * 2
);
canvas.set_draw_color(Color::RGB(16, 64, 64));
canvas.fill_rect(Rect::new(0,
status_position.y - 5,
window_size.0,
glyph_metrics.height as u32 + 10
))?;
canvas.set_draw_color(Color::RGB(127, 240, 240));
let status_bar = editor_render::draw_text(
&glyph_atlas,
&glyph_metrics,
&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_metrics,
pos,
text,
pad_offset
);
canvas.draw_line(fb_cursor.0, fb_cursor.1)?;
canvas.present();
Ok(())
};
let glyph_atlas = editor_render::generate_glyph_data();
// Easier way to please the borrow checker
macro_rules! draw {
() => {
draw(window_size, &buffer, cursor_position)?
draw(&mut canvas, window_size, &glyph_atlas, &buffer, cursor_position)?
};
}

Loading…
Cancel
Save