spottedcat 1.0.2

Rusty SpottedCat simple game engine
Documentation
use spottedcat::{Context, DrawOption, Image, Pt, Spot, WindowConfig, run};

mod example_font;

struct SevenLevelNestTestSpot {
    font_id: u32,
    images: Vec<Image>,
    white_image: Image,
}

impl Spot for SevenLevelNestTestSpot {
    fn initialize(ctx: &mut Context) -> Self {
        let font_id = example_font::register(ctx);
        let mut images = Vec::new();

        // Level 1-7 - Render targets
        for _ in 0..6 {
            images.push(
                spottedcat::Texture::new_render_target(ctx, Pt::from(200.0), Pt::from(200.0))
                    .view(),
            );
        }
        // Final level at half size
        images.push(
            spottedcat::Texture::new_render_target(ctx, Pt::from(100.0), Pt::from(100.0)).view(),
        );

        let white_image =
            spottedcat::Image::new(ctx, Pt::from(1.0), Pt::from(1.0), &[255, 255, 255, 255])
                .expect("Failed to create white image");

        Self {
            font_id,
            images,
            white_image,
        }
    }

    fn draw(&mut self, ctx: &mut Context, screen: spottedcat::Image) {
        let colors = [
            [1.0, 0.0, 0.0, 1.0], // Red
            [0.0, 0.0, 1.0, 1.0], // Blue
            [0.0, 1.0, 0.0, 1.0], // Green
            [1.0, 1.0, 0.0, 1.0], // Yellow
            [0.0, 1.0, 1.0, 1.0], // Cyan
            [1.0, 0.0, 1.0, 1.0], // Magenta
            [1.0, 1.0, 1.0, 1.0], // White (Level 7)
        ];

        for (i, color) in colors.into_iter().enumerate() {
            let target = self.images[i];
            target.draw_with_shader(
                ctx,
                self.white_image,
                1,
                DrawOption::default()
                    .with_scale([target.width().as_f32(), target.height().as_f32()]),
                spottedcat::ShaderOpts::default().with_color(color),
            );
        }

        // Composite each level into its parent, from innermost to outermost.
        self.images[5].draw(
            ctx,
            &self.images[6],
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]),
        );

        self.images[4].draw(
            ctx,
            &self.images[5],
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]),
        );

        self.images[3].draw(
            ctx,
            &self.images[4],
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]),
        );

        self.images[2].draw(
            ctx,
            &self.images[3],
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]),
        );

        self.images[1].draw(
            ctx,
            &self.images[2],
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]),
        );

        self.images[0].draw(
            ctx,
            &self.images[1],
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(10.0)]),
        );

        screen.draw(
            ctx,
            &self.images[0],
            DrawOption::default().with_position([Pt::from(50.0), Pt::from(50.0)]),
        );

        let text = spottedcat::Text::new("7级嵌套递归渲染测试", self.font_id)
            .with_font_size(Pt::from(16.0))
            .with_color([0.0, 0.0, 0.0, 1.0]);

        self.images[6].draw(
            ctx,
            &text,
            DrawOption::default().with_position([Pt::from(10.0), Pt::from(40.0)]),
        );
    }
}

fn main() {
    run::<SevenLevelNestTestSpot>(WindowConfig::default());
}