Add code to render text, refactor render into OOPish

This commit is contained in:
Nahuel Lofeudo 2026-04-19 20:18:17 +01:00
parent e28d4fdb22
commit dc0581c8cc
4 changed files with 61 additions and 38 deletions

View File

@ -7,7 +7,7 @@ use std::{
}; };
use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}}; use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}};
pub fn _get_next_stops(_gtfs: &Gtfs) -> Vec<Arrival> { pub fn _get_next_stops(_gtfs: &Gtfs) -> Vec<Arrival<'_>> {
let arrivals = Vec::<Arrival>::new(); let arrivals = Vec::<Arrival>::new();
return arrivals; return arrivals;

View File

@ -1,6 +1,8 @@
mod gtfs; mod gtfs;
mod renderer; mod renderer;
use std::{collections::HashSet}; use std::{collections::HashSet, thread, time::Duration};
use crate::renderer::Screen;
const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip"; const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";
@ -13,16 +15,22 @@ fn main() {
}; };
let screen_prefs = renderer::structs::Prefs { let screen_prefs = renderer::structs::Prefs {
font_path: String::from("todo!()"), font_path: String::from("resources/jd-lcd-rounded.ttf/jd-lcd-rounded.ttf"),
screen_width: 1920, screen_width: 1920,
screen_height: 720, screen_height: 720,
}; };
// Init GTFS static info // Init GTFS static info
println!("Loading GTFS data..."); println!("Loading GTFS data...");
let _gtfs = gtfs::load(SRC_FILE, &gtfs_prefs); //let _gtfs = gtfs::load(SRC_FILE, &gtfs_prefs);
// Init screen // Init screen
let _screen = renderer::init(&screen_prefs); println!("Initializing screen...");
let mut screen = Screen::init(&screen_prefs);
println!("Startup done.");
screen.print(0, "foo!");
thread::sleep(Duration::new(10, 0));
} }

View File

@ -1,12 +1,36 @@
pub mod structs; pub mod structs;
use sdl3::{pixels::Color};
use structs::{Prefs, Screen}; use sdl3::{pixels::Color, rect::Rect, render::Canvas, sys::metadata::render, ttf::Font, video::Window};
use structs::{Prefs};
pub struct Screen<'a> {
canvas: Box<Canvas<Window>>,
font: Box<Font<'a>>
}
impl Screen<'_> {
pub fn print(&mut self, line: u32, text: &str) {
let rendered_text = self.font.render(text).solid(Color::RED).unwrap();
let texture_creator = self.canvas.texture_creator();
let texture = rendered_text.as_texture(&texture_creator).unwrap();
let _ = self.canvas.copy(&texture,
Rect::new(0, 0, rendered_text.width(), rendered_text.height()),
Rect::new(0, (line * rendered_text.height()).try_into().unwrap(), rendered_text.width(), rendered_text.height()));
self.canvas.present();
}
pub fn clear(&mut self) {
self.canvas.set_draw_color(Color::BLACK);
self.canvas.clear();
self.canvas.present();
}
/// Initialize video, allocate buffers and load fonts /// Initialize video, allocate buffers and load fonts
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs /// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
pub fn init(prefs: &Prefs) -> Screen {
pub fn init(prefs: &Prefs) -> &mut structs::Screen {
// Initialize the screen // Initialize the screen
let sdl_context = sdl3::init().unwrap(); let sdl_context = sdl3::init().unwrap();
let video_subsys = sdl_context.video().unwrap(); let video_subsys = sdl_context.video().unwrap();
@ -20,17 +44,14 @@ pub fn init(prefs: &Prefs) -> &mut structs::Screen {
// Load font // Load font
let ttf_context = sdl3::ttf::init().unwrap(); let ttf_context = sdl3::ttf::init().unwrap();
let mut font = ttf_context.load_font(&prefs.font_path, 128.0).unwrap(); let font = ttf_context.load_font(&prefs.font_path, 128.0).unwrap();
// Clear screen let mut screen: Screen = Screen {
let mut canvas = window.into_canvas(); canvas: Box::new(window.into_canvas()),
canvas.set_draw_color(Color::BLACK); font: Box::new(font),
canvas.clear();
canvas.present();
let screen = Screen {
canvas: &canvas,
font: &font,
}; };
return &mut &screen;
screen.clear();
return screen;
}
} }

View File

@ -1,13 +1,7 @@
use sdl3::{render::Canvas, ttf::Font, video::Window}; use sdl3::{render::Canvas, ttf::Font, video::Window};
pub struct Prefs { pub struct Prefs {
pub font_path: String, pub font_path: String,
pub screen_width: u32, pub screen_width: u32,
pub screen_height: u32, pub screen_height: u32,
} }
pub struct Screen<'a> {
pub canvas: &'a Canvas<Window>,
pub font: &'a Font<'a>,
}