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