diff options
Diffstat (limited to 'pixel-client/src/render')
| -rw-r--r-- | pixel-client/src/render/font.rs | 68 | ||||
| -rw-r--r-- | pixel-client/src/render/misc.rs | 46 | ||||
| -rw-r--r-- | pixel-client/src/render/mod.rs | 192 | ||||
| -rw-r--r-- | pixel-client/src/render/sprite.rs | 123 | 
4 files changed, 0 insertions, 429 deletions
diff --git a/pixel-client/src/render/font.rs b/pixel-client/src/render/font.rs deleted file mode 100644 index 60d27083..00000000 --- a/pixel-client/src/render/font.rs +++ /dev/null @@ -1,68 +0,0 @@ -/* -    Hurry Curry! - a game about cooking -    Copyright (C) 2025 Hurry Curry! Contributors - -    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 super::{sprite::SpriteDraw, AtlasLayout, Renderer}; -use hurrycurry_protocol::glam::Vec2; -use sdl2::rect::Rect; - -pub struct FontTextures { -    pub glyphs: [Rect; 128], -} - -impl FontTextures { -    pub fn init(layout: &AtlasLayout) -> Self { -        FontTextures { -            glyphs: (0..128) -                .map(|n| { -                    layout -                        .get(&format!("letter_{n}+a")) -                        .copied() -                        .unwrap_or(Rect::new(0, 0, 0, 0)) -                }) -                .collect::<Vec<_>>() -                .try_into() -                .expect("some letters are missing in the font"), -        } -    } -} - -impl Renderer<'_> { -    pub fn draw_text( -        &mut self, -        position: Vec2, -        text: &str, -        scale: f32, -        tint: Option<[u8; 4]>, -    ) -> Vec2 { -        let mut cursor = position; -        let mut line_height = 0f32; -        for c in text.chars() { -            if c == '\n' { -                cursor.y += line_height; -                cursor.x = position.x -            } -            if (c as u32) < 128 { -                let r = self.font_textures.glyphs[c as usize]; -                let size = Vec2::new(r.width() as f32, r.height() as f32) * scale; -                self.draw_ui(SpriteDraw::overlay(r, cursor, size, tint)); -                cursor.x += size.x; -                line_height = line_height.max(size.y) -            } -        } -        (cursor - position.y) + Vec2::Y * line_height -    } -} diff --git a/pixel-client/src/render/misc.rs b/pixel-client/src/render/misc.rs deleted file mode 100644 index c9f25462..00000000 --- a/pixel-client/src/render/misc.rs +++ /dev/null @@ -1,46 +0,0 @@ -/* -    Hurry Curry! - a game about cooking -    Copyright (C) 2025 Hurry Curry! Contributors - -    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 super::{sprite::Sprite, AtlasLayout}; -use hurrycurry_protocol::glam::Vec2; -use sdl2::rect::Rect; - -pub struct MiscTextures { -    pub chef: Sprite, -    pub customer: Sprite, -    pub interact_target: Sprite, -    pub solid: Rect, -    pub clouds: Rect, -    pub itembubble: Sprite, -} - -impl MiscTextures { -    pub fn init(layout: &AtlasLayout) -> Self { -        MiscTextures { -            chef: Sprite::new(*layout.get("chef+a").unwrap(), Vec2::Y * 0.3, 0.5 + 0.3), -            customer: Sprite::new(*layout.get("customer+a").unwrap(), Vec2::Y * 0.3, 0.5 + 0.3), -            interact_target: Sprite::new( -                *layout.get("interact-target-thick+a").unwrap(), -                Vec2::new(0.5, 1.0), -                10., -            ), -            solid: *layout.get("solid+a").unwrap(), -            clouds: *layout.get("clouds+a").unwrap(), -            itembubble: Sprite::new(*layout.get("itembubble+a").unwrap(), Vec2::Y * -1., 1.), -        } -    } -} diff --git a/pixel-client/src/render/mod.rs b/pixel-client/src/render/mod.rs deleted file mode 100644 index 008e015d..00000000 --- a/pixel-client/src/render/mod.rs +++ /dev/null @@ -1,192 +0,0 @@ -/* -    Hurry Curry! - a game about cooking -    Copyright (C) 2025 Hurry Curry! Contributors - -    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/>. - -*/ -pub mod font; -pub mod misc; -pub mod sprite; - -use font::FontTextures; -use hurrycurry_protocol::glam::Vec2; -use misc::MiscTextures; -use sdl2::{ -    pixels::PixelFormatEnum, -    rect::{FRect, Rect}, -    render::{BlendMode, Canvas, Texture, TextureAccess, TextureCreator}, -    video::{Window, WindowContext}, -}; -use sprite::SpriteDraw; -use std::collections::HashMap; - -pub struct Renderer<'a> { -    metadata: AtlasLayout, - -    font_textures: FontTextures, -    pub misc_textures: MiscTextures, - -    pub size: Vec2, -    pub ui_size: Vec2, -    texture: Texture<'a>, - -    world_scale: Vec2, -    world_offset: Vec2, -    pub ui_scale: Vec2, - -    sprites: Vec<SpriteDraw>, -} - -pub type AtlasLayout = HashMap<String, Rect>; - -impl<'a> Renderer<'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] = 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 atlas_layout = 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 { -            ui_scale: Vec2::ZERO, -            ui_size: Vec2::ZERO, -            misc_textures: MiscTextures::init(&atlas_layout), -            texture, -            font_textures: FontTextures::init(&atlas_layout), -            size: Vec2::ONE, -            metadata: atlas_layout, -            sprites: vec![], -            world_offset: Vec2::ZERO, -            world_scale: Vec2::ZERO, -        } -    } - -    pub fn set_world_view(&mut self, offset: Vec2, scale: f32) { -        self.world_offset = offset; -        self.world_scale = Vec2::new(32., 24.) * scale; -    } -    pub fn set_ui_view(&mut self, scale: f32) { -        self.ui_scale = Vec2::splat(scale); -        self.ui_size = self.size / self.ui_scale; -    } -    pub fn get_world_scale(&self) -> Vec2 { -        self.world_scale -    } - -    #[inline] -    pub fn atlas_layout(&self) -> &HashMap<String, Rect> { -        &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 { -            tint: sprite.tint, -            z_order: sprite.z_order, -            src: sprite.src, -            dst: FRect::new( -                (sprite.dst.x + self.world_offset.x) * self.world_scale.x, -                (sprite.dst.y + self.world_offset.y) * self.world_scale.y, -                sprite.dst.w * self.world_scale.x, -                sprite.dst.h * self.world_scale.y, -            ), -        }) -    } - -    pub fn draw_ui(&mut self, sprite: SpriteDraw) { -        self.sprites.push(SpriteDraw { -            tint: sprite.tint, -            z_order: sprite.z_order, -            src: sprite.src, -            dst: FRect::new( -                sprite.dst.x * self.ui_scale.x, -                sprite.dst.y * self.ui_scale.y, -                sprite.dst.w * self.ui_scale.x, -                sprite.dst.h * self.ui_scale.y, -            ), -        }) -    } - -    pub fn submit(&mut self, canvas: &mut Canvas<Window>) { -        self.sprites.sort(); -        for SpriteDraw { src, dst, tint, .. } in self.sprites.drain(..) { -            self.texture.set_color_mod(tint[0], tint[1], tint[2]); -            self.texture.set_alpha_mod(tint[3]); -            canvas.copy_f(&self.texture, src, dst).unwrap(); -        } -    } - -    pub fn num_sprites(&self) -> usize { -        self.sprites.len() -    } -} diff --git a/pixel-client/src/render/sprite.rs b/pixel-client/src/render/sprite.rs deleted file mode 100644 index ae06165a..00000000 --- a/pixel-client/src/render/sprite.rs +++ /dev/null @@ -1,123 +0,0 @@ -/* -    Hurry Curry! - a game about cooking -    Copyright (C) 2025 Hurry Curry! Contributors - -    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::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) / 24., -        ); -        Self { -            z_offset: elevation, -            src, -            relative_dst, -        } -    } -    pub fn new_tile(src: Rect) -> Self { -        Self::new(src, Vec2::new(0.5, 1.0), 0.5) -    } -    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, -            ), -            tint: [0xff; 4], -        } -    } -} - -#[derive(Debug, Clone, Copy)] -pub struct SpriteDraw { -    pub tint: [u8; 4], -    pub z_order: i32, -    pub src: Rect, -    pub dst: FRect, -} - -impl SpriteDraw { -    pub fn screen(src: Rect, z_order: i32, pos: Vec2, size: Vec2, tint: Option<[u8; 4]>) -> Self { -        Self { -            dst: FRect::new(pos.x, pos.y, size.x, size.y), -            src, -            tint: tint.unwrap_or([0xff; 4]), -            z_order, -        } -    } -    pub fn overlay(src: Rect, pos: Vec2, size: Vec2, tint: Option<[u8; 4]>) -> Self { -        SpriteDraw::screen(src, i32::MAX, pos, size, tint) -    } -    pub fn underlay(src: Rect, pos: Vec2, size: Vec2, tint: Option<[u8; 4]>) -> Self { -        SpriteDraw::screen(src, i32::MIN, pos, size, tint) -    } -    pub fn alpha(mut self, alpha: f32) -> Self { -        self.tint[3] = (alpha.clamp(0., 1.) * 255.) as u8; -        self -    } -    pub fn tint(mut self, r: u8, g: u8, b: u8) -> Self { -        self.tint[0] = r; -        self.tint[1] = g; -        self.tint[2] = b; -        self -    } -    pub fn elevate(mut self, offset: f32) -> SpriteDraw { -        self.z_order += (offset * 24.) as i32; -        self.dst.set_y(self.dst.y() - offset); -        self -    } -    pub fn scale(mut self, factor: f32) -> SpriteDraw { -        self.dst -            .set_x(self.dst.x() + self.dst.width() * 0.5 * (1. - factor)); -        self.dst -            .set_y(self.dst.y() + self.dst.height() * 0.5 * (1. - factor)); -        self.dst.set_width(self.dst.width() * factor); -        self.dst.set_height(self.dst.height() * factor); -        self -    } -} - -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 -    } -}  |