Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a series of numbered tutorials that are written in Rustdoc comments, so they are checked in CI and cargo test automatically. These tutorials should allow users to be walked through Quicksilver step-by-step rather than just reading the examples and the API docs.
- Loading branch information
1 parent
63f51c1
commit f3cbb8e
Showing
18 changed files
with
927 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Demonstrate adding a View to the draw-geometry example | ||
// The camera can be controlled with the arrow keys | ||
extern crate quicksilver; | ||
|
||
use quicksilver::{ | ||
Result, | ||
geom::{Circle, Line, Rectangle, Shape, Transform, Triangle, Vector}, | ||
graphics::{Background::Col, Color, View}, | ||
input::{Key}, | ||
lifecycle::{Settings, State, Window, run}, | ||
}; | ||
|
||
struct Camera { | ||
view: Rectangle | ||
} | ||
|
||
impl State for Camera { | ||
// Initialize the struct | ||
fn new() -> Result<Camera> { | ||
Ok(Camera { | ||
view: Rectangle::new_sized((800, 600)) | ||
}) | ||
} | ||
|
||
fn update(&mut self, window: &mut Window) -> Result<()> { | ||
if window.keyboard()[Key::Left].is_down() { | ||
self.view = self.view.translate((-4, 0)); | ||
} | ||
if window.keyboard()[Key::Right].is_down() { | ||
self.view = self.view.translate((4, 0)); | ||
} | ||
if window.keyboard()[Key::Down].is_down() { | ||
self.view = self.view.translate((0, 4)); | ||
} | ||
if window.keyboard()[Key::Up].is_down() { | ||
self.view = self.view.translate((0, -4)); | ||
} | ||
window.set_view(View::new(self.view)); | ||
Ok(()) | ||
} | ||
|
||
fn draw(&mut self, window: &mut Window) -> Result<()> { | ||
window.clear(Color::WHITE)?; | ||
window.draw(&Rectangle::new((100, 100), (32, 32)), Col(Color::BLUE)); | ||
window.draw_ex(&Rectangle::new((400, 300), (32, 32)), Col(Color::BLUE), Transform::rotate(45), 10); | ||
window.draw(&Circle::new((400, 300), 100), Col(Color::GREEN)); | ||
window.draw_ex( | ||
&Line::new((50, 80),(600, 450)).with_thickness(2.0), | ||
Col(Color::RED), | ||
Transform::IDENTITY, | ||
5 | ||
); | ||
window.draw_ex( | ||
&Triangle::new((500, 50), (450, 100), (650, 150)), | ||
Col(Color::RED), | ||
Transform::rotate(45) * Transform::scale((0.5, 0.5)), | ||
0 | ||
); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
run::<Camera>("Camera", Vector::new(800, 600), Settings::default()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//! Our first tutorial is simple: Just create a blank window. | ||
//! | ||
//! Here's the full source: | ||
//! | ||
//! ```no_run | ||
//! extern crate quicksilver; | ||
//! | ||
//! use quicksilver::{ | ||
//! Result, | ||
//! geom::Vector, | ||
//! lifecycle::{State, run} | ||
//! }; | ||
//! | ||
//! struct Screen; | ||
//! | ||
//! impl State for Screen { | ||
//! fn new() -> Result<Screen> { | ||
//! Ok(Screen) | ||
//! } | ||
//! } | ||
//! | ||
//! fn main() { | ||
//! run::<Screen>("Hello World", Vector::new(800, 600), Default::default()); | ||
//! } | ||
//! ``` | ||
//! Let's start with importing what we need from Quicksilver: | ||
//! ```no_run | ||
//! extern crate quicksilver; | ||
//! | ||
//! use quicksilver::{ | ||
//! Result, | ||
//! geom::Vector, | ||
//! lifecycle::{State, run} | ||
//! }; | ||
//! ``` | ||
//! | ||
//! Quicksilver has its own `Result` type, which is just the same as `std::result::Result<T, quicksilver::Error>`. | ||
//! We use `Vector` for anything 2-dimensional: position, speed, or size, for example. | ||
//! `State` is the trait that defines how we handle the core loop of Quicksilver | ||
//! `run` is the function that kicks off the core loop. | ||
//! | ||
//! Next we declare our `State` handler: | ||
//! ```no_run | ||
//! struct Screen; | ||
//! ``` | ||
//! It's a unit struct (a struct with no fields) because we don't need to store anything. | ||
//! | ||
//! Now we implement `State` for our handler: | ||
//! | ||
//! ```no_run | ||
//! # use quicksilver::{ | ||
//! # Result, | ||
//! # geom::Vector, | ||
//! # lifecycle::{State, run} | ||
//! # }; | ||
//! # struct Screen; | ||
//! impl State for Screen { | ||
//! fn new() -> Result<Screen> { | ||
//! Ok(Screen) | ||
//! } | ||
//! } | ||
//! ``` | ||
//! All we have to do is implement the `new` function, and Quicksilver will take care of all the other functions. | ||
//! The other functions we could override are `draw`, `update`, and `event`, which will be covered in later tutorials. | ||
//! ```no_run | ||
//! # use quicksilver::{ | ||
//! # Result, | ||
//! # geom::Vector, | ||
//! # lifecycle::{State, run} | ||
//! # }; | ||
//! # struct Screen; | ||
//! # impl State for Screen { | ||
//! # fn new() -> Result<Screen> { | ||
//! # Ok(Screen) | ||
//! # } | ||
//! # } | ||
//! fn main() { | ||
//! run::<Screen>("Hello World", Vector::new(800, 600), Default::default()); | ||
//! } | ||
//! ``` | ||
//! Lastly, we create a main that calls `run`, starting the event loop and showing our window |
Oops, something went wrong.