-
-
Save taotao54321/f174fa5373aca9fc60442abfed2bda31 to your computer and use it in GitHub Desktop.
[tetra] spritesheet off by 1 px
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "spritesheet" | |
version = "0.1.0" | |
authors = ["taotao54321 <taotao54321@gmail.com>"] | |
edition = "2018" | |
[dependencies] | |
tetra = "0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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