Rust:piston_test
This commit is contained in:
parent
9fde75355e
commit
7ed665c6d2
1
rust/testing_stuff/getting-started/.gitignore
vendored
Normal file
1
rust/testing_stuff/getting-started/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
target
|
1072
rust/testing_stuff/getting-started/Cargo.lock
generated
Normal file
1072
rust/testing_stuff/getting-started/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
rust/testing_stuff/getting-started/Cargo.toml
Normal file
13
rust/testing_stuff/getting-started/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "getting-started"
|
||||
version = "0.1.0"
|
||||
authors = ["Logen Kain <logen@sudotask.com>"]
|
||||
|
||||
[[bin]]
|
||||
name = "spinning-square"
|
||||
|
||||
[dependencies]
|
||||
piston = "0.34.0"
|
||||
piston2d-graphics = "0.22.0"
|
||||
pistoncore-glutin_window = "0.40.0"
|
||||
piston2d-opengl_graphics = "0.47.0"
|
78
rust/testing_stuff/getting-started/src/main.rs
Normal file
78
rust/testing_stuff/getting-started/src/main.rs
Normal file
@ -0,0 +1,78 @@
|
||||
extern crate piston;
|
||||
extern crate graphics;
|
||||
extern crate glutin_window;
|
||||
extern crate opengl_graphics;
|
||||
|
||||
use piston::window::WindowSettings;
|
||||
use piston::event_loop::*;
|
||||
use piston::input::*;
|
||||
use glutin_window::GlutinWindow as Window;
|
||||
use opengl_graphics::{ GlGraphics, OpenGL };
|
||||
|
||||
pub struct App {
|
||||
gl: GlGraphics, // OpenGL drawing backend.
|
||||
rotation: f64 // Rotation for the square.
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn render(&mut self, args: &RenderArgs) {
|
||||
use graphics::*;
|
||||
|
||||
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
|
||||
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
|
||||
|
||||
let square = rectangle::square(0.0, 0.0, 50.0);
|
||||
let rotation = self.rotation;
|
||||
let (x, y) = ((args.width / 2) as f64,
|
||||
(args.height / 2) as f64);
|
||||
|
||||
self.gl.draw(args.viewport(), |c, gl| {
|
||||
// Clear the screen.
|
||||
clear(GREEN, gl);
|
||||
|
||||
let transform = c.transform.trans(x, y)
|
||||
.rot_rad(rotation)
|
||||
.trans(-25.0, -25.0);
|
||||
|
||||
// Draw a box rotating around the middle of the screen.
|
||||
rectangle(RED, square, transform, gl);
|
||||
});
|
||||
}
|
||||
|
||||
fn update(&mut self, args: &UpdateArgs) {
|
||||
// Rotate 2 radians per second.
|
||||
self.rotation += 2.0 * args.dt;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Change this to OpenGL::V2_1 if not working.
|
||||
let opengl = OpenGL::V3_2;
|
||||
|
||||
// Create an Glutin window.
|
||||
let mut window: Window = WindowSettings::new(
|
||||
"spinning-square",
|
||||
[200, 200]
|
||||
)
|
||||
.opengl(opengl)
|
||||
.exit_on_esc(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
// Create a new game and run it.
|
||||
let mut app = App {
|
||||
gl: GlGraphics::new(opengl),
|
||||
rotation: 0.0
|
||||
};
|
||||
|
||||
let mut events = Events::new(EventSettings::new());
|
||||
while let Some(e) = events.next(&mut window) {
|
||||
if let Some(r) = e.render_args() {
|
||||
app.render(&r);
|
||||
}
|
||||
|
||||
if let Some(u) = e.update_args() {
|
||||
app.update(&u);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user