Add ttf rendering test code

This commit is contained in:
maddiebaka 2023-06-21 19:43:17 -04:00
parent a7ca18eeeb
commit 09f39c84fa
4 changed files with 133 additions and 3 deletions

56
Cargo.lock generated
View File

@ -2,6 +2,62 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.146"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b"
[[package]]
name = "sdl2"
version = "0.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a"
dependencies = [
"bitflags",
"lazy_static",
"libc",
"sdl2-sys",
]
[[package]]
name = "sdl2-sys"
version = "0.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0"
dependencies = [
"cfg-if",
"libc",
"version-compare",
]
[[package]]
name = "touchstone-ui"
version = "0.1.0"
dependencies = [
"sdl2",
]
[[package]]
name = "version-compare"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"

View File

@ -5,4 +5,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[dependencies.sdl2]
sdl2 = "0.35.2"
default-features = false
features = ["ttf"]

Binary file not shown.

View File

@ -1,3 +1,74 @@
fn main() {
println!("Hello, world!");
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;
use std::path::Path;
use sdl2::ttf::*;
static SCREEN_WIDTH: u32 = 480;
static SCREEN_HEIGHT: u32 = 320;
macro_rules! rect(
($x:expr, $y:expr, $w:expr, $h:expr) => (
Rect::new($x as i32, $y as i32, $w as u32, $h as u32)
)
);
pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("rust-sdl2 demo", 480, 320)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
let texture_creator = canvas.texture_creator();
// Font stuff
let ttf_context = sdl2::ttf::init().map_err(|e| e.to_string()).unwrap();
let font_path = Path::new("assets/handel_gothic_regular.ttf");
let mut font = ttf_context.load_font(font_path, 128).unwrap();
let surface = font
.render("LIFE SUPPORT")
.blended(Color::RGBA(200, 200, 200, 255))
.map_err(|e| e.to_string()).unwrap();
let texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string()).unwrap();
let target = rect!(110, 20, 260, 24);
canvas.set_draw_color(Color::RGB(0, 255, 255));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut i = 0;
'running: loop {
i = (i + 1) % 255;
canvas.set_draw_color(Color::RGB(48, 39, 49));
canvas.clear();
canvas.copy(&texture, None, Some(target)).unwrap();
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
// The rest of the game loop goes here...
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}
}