From bcda449dea3673fecf4c0ea5c96799f4bc4e59a2 Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Sun, 18 Jun 2017 18:38:11 -0400 Subject: [PATCH] Allow selecting text fg and bg colors. Also use a grayscale bitmap for the C64 font. --- Cargo.toml | 2 +- c64-font.png | Bin 2574 -> 886 bytes main.rs | 1 + text.rs | 53 ++++++++++++++++++++++++++++++++++----------------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f3f65b9..4e927c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ cgmath = "*" glium = "0.16.0" glutin = "0.7.4" genmesh = "0.4.1" -image = { version = "0.13.0", features = ["png_codec"] } +image = { version = "0.14.0", features = ["png_codec"] } libxm = "1.0.0" obj = { version = "0.5", features = ["usegenmesh"] } rand = "*" diff --git a/c64-font.png b/c64-font.png index 61d5e562182f778ff515ce57181e1163f87088d5..4342a5db80f814d0b261fb5a47bdeb7ccfa5d63c 100644 GIT binary patch delta 856 zcmV-e1E>6s6!r#?7zqIY0002%MOUqC3KcBZ_gA;HE42@f%Wtr;=cDVd z>gj*}A))3{isWfVk$qen*hv&G+(!b_L1^cP*lVxD+HZMO6!aQ3u`2sR0nZ8$xd(sP z@(Y0UgY?2)1wR}8*>oU!HvmbvGOmBR4}pKDp&$CBx=~GC7o;Oa7c_kl4c!pZ2oMEe zWM!=25hc;HqI2JM&w)FNJ`S^K1mWhiy7?& z>s4nFd)88-v|D@safku)a+iXqR}KsH14~bKh5;p=A3X{&BUV_?s9sHpZ4Puv31@4) zh|z`Lje%Z+9I#5|qsDS_qPmS3gu^y|dBrs4ut7K+;+T|j8loN!yToB1UkrcL6jX!FB-1y^?AHV!j^W*;dKR@2*$76wAFyl-P#$B+vrvmtusAqr51uWLnel}4a z8TSBh^V76mRDhZ;oF3jLEUFp?@PV3U`>AM@E~bq|*QZv2o1j7T6zLnWf5fYf+blFj z`i_s%!oWx_8AwTek5}D&s^9^3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JEL{0li8dB>;4Sp&_+?V`?rph zV*){}mua(IAL40N44`E9gJuL-!7P;qW@PO~P&OI~jG**UEGA+|RV+Yy{*D7YoabAO zHUMpIU3tyCs?L9O3dq>->S0ta3~)}W9Hr#h#i&;@;EF<-&8vtkBY0BDIjw(hyzhCY zT1>Kt`Ar9I7tFID3RMpzNxwtoK2Uzylzs7m120c;pm;7`!%MGKxbW9)?N^ zMSdJvsJQzaeSLS-2&!c1$q`9Kn~niydk|0Oj|98g4yBCPL|d(+ck>yeS3jS>{QT$l z5sGK)+C{l{WRqniR=~zd7&(8lHjbX#!Q9Au>b15Ns=|e8Ir){j`4$6Maf)mRrLeXi zui_4^N}ZDY86L@tk*XHFdZ&jkNdq&uIXWj+W%GC|cUCA|>I2CLj+t(#;?P8|eA3C= z)$iwhrpKUqe;amiMlyLz9|9=VNhbEBeU5uRLG9%r;tQShnm4we8@_PVq+_pzzc+3Wt+Rzr>HMO$fIz>W568?`T>vLtF|qGMH=)#$rMF+jzRseN?po43bzTPx&umagdsc4+Vx?dX!eD3HuW(T@vU=Mr;LY1#zTZ$6 TXA+ { - tex: texture::CompressedSrgbTexture2d, + tex: texture::UnsignedTexture2d, vertex_buffer: glium::VertexBuffer, index_buffer: glium::IndexBuffer, program: glium::Program, @@ -23,13 +23,23 @@ pub struct Text<'a> { impl<'a> Text<'a> { pub fn new(display: &glium::Display) -> Text { - let image = image::load(Cursor::new(&include_bytes!("c64-font.png")[..]), image::PNG) - .unwrap() - .to_rgba(); - let dimensions = image.dimensions(); let image = - glium::texture::RawImage2d::from_raw_rgba_reversed(image.into_raw(), dimensions); - let tex = glium::texture::CompressedSrgbTexture2d::new(display, image).unwrap(); + image::load_from_memory_with_format(&include_bytes!("c64-font.png")[..], image::PNG) + .unwrap() + .to_luma(); + let (w, h) = image.dimensions(); + let image = glium::texture::RawImage2d { + data: std::borrow::Cow::from(image.into_raw()), + width: w, + height: h, + format: glium::texture::ClientFormat::U8, + }; + let tex = glium::texture::UnsignedTexture2d::with_format( + display, + image, + glium::texture::UncompressedUintFormat::U8, + glium::texture::MipmapsOption::NoMipmap, + ).unwrap(); // building the vertex buffer, which contains all the vertices that we will draw let vertex_buffer = { @@ -38,19 +48,19 @@ impl<'a> Text<'a> { &[ Vertex { position: [-1.0, -1.0], - tex_coords: [0.0, 0.0], + tex_coords: [0.0, 1.0], }, Vertex { position: [-1.0, 1.0], - tex_coords: [0.0, 1.0], + tex_coords: [0.0, 0.0], }, Vertex { position: [1.0, 1.0], - tex_coords: [1.0, 1.0], + tex_coords: [1.0, 0.0], }, Vertex { position: [1.0, -1.0], - tex_coords: [1.0, 0.0], + tex_coords: [1.0, 1.0], }, ], ).unwrap() @@ -76,26 +86,29 @@ impl<'a> Text<'a> { in vec2 tex_coords; out vec2 v_tex_coords; + out ivec4 v_fgcolor; void main() { gl_Position = perspview * model * vec4(position, 0.0, 1.0); // Characters are arranged in a 16x16 square. - // Texture oordinates originate in the bottom-left corner. int xpos = index % 16; - int ypos = 15 - index / 16; + int ypos = index / 16; v_tex_coords = (tex_coords) / 16.0 + vec2(xpos / 16., ypos / 16.); } ", fragment: " #version 140 - uniform sampler2D tex; + uniform usampler2D tex; + uniform vec4 bgcolor; + uniform vec4 fgcolor; + in vec2 v_tex_coords; out vec4 f_color; void main() { - f_color = texture(tex, v_tex_coords); + f_color = texture(tex, v_tex_coords).x == 0U ? bgcolor : fgcolor; } " }).unwrap(); @@ -121,12 +134,16 @@ impl<'a> Text<'a> { } pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) { - let uniforms = uniform! { + let uniforms = + uniform! { model: array4x4(self.model), perspview: *perspview, tex: self.tex.sampled() .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest), - index: 'A' as i32, + 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], }; frame .draw( -- 2.25.1