Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 12, 2019 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/36e1953e9ff00c05ef3e0c051efc8a84 to your computer and use it in GitHub Desktop.
Save rust-play/36e1953e9ff00c05ef3e0c051efc8a84 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![allow(unused)]
pub mod sdl {
pub struct Sdl2<'a> {
pub version: &'a str
}
impl<'a> Sdl2<'a> {
pub fn new(ver: &str) -> Sdl2 {
Sdl2 {
version: ver
}
}
}
}
pub mod ttf {
use crate::sdl::Sdl2;
pub struct TextEngine<'a> {
sdl: &'a Sdl2<'a>
}
impl<'a> TextEngine<'a> {
pub fn new(sdl: &'a Sdl2) -> Self {
TextEngine {
sdl: sdl
}
}
pub fn hello(&self) -> String {
format!("Hello. I am a TextEngine. My SDL version is {}.", self.sdl.version)
}
}
}
pub mod ux {
use crate::ttf;
use component::{Widget};
mod component {
pub struct Widget {
id: u32,
}
impl Widget {
pub fn new(id: u32) -> Widget {
Widget {
id: id
}
}
pub fn render(&self) {
println!("I am a widget. My id is {}.", self.id);
}
}
}
pub struct UI<'a> {
pub texter: &'a ttf::TextEngine<'a>,
widgets: Vec<Widget>,
}
impl<'a> UI<'a> {
pub fn new(engine: &'a ttf::TextEngine) -> Self {
let mut v: Vec<Widget> = Vec::new();
v.push(Widget::new(0));
v.push(Widget::new(1));
v.push(Widget::new(2));
v.push(Widget::new(3));
UI {
texter: engine,
widgets: v,
}
}
pub fn render(&self) {
for w in &self.widgets {
w.render();
}
}
}
}
fn main() {
let sdl2 = sdl::Sdl2::new("2.0.1");
let te = ttf::TextEngine::new(&sdl2);
let ui = ux::UI::new(&te);
println!("{}", ui.texter.hello());
ui.render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment