-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
shapes.rs
49 lines (41 loc) 路 1.63 KB
/
shapes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use tetra::graphics::mesh::{GeometryBuilder, Mesh, ShapeStyle};
use tetra::graphics::{self, Color};
use tetra::math::Vec2;
use tetra::{Context, ContextBuilder, State};
struct GameState {
simple: Mesh,
complex: Mesh,
}
impl GameState {
fn new(ctx: &mut Context) -> tetra::Result<GameState> {
// For simple one-off shapes, `Mesh` has simple constructors.
let simple = Mesh::circle(ctx, ShapeStyle::Stroke(16.0), Vec2::zero(), 16.0)?;
// If you want to create a `Mesh` with multiple shapes, there is a `GeometryBuilder`
// type that lets you do this. You can also use it to create buffers, or generate
// raw vertex data that you can process further yourself.
let complex = GeometryBuilder::new()
// Background
.set_color(Color::rgb(1.0, 1.0, 0.0))
.circle(ShapeStyle::Fill, Vec2::zero(), 64.0)?
// Face
.set_color(Color::BLACK)
.circle(ShapeStyle::Fill, Vec2::new(-16.0, -16.0), 8.0)?
.circle(ShapeStyle::Fill, Vec2::new(16.0, -16.0), 8.0)?
.polyline(8.0, &[Vec2::new(-16.0, 24.0), Vec2::new(16.0, 24.0)])?
.build_mesh(ctx)?;
Ok(GameState { simple, complex })
}
}
impl State for GameState {
fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));
self.simple.draw(ctx, Vec2::new(64.0, 64.0));
self.complex.draw(ctx, Vec2::new(256.0, 64.0));
Ok(())
}
}
fn main() -> tetra::Result {
ContextBuilder::new("Shape Drawing", 1280, 720)
.build()?
.run(GameState::new)
}