Skip to content

Commit

Permalink
Merge branch 'master' into improvement/update-wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Aug 9, 2019
2 parents 80ec1b7 + bcdc141 commit 1204675
Show file tree
Hide file tree
Showing 27 changed files with 451 additions and 177 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `Task::succeed`, which replaces the old `Task::new`. [#66]
- `Default` implementation for `ui::widget::slider::State`.
- `Default`, `Clone`, `Copy`, `PartialEq`, and `Eq` implementations for
`ui::widget::button::State`.
- Additional color constants: `Color::RED`, `Color::GREEN`, and `Color::BLUE`. [#77]
- `Color::from_rgb_u32`, which allows to constructs a `Color` using an
hexadecimal literal (`0xRRGGBB`). [#77]
- `From<nalgebra::Matrix3>` and `Into<nalgebra::Matrix3>` implementations for
`Transformation`. [#78]

### Changed
- `Mesh::stroke` now takes an `f32` as `line_width` instead of a `u16`.
- `Task::new` now supports a lazy operation that can fail. [#66]
- Face culling has been disabled for Vulkan, Metal, D3D11, and D3D12 backends.
In OpenGL, face culling was already disabled.
- `Transformation::nonuniform_scale` now takes a `Vector`. [#78]

### Fixed
- Incorrect buffer sizes in the `Mesh` pipeline. This caused vertices to entirely
disappear when rendering big meshes, leading to a potential crash.

[#66]: https://github.com/hecrj/coffee/pull/66
[#77]: https://github.com/hecrj/coffee/pull/77
[#78]: https://github.com/hecrj/coffee/pull/78


## [0.3.1] - 2019-06-20
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Game for MyGame {

fn load(_window: &Window) -> Task<MyGame> {
// Load your game assets here. Check out the `load` module!
Task::new(|| MyGame { /* ... */ })
Task::succeed(|| MyGame { /* ... */ })
}

fn draw(&mut self, frame: &mut Frame, _timer: &Timer) {
Expand Down
2 changes: 1 addition & 1 deletion examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Game for Counter {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Counter> {
Task::new(|| Counter {
Task::succeed(|| Counter {
value: 0,
increment_button: button::State::new(),
decrement_button: button::State::new(),
Expand Down
2 changes: 1 addition & 1 deletion examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Game for GamepadExample {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<GamepadExample> {
Task::new(|| GamepadExample {
Task::succeed(|| GamepadExample {
last_event: "None".to_string(),
})
}
Expand Down
13 changes: 7 additions & 6 deletions examples/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use coffee::graphics::{
Color, Frame, HorizontalAlignment, Mesh, Point, Rectangle, Shape, Window,
WindowSettings,
};
use coffee::input::KeyboardAndMouse;
use coffee::input::mouse::{self, Mouse};
use coffee::load::Task;
use coffee::ui::{
slider, Align, Column, Element, Justify, Radio, Renderer, Row, Slider,
Expand Down Expand Up @@ -51,11 +51,11 @@ enum ModeOption {
}

impl Game for Example {
type Input = KeyboardAndMouse;
type Input = Mouse;
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Example> {
Task::new(move || Example {
Task::succeed(move || Example {
shape: ShapeOption::Rectangle,
mode: ModeOption::Fill,
color: Color::WHITE,
Expand All @@ -71,10 +71,11 @@ impl Game for Example {
})
}

fn interact(&mut self, input: &mut KeyboardAndMouse, _window: &mut Window) {
fn interact(&mut self, mouse: &mut Mouse, _window: &mut Window) {
match self.shape {
ShapeOption::Polyline => {
self.polyline_points.extend(input.left_clicks());
self.polyline_points
.extend(mouse.button_clicks(mouse::Button::Left));
}
_ => {}
}
Expand Down Expand Up @@ -117,7 +118,7 @@ impl Game for Example {
mesh.fill(shape, self.color);
}
ModeOption::Stroke => {
mesh.stroke(shape, self.color, self.stroke_width);
mesh.stroke(shape, self.color, self.stroke_width as f32);
}
}

Expand Down
17 changes: 10 additions & 7 deletions examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use coffee::graphics::{
Batch, Color, Frame, Image, Point, Rectangle, Sprite, Vector, Window,
WindowSettings,
};
use coffee::input::{keyboard, KeyboardAndMouse};
use coffee::input::{keyboard, mouse, KeyboardAndMouse};
use coffee::load::{loading_screen::ProgressBar, Join, Task};
use coffee::ui::{Checkbox, Column, Element, Justify, Renderer, UserInterface};
use coffee::{Game, Result, Timer};
Expand Down Expand Up @@ -41,7 +41,7 @@ impl Particles {
const CENTER_MASS: f32 = 200.0;

fn generate(max_x: f32, max_y: f32) -> Task<Vec<Particle>> {
Task::new(move || {
Task::succeed(move || {
let rng = &mut rand::thread_rng();

(0..Self::AMOUNT)
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Game for Particles {
Task::stage("Loading assets...", Self::load_palette()),
Task::stage(
"Showing off the loading screen for a bit...",
Task::new(|| thread::sleep(time::Duration::from_secs(2))),
Task::succeed(|| thread::sleep(time::Duration::from_secs(2))),
),
)
.join()
Expand All @@ -89,15 +89,18 @@ impl Game for Particles {
}

fn interact(&mut self, input: &mut KeyboardAndMouse, window: &mut Window) {
self.gravity_centers[0] = input.cursor_position();
let mouse = input.mouse();
let keyboard = input.keyboard();

self.gravity_centers.extend(input.left_clicks());
self.gravity_centers[0] = mouse.cursor_position();
self.gravity_centers
.extend(mouse.button_clicks(mouse::Button::Left));

if input.was_key_released(keyboard::KeyCode::I) {
if keyboard.was_key_released(keyboard::KeyCode::I) {
self.interpolate = !self.interpolate;
}

if input.was_key_released(keyboard::KeyCode::F) {
if keyboard.was_key_released(keyboard::KeyCode::F) {
window.toggle_fullscreen();
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Game for Tour {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Tour> {
Task::new(|| Tour {
Task::succeed(|| Tour {
steps: Steps::new(),
back_button: button::State::new(),
next_button: button::State::new(),
Expand Down
11 changes: 10 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ pub trait Game {
true
}

/// Returns whether the game is finished or not.
///
/// If this function returns true, the game will be closed gracefully.
///
/// By default, it always returns false.
fn is_finished(&self) -> bool {
false
}

/// Runs the [`Game`] with the given [`WindowSettings`].
///
/// You probably want to call this in your `main` function to run your game!
Expand Down Expand Up @@ -181,7 +190,7 @@ pub trait Game {
let mut timer = Timer::new(Self::TICKS_PER_SECOND);
let mut alive = true;

while alive {
while alive && !game.is_finished() {
debug.frame_started();
timer.update();

Expand Down
2 changes: 1 addition & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! # type LoadingScreen = ();
//! #
//! # fn load(window: &Window) -> Task<MyGame> {
//! # Task::new(|| MyGame)
//! # Task::succeed(|| MyGame)
//! # }
//! #
//! // ...
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/backend_gfx/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gfx_device_gl as gl;

use super::format::{Channel, Surface};
use super::types::{RawTexture, ShaderResource, TargetView};
use crate::graphics::vector::Vector;
use crate::graphics::Transformation;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -139,7 +140,7 @@ impl Drawable {
}

pub fn render_transformation() -> Transformation {
Transformation::nonuniform_scale(1.0, -1.0)
Transformation::nonuniform_scale(Vector::new(1.0, -1.0))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/backend_wgpu/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Pipeline {
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
cull_mode: wgpu::CullMode::Back,
cull_mode: wgpu::CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
Expand Down
11 changes: 6 additions & 5 deletions src/graphics/backend_wgpu/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Pipeline {
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
cull_mode: wgpu::CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
Expand Down Expand Up @@ -110,12 +110,13 @@ impl Pipeline {
});

let vertices = device.create_buffer(&wgpu::BufferDescriptor {
size: Self::INITIAL_BUFFER_SIZE as u64,
size: mem::size_of::<Vertex>() as u64
* Self::INITIAL_BUFFER_SIZE as u64,
usage: wgpu::BufferUsage::VERTEX,
});

let indices = device.create_buffer(&wgpu::BufferDescriptor {
size: Self::INITIAL_BUFFER_SIZE as u64,
size: Self::INITIAL_BUFFER_SIZE as u64 * 2,
usage: wgpu::BufferUsage::INDEX,
});

Expand Down Expand Up @@ -162,12 +163,12 @@ impl Pipeline {
let new_size = vertices.len().max(indices.len()) as u32;

self.vertices = device.create_buffer(&wgpu::BufferDescriptor {
size: new_size as u64,
size: mem::size_of::<Vertex>() as u64 * new_size as u64,
usage: wgpu::BufferUsage::VERTEX,
});

self.indices = device.create_buffer(&wgpu::BufferDescriptor {
size: new_size as u64,
size: new_size as u64 * 2,
usage: wgpu::BufferUsage::INDEX,
});

Expand Down
46 changes: 46 additions & 0 deletions src/graphics/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,42 @@ impl Color {
a: 1.0,
};

/// Red color.
pub const RED: Self = Self {
r: 1.0,
g: 0.0,
b: 0.0,
a: 1.0,
};

/// Green color.
pub const GREEN: Self = Self {
r: 0.0,
g: 1.0,
b: 0.0,
a: 1.0,
};

/// Blue color.
pub const BLUE: Self = Self {
r: 0.0,
g: 0.0,
b: 1.0,
a: 1.0,
};

/// Creates a new [`Color`] from components in the [0, 1.0] range.
///
/// [`Color`]: struct.Color.html
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
debug_assert!(r >= 0.0, "Red component is < 0.0");
debug_assert!(r <= 1.0, "Red component is > 1.0");
debug_assert!(g >= 0.0, "Green component is < 0.0");
debug_assert!(g <= 1.0, "Green component is > 1.0");
debug_assert!(b >= 0.0, "Blue component is < 0.0");
debug_assert!(b <= 1.0, "Blue component is > 1.0");
debug_assert!(a >= 0.0, "Alpha component is < 0.0");
debug_assert!(a <= 1.0, "Alpha component is > 1.0");
Color { r, g, b, a }
}

Expand All @@ -50,6 +82,20 @@ impl Color {
}
}

/// Creates a new [`Color`] from its RGB representation (0xRRGGBB).
///
/// [`Color`]: struct.Color.html
pub fn from_rgb_u32(color: u32) -> Color {
debug_assert!(
color <= 0xFFFFFF,
"Color contains value higher than 0xFFFFFF"
);
let r = ((color & 0xFF0000) >> 16) as u8;
let g = ((color & 0x00FF00) >> 8) as u8;
let b = ((color & 0x0000FF) >> 0) as u8;
Color::from_rgb(r, g, b)
}

/// Returns the [`Color`] components in the [0, 255] range.
///
/// [`Color`]: struct.Color.html
Expand Down
8 changes: 4 additions & 4 deletions src/graphics/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Mesh {
/// [`Shape`]: enum.Shape.html
/// [`Mesh`]: struct.Mesh.html
#[inline]
pub fn stroke(&mut self, shape: Shape, color: Color, width: u16) {
pub fn stroke(&mut self, shape: Shape, color: Color, width: f32) {
let mut builder = lyon::BuffersBuilder::new(
&mut self.buffers,
WithColor(color.into_linear()),
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Mesh {
&Self::stroke_options(width),
&mut builder,
)
.expect("Fill polyline");
.expect("Stroke polyline");
}
}
}
Expand All @@ -163,8 +163,8 @@ impl Mesh {
lyon::FillOptions::DEFAULT.with_normals(false)
}

fn stroke_options(width: u16) -> lyon::StrokeOptions {
lyon::StrokeOptions::DEFAULT.with_line_width(width as f32)
fn stroke_options(width: f32) -> lyon::StrokeOptions {
lyon::StrokeOptions::DEFAULT.with_line_width(width)
}
}

Expand Down
Loading

0 comments on commit 1204675

Please sign in to comment.