-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_drawing.rs
189 lines (161 loc) · 5.2 KB
/
03_drawing.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! A collection of semi-random shape and image drawing examples.
use cgmath;
use ggez;
use ggez::event;
use ggez::graphics;
use ggez::graphics::{Color, DrawMode, DrawParam};
use ggez::nalgebra::Point2;
use ggez::timer;
use ggez::{Context, GameResult};
use std::env;
use std::path;
struct MainState {
image1: graphics::Image,
image2_linear: graphics::Image,
image2_nearest: graphics::Image,
meshes: Vec<graphics::Mesh>,
rotation: f32,
}
impl MainState {
/// Load images and create meshes.
fn new(ctx: &mut Context) -> GameResult<MainState> {
let image1 = graphics::Image::new(ctx, "/dragon1.png")?;
let image2_linear = graphics::Image::new(ctx, "/shot.png")?;
let mut image2_nearest = graphics::Image::new(ctx, "/shot.png")?;
image2_nearest.set_filter(graphics::FilterMode::Nearest);
let meshes = vec![build_mesh(ctx)?, build_textured_triangle(ctx)?];
let s = MainState {
image1,
image2_linear,
image2_nearest,
meshes,
rotation: 1.0,
};
Ok(s)
}
}
fn build_mesh(ctx: &mut Context) -> GameResult<graphics::Mesh> {
let mb = &mut graphics::MeshBuilder::new();
mb.line(
&[
Point2::new(200.0, 200.0),
Point2::new(400.0, 200.0),
Point2::new(400.0, 400.0),
Point2::new(200.0, 400.0),
Point2::new(200.0, 300.0),
],
4.0,
Color::new(1.0, 0.0, 0.0, 1.0),
)?;
mb.ellipse(
DrawMode::fill(),
Point2::new(600.0, 200.0),
50.0,
120.0,
1.0,
Color::new(1.0, 1.0, 0.0, 1.0),
);
mb.circle(
DrawMode::fill(),
Point2::new(600.0, 380.0),
40.0,
1.0,
Color::new(1.0, 0.0, 1.0, 1.0),
);
mb.build(ctx)
}
fn build_textured_triangle(ctx: &mut Context) -> GameResult<graphics::Mesh> {
let mb = &mut graphics::MeshBuilder::new();
let triangle_verts = vec![
graphics::Vertex {
pos: [100.0, 100.0],
uv: [1.0, 1.0],
color: [1.0, 0.0, 0.0, 1.0],
},
graphics::Vertex {
pos: [0.0, 100.0],
uv: [0.0, 1.0],
color: [0.0, 1.0, 0.0, 1.0],
},
graphics::Vertex {
pos: [0.0, 0.0],
uv: [0.0, 0.0],
color: [0.0, 0.0, 1.0, 1.0],
},
];
let triangle_indices = vec![0, 1, 2];
let i = graphics::Image::new(ctx, "/rock.png")?;
mb.raw(&triangle_verts, &triangle_indices, Some(i));
mb.build(ctx)
}
impl event::EventHandler for MainState {
fn update(&mut self, ctx: &mut Context) -> GameResult {
const DESIRED_FPS: u32 = 60;
while timer::check_update_time(ctx, DESIRED_FPS) {
self.rotation += 0.01;
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
// Draw an image.
let dst = cgmath::Point2::new(20.0, 20.0);
graphics::draw(ctx, &self.image1, (dst,))?;
// Draw an image with some options, and different filter modes.
let dst = cgmath::Point2::new(200.0, 100.0);
let dst2 = cgmath::Point2::new(400.0, 400.0);
let scale = cgmath::Vector2::new(10.0, 10.0);
graphics::draw(
ctx,
&self.image2_linear,
graphics::DrawParam::new()
.dest(dst)
.rotation(self.rotation)
.scale(scale),
)?;
graphics::draw(
ctx,
&self.image2_nearest,
graphics::DrawParam::new()
.dest(dst2)
.rotation(self.rotation)
.offset(Point2::new(0.5, 0.5))
.scale(scale),
)?;
// Create and draw a filled rectangle mesh.
let rect = graphics::Rect::new(450.0, 450.0, 50.0, 50.0);
let r1 =
graphics::Mesh::new_rectangle(ctx, graphics::DrawMode::fill(), rect, graphics::WHITE)?;
graphics::draw(ctx, &r1, DrawParam::default())?;
// Create and draw a stroked rectangle mesh.
let rect = graphics::Rect::new(450.0, 450.0, 50.0, 50.0);
let r2 = graphics::Mesh::new_rectangle(
ctx,
graphics::DrawMode::stroke(1.0),
rect,
graphics::Color::new(1.0, 0.0, 0.0, 1.0),
)?;
graphics::draw(ctx, &r2, DrawParam::default())?;
// Draw some pre-made meshes
for m in &self.meshes {
graphics::draw(ctx, m, DrawParam::new())?;
}
// Finished drawing, show it all on the screen!
graphics::present(ctx)?;
Ok(())
}
}
pub fn main() -> GameResult {
let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
let mut path = path::PathBuf::from(manifest_dir);
path.push("resources");
path
} else {
path::PathBuf::from("./resources")
};
let cb = ggez::ContextBuilder::new("drawing", "ggez").add_resource_path(resource_dir);
let (ctx, events_loop) = &mut cb.build()?;
println!("{}", graphics::renderer_info(ctx)?);
let state = &mut MainState::new(ctx).unwrap();
event::run(ctx, events_loop, state)
}