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. 125
      src/main.rs

@ -7,6 +7,8 @@ use std::{fs::File, path::Path, io::Read};
use sdl2::rect::Point; use sdl2::rect::Point;
type Glyph = Vec<Point>;
pub struct GlyphMetrics { pub struct GlyphMetrics {
pub width: usize, pub width: usize,
pub height: 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 /// Reads the file and turns it into a Vec of u8s
fn read_file(file_name: String) -> Vec<u8> { fn read_file(file_name: String) -> Vec<u8> {
@ -35,7 +40,7 @@ fn read_file(file_name: String) -> Vec<u8> {
file_content file_content
} }
pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) { pub fn generate_glyph_data() -> GlyphAtlas {
// Retrieve font data from file // Retrieve font data from file
let file_path = String::from("./fonts/Terminus14x8.data"); let file_path = String::from("./fonts/Terminus14x8.data");
let contents = read_file(file_path); 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.push(new_glyph);
} }
(glyph_atlas, glyph_metrics) 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

@ -1,11 +1,16 @@
extern crate sdl2; extern crate sdl2;
use clipboard::{ClipboardProvider, ClipboardContext}; use clipboard::{ClipboardProvider, ClipboardContext};
use editor_render::GlyphAtlas;
use num_format::{Locale, ToFormattedString}; use num_format::{Locale, ToFormattedString};
use sdl2::event::{Event, WindowEvent}; use sdl2::{
use sdl2::keyboard::Keycode; event::{Event, WindowEvent},
use sdl2::pixels::Color; keyboard::Keycode,
use sdl2::rect::{Point, Rect}; pixels::Color,
rect::{Point, Rect},
render::Canvas,
video::Window,
};
mod editor_render; mod editor_render;
@ -25,45 +30,19 @@ struct ModifierKeys {
shift: bool, shift: bool,
} }
pub fn main() -> Result<(), String> { // struct EditorGraphics {
// Initialize clipboard // canvas: Canvas<Window>,
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap(); // window_size: (u32, u32),
// glyph_atlas: GlyphAtlas,
// Initialize SDL2, window, and canvas // }
let sdl_context = sdl2::init()?;
let video_subsys = sdl_context.video()?; /// Draw all contents to the window
fn draw(canvas: &mut Canvas<Window>,
let window = video_subsys window_size: (u32, u32),
.window("Rude", SCREEN_WIDTH, SCREEN_HEIGHT) glyph_atlas: &GlyphAtlas,
.position_centered() text: &str,
.resizable() pos: usize) -> Result<(), String> {
.opengl() let text_offset = Point::new(10, 10);
.build()
.map_err(|e| e.to_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();
// Initialize undo data & values
let mut undo_history: Vec<(String, usize)> = vec![];
let mut undo_position: usize = 0;
let mut undo_timer: u32 = UNDO_TIME_COUNT;
// Initialize input values
let mut modifier_keys = ModifierKeys {alt: false, ctrl: false, shift: false};
let mut cursor_position = 0;
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 // Draw background
canvas.set_draw_color(Color::RGB(32, 32, 32)); canvas.set_draw_color(Color::RGB(32, 32, 32));
canvas.clear(); canvas.clear();
@ -71,31 +50,31 @@ pub fn main() -> Result<(), String> {
// Draw text // Draw text
canvas.set_draw_color(Color::RGB(240, 240, 240)); canvas.set_draw_color(Color::RGB(240, 240, 240));
let fb_text = editor_render::draw_text( let fb_text = editor_render::draw_text(
&glyph_atlas, &glyph_atlas.glyphs,
&glyph_metrics, &glyph_atlas.metrics,
text, text,
pad_offset text_offset
); );
canvas.draw_points(&fb_text[..])?; canvas.draw_points(&fb_text[..])?;
// Draw info // Draw info
let status = text.len().to_formatted_string(&Locale::en); let status = text.len().to_formatted_string(&Locale::en);
let status_position = Point::new( let status_position = Point::new(
pad_offset.x, text_offset.x,
window_size.1 as i32 - glyph_metrics.height as i32 * 2 window_size.1 as i32 - glyph_atlas.metrics.height as i32 * 2
); );
canvas.set_draw_color(Color::RGB(16, 64, 64)); canvas.set_draw_color(Color::RGB(16, 64, 64));
canvas.fill_rect(Rect::new(0, canvas.fill_rect(Rect::new(0,
status_position.y - 5, status_position.y - 5,
window_size.0, window_size.0,
glyph_metrics.height as u32 + 10 glyph_atlas.metrics.height as u32 + 10
))?; ))?;
canvas.set_draw_color(Color::RGB(127, 240, 240)); canvas.set_draw_color(Color::RGB(127, 240, 240));
let status_bar = editor_render::draw_text( let status_bar = editor_render::draw_text(
&glyph_atlas, &glyph_atlas.glyphs,
&glyph_metrics, &glyph_atlas.metrics,
&status, &status,
status_position status_position
); );
@ -104,22 +83,58 @@ pub fn main() -> Result<(), String> {
// Draw cursor // Draw cursor
canvas.set_draw_color(Color::RGB(64, 240, 240)); canvas.set_draw_color(Color::RGB(64, 240, 240));
let fb_cursor = editor_render::draw_cursor( let fb_cursor = editor_render::draw_cursor(
&glyph_metrics, &glyph_atlas.metrics,
pos, pos,
text, text,
pad_offset text_offset
); );
canvas.draw_line(fb_cursor.0, fb_cursor.1)?; canvas.draw_line(fb_cursor.0, fb_cursor.1)?;
canvas.present(); canvas.present();
Ok(()) Ok(())
}; }
pub fn main() -> Result<(), String> {
// Initialize clipboard
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap();
// Initialize SDL2, window, and canvas
let sdl_context = sdl2::init()?;
let video_subsys = sdl_context.video()?;
let window = video_subsys
.window("Rude", SCREEN_WIDTH, SCREEN_HEIGHT)
.position_centered()
.resizable()
.opengl()
.build()
.map_err(|e| e.to_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();
// Initialize undo data & values
let mut undo_history: Vec<(String, usize)> = vec![];
let mut undo_position: usize = 0;
let mut undo_timer: u32 = UNDO_TIME_COUNT;
// Initialize input values
let mut modifier_keys = ModifierKeys {alt: false, ctrl: false, shift: false};
let mut cursor_position = 0;
let mut selection_anchor: Option<usize> = None;
// Initialize graphics data and values
let glyph_atlas = editor_render::generate_glyph_data();
// Easier way to please the borrow checker // Easier way to please the borrow checker
macro_rules! draw { macro_rules! draw {
() => { () => {
draw(window_size, &buffer, cursor_position)? draw(&mut canvas, window_size, &glyph_atlas, &buffer, cursor_position)?
}; };
} }

Loading…
Cancel
Save