Load the c64 font as a raw bitmap without using the image crate.
[mandelwow.git] / text.rs
diff --git a/text.rs b/text.rs
index 82d4d1703a79da5bd4efed53dc5c9205f4493f2b..14b2633b8a6712910bb49b7abcb46f57bb8d0f59 100644 (file)
--- a/text.rs
+++ b/text.rs
@@ -1,10 +1,42 @@
 use cgmath::conv::array4x4;
-use cgmath::{Matrix4, One};
+use cgmath::{Matrix4, Vector3};
 use glium;
 use glium::{Surface, texture};
-use image;
 use std;
 
+fn gamma<T>(x: T) -> f32
+where
+    f32: From<T>,
+    T: Copy,
+{
+    ((f32::from(x)) / 255.).powf(2.2)
+}
+
+fn srgb<T>(c: [T; 3]) -> [f32; 4]
+where
+    f32: From<T>,
+    T: Copy,
+{
+    [gamma(c[0]), gamma(c[1]), gamma(c[2]), 0.0]
+}
+
+#[cfg(feature = "image")]
+fn c64_font() -> (u32, u32, Vec<u8>) {
+    use image;
+    let image =
+        image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG)
+            .unwrap()
+            .to_luma();
+    let (w, h) = image.dimensions();
+    (w, h, image.into_raw())
+}
+
+#[cfg(not(feature = "image"))]
+fn c64_font() -> (u32, u32, Vec<u8>) {
+    let pixels = &include_bytes!("c64-font.gray")[..];
+    (128, 128, Vec::from(pixels))
+}
+
 #[derive(Copy, Clone)]
 struct Vertex {
     position: [f32; 2],
@@ -13,7 +45,7 @@ struct Vertex {
 implement_vertex!(Vertex, position, tex_coords);
 
 pub struct Text<'a> {
-    tex: texture::UnsignedTexture2d,
+    tex: texture::Texture2d,
     vertex_buffer: glium::VertexBuffer<Vertex>,
     index_buffer: glium::IndexBuffer<u16>,
     program: glium::Program,
@@ -23,21 +55,17 @@ pub struct Text<'a> {
 
 impl<'a> Text<'a> {
     pub fn new(display: &glium::Display) -> Text {
-        let image =
-            image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG)
-                .unwrap()
-                .to_luma();
-        let (w, h) = image.dimensions();
+        let (w, h, pixels) = c64_font();
         let image = glium::texture::RawImage2d {
-            data: std::borrow::Cow::from(image.into_raw()),
+            data: std::borrow::Cow::from(pixels),
             width: w,
             height: h,
             format: glium::texture::ClientFormat::U8,
         };
-        let tex = glium::texture::UnsignedTexture2d::with_format(
+        let tex = glium::texture::Texture2d::with_format(
             display,
             image,
-            glium::texture::UncompressedUintFormat::U8,
+            glium::texture::UncompressedFloatFormat::U8,
             glium::texture::MipmapsOption::NoMipmap,
         ).unwrap();
 
@@ -94,13 +122,13 @@ impl<'a> Text<'a> {
                     // Characters are arranged in a 16x16 square.
                     int xpos = index % 16;
                     int ypos = index / 16;
-                    v_tex_coords = (tex_coords) / 16.0 + vec2(xpos / 16., ypos / 16.);
+                    v_tex_coords = (tex_coords + vec2(xpos, ypos)) / 16.;
                 }
             ",
 
             fragment: "
                 #version 140
-                uniform usampler2D tex;
+                uniform sampler2D tex;
                 uniform vec4 bgcolor;
                 uniform vec4 fgcolor;
 
@@ -124,7 +152,7 @@ impl<'a> Text<'a> {
         };
 
         Text {
-            model: Matrix4::one(),
+            model: Matrix4::from_translation(Vector3::unit_z() * (-1.0)),
             tex: tex,
             vertex_buffer: vertex_buffer,
             index_buffer: index_buffer,
@@ -142,8 +170,8 @@ impl<'a> Text<'a> {
                 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),
             index: 'C' as i32,
             // RGB values from http://unusedino.de/ec64/technical/misc/vic656x/colors/
-            bgcolor: [  53./255.,  40./255., 121./255.,   0.0/255. ] as [f32; 4],
-            fgcolor: [ 120./255., 106./255., 255./255., 188.0/255. ] as [f32; 4],
+            bgcolor: srgb([ 64,  50, 133u8]),  //  6 - blue
+            fgcolor: srgb([120, 106, 189u8]),  // 14 - light blue
         };
         frame
             .draw(