1use crate::video::ll::SDL_Surface;
9
10pub mod ll {
11 use crate::video;
12
13 #[repr(C)]
14 pub struct nSDL_Font {
15 pub chars: [*mut video::ll::SDL_Surface; 256usize],
16 pub char_width: [cty::uint8_t; 256usize],
17 pub hspacing: cty::c_int,
18 pub vspacing: cty::c_int,
19 pub monospaced: bool,
20 }
21
22 extern "C" {
23 pub fn nSDL_LoadFont(font_index: i32, r: u8, g: u8, b: u8) -> *mut nSDL_Font;
24 pub fn nSDL_DrawString(
25 surface: *mut video::ll::SDL_Surface,
26 font: *const nSDL_Font,
27 x: i32,
28 y: i32,
29 str: *const cty::c_char,
30 );
31 pub fn nSDL_FreeFont(font: *mut nSDL_Font);
32 pub fn nSDL_GetStringWidth(font: *mut nSDL_Font, str: *const cty::c_char) -> i32;
33 pub fn nSDL_GetStringHeight(font: *mut nSDL_Font, str: *const cty::c_char) -> i32;
34 pub fn nSDL_EnableFontMonospaced(font: *mut nSDL_Font, monospaced: i32);
35 pub fn nSDL_SetFontSpacing(font: *mut nSDL_Font, hspacing: i32, vspacing: i32);
36 }
37}
38
39#[repr(i32)]
40#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
41pub enum FontOptions {
42 Thin = 0,
43 Space,
44 VGA,
45 Fantasy,
46 ThinType,
47}
48
49#[derive(Clone, Debug)]
50pub struct Font {
51 pub font: *mut ll::nSDL_Font,
52}
53
54impl Font {
55 pub fn new(font: FontOptions, r: u8, g: u8, b: u8) -> Self {
56 let font = unsafe { ll::nSDL_LoadFont(font as i32, r, g, b) };
57 if font.is_null() {
58 panic!("Error loading font")
59 }
60 Self { font }
61 }
62
63 pub fn draw(&self, surface: *mut SDL_Surface, msg: &str, x: i32, y: i32) {
64 let msg = ndless::cstr!(msg);
65 unsafe { ll::nSDL_DrawString(surface, self.font, x, y, msg.as_ptr()) }
66 }
67
68 pub fn get_width(&self, msg: &str) -> i32 {
69 let msg = ndless::cstr!(msg);
70 unsafe { ll::nSDL_GetStringWidth(self.font, msg.as_ptr()) }
71 }
72
73 pub fn get_height(&self, msg: &str) -> i32 {
74 let msg = ndless::cstr!(msg);
75 unsafe { ll::nSDL_GetStringHeight(self.font, msg.as_ptr()) }
76 }
77
78 pub fn monospaced(&self, monospaced: bool) {
79 unsafe { ll::nSDL_EnableFontMonospaced(self.font, if monospaced { 1 } else { 0 }) }
80 }
81
82 pub fn spacing(&self, horizontal: i32, vertical: i32) {
83 unsafe { ll::nSDL_SetFontSpacing(self.font, horizontal, vertical) }
84 }
85}
86
87impl Drop for Font {
88 fn drop(&mut self) {
89 unsafe { ll::nSDL_FreeFont(self.font) }
90 }
91}