Skip to content

Instantly share code, notes, and snippets.

@taotao54321
Last active July 27, 2020 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taotao54321/f174fa5373aca9fc60442abfed2bda31 to your computer and use it in GitHub Desktop.
Save taotao54321/f174fa5373aca9fc60442abfed2bda31 to your computer and use it in GitHub Desktop.
[tetra] spritesheet off by 1 px
[package]
name = "spritesheet"
version = "0.1.0"
authors = ["taotao54321 <taotao54321@gmail.com>"]
edition = "2018"
[dependencies]
tetra = "0.4"
use tetra::graphics::{self, DrawParams, Rectangle, Texture};
use tetra::math::Vec2;
use tetra::{Context, ContextBuilder, State};
const SPRITE_W: f32 = 128.0;
const SPRITE_H: f32 = 128.0;
struct GameState {
tex: Texture,
}
impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<Self> {
Ok(Self {
tex: Texture::new(ctx, concat!(env!("CARGO_MANIFEST_DIR"), "/tile.png"))?,
})
}
}
impl State for GameState {
fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
for i in 0..4 {
let clip = Rectangle::new(0.0, i as f32 * SPRITE_H, SPRITE_W, SPRITE_H);
graphics::draw(
ctx,
&self.tex,
DrawParams::default()
.clip(clip)
.position(Vec2::new(40.0 + (SPRITE_W + 20.0) * i as f32, 39.5)),
);
}
Ok(())
}
}
fn main() -> tetra::Result {
ContextBuilder::new("spritesheet", 640, 200)
.quit_on_escape(true)
.build()?
.run(GameState::new)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment