forked from ggez/ggez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.rs
142 lines (129 loc) · 4.42 KB
/
transforms.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
//! Demonstrates various projection and matrix fiddling/testing.
use ggez;
use nalgebra;
use ggez::event::{self, KeyCode, KeyMods};
use ggez::graphics::{self, DrawMode};
use ggez::{Context, GameResult};
use ggez::nalgebra as na;
use std::env;
use std::path;
struct MainState {
pos_x: f32,
gridmesh: graphics::Mesh,
angle: graphics::Image,
screen_bounds: Vec<graphics::Rect>,
screen_bounds_idx: usize,
}
impl MainState {
const GRID_INTERVAL: f32 = 100.0;
const GRID_SIZE: usize = 10;
const GRID_POINT_RADIUS: f32 = 5.0;
fn new(ctx: &mut Context) -> GameResult<MainState> {
let angle = graphics::Image::new(ctx, "/angle.png")?;
let gridmesh_builder = &mut graphics::MeshBuilder::new();
for x in 0..Self::GRID_SIZE {
for y in 0..Self::GRID_SIZE {
let fx = x as f32;
let fy = y as f32;
let fsize = Self::GRID_SIZE as f32;
let point = na::Point2::new(fx * Self::GRID_INTERVAL, fy * Self::GRID_INTERVAL);
let color = graphics::Color::new(fx / fsize, 0.0, fy / fsize, 1.0);
gridmesh_builder.circle(
DrawMode::fill(),
point,
Self::GRID_POINT_RADIUS,
2.0,
color,
);
}
}
let gridmesh = gridmesh_builder.build(ctx)?;
// An array of rects to cycle the screen coordinates through.
let screen_bounds = vec![
graphics::Rect::new(0.0, 0.0, 800.0, 600.0),
graphics::Rect::new(0.0, 600.0, 800.0, -600.0),
];
let screen_bounds_idx = 0;
let s = MainState {
pos_x: 0.0,
gridmesh,
angle,
screen_bounds,
screen_bounds_idx,
};
Ok(s)
}
fn draw_coord_labels(&self, ctx: &mut Context) -> GameResult {
for x in 0..Self::GRID_SIZE {
for y in 0..Self::GRID_SIZE {
let point = na::Point2::new(
x as f32 * Self::GRID_INTERVAL,
y as f32 * Self::GRID_INTERVAL,
);
let s = format!("({}, {})", point.x, point.y);
let t = graphics::Text::new(s);
graphics::queue_text(ctx, &t, point, None);
}
}
graphics::draw_queued_text(
ctx,
graphics::DrawParam::default(),
None,
graphics::FilterMode::Linear,
)
}
}
impl event::EventHandler for MainState {
fn update(&mut self, _ctx: &mut Context) -> GameResult {
self.pos_x = self.pos_x % 800.0 + 1.0;
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
let origin: na::Point2<f32> = na::Point2::origin();
graphics::draw(ctx, &self.gridmesh, (origin, graphics::WHITE))?;
let param = graphics::DrawParam::new()
.dest(na::Point2::new(400.0, 400.0))
.rotation(self.pos_x / 100.0)
.offset(na::Point2::new(0.5, 0.5))
.scale(na::Vector2::new(1.0, 1.0));
self.draw_coord_labels(ctx)?;
graphics::draw(ctx, &self.angle, param)?;
graphics::present(ctx)?;
Ok(())
}
fn key_down_event(
&mut self,
ctx: &mut Context,
keycode: KeyCode,
_keymod: KeyMods,
_repeat: bool,
) {
match keycode {
event::KeyCode::Space => {
self.screen_bounds_idx = (self.screen_bounds_idx + 1) % self.screen_bounds.len();
graphics::set_screen_coordinates(ctx, self.screen_bounds[self.screen_bounds_idx])
.unwrap();
}
_ => (),
}
}
}
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("transforms", "ggez")
.window_setup(
ggez::conf::WindowSetup::default()
.title("transforms -- Press spacebar to cycle projection!"),
)
.add_resource_path(resource_dir);
let (ctx, event_loop) = &mut cb.build()?;
let state = &mut MainState::new(ctx)?;
event::run(ctx, event_loop, state)
}