1 use cgmath::conv::array4x4;
2 use cgmath::{Matrix4, One};
4 use glium::{Surface, texture};
13 implement_vertex!(Vertex, position, tex_coords);
16 tex: texture::UnsignedTexture2d,
17 vertex_buffer: glium::VertexBuffer<Vertex>,
18 index_buffer: glium::IndexBuffer<u16>,
19 program: glium::Program,
20 params: glium::DrawParameters<'a>,
25 pub fn new(display: &glium::Display) -> Text {
27 image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG)
30 let (w, h) = image.dimensions();
31 let image = glium::texture::RawImage2d {
32 data: std::borrow::Cow::from(image.into_raw()),
35 format: glium::texture::ClientFormat::U8,
37 let tex = glium::texture::UnsignedTexture2d::with_format(
40 glium::texture::UncompressedUintFormat::U8,
41 glium::texture::MipmapsOption::NoMipmap,
44 // building the vertex buffer, which contains all the vertices that we will draw
46 glium::VertexBuffer::new(
50 position: [-1.0, -1.0],
51 tex_coords: [0.0, 1.0],
54 position: [-1.0, 1.0],
55 tex_coords: [0.0, 0.0],
59 tex_coords: [1.0, 0.0],
62 position: [1.0, -1.0],
63 tex_coords: [1.0, 1.0],
69 let index_buffer = glium::IndexBuffer::new(
71 glium::index::PrimitiveType::TriangleStrip,
75 // compiling shaders and linking them together
76 let program = program!(display,
82 uniform mat4 perspview;
88 out vec2 v_tex_coords;
92 gl_Position = perspview * model * vec4(position, 0.0, 1.0);
94 // Characters are arranged in a 16x16 square.
95 int xpos = index % 16;
96 int ypos = index / 16;
97 v_tex_coords = (tex_coords) / 16.0 + vec2(xpos / 16., ypos / 16.);
103 uniform usampler2D tex;
104 uniform vec4 bgcolor;
105 uniform vec4 fgcolor;
107 in vec2 v_tex_coords;
111 f_color = texture(tex, v_tex_coords).x == 0U ? bgcolor : fgcolor;
116 let params = glium::DrawParameters {
117 depth: glium::Depth {
118 test: glium::draw_parameters::DepthTest::IfLess,
127 model: Matrix4::one(),
129 vertex_buffer: vertex_buffer,
130 index_buffer: index_buffer,
136 pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) {
139 model: array4x4(self.model),
140 perspview: *perspview,
141 tex: self.tex.sampled()
142 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
144 // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
145 bgcolor: [ 53./255., 40./255., 121./255., 0.0/255. ] as [f32; 4],
146 fgcolor: [ 120./255., 106./255., 255./255., 188.0/255. ] as [f32; 4],