1 use cgmath::conv::array4x4;
4 use glium::{Display, Program, Surface, implement_vertex, texture, uniform};
7 fn gamma<T>(x: T) -> f32
12 ((f32::from(x)) / 255.).powf(2.2)
15 fn srgb<T>(c: [T; 3]) -> [f32; 4]
20 [gamma(c[0]), gamma(c[1]), gamma(c[2]), 0.0]
23 #[cfg(feature = "image")]
24 fn c64_font() -> (u32, u32, Vec<u8>) {
25 let image = image::load_from_memory_with_format(
26 &include_bytes!("textures/c64-font.png")[..],
30 let (w, h) = image.dimensions();
31 (w, h, image.into_raw())
34 #[cfg(not(feature = "image"))]
35 fn c64_font() -> (u32, u32, Vec<u8>) {
36 let pixels = &include_bytes!("textures/c64-font.gray")[..];
37 (128, 128, Vec::from(pixels))
40 pub fn text_program(display: &Display) -> Program {
41 //load_program(display, "shaders/text.vert", "shaders/text.frag");
42 let vertex_shader_src = include_str!("shaders/text.vert");
43 let fragment_shader_src = include_str!("shaders/text.frag");
44 Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
47 #[derive(Copy, Clone)]
52 implement_vertex!(Vertex, position, tex_coords);
55 tex: texture::Texture2d,
56 vertex_buffer: glium::VertexBuffer<Vertex>,
57 index_buffer: glium::IndexBuffer<u16>,
58 program: glium::Program,
59 params: glium::DrawParameters<'a>,
63 pub fn new(display: &Display) -> Text<'a> {
64 let (w, h, pixels) = c64_font();
65 let image = glium::texture::RawImage2d {
66 data: std::borrow::Cow::from(pixels),
69 format: glium::texture::ClientFormat::U8,
71 let tex = glium::texture::Texture2d::with_format(
74 glium::texture::UncompressedFloatFormat::U8,
75 glium::texture::MipmapsOption::NoMipmap,
78 // building the vertex buffer, which contains all the vertices that we will draw
80 glium::VertexBuffer::new(
84 position: [-0.5, -0.5],
85 tex_coords: [0.0, 1.0],
88 position: [-0.5, 0.5],
89 tex_coords: [0.0, 0.0],
93 tex_coords: [1.0, 0.0],
96 position: [0.5, -0.5],
97 tex_coords: [1.0, 1.0],
103 let index_buffer = glium::IndexBuffer::new(
105 glium::index::PrimitiveType::TriangleStrip,
106 &[1 as u16, 2, 0, 3],
109 let params = glium::DrawParameters {
110 depth: glium::Depth {
111 test: glium::draw_parameters::DepthTest::IfLess,
121 vertex_buffer: vertex_buffer,
122 index_buffer: index_buffer,
123 program: text_program(display),
128 pub fn draw(&self, frame: &mut glium::Frame, c: char, model: &Matrix4<f32>, perspview: &[[f32; 4]; 4]) {
131 model: array4x4(*model),
132 perspview: *perspview,
133 tex: self.tex.sampled()
134 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
136 // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
137 bgcolor: srgb([ 64, 50, 133u8]), // 6 - blue
138 fgcolor: srgb([120, 106, 189u8]), // 14 - light blue