diff options
author | metamuffin <yvchraiqi@protonmail.com> | 2022-06-09 16:01:55 +0200 |
---|---|---|
committer | metamuffin <yvchraiqi@protonmail.com> | 2022-06-09 16:01:55 +0200 |
commit | c1b5937b64649904689fea8da2c06e0b66ae8332 (patch) | |
tree | 458e7a8b5e6489e26e58ffbb134fd40c7f04dd5e /renderer/src | |
parent | 024a73e9575980b40c93d5f60c0e4e3f8e54187c (diff) | |
download | twclient-c1b5937b64649904689fea8da2c06e0b66ae8332.tar twclient-c1b5937b64649904689fea8da2c06e0b66ae8332.tar.bz2 twclient-c1b5937b64649904689fea8da2c06e0b66ae8332.tar.zst |
asdfss
Diffstat (limited to 'renderer/src')
-rw-r--r-- | renderer/src/main.rs | 9 | ||||
-rw-r--r-- | renderer/src/map.rs | 91 | ||||
-rw-r--r-- | renderer/src/tee.rs | 94 |
3 files changed, 119 insertions, 75 deletions
diff --git a/renderer/src/main.rs b/renderer/src/main.rs index 63396cc..985a717 100644 --- a/renderer/src/main.rs +++ b/renderer/src/main.rs @@ -15,7 +15,7 @@ use signal_hook::{ }; use skia_safe::{ gpu::{gl::FramebufferInfo, BackendRenderTarget, SurfaceOrigin}, - Canvas, Color, Color4f, ColorSpace, ColorType, Paint, Point, Surface, + Canvas, Color, ColorType, Surface, }; use std::{ collections::HashSet, convert::TryInto, net::IpAddr, process::exit, str::FromStr, @@ -228,7 +228,12 @@ impl Renderer { } pub fn draw(&mut self, canvas: &mut Canvas, dims: (f32, f32)) { canvas.clear(Color::TRANSPARENT); - let center = self.world.local_tee().map(|t| (t.x, t.y)).unwrap_or((0, 0)); + let center = self + .world + .tees + .local() + .map(|t| (t.x, t.y)) + .unwrap_or((0, 0)); canvas.save(); canvas.translate((dims.0 / 2.0, dims.1 / 2.0)); diff --git a/renderer/src/map.rs b/renderer/src/map.rs index f7672c1..a1e8d94 100644 --- a/renderer/src/map.rs +++ b/renderer/src/map.rs @@ -1,17 +1,17 @@ use std::collections::HashMap; -use log::{info, warn}; +use log::info; use skia_safe::{ - canvas::SrcRectConstraint, BlendMode, Canvas, Color4f, ColorSpace, ISize, Image, Matrix, Paint, - Rect, + canvas::SrcRectConstraint, Canvas, Color4f, ColorSpace, ISize, Image, Matrix, Paint, Rect, }; use twclient::world::{ + helper::Color, map::{format, TILE_NUM}, World, }; pub struct MapRenderer { - tileset: HashMap<Option<usize>, Image>, + tileset: HashMap<(Option<usize>, Color), Image>, } const TILE_SIZE: f32 = 32.0; @@ -27,27 +27,35 @@ impl MapRenderer { pub fn map_changed(&mut self, world: &World) { self.tileset.clear(); for (key, t) in &world.map.tilesets { - let mut bytes: Vec<u8> = Vec::with_capacity(t.dim().0 * t.dim().1 * 4); - info!("loading tileset: {:?} => {:?}", key, t.dim()); - for ((_x, _y), c) in t.indexed_iter() { - bytes.push(c.red); - bytes.push(c.green); - bytes.push(c.blue); - bytes.push(c.alpha); + for layer in world.map.layers.iter().filter(|l| l.image == *key) { + let tint = layer.color; + let mut bytes: Vec<u8> = Vec::with_capacity(t.dim().0 * t.dim().1 * 4); + info!( + "loading tileset: (texture: {:?}, tint: {}) => {:?}", + key, + tint, + t.dim() + ); + for ((_x, _y), c) in t.indexed_iter() { + bytes.push((c.r as u32 * tint.r as u32) as u8); + bytes.push((c.g as u32 * tint.g as u32) as u8); + bytes.push((c.b as u32 * tint.b as u32) as u8); + bytes.push((c.a as u32 * tint.a as u32) as u8); + } + let d = skia_safe::Data::new_copy(&bytes); + let v = skia_safe::Image::from_raster_data( + &skia_safe::ImageInfo::new( + ISize::new(t.dim().0 as i32, t.dim().1 as i32), + skia_safe::ColorType::RGBA8888, + skia_safe::AlphaType::Premul, + ColorSpace::new_srgb_linear(), + ), + d, + t.dim().0 * 4, + ) + .unwrap(); + self.tileset.insert((*key, tint), v); } - let d = skia_safe::Data::new_copy(&bytes); - let v = skia_safe::Image::from_raster_data( - &skia_safe::ImageInfo::new( - ISize::new(t.dim().0 as i32, t.dim().1 as i32), - skia_safe::ColorType::RGBA8888, - skia_safe::AlphaType::Premul, - ColorSpace::new_srgb_linear(), - ), - d, - t.dim().0 * 4, - ) - .unwrap(); - self.tileset.insert(*key, v); } } @@ -66,25 +74,24 @@ impl MapRenderer { grid_paint.set_anti_alias(true); let center = world - .local_tee() + .tees + .local() .map(|t| (t.x / 32, t.y / 32)) .unwrap_or((0, 0)); for l in &world.map.layers { - let tileset = self.tileset.get(&l.image).unwrap(); + let tileset = self.tileset.get(&(l.image, l.color)).unwrap(); let mut layer_tint = Paint::new( Color4f { - a: 0.0, - b: 0.0, - g: 0.0, - r: 0.0, + a: 1.0, + b: 1.0, + g: 1.0, + r: 1.0, }, &ColorSpace::new_srgb(), ); layer_tint.set_style(skia_safe::PaintStyle::Fill); - layer_tint.set_blend_mode(BlendMode::Multiply); - layer_tint.set_argb(l.color.alpha, l.color.red, l.color.green, l.color.blue); for layer_y in (center.1 - 15)..(center.1 + 15) { for layer_x in (center.0 - 15)..(center.0 + 15) { @@ -142,26 +149,6 @@ impl MapRenderer { &layer_tint, ); - // if hflip { - // canvas.draw_rect( - // Rect { - // top: -TILE_SIZE / 2.0, - // bottom: TILE_SIZE / 2.0, - // left: -TILE_SIZE / 2.0, - // right: TILE_SIZE / 2.0, - // }, - // &Paint::new( - // Color4f { - // a: 0.5, - // b: 1.0, - // g: 0.0, - // r: 1.0, - // }, - // &ColorSpace::new_srgb(), - // ), - // ); - // } - canvas.restore(); } } diff --git a/renderer/src/tee.rs b/renderer/src/tee.rs index 98ac218..d796f5e 100644 --- a/renderer/src/tee.rs +++ b/renderer/src/tee.rs @@ -1,22 +1,41 @@ -use skia_safe::{Canvas, Color4f, ColorSpace, Font, Paint, Point}; -use twclient::world::World; +use std::{collections::BTreeMap, fs::File, io::Read}; -const TEE_RADIUS: f32 = 16.0; +use skia_safe::{ + canvas::SrcRectConstraint, utils::text_utils::Align, Canvas, Color4f, ColorSpace, Data, Font, + Image, Paint, Point, Rect, +}; +use twclient::world::{tee::Tee, World}; -pub struct TeeRenderer {} +const TEE_COLL_RADIUS: f32 = 16.0; +const TEE_REND_RADIUS: f32 = 32.0; + +pub struct TeeRenderer { + skins: BTreeMap<String, Image>, +} impl TeeRenderer { pub fn new() -> Self { - Self {} + Self { + skins: BTreeMap::new(), + } } pub fn draw(&mut self, world: &World, canvas: &mut Canvas) { + for t in world.tees.inner.values() { + canvas.save(); + canvas.translate((t.x as f32, t.y as f32)); + self.draw_tee(canvas, t); + canvas.restore(); + } + } + + pub fn draw_tee(&mut self, canvas: &mut Canvas, tee: &Tee) { let tee_paint = Paint::new( Color4f { a: 1.0, - r: 0.2, - g: 0.0, - b: 0.2, + r: 1.0, + g: 1.0, + b: 1.0, }, &ColorSpace::new_srgb(), ); @@ -29,18 +48,51 @@ impl TeeRenderer { }, &ColorSpace::new_srgb(), ); - for t in world.tees.values() { - let origin = Point { - x: t.x as f32, - y: t.y as f32, - }; - canvas.draw_circle(origin, TEE_RADIUS, &tee_paint); - canvas.draw_str( - t.name.as_str(), - (origin.x, origin.y - 20.0), - &Font::default(), - &name_paint, - ); - } + let skin_texture = self + .skins + .entry(tee.skin.clone()) + .or_insert_with(|| TeeRenderer::load_skin(tee.name.as_str()).unwrap()); + + let origin = Point { + x: tee.x as f32, + y: tee.y as f32, + }; + + canvas.draw_image_rect( + skin_texture, + Some(( + &Rect { + top: 16.0, + left: 16.0, + right: 96.0, + bottom: 96.0, + }, + SrcRectConstraint::Strict, + )), + Rect { + top: -TEE_REND_RADIUS, + left: -TEE_REND_RADIUS, + right: TEE_REND_RADIUS, + bottom: TEE_REND_RADIUS, + }, + &tee_paint, + ); + + canvas.draw_str_align( + tee.name.as_str(), + (origin.x, origin.y - 20.0), + &Font::default(), + &name_paint, + Align::Center, + ); + } + + pub fn load_skin(_name: &str) -> Option<Image> { + let path = "/usr/share/ddnet/data/skins/default.png"; + let mut file = File::open(path).unwrap(); + let mut data = Vec::new(); + file.read_to_end(&mut data).unwrap(); + let data = Data::new_copy(&data); + Image::from_encoded(data) } } |