diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-16 18:05:29 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-16 18:05:29 +0200 |
commit | 511199443a419f549aeb500d7b013baef10152de (patch) | |
tree | 10fb8dd4b9166e57d95ad0d31a1a45734745ebd0 /light-client/src/sprite_renderer.rs | |
parent | 9dfc5afb299ed74b277735bcf06f47b52f68caee (diff) | |
download | hurrycurry-511199443a419f549aeb500d7b013baef10152de.tar hurrycurry-511199443a419f549aeb500d7b013baef10152de.tar.bz2 hurrycurry-511199443a419f549aeb500d7b013baef10152de.tar.zst |
refactor renderer again
Diffstat (limited to 'light-client/src/sprite_renderer.rs')
-rw-r--r-- | light-client/src/sprite_renderer.rs | 200 |
1 files changed, 0 insertions, 200 deletions
diff --git a/light-client/src/sprite_renderer.rs b/light-client/src/sprite_renderer.rs deleted file mode 100644 index aadbfbe3..00000000 --- a/light-client/src/sprite_renderer.rs +++ /dev/null @@ -1,200 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <https://www.gnu.org/licenses/>. - -*/ -use hurrycurry_protocol::glam::Vec2; -use sdl2::{ - pixels::PixelFormatEnum, - rect::{FRect, Rect}, - render::{BlendMode, Canvas, Texture, TextureAccess, TextureCreator}, - video::{Window, WindowContext}, -}; -use std::collections::HashMap; - -pub struct SpriteRenderer<'a> { - metadata: HashMap<String, Rect>, - misc_textures: MiscTextures, - - texture: Texture<'a>, - - view_scale: Vec2, - view_offset: Vec2, - - sprites: Vec<SpriteDraw>, -} - -pub struct MiscTextures { - pub player: Rect, -} - -pub struct SpriteDraw { - z_order: i32, - src: Rect, - dst: FRect, -} - -pub struct SpriteRect { - z_offset: f32, - src: Rect, - relative_dst: FRect, -} - -impl<'a> SpriteRenderer<'a> { - pub fn init(texture_creator: &'a TextureCreator<WindowContext>) -> Self { - let palette = include_str!("../assets/palette.csv") - .split('\n') - .filter(|l| !l.is_empty()) - .map(|s| { - let mut toks = s.split(","); - ( - toks.next().unwrap().chars().next().unwrap(), - [ - toks.next().unwrap().parse::<u8>().unwrap(), - toks.next().unwrap().parse::<u8>().unwrap(), - toks.next().unwrap().parse::<u8>().unwrap(), - toks.next().unwrap().parse::<u8>().unwrap(), - ], - ) - }) - .collect::<HashMap<_, _>>(); - - let mut texels = vec![255; 1024 * 1024 * 4]; - - for (y, line) in include_str!("../assets/atlas.ta").lines().enumerate() { - if line.is_empty() { - continue; - } - for (x, char) in line.chars().enumerate() { - let color = palette.get(&char).unwrap(); - texels[(y * 1024 + x) * 4 + 0] = color[3]; - texels[(y * 1024 + x) * 4 + 1] = color[2]; - texels[(y * 1024 + x) * 4 + 2] = color[1]; - texels[(y * 1024 + x) * 4 + 3] = color[0]; - } - } - - let mut texture = texture_creator - .create_texture( - Some(PixelFormatEnum::RGBA8888), - TextureAccess::Streaming, - 1024, - 1024, - ) - .unwrap(); - - texture.update(None, &texels, 1024 * 4).unwrap(); - texture.set_blend_mode(BlendMode::Blend); - - let metadata = include_str!("../assets/atlas.meta.csv") - .lines() - .filter(|l| !l.is_empty()) - .map(|l| { - let mut toks = l.split(","); - let x: i32 = toks.next().unwrap().parse().unwrap(); - let y: i32 = toks.next().unwrap().parse().unwrap(); - let w: u32 = toks.next().unwrap().parse().unwrap(); - let h: u32 = toks.next().unwrap().parse().unwrap(); - let name = toks.next().unwrap().to_string(); - (name, Rect::new(x, y, w, h)) - }) - .collect::<HashMap<_, _>>(); - - Self { - misc_textures: MiscTextures { - player: *metadata.get("player+a").unwrap(), - }, - texture, - metadata, - sprites: vec![], - view_offset: Vec2::ZERO, - view_scale: Vec2::splat(3.), - } - } - - #[inline] - pub fn metadata(&self) -> &HashMap<String, Rect> { - &self.metadata - } - #[inline] - pub fn misc_textures(&self) -> &MiscTextures { - &self.misc_textures - } - - pub fn draw(&mut self, z_order: f32, src: Rect, dst: FRect) { - self.sprites.push(SpriteDraw { - z_order: (z_order * 24.) as i32, - src, - dst: FRect::new( - ((dst.x + self.view_offset.x) * 32.).round() * self.view_scale.x, - ((dst.y + self.view_offset.y) * 24.).round() * self.view_scale.y, - (dst.w * 32.).round() * self.view_scale.x, - (dst.h * 24.).round() * self.view_scale.y, - ), - }) - } - - pub fn submit(&mut self, canvas: &mut Canvas<Window>) { - self.sprites.sort(); - for SpriteDraw { src, dst, .. } in self.sprites.drain(..) { - canvas.copy_f(&self.texture, src, dst).unwrap(); - } - } -} - -impl Ord for SpriteDraw { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.z_order.cmp(&other.z_order) - } -} -impl PartialOrd for SpriteDraw { - fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { - Some(self.cmp(&other)) - } -} -impl Eq for SpriteDraw {} -impl PartialEq for SpriteDraw { - fn eq(&self, other: &Self) -> bool { - self.z_order == other.z_order && self.src == other.src && self.dst == other.dst - } -} - -impl SpriteRect { - pub fn draw_at(&self, ctx: &mut SpriteRenderer, pos: Vec2) { - ctx.draw( - self.z_offset + pos.y + 5., - self.src, - FRect::new( - self.relative_dst.x + pos.x, - self.relative_dst.y + pos.y, - self.relative_dst.w, - self.relative_dst.h, - ), - ) - } - pub fn new(src: Rect) -> Self { - let relative_dst = FRect::new( - 0.0 - src.width() as f32 / 32. / 2., - 0.3 - src.height() as f32 / 24., - src.width() as f32 / 32., - src.height() as f32 / 24., - ); - Self { - z_offset: -relative_dst.h, - relative_dst, - src, - } - } -} |