Skip to content

Instantly share code, notes, and snippets.

@17cupsofcoffee
Created March 4, 2021 18:05
Show Gist options
  • Save 17cupsofcoffee/fe4b974a7e709d2dc7ecdbaddf63976d to your computer and use it in GitHub Desktop.
Save 17cupsofcoffee/fe4b974a7e709d2dc7ecdbaddf63976d to your computer and use it in GitHub Desktop.
A blank Tetra template
use tetra::graphics::scaling::{ScalingMode, ScreenScaler};
use tetra::graphics::{self, Color};
use tetra::input::{self, Key};
use tetra::{Context, ContextBuilder, Event, State};
struct Assets {
// assets go here...
}
impl Assets {
fn load(ctx: &mut Context) -> tetra::Result<Assets> {
Ok(Assets {})
}
}
struct GameState {
assets: Assets,
scaler: ScreenScaler,
}
impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<GameState> {
Ok(GameState {
assets: Assets::load(ctx)?,
scaler: ScreenScaler::with_window_size(
ctx,
320,
180,
ScalingMode::ShowAllPixelPerfect,
)?,
})
}
}
impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
if input::is_key_pressed(ctx, Key::F5) {
self.assets = Assets::load(ctx)?;
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
graphics::set_canvas(ctx, self.scaler.canvas());
graphics::clear(ctx, Color::BLACK);
// draw stuff here...
graphics::reset_canvas(ctx);
graphics::clear(ctx, Color::BLACK);
self.scaler.draw(ctx);
Ok(())
}
fn event(&mut self, _: &mut Context, event: Event) -> tetra::Result {
if let Event::Resized { width, height, .. } = event {
self.scaler.set_outer_size(width, height);
}
Ok(())
}
}
fn main() -> tetra::Result {
ContextBuilder::new("Tetra", 320 * 4, 180 * 4)
.resizable(true)
.build()?
.run(GameState::new)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment