some cleanup

type-render
korin 3 years ago
parent e9e6ba42ba
commit 8eeb02b636
  1. 47
      src/main.rs
  2. 15
      src/render.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>,
// 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,
buffer: &str,
cursor_position: usize) -> Result<(), String> {
fn draw(canvas: &mut Canvas<Window>, 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<Window>,
))?;
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<usize> = 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(())

@ -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>, glyph_metrics: &GlyphMetrics, content: &str, offset: Point) -> Vec<Point> {
pub fn draw_text(glyph_atlas: &GlyphAtlas, content: &str, offset: Point) -> Vec<Point> {
let mut points: Vec<Point> = 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>, 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>, 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;
Loading…
Cancel
Save