hecrj / coffee

An opinionated 2D game engine for Rust
https://docs.rs/coffee
MIT License
1.08k stars 55 forks source link

Using Image as tile sheet #123

Closed buxx closed 4 years ago

buxx commented 4 years ago

Hello, I have question about graphics::Image usage. I would like use image as tile sheet. As i understand, Image.draw require a Quad like Rectangle which represent the part of image to keep.

So, for a tile sheet of 16px tiles, if I want to draw tile at 3rd row and 2nd column:

        frame.clear(Color {r: 0.4, g: 0.6, b: 0.8, a: 1.0,});
        let image = Image::new(frame.gpu(), "resources/tilesheet.png").unwrap();
        // Tiles are 16x
        let tile_width_ratio: f32 = 16.0 / image.width() as f32;
        let tile_height_ratio: f32 = 16.0 / image.height() as f32;
        image.draw(
            Quad {
                source: Rectangle {
                    x: tile_width_ratio * 1.0,
                    y: tile_height_ratio * 2.0,
                    width: tile_width_ratio,
                    height: tile_height_ratio,
                },
                position: Point::new(10.0, 10.0),
                size: (16.0, 16.0),
            },
            &mut frame.as_target(),
        );

Why I have to set image ratio instead simple "coordinates" and width/height ? Like that:

                source: Rectangle {
                    x: 16 * 1,
                    y: 16 * 2,
                    width: 16,
                    height: 16,
                },

Using float disturb me and I'm afraid about "round" errors ^^ Is that possible to use simple "coordinates" and width/height ?

buxx commented 4 years ago

Oh, after read some source code of coffee, i noticed i just have to use Sprite instead Quad!