diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/overlay.rs | 27 | ||||
| -rw-r--r-- | src/common/overlay/terminal.rs | 57 |
2 files changed, 73 insertions, 11 deletions
diff --git a/src/common/overlay.rs b/src/common/overlay.rs index dd69040..6e68974 100644 --- a/src/common/overlay.rs +++ b/src/common/overlay.rs @@ -7,6 +7,7 @@ use teleia::*; use std::{collections::HashMap, f32::consts::PI}; use lexpr::sexp; +use base64::prelude::*; pub struct Overlay { assets: assets::Assets, @@ -64,8 +65,22 @@ impl Overlay { Some(()) } pub fn handle_text(&mut self, msg: fig::Message) -> Option<()> { - let s = msg.data.get(0)?.as_str()?; - self.terminal.fill_string(s); + let s = BASE64_STANDARD.decode(msg.data.get(0)?.as_str()?).ok()?; + self.terminal.fill_string(std::str::from_utf8(&s).ok()?); + Some(()) + } + pub fn handle_frame(&mut self, msg: fig::Message) -> Option<()> { + let data = BASE64_STANDARD.decode(msg.data.get(0)?.as_str()?).ok()?; + for (i, c) in data.chunks_exact(3).enumerate() { + if let [r, g, b] = c { + let ii = i as i32; + let p = terminal::Pos::new(ii % 64, ii / 64); + self.terminal.set_color.set( + p, + glam::Vec3::new(*r as f32 / 255.0, *g as f32 / 255.0, *b as f32 / 255.0) + ); + } + } Some(()) } } @@ -95,6 +110,8 @@ impl teleia::state::Game for Overlay { if self.handle_tracking(msg).is_none() { log::warn!("{}", malformed) } } else if msg.event == sexp!((avatar text)) { if self.handle_text(msg).is_none() { log::warn!("{}", malformed) } + } else if msg.event == sexp!((avatar frame)) { + if self.handle_frame(msg).is_none() { log::warn!("{}", malformed) } } else { log::info!("received unhandled event {} with data: {}", msg.event, msg.data); } @@ -106,19 +123,19 @@ impl teleia::state::Game for Overlay { } fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { self.model_fb.bind(ctx); - ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 1.0)); + ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 0.0)); ctx.clear(); st.bind_3d(ctx, &self.assets.shader_scene); self.assets.shader_scene.set_position_3d( ctx, &glam::Mat4::from_translation( - glam::Vec3::new(0.0, -1.6, 0.5), + glam::Vec3::new(0.0, -1.63, 0.42), ), ); self.model.render(ctx, &self.assets.shader_scene); st.render_framebuffer.bind(ctx); self.terminal.update(ctx, &self.model_fb); - self.terminal.render(ctx, &glam::Vec2::new(400.0, 200.0)); + self.terminal.render(ctx, &glam::Vec2::new(12.0, 250.0)); // self.model_fb.blit( // ctx, &st.render_framebuffer, // &glam::Vec2::new(ctx.render_width / 2.0 - 512.0, ctx.render_height / 2.0 - 512.0), diff --git a/src/common/overlay/terminal.rs b/src/common/overlay/terminal.rs index a2382b1..1b8df4c 100644 --- a/src/common/overlay/terminal.rs +++ b/src/common/overlay/terminal.rs @@ -67,6 +67,25 @@ impl Layer<glam::Vec3> { pub fn from_framebuffer(&mut self, ctx: &context::Context, fb: &framebuffer::Framebuffer) { fb.get_pixels(ctx, &mut self.data); } + pub fn get_surrounding(&self, pos: Pos, bgcolor: &glam::Vec3) -> u8 { + let offs = [ + Pos::new(-1, -1), + Pos::new(0, -1), + Pos::new(1, -1), + Pos::new(-1, 0), + Pos::new(1, 0), + Pos::new(-1, 1), + Pos::new(0, 1), + Pos::new(1, 1), + ]; + let mut ret = 0; + for o in offs { + ret <<= 1; + let v = (self.get(pos + o) != Some(bgcolor)) as u8; + ret |= v; + } + ret + } } pub struct Terminal { @@ -87,11 +106,17 @@ impl Terminal { } } pub fn get_color(&self, pos: Pos) -> glam::Vec3 { + if let Some(c) = self.set_color.get(pos) { + if *c != glam::Vec3::new(0.0, 0.0, 0.0) { + return *c; + } + } if let Some(c) = self.base_color.get(pos) { - c.clone() - } else { - glam::Vec3::new(1.0, 1.0, 1.0) + if *c != glam::Vec3::new(0.0, 0.0, 0.0) { + return *c; + } } + glam::Vec3::new(0.0, 0.0, 0.0) } pub fn update(&mut self, ctx: &context::Context, fb: &framebuffer::Framebuffer) { self.base_color.from_framebuffer(ctx, fb); @@ -99,20 +124,40 @@ impl Terminal { pub fn fill_string(&mut self, s: &str) { self.set_char.from_str(s); } + pub fn outline_pattern(&self, pos: Pos) -> Option<String> { + let sur = self.base_color.get_surrounding(pos, &glam::Vec3::new(0.0, 0.0, 0.0)); + let res = match sur { + 0b01101011 | 0b01101111 => " |", + 0b11010110 | 0b11010111 => "| ", + 0b00101011 | 0b00101111 | 0b00101110 | 0b00101100 | 0b00001011 => " /", + 0b10010110 | 0b10010111 | 0b10010011 | 0b10010001 | 0b00010110 => "\\ ", + 0b00111111 => "-/", 0b10011111 => "\\-", + 0b00011111 => "--", 0b00000111 => "__", + 0b00001111 => " _", 0b00010111 => "_ ", + _ => return None, + }; + Some(res.to_owned()) + } pub fn render(&self, ctx: &context::Context, pos: &glam::Vec2) { let mut s = String::new(); let mut colors = Vec::new(); for row in 0..64 { for col in 0..64 { let pos = Pos::new(col, row); + let c = self.get_color(pos); + colors.push(c); colors.push(c); let new = if let Some(p) = self.set_char.get(pos) { - format!("{}{}", p.first, if let Some(snd) = p.second { snd } else { ' ' }) + if c == glam::Vec3::new(0.0, 0.0, 0.0) { + String::from(" ") + } else if let Some(pat) = self.outline_pattern(pos) { + pat + } else { + format!("{}{}", p.first, if let Some(snd) = p.second { snd } else { ' ' }) + } } else { String::from(" ") }; s += &new; - let c = self.get_color(pos); - colors.push(c); colors.push(c); } s += "\n"; colors.push(glam::Vec3::new(1.0, 1.0, 1.0)); |
