aboutsummaryrefslogtreecommitdiffstats
path: root/src/font.rs
blob: 056c6912fdfa748d22770927919fb3f2e47c379b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
pub const UWU_HEIGHT: u32 = 24;
pub const UWU_WIDTH: u32 = 16;

use std::sync::LazyLock;

pub struct Glyph {
  x: i32,
}
fn insert_value(arr: &mut Vec<*const Glyph>, idx: usize, glyph: *const Glyph) {
  if arr.len() <= idx {
    while arr.len() <= idx {
      arr.push(0 as *const Glyph);
    }
  }
  arr[idx] = glyph;
}

pub struct RenderableCharacter {
  /** The width of the character, indicating where to break into a newline */
  pub width: u16,
  /** The height of the character, derived from data's length divided by width */
  pub height: u16,
  /** The raw alpha layer of the character */
  pub data: &'static [u8],
}
/**
 A trait describing a generated font.
 We use traits implemented by each font because it's somehow optimized better in preliminary testing(?)
*/
pub trait BakedFont {
  fn has_char(&self, character: char) -> bool;
  fn get_char_bytes(&self, character: char) -> &'static [u8];
  fn get_char(&self, character: char) -> RenderableCharacter {
    let bytes = self.get_char_bytes(character);
    let width = (bytes[0] as u16) | (bytes[1] as u16 >> 8);
    let data = &bytes[2..];
    RenderableCharacter {
      width,
      height: data.len() as u16 / width,
      data,
    }
  }
}
impl RenderableCharacter {
  /** Alpha value of colour is currently ignored! */
  pub fn to_surface(&self, colour: Color) -> Surface<'static> {
    let mut surface = Surface::new(
      self.width.into(),
      self.height.into(),
      PixelFormatEnum::RGBA32,
    )
    .unwrap();
    surface.with_lock_mut(|buffer: &mut [u8]| {
      let mut idx: usize = 0;
      print!("{} ({}x{})", self.data.len() * 4, self.width, self.height);
      for pixel in self.data {
        let index = idx * 4;
        buffer[index] = colour.r; // Red
        buffer[index + 1] = colour.g; // Green
        buffer[index + 2] = colour.b; // Blue
        buffer[index + 3] = *pixel; // Alpha
        idx += 1;
      }
    });
    surface
  }
  /** Colour Alpha Channel is ignored */
  pub fn to_texture<'a>(
    &self,
    texture_creator: &'a TextureCreator<WindowContext>,
    colour: Color,
  ) -> Result<Texture<'a>, TextureValueError> {
    let surface = self.to_surface(colour);
    surface.as_texture(texture_creator)
  }
  pub fn to_rect(&self, x: i32, y: i32) -> Rect {
    Rect::new(x, y, self.width.into(), self.height.into())
  }
}
pub struct Font<'a> {
  pub texture: &'a [u8],
  pub glyphs: Vec<*const Glyph>,
}

pub const MEOW_FONT: LazyLock<Font> = LazyLock::new(|| Font {
  texture: include_bytes!("../assets/fonts/uwu-logo-20px.png"),
  glyphs: {
    let a: &mut Vec<*const Glyph> = &mut Vec::new();
    insert_value(a, 32, &Glyph { x: 2 });
    a.to_vec()
  },
});

mod sprite_sheets {
  use super::{BakedFont, MEOW_FONT, UWU_HEIGHT, UWU_WIDTH};
  use sdl2::{
    // image::LoadTexture,
    pixels::PixelFormatEnum,
    rect::Rect,
    render::{Canvas, TextureCreator},
    surface::Surface,
    video::WindowContext,
  };

  pub struct UwUFont<'a> {
    sheet: Surface<'a>,
  }

  fn uwu_get_n(n: u32) -> Rect {
    Rect::new((UWU_WIDTH * n) as i32, 0, UWU_WIDTH, UWU_HEIGHT)
  }

  #[inline]
  fn uwu_u() -> Rect {
    return uwu_get_n(0);
  }

  #[inline]
  fn uwu_w() -> Rect {
    return uwu_get_n(1);
  }

  #[inline]
  fn uwu_s() -> Rect {
    return uwu_get_n(2);
  }

  #[inline]
  fn uwu_p() -> Rect {
    return uwu_get_n(3);
  }

  #[inline]
  fn uwu_a() -> Rect {
    return uwu_get_n(4);
  }

  #[inline]
  fn uwu_c() -> Rect {
    return uwu_get_n(5);
  }

  #[inline]
  fn uwu_e() -> Rect {
    return uwu_get_n(6);
  }

  impl<'a> UwUFont<'a> {
    pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> UwUFont<'a> {
      let surface = Surface::new(112, 24, PixelFormatEnum::RGB24).unwrap();
      let mut canvas = Canvas::from_surface(surface).unwrap();
      let tc = canvas.texture_creator();
      // let tx = tc.load_texture_bytes(MEOW_FONT.texture).unwrap();
      // canvas.copy(&tx, None, None).unwrap();
      canvas.present();
      let sheet = canvas.into_surface();

      UwUFont { sheet }
    }

    pub fn pos_to_rect(x: i32, y: i32) -> Rect {
      return Rect::new(x, y, UWU_WIDTH, UWU_HEIGHT);
    }

    pub fn char_to_surface(&self, char: Rect) -> Surface {
      let mut char_surface = Surface::new(UWU_WIDTH, UWU_HEIGHT, PixelFormatEnum::RGB24).unwrap();
      self
        .sheet
        .blit(char, &mut char_surface, Self::pos_to_rect(0, 0))
        .unwrap();
      char_surface
    }

    pub fn u_surface(&self) -> Surface {
      return self.char_to_surface(uwu_u());
    }

    pub fn w_surface(&self) -> Surface {
      return self.char_to_surface(uwu_w());
    }

    pub fn p_surface(&self) -> Surface {
      return self.char_to_surface(uwu_p());
    }
  }
}

use std::str::Bytes;

use sdl2::pixels::{Color, PixelFormatEnum};
use sdl2::rect::Rect;
use sdl2::render::{Canvas, Texture, TextureCreator, TextureValueError};
use sdl2::surface::Surface;
use sdl2::video::WindowContext;
pub use sprite_sheets::UwUFont;