forked from ggez/ggez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics_settings.rs
268 lines (242 loc) · 8.68 KB
/
graphics_settings.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! An example of how to play with various graphics modes settings,
//! resize windows, etc.
//!
//! Prints instructions to the console.
extern crate ggez;
extern crate nalgebra;
extern crate structopt;
use ggez::conf;
use ggez::event::{self, KeyCode, KeyMods};
use ggez::graphics::{self, DrawMode, Drawable};
use ggez::timer;
use ggez::{Context, GameResult};
use structopt::StructOpt;
use std::env;
use std::path;
use nalgebra as na;
type Point2 = na::Point2<f32>;
struct WindowSettings {
window_size_toggle: bool,
toggle_fullscreen: bool,
is_fullscreen: bool,
resize_projection: bool,
}
struct MainState {
angle: f32, // in radians
zoom: f32,
image: graphics::Image,
window_settings: WindowSettings,
}
impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let s = MainState {
angle: 0.0,
zoom: 1.0,
image: graphics::Image::new(ctx, "/tile.png")?,
window_settings: WindowSettings {
toggle_fullscreen: false,
window_size_toggle: false,
is_fullscreen: false,
resize_projection: false,
},
};
Ok(s)
}
}
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.angle += 0.01;
if self.window_settings.toggle_fullscreen {
let fullscreen_type = if self.window_settings.is_fullscreen {
conf::FullscreenType::Desktop
} else {
conf::FullscreenType::Windowed
};
ggez::graphics::set_fullscreen(ctx, fullscreen_type)?;
self.window_settings.toggle_fullscreen = false;
}
/*
if self.window_settings.window_size_toggle {
let resolutions = ggez::graphics::fullscreen_modes(ctx, 0)?;
let (width, height) = resolutions[self.window_settings.resolution_index];
ggez::graphics::set_resolution(ctx, width, height)?;
self.window_settings.window_size_toggle = false;
}
*/
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, graphics::BLACK);
let rotation = timer::ticks(ctx) % 1000;
let circle = graphics::Mesh::new_circle(
ctx,
DrawMode::Line(3.0),
Point2::new(0.0, 0.0),
100.0,
4.0,
graphics::WHITE,
)?;
graphics::draw(
ctx,
&self.image,
(Point2::new(400.0, 300.0), 0.0, graphics::WHITE),
)?;
graphics::draw(
ctx,
&circle,
(Point2::new(400.0, 300.0), rotation as f32, graphics::WHITE),
)?;
// Let's draw a grid so we can see where the window bounds are.
const COUNT: i32 = 10;
let mut mb = graphics::MeshBuilder::new();
for x in -COUNT..COUNT {
for y in -COUNT..COUNT {
const SPACING: i32 = 100;
let fx = (x * SPACING) as f32;
let fy = (y * SPACING) as f32;
// println!("POS: {},{}", fx, fy);
let r = (x as f32) / (COUNT as f32);
let b = (y as f32) / (COUNT as f32);
// println!("R: {}", r);
let color = graphics::Color::new(r, 0.0, b, 1.0);
// graphics::rectangle(
// ctx,
// color,
// graphics::DrawMode::Fill,
// graphics::Rect::new(fx, fy, 5.0, 5.0),
// )?
mb.rectangle(DrawMode::Fill, graphics::Rect::new(fx, fy, 5.0, 5.0), color);
}
}
let mesh = mb.build(ctx)?;
graphics::draw(ctx, &mesh, (ggez::mint::Point2 { x: 0.0, y: 0.0 },))?;
graphics::present(ctx)?;
Ok(())
}
fn mouse_button_down_event(
&mut self,
_ctx: &mut Context,
_btn: event::MouseButton,
x: f32,
y: f32,
) {
println!("Button clicked at: {} {}", x, y);
}
fn key_up_event(&mut self, ctx: &mut Context, keycode: KeyCode, _keymod: KeyMods) {
match keycode {
KeyCode::F => {
self.window_settings.toggle_fullscreen = true;
self.window_settings.is_fullscreen = !self.window_settings.is_fullscreen;
}
/*
KeyCode::H => {
self.window_settings.window_size_toggle = true;
self.window_settings.resolution_index += 1;
self.window_settings.resolution_index %= self.window_settings.num_of_resolutions;
}
KeyCode::G => {
if self.window_settings.resolution_index > 0 {
self.window_settings.window_size_toggle = true;
self.window_settings.resolution_index -= 1;
self.window_settings.resolution_index %=
self.window_settings.num_of_resolutions;
}
}
*/
KeyCode::Up => {
self.zoom += 0.1;
println!("Zoom is now {}", self.zoom);
let (w, h) = graphics::size(ctx);
let new_rect =
graphics::Rect::new(0.0, 0.0, w as f32 * self.zoom, h as f32 * self.zoom);
graphics::set_screen_coordinates(ctx, new_rect).unwrap();
}
KeyCode::Down => {
self.zoom -= 0.1;
println!("Zoom is now {}", self.zoom);
let (w, h) = graphics::size(ctx);
let new_rect =
graphics::Rect::new(0.0, 0.0, w as f32 * self.zoom, h as f32 * self.zoom);
graphics::set_screen_coordinates(ctx, new_rect).unwrap();
}
KeyCode::Space => {
self.window_settings.resize_projection = !self.window_settings.resize_projection;
println!(
"Resizing the projection on window resize is now: {}",
self.window_settings.resize_projection
);
}
_ => {}
}
}
fn resize_event(&mut self, ctx: &mut Context, width: f32, height: f32) {
println!("Resized screen to {}, {}", width, height);
if self.window_settings.resize_projection {
let new_rect = graphics::Rect::new(
0.0,
0.0,
width as f32 * self.zoom,
height as f32 * self.zoom,
);
graphics::set_screen_coordinates(ctx, new_rect).unwrap();
}
}
}
fn print_help() {
println!("GRAPHICS SETTING EXAMPLE:");
println!(" F: toggle fullscreen");
println!(" G/H: Increase/decrease window sizes");
println!(" Up/Down: Zoom in/out");
println!(
" Spacebar: Toggle whether or not to resize the projection when the window is resized"
);
println!(" ");
println!(" To see command-line options, run with `cargo run --example graphics_settings -- --help`");
println!(" ");
}
#[derive(StructOpt, Debug)]
#[structopt(name = "graphics_settings")]
struct Opt {
/// What level of MSAA to try to use (1, 2, 4, 8)
#[structopt(short = "m", long = "msaa", default_value = "1")]
msaa: u32,
/// Use OpenGL ES instead of regular OpenGL.
#[structopt(long = "es")]
es: bool,
}
pub fn main() -> GameResult {
let opt = Opt::from_args();
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 mut backend = conf::Backend::default();
if opt.es {
backend = backend.gles().version(3, 0);
}
let cb = ggez::ContextBuilder::new("graphics_settings", "ggez")
.window_mode(
conf::WindowMode::default()
.fullscreen_type(conf::FullscreenType::Windowed)
.resizable(true),
)
.window_setup(
conf::WindowSetup::default().samples(
conf::NumSamples::from_u32(opt.msaa)
.expect("Option msaa needs to be 1, 2, 4, 8 or 16!"),
),
)
.backend(backend)
.add_resource_path(resource_dir);
let (ctx, events_loop) = &mut cb.build()?;
println!("Renderer: {}", graphics::renderer_info(ctx).unwrap());
print_help();
let state = &mut MainState::new(ctx)?;
event::run(ctx, events_loop, state)
}