Add code to render text, refactor render into OOPish
This commit is contained in:
+48
-27
@@ -1,36 +1,57 @@
|
||||
|
||||
pub mod structs;
|
||||
use sdl3::{pixels::Color};
|
||||
use structs::{Prefs, Screen};
|
||||
|
||||
/// Initialize video, allocate buffers and load fonts
|
||||
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
|
||||
use sdl3::{pixels::Color, rect::Rect, render::Canvas, sys::metadata::render, ttf::Font, video::Window};
|
||||
use structs::{Prefs};
|
||||
|
||||
pub fn init(prefs: &Prefs) -> &mut structs::Screen {
|
||||
// Initialize the screen
|
||||
let sdl_context = sdl3::init().unwrap();
|
||||
let video_subsys = sdl_context.video().unwrap();
|
||||
pub struct Screen<'a> {
|
||||
canvas: Box<Canvas<Window>>,
|
||||
font: Box<Font<'a>>
|
||||
}
|
||||
|
||||
let window = video_subsys
|
||||
.window("Dublin Bus", prefs.screen_width, prefs.screen_height)
|
||||
.position_centered()
|
||||
.borderless()
|
||||
//.fullscreen()
|
||||
.build().unwrap();
|
||||
impl Screen<'_> {
|
||||
|
||||
// Load font
|
||||
let ttf_context = sdl3::ttf::init().unwrap();
|
||||
let mut font = ttf_context.load_font(&prefs.font_path, 128.0).unwrap();
|
||||
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();
|
||||
}
|
||||
|
||||
// Clear screen
|
||||
let mut canvas = window.into_canvas();
|
||||
canvas.set_draw_color(Color::BLACK);
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
|
||||
let screen = Screen {
|
||||
canvas: &canvas,
|
||||
font: &font,
|
||||
};
|
||||
return &mut &screen;
|
||||
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
|
||||
/// Based on https://github.com/vhspace/sdl3-rs/blob/master/examples/ttf-demo.rs
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,7 @@
|
||||
use sdl3::{render::Canvas, ttf::Font, video::Window};
|
||||
|
||||
|
||||
pub struct Prefs {
|
||||
pub font_path: String,
|
||||
pub screen_width: u32,
|
||||
pub screen_height: u32,
|
||||
}
|
||||
|
||||
pub struct Screen<'a> {
|
||||
pub canvas: &'a Canvas<Window>,
|
||||
pub font: &'a Font<'a>,
|
||||
}
|
||||
Reference in New Issue
Block a user