From 9dfc5afb299ed74b277735bcf06f47b52f68caee Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 16 Jul 2024 17:51:59 +0200 Subject: a --- light-client/src/main.rs | 1 + light-client/src/render/mod.rs | 158 ++++++++++++++++++++++++++++++++++++++ light-client/src/render/sprite.rs | 62 +++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 light-client/src/render/mod.rs create mode 100644 light-client/src/render/sprite.rs (limited to 'light-client/src') diff --git a/light-client/src/main.rs b/light-client/src/main.rs index b00ba734..a1c02475 100644 --- a/light-client/src/main.rs +++ b/light-client/src/main.rs @@ -29,6 +29,7 @@ pub mod game; pub mod network; pub mod sprite_renderer; pub mod tilemap; +pub mod render; fn main() { env_logger::init_from_env("LOG"); diff --git a/light-client/src/render/mod.rs b/light-client/src/render/mod.rs new file mode 100644 index 00000000..fbf3d7a5 --- /dev/null +++ b/light-client/src/render/mod.rs @@ -0,0 +1,158 @@ +/* + 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 . + +*/ +pub mod sprite; + +use hurrycurry_protocol::glam::Vec2; +use sdl2::{ + pixels::PixelFormatEnum, + rect::{FRect, Rect}, + render::{BlendMode, Canvas, Texture, TextureAccess, TextureCreator}, + video::{Window, WindowContext}, +}; +use sprite::{Sprite, SpriteDraw}; +use std::collections::HashMap; + +pub struct SpriteRenderer<'a> { + metadata: HashMap, + + texture: Texture<'a>, + + view_scale: Vec2, + view_offset: Vec2, + + sprites: Vec, +} + +impl<'a> SpriteRenderer<'a> { + pub fn init(texture_creator: &'a TextureCreator) -> 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::().unwrap(), + toks.next().unwrap().parse::().unwrap(), + toks.next().unwrap().parse::().unwrap(), + toks.next().unwrap().parse::().unwrap(), + ], + ) + }) + .collect::>(); + + 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::>(); + + Self { + texture, + metadata, + sprites: vec![], + view_offset: Vec2::ZERO, + view_scale: Vec2::splat(3.), + } + } + + #[inline] + pub fn metadata(&self) -> &HashMap { + &self.metadata + } + + pub fn set_modulation(&mut self, r: u8, g: u8, b: u8, a: u8) { + self.texture.set_alpha_mod(a); + self.texture.set_color_mod(r, g, b); + } + pub fn reset_modulation(&mut self) { + self.set_modulation(255, 255, 255, 255) + } + + pub fn draw_world(&mut self, sprite: SpriteDraw) { + self.sprites.push(SpriteDraw { + z_order: sprite.z_order, + src: sprite.src, + dst: FRect::new( + ((sprite.dst.x + self.view_offset.x) * 32.).round() * self.view_scale.x, + ((sprite.dst.y + self.view_offset.y) * 24.).round() * self.view_scale.y, + (sprite.dst.w * 32.).round() * self.view_scale.x, + (sprite.dst.h * 24.).round() * self.view_scale.y, + ), + }) + } + + pub fn submit(&mut self, canvas: &mut Canvas) { + self.sprites.sort(); + for SpriteDraw { src, dst, .. } in self.sprites.drain(..) { + canvas.copy_f(&self.texture, src, dst).unwrap(); + } + } +} + +pub struct MiscTextures { + pub player: Sprite, +} + +impl MiscTextures { + pub fn init(renderer: &SpriteRenderer) -> Self { + MiscTextures { + player: Sprite::new( + *renderer.metadata().get("player+a").unwrap(), + Vec2::Y * 0.3, + 0., + ), + } + } +} diff --git a/light-client/src/render/sprite.rs b/light-client/src/render/sprite.rs new file mode 100644 index 00000000..942db611 --- /dev/null +++ b/light-client/src/render/sprite.rs @@ -0,0 +1,62 @@ +use hurrycurry_protocol::glam::Vec2; +use sdl2::rect::{FRect, Rect}; + +pub struct Sprite { + z_offset: f32, + src: Rect, + relative_dst: FRect, +} + +impl Sprite { + pub fn new(src: Rect, anchor: Vec2, elevation: f32) -> Self { + let relative_dst = FRect::new( + anchor.x - (src.w as f32) / 32. / 2., + anchor.y - (src.h as f32) / 24., + (src.w as f32) / 32., + (src.h as f32) / 32., + ); + Self { + z_offset: -relative_dst.h + anchor.y - elevation, + src, + relative_dst, + } + } + pub fn new_tile(src: Rect) { + Self::new(src, Vec2::new(0.5, 1.0), 0.); + } + pub fn at(&self, pos: Vec2) -> SpriteDraw { + SpriteDraw { + z_order: ((self.z_offset + pos.y) * 24.) as i32, + src: self.src, + dst: FRect::new( + self.relative_dst.x + pos.x, + self.relative_dst.y + pos.y, + self.relative_dst.w, + self.relative_dst.h, + ), + } + } +} + +pub struct SpriteDraw { + pub z_order: i32, + pub src: Rect, + pub dst: FRect, +} + +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 { + 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 + } +} -- cgit v1.2.3-70-g09d2 From 511199443a419f549aeb500d7b013baef10152de Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 16 Jul 2024 18:05:29 +0200 Subject: refactor renderer again --- light-client/src/game.rs | 27 +++-- light-client/src/main.rs | 7 +- light-client/src/render/sprite.rs | 11 +- light-client/src/sprite_renderer.rs | 200 ------------------------------------ light-client/src/tilemap.rs | 28 ++--- 5 files changed, 32 insertions(+), 241 deletions(-) delete mode 100644 light-client/src/sprite_renderer.rs (limited to 'light-client/src') diff --git a/light-client/src/game.rs b/light-client/src/game.rs index b6bacf74..ac5c3571 100644 --- a/light-client/src/game.rs +++ b/light-client/src/game.rs @@ -16,7 +16,7 @@ */ use crate::{ - sprite_renderer::{SpriteRect, SpriteRenderer}, + render::{sprite::Sprite, MiscTextures, SpriteRenderer}, tilemap::Tilemap, }; use hurrycurry_protocol::{ @@ -27,7 +27,7 @@ use hurrycurry_protocol::{ use log::{info, warn}; use sdl2::{ keyboard::{KeyboardState, Scancode}, - rect::{FRect, Rect}, + rect::Rect, }; use std::collections::{HashMap, HashSet, VecDeque}; @@ -39,7 +39,8 @@ pub struct Game { players: HashMap, my_id: PlayerID, - item_sprites: Vec, + misc_textures: MiscTextures, + item_sprites: Vec, movement_send_cooldown: f32, } @@ -59,7 +60,7 @@ pub struct Item { } impl Game { - pub fn new() -> Self { + pub fn new(renderer: &SpriteRenderer) -> Self { Self { tiles: HashMap::new(), players: HashMap::new(), @@ -68,6 +69,7 @@ impl Game { data: ClientGamedata::default(), collision_map: HashSet::new(), movement_send_cooldown: 0., + misc_textures: MiscTextures::init(renderer), item_sprites: Vec::new(), } } @@ -81,12 +83,14 @@ impl Game { .item_names .iter() .map(|name| { - SpriteRect::new( + Sprite::new( renderer .metadata() .get(&format!("{name}:a")) .copied() .unwrap_or(Rect::new(0, 0, 32, 24)), + Vec2::ZERO, + 0.1, ) }) .collect(); @@ -236,14 +240,7 @@ impl Game { self.tilemap.draw(ctx); for p in self.players.values() { - let src = ctx.misc_textures().player; - let dst = FRect::new( - p.movement.position.x - src.width() as f32 / 32. / 2., - p.movement.position.y + 0.3 - src.height() as f32 / 24., - src.width() as f32 / 32., - src.height() as f32 / 24., - ); - ctx.draw(dst.y + dst.h + 1., src, dst); + ctx.draw_world(self.misc_textures.player.at(p.movement.position)); if let Some(item) = &p.item { item.draw(ctx, &self.item_sprites) } @@ -257,7 +254,7 @@ impl Game { } impl Item { - pub fn draw(&self, ctx: &mut SpriteRenderer, item_sprites: &[SpriteRect]) { - item_sprites[self.kind.0].draw_at(ctx, self.position) + pub fn draw(&self, ctx: &mut SpriteRenderer, item_sprites: &[Sprite]) { + ctx.draw_world(item_sprites[self.kind.0].at(self.position)) } } diff --git a/light-client/src/main.rs b/light-client/src/main.rs index a1c02475..278229d2 100644 --- a/light-client/src/main.rs +++ b/light-client/src/main.rs @@ -17,19 +17,18 @@ */ use game::Game; use network::Network; +use render::SpriteRenderer; use sdl2::{ event::Event, keyboard::{KeyboardState, Keycode}, pixels::Color, }; -use sprite_renderer::SpriteRenderer; use std::time::{Duration, Instant}; pub mod game; pub mod network; -pub mod sprite_renderer; -pub mod tilemap; pub mod render; +pub mod tilemap; fn main() { env_logger::init_from_env("LOG"); @@ -53,8 +52,8 @@ fn main() { let texture_creator = canvas.texture_creator(); let mut net = Network::connect("ws://127.0.0.1/").unwrap(); - let mut game = Game::new(); let mut renderer = SpriteRenderer::init(&texture_creator); + let mut game = Game::new(&renderer); net.queue_out.push_back(hurrycurry_protocol::PacketS::Join { name: "light".to_string(), diff --git a/light-client/src/render/sprite.rs b/light-client/src/render/sprite.rs index 942db611..13ede525 100644 --- a/light-client/src/render/sprite.rs +++ b/light-client/src/render/sprite.rs @@ -10,10 +10,10 @@ pub struct Sprite { impl Sprite { pub fn new(src: Rect, anchor: Vec2, elevation: f32) -> Self { let relative_dst = FRect::new( - anchor.x - (src.w as f32) / 32. / 2., - anchor.y - (src.h as f32) / 24., + -anchor.x - (src.w as f32) / 32. / 2., + -anchor.y - (src.h as f32) / 24., (src.w as f32) / 32., - (src.h as f32) / 32., + (src.h as f32) / 24., ); Self { z_offset: -relative_dst.h + anchor.y - elevation, @@ -21,8 +21,8 @@ impl Sprite { relative_dst, } } - pub fn new_tile(src: Rect) { - Self::new(src, Vec2::new(0.5, 1.0), 0.); + pub fn new_tile(src: Rect) -> Self { + Self::new(src, Vec2::new(0.5, 1.0), 0.) } pub fn at(&self, pos: Vec2) -> SpriteDraw { SpriteDraw { @@ -38,6 +38,7 @@ impl Sprite { } } +#[derive(Debug, Clone, Copy)] pub struct SpriteDraw { pub z_order: i32, pub src: Rect, 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 . - -*/ -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, - misc_textures: MiscTextures, - - texture: Texture<'a>, - - view_scale: Vec2, - view_offset: Vec2, - - sprites: Vec, -} - -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) -> 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::().unwrap(), - toks.next().unwrap().parse::().unwrap(), - toks.next().unwrap().parse::().unwrap(), - toks.next().unwrap().parse::().unwrap(), - ], - ) - }) - .collect::>(); - - 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::>(); - - 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 { - &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) { - 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 { - 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, - } - } -} diff --git a/light-client/src/tilemap.rs b/light-client/src/tilemap.rs index e84db8b1..cf8c5eef 100644 --- a/light-client/src/tilemap.rs +++ b/light-client/src/tilemap.rs @@ -15,15 +15,19 @@ along with this program. If not, see . */ -use crate::sprite_renderer::SpriteRenderer; use hurrycurry_protocol::{glam::IVec2, TileIndex}; -use sdl2::rect::{FRect, Rect}; +use sdl2::rect::Rect; use std::collections::HashMap; +use crate::render::{ + sprite::{Sprite, SpriteDraw}, + SpriteRenderer, +}; + #[derive(Default)] pub struct Tilemap { tile_srcs: Vec<[Rect; 16]>, - tiles: HashMap, + tiles: HashMap, } impl Tilemap { @@ -72,23 +76,13 @@ impl Tilemap { idx |= 0b0010 * (Some(tile) == neighbors[3]) as usize; let src = self.tile_srcs[tile.0][idx]; - self.tiles.insert( - pos, - ( - src, - FRect::new( - pos.x as f32, - pos.y as f32 + 1. - src.height() as f32 / 24., - src.width() as f32 / 32., - src.height() as f32 / 24., - ), - ), - ); + self.tiles + .insert(pos, Sprite::new_tile(src).at(pos.as_vec2() + 0.5)); } pub fn draw(&self, ctx: &mut SpriteRenderer) { - for &(src, dst) in self.tiles.values() { - ctx.draw(dst.y + dst.h, src, dst); + for &sprite in self.tiles.values() { + ctx.draw_world(sprite); } } } -- cgit v1.2.3-70-g09d2 From 4064c56783bd78b96b0e79d5a7b15b1fb0d8edad Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 16 Jul 2024 18:32:14 +0200 Subject: fix z-order problems partially --- light-client/src/render/sprite.rs | 6 +++--- light-client/src/tilemap.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'light-client/src') diff --git a/light-client/src/render/sprite.rs b/light-client/src/render/sprite.rs index 13ede525..c19f8392 100644 --- a/light-client/src/render/sprite.rs +++ b/light-client/src/render/sprite.rs @@ -10,13 +10,13 @@ pub struct Sprite { impl Sprite { pub fn new(src: Rect, anchor: Vec2, elevation: f32) -> Self { let relative_dst = FRect::new( - -anchor.x - (src.w as f32) / 32. / 2., - -anchor.y - (src.h as f32) / 24., + anchor.x - (src.w as f32) / 32. / 2., + anchor.y - (src.h as f32) / 24., (src.w as f32) / 32., (src.h as f32) / 24., ); Self { - z_offset: -relative_dst.h + anchor.y - elevation, + z_offset: elevation, src, relative_dst, } diff --git a/light-client/src/tilemap.rs b/light-client/src/tilemap.rs index cf8c5eef..e7341efa 100644 --- a/light-client/src/tilemap.rs +++ b/light-client/src/tilemap.rs @@ -77,7 +77,7 @@ impl Tilemap { let src = self.tile_srcs[tile.0][idx]; self.tiles - .insert(pos, Sprite::new_tile(src).at(pos.as_vec2() + 0.5)); + .insert(pos, Sprite::new_tile(src).at(pos.as_vec2())); } pub fn draw(&self, ctx: &mut SpriteRenderer) { -- cgit v1.2.3-70-g09d2