Skip to content

Commit

Permalink
Example cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
icefoxen committed Jul 3, 2019
1 parent 6c30c2b commit dd9c36b
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 67 deletions.
2 changes: 1 addition & 1 deletion examples/01_super_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn main() -> GameResult {
path::PathBuf::from("./resources")
};

let cb = ggez::ContextBuilder::new("bunnymark", "ggez").add_resource_path(resource_dir);
let cb = ggez::ContextBuilder::new("super_simple", "ggez").add_resource_path(resource_dir);
let (ctx, event_loop) = &mut cb.build()?;
let state = &mut MainState::new(ctx)?;
event::run(ctx, event_loop, state)
Expand Down
5 changes: 2 additions & 3 deletions examples/02_hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Basic hello world example.
extern crate cgmath;
extern crate ggez;
use cgmath;
use ggez;

use ggez::event;
use ggez::graphics;
Expand Down Expand Up @@ -43,7 +43,6 @@ impl event::EventHandler for MainState {
// Drawables are drawn from their top-left corner.
let offset = self.frames as f32 / 10.0;
let dest_point = cgmath::Point2::new(offset, offset);
// let dest_point = cgmath::Point2::new(0.5, -0.5);
graphics::draw(ctx, &self.text, (dest_point,))?;
graphics::present(ctx)?;

Expand Down
4 changes: 2 additions & 2 deletions examples/03_drawing.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! A collection of semi-random shape and image drawing examples.
extern crate cgmath;
extern crate ggez;
use cgmath;

use ggez;
use ggez::event;
use ggez::graphics;
use ggez::graphics::{Color, DrawMode, DrawParam};
Expand Down
6 changes: 2 additions & 4 deletions examples/04_snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// First we'll import the crates we need for our game;
// in this case that is just `ggez` and `rand`.
extern crate ggez;
extern crate rand;
use ggez;
use rand;

// Next we need to actually `use` the pieces of ggez that we are going
// to need frequently.
Expand Down Expand Up @@ -222,8 +222,6 @@ impl Food {
// Then we draw a rectangle with the Fill draw mode, and we convert the
// Food's position into a `ggez::Rect` using `.into()` which we can do
// since we implemented `From<GridPosition>` for `Rect` earlier.
// graphics::rectangle(ctx, color, graphics::DrawMode::fill(), self.pos.into())

let rectangle =
graphics::Mesh::new_rectangle(ctx, graphics::DrawMode::fill(), self.pos.into(), color)?;
graphics::draw(ctx, &rectangle, (ggez::mint::Point2 { x: 0.0, y: 0.0 },))
Expand Down
18 changes: 10 additions & 8 deletions examples/05_astroblasto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ggez::audio::SoundSource;
use ggez::conf;
use ggez::event::{self, EventHandler, KeyCode, KeyMods};
use ggez::graphics;
use ggez::nalgebra as na;
use nalgebra as na;
use ggez::timer;
use ggez::{Context, ContextBuilder, GameResult};
use rand;
Expand All @@ -21,8 +21,8 @@ type Vector2 = na::Vector2<f32>;

/// *********************************************************************
/// Basic stuff, make some helpers for vector functions.
/// ggez includes the nalgebra math library to provide lots of
/// math stuff. We just add some helpers.
/// We use the nalgebra math library to provide lots of
/// math stuff. This just adds some helpers.
/// **********************************************************************
/// Create a unit vector representing the
Expand All @@ -33,7 +33,7 @@ fn vec_from_angle(angle: f32) -> Vector2 {
Vector2::new(vx, vy)
}

/// Just makes a random `Vector2` with the given max magnitude.
/// Makes a random `Vector2` with the given max magnitude.
fn random_vec(max_magnitude: f32) -> Vector2 {
let angle = rand::random::<f32>() * 2.0 * std::f32::consts::PI;
let mag = rand::random::<f32>() * max_magnitude;
Expand Down Expand Up @@ -150,14 +150,16 @@ fn create_rocks(num: i32, exclusion: Point2, min_radius: f32, max_radius: f32) -
/// the coordinate system so that +y is up and -y is down.
/// **********************************************************************
/// How fast shots move.
const SHOT_SPEED: f32 = 200.0;
/// Angular velocity of how fast shots rotate.
const SHOT_ANG_VEL: f32 = 0.1;

// Acceleration in pixels per second.
/// Acceleration in pixels per second.
const PLAYER_THRUST: f32 = 100.0;
// Rotation in radians per second.
/// Rotation in radians per second.
const PLAYER_TURN_RATE: f32 = 3.0;
// Seconds between shots
/// Refire delay between shots, in seconds.
const PLAYER_SHOT_TIME: f32 = 0.5;

fn player_handle_input(actor: &mut Actor, input: &InputState, dt: f32) {
Expand Down Expand Up @@ -292,7 +294,7 @@ impl Default for InputState {
/// game's "global" state, it keeps track of everything we need for
/// actually running the game.
///
/// Our game objects are simply a vector for each actor type, and we
/// We simply keep game objects in a vector for each actor type, and we
/// probably mingle gameplay-state (like score) and hardware-state
/// (like `input`) a little more than we should, but for something
/// this small it hardly matters.
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas_subframe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! An example of how to use a `SpriteBatch`.
//! An example of how to use a `SpriteBatch` with a `Canvas`.
//!
//! You really want to run this one in release mode.
Expand Down
4 changes: 4 additions & 0 deletions examples/cube.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//! How to draw a 3D cube in ggez.
//!
//! ggez doesn't provide any 3D drawing itself, but it exposes
//! the underlying `gfx-rs` data types, so you can bypass ggez's
//! drawing code entirely and write your own.
#[macro_use]
extern crate gfx;
Expand Down
4 changes: 2 additions & 2 deletions examples/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
//!
//! It is functionally identical to the `super_simple.rs` example apart from that.
extern crate cgmath;
extern crate ggez;
use cgmath;
use ggez;

use ggez::event::winit_event::{Event, KeyboardInput, WindowEvent};
use ggez::graphics::{self, DrawMode};
Expand Down
4 changes: 2 additions & 2 deletions examples/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! It doesn't use an event loop, it just runs once and exits,
//! printing a bunch of stuff to the console.
extern crate ggez;
use ggez;

use ggez::{conf, filesystem, ContextBuilder, GameResult};
use std::env;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn main() -> GameResult {
}
println!("Wrote to test file");
{
let mut options = filesystem::OpenOptions::new();
let options = filesystem::OpenOptions::new();
options.append(true);
let mut file = filesystem::open_options(ctx, test_file, options)?;
file.write_all(bytes)?;
Expand Down
6 changes: 3 additions & 3 deletions examples/graphics_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//!
//! Prints instructions to the console.
extern crate ggez;
extern crate nalgebra;
extern crate structopt;
use ggez;
use nalgebra;
use structopt;

use ggez::conf;
use ggez::event::{self, KeyCode, KeyMods};
Expand Down
7 changes: 4 additions & 3 deletions examples/hello_canvas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Basic hello world example.
//! Basic hello world example, drawing
//! to a canvas.
extern crate ggez;
extern crate nalgebra;
use ggez;
use nalgebra;

use ggez::event;
use ggez::graphics::{self, Color};
Expand Down
10 changes: 3 additions & 7 deletions examples/imageview.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate cgmath;
extern crate ggez;
extern crate rand;
use cgmath;
use ggez;
use rand;

use ggez::audio;
use ggez::audio::SoundSource;
Expand Down Expand Up @@ -118,10 +118,6 @@ impl event::EventHandler for MainState {
}
}

// Creating a gamestate depends on having an SDL context to load resources.
// Creating a context depends on loading a config file.
// Loading a config file depends on having FS (or we can just fake our way around it
// by creating an FS and then throwing it away; the costs are not huge.)
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);
Expand Down
3 changes: 1 addition & 2 deletions examples/input_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Example that just prints out all the input events.
extern crate cgmath;
extern crate ggez;
use ggez;

use ggez::event::{self, Axis, Button, GamepadId, KeyCode, KeyMods, MouseButton};
use ggez::graphics::{self, DrawMode};
Expand Down
4 changes: 2 additions & 2 deletions examples/logging.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This example shows two ways to set up logging in apps, using `log` crate macros with `fern`
//! This example shows two ways to set up logging in games, using the `log` crate macros with `fern`
//! frontend, to display neatly formatted console output and write same output to a file.
//!
//! Output in question is a trace of app's initialization, and keyboard presses when it's running.
Expand Down Expand Up @@ -52,7 +52,7 @@ impl FileLogger {
/// Intended to be called in `EventHandler::update()`, to avoid using threads.
/// (which you totally shouldn't actively avoid, Rust is perfect for concurrency)
fn update(&mut self) -> GameResult {
// try_recv() doesn't block, it returns Err if there's no message to pop.
// try_recv() doesn't block, it returns Err if there's no message to receive.
while let Ok(msg) = self.receiver.try_recv() {
// std::io::Write::write_all() takes a byte array.
self.file.write_all(msg.as_bytes())?;
Expand Down
6 changes: 3 additions & 3 deletions examples/render_to_image.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! An example of how to render to images using the `Canvas` type.
//! An example of how to draw to `Image`'s using the `Canvas` type.
extern crate cgmath;
extern crate ggez;
use cgmath;
use ggez;

use ggez::event;
use ggez::graphics::{self, Color, DrawParam};
Expand Down
9 changes: 5 additions & 4 deletions examples/shader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! A very simple shader example.
#[macro_use]
extern crate gfx;
extern crate cgmath;
extern crate ggez;

use gfx::{self, *};
use cgmath;
use ggez;

use ggez::event;
use ggez::graphics::{self, DrawMode};
Expand All @@ -12,6 +12,7 @@ use ggez::{Context, GameResult};
use std::env;
use std::path;

// Define the input struct for our shader.
gfx_defines! {
constant Dim {
rate: f32 = "u_Rate",
Expand Down
7 changes: 3 additions & 4 deletions examples/shadows.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! A more sophisticated example of how to use shaders
//! and canvas's to do 2D GPU shadows.
#[macro_use]
extern crate gfx;
extern crate cgmath;
extern crate ggez;
use gfx::{self, *};
use cgmath;
use ggez;

use cgmath::{Point2, Vector2};
use ggez::conf;
Expand Down
2 changes: 1 addition & 1 deletion examples/sounds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern crate ggez;
use ggez;

use ggez::audio;
use ggez::audio::SoundSource;
Expand Down
3 changes: 1 addition & 2 deletions examples/spritebatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//!
//! You really want to run this one in release mode.
extern crate ggez;
extern crate rand;
use ggez;

use ggez::event;
use ggez::graphics;
Expand Down
7 changes: 3 additions & 4 deletions examples/text.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! This example demonstrates how to use `Text` to draw TrueType font texts efficiently.
//! Powered by `gfx_glyph` crate.
extern crate cgmath;
extern crate ggez;
extern crate rand;
use cgmath;
use ggez;
use rand;

use cgmath::Point2;
use ggez::conf::{WindowMode, WindowSetup};
Expand Down
12 changes: 3 additions & 9 deletions examples/transforms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Demonstrates various projection and matrix fiddling/testing.
//!
extern crate ggez;
extern crate nalgebra;
use ggez;
use nalgebra;

use ggez::event::{self, KeyCode, KeyMods};
use ggez::graphics::{self, DrawMode};
Expand Down Expand Up @@ -94,12 +93,8 @@ impl event::EventHandler for MainState {
.scale(na::Vector2::new(1.0, 1.0))
// .src(graphics::Rect::new(0.0, 0.0, 0.5, 0.5))
;
// TODO: I made DrawTransform private but maybe it's still useful for
// this sort of stuff?
// let dt = graphics::DrawTransform::from(param);
// println!("transform: {}", dt.matrix);

// self.draw_coord_labels(ctx)?;
self.draw_coord_labels(ctx)?;

graphics::draw(ctx, &self.angle, param)?;
graphics::present(ctx)?;
Expand Down Expand Up @@ -134,7 +129,6 @@ pub fn main() -> GameResult {
};
let cb = ggez::ContextBuilder::new("transforms", "ggez")
.add_resource_path(resource_dir)
// .window_setup(ggez::conf::WindowSetup::default().srgb(false))
;
let (ctx, event_loop) = &mut cb.build()?;
let state = &mut MainState::new(ctx)?;
Expand Down

0 comments on commit dd9c36b

Please sign in to comment.