Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Font::draw support a Target instead of a Frame #25

Merged
merged 3 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ gfx_core = { version = "0.9", optional = true }
glutin = { version = "0.20", optional = true }
gfx_device_gl = { version = "0.16", optional = true }
gfx_window_glutin = { version = "0.30", optional = true }
gfx_glyph = { version = "0.14", optional = true }
gfx_glyph = { version = "0.15", optional = true }
gfx_winit = { package = "winit", version = "0.19", optional = true }

# wgpu (Vulkan, Metal, D3D)
wgpu = { version = "0.2", optional = true }
wgpu_glyph = { version = "0.2", optional = true }
wgpu_glyph = { version = "0.3", optional = true }

[dev-dependencies]
# Example dependencies
Expand Down
8 changes: 5 additions & 3 deletions examples/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ impl Game for Colors {
let mut frame = window.frame();
frame.clear(Color::new(0.5, 0.5, 0.5, 1.0));

let target = &mut frame.as_target();

view.palette.draw(
Quad {
source: Rectangle {
Expand All @@ -46,18 +48,18 @@ impl Game for Colors {
position: Point::new(0.0, 0.0),
size: (500.0, 500.0),
},
&mut frame.as_target(),
target,
);

view.font.add(Text {
content: String::from("Prussian blue"),
position: Point::new(20.0, 500.0),
size: 50.0,
bounds: (frame.width(), frame.height()),
color: View::PRUSSIAN_BLUE,
..Text::default()
});

view.font.draw(&mut frame);
view.font.draw(target);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Game for InputExample {
110.0,
);

view.font.draw(&mut frame);
view.font.draw(&mut frame.as_target());

// Draw a small square at the mouse cursor's position.
view.palette.draw(
Expand Down
2 changes: 1 addition & 1 deletion examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl Game for Particles {
color: Color::WHITE,
});

view.font.draw(&mut frame);
view.font.draw(&mut frame.as_target());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/debug/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Debug {
self.font.add(text.clone());
}

self.font.draw(frame);
self.font.draw(&mut frame.as_target());
self.frames_until_refresh -= 1;
}

Expand Down
13 changes: 5 additions & 8 deletions src/graphics/backend_gfx/font.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gfx_device_gl as gl;

use crate::graphics::gpu::{DepthView, TargetView};
use crate::graphics::gpu::{TargetView, Transformation};
use crate::graphics::Text;

pub struct Font {
Expand Down Expand Up @@ -35,20 +35,17 @@ impl Font {
&mut self,
encoder: &mut gfx::Encoder<gl::Resources, gl::CommandBuffer>,
target: &TargetView,
depth: &DepthView,
transformation: Transformation,
) {
let typed_target: gfx::handle::RenderTargetView<
gl::Resources,
gfx::format::Srgba8,
> = gfx::memory::Typed::new(target.clone());

let typed_depth: gfx::handle::DepthStencilView<
gl::Resources,
gfx::format::Depth,
> = gfx::memory::Typed::new(depth.clone());

self.glyphs
.draw_queued(encoder, &typed_target, &typed_depth)
.use_queue()
.transform(transformation)
.draw(encoder, &typed_target)
.expect("Font draw");
}
}
8 changes: 3 additions & 5 deletions src/graphics/backend_gfx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use font::Font;
pub use pipeline::Instance;
pub use surface::{winit, Surface};
pub use texture::Texture;
pub use types::{DepthView, TargetView};
pub use types::TargetView;

use gfx::{self, Device};
use gfx_device_gl as gl;
Expand Down Expand Up @@ -128,10 +128,8 @@ impl Gpu {
&mut self,
font: &mut Font,
target: &TargetView,
depth: &DepthView,
_target_width: u32,
_target_height: u32,
transformation: Transformation,
) {
font.draw(&mut self.encoder, target, depth);
font.draw(&mut self.encoder, target, transformation);
}
}
22 changes: 4 additions & 18 deletions src/graphics/backend_gfx/surface.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use gfx_device_gl as gl;
pub use gfx_winit as winit;

use super::{format, DepthView, Gpu, TargetView};
use super::{format, Gpu, TargetView};
use crate::{Error, Result};

pub struct Surface {
context: glutin::WindowedContext,
target: TargetView,
depth: DepthView,
}

impl Surface {
Expand All @@ -23,7 +22,7 @@ impl Surface {
.with_pixel_format(24, 8)
.with_vsync(true);

let (context, device, factory, target, depth) =
let (context, device, factory, target, _depth) =
gfx_window_glutin::init_raw(
builder,
gl_builder,
Expand All @@ -33,15 +32,7 @@ impl Surface {
)
.map_err(|error| Error::WindowCreation(error.to_string()))?;

Ok((
Self {
context,
target,
depth,
},
device,
factory,
))
Ok((Self { context, target }, device, factory))
}

pub fn window(&self) -> &winit::Window {
Expand All @@ -52,21 +43,16 @@ impl Surface {
&self.target
}

pub fn depth(&self) -> &DepthView {
&self.depth
}

pub fn update_viewport(&mut self, _gpu: &mut Gpu) {
let dimensions = self.target.get_dimensions();

if let Some((target, depth)) = gfx_window_glutin::update_views_raw(
if let Some((target, _depth)) = gfx_window_glutin::update_views_raw(
&self.context,
dimensions,
format::COLOR,
format::DEPTH,
) {
self.target = target;
self.depth = depth;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/graphics/backend_gfx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use super::format;

pub type TargetView = gfx::handle::RawRenderTargetView<gl::Resources>;

pub type DepthView = gfx::handle::RawDepthStencilView<gl::Resources>;

pub type RawTexture = gfx::handle::RawTexture<gl::Resources>;

pub type ShaderResource =
Expand Down
12 changes: 8 additions & 4 deletions src/graphics/backend_wgpu/font.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::graphics::gpu::TargetView;
use crate::graphics::Text;
use crate::graphics::{Text, Transformation};

pub struct Font {
glyphs: wgpu_glyph::GlyphBrush<'static>,
Expand Down Expand Up @@ -33,11 +33,15 @@ impl Font {
device: &mut wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
target: &TargetView,
target_width: u32,
target_height: u32,
transformation: Transformation,
) {
self.glyphs
.draw_queued(device, encoder, target, target_width, target_height)
.draw_queued_with_transform(
transformation.into(),
device,
encoder,
target,
)
.expect("Draw font");
}
}
14 changes: 3 additions & 11 deletions src/graphics/backend_wgpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use font::Font;
pub use pipeline::Instance;
pub use surface::{winit, Surface};
pub use texture::Texture;
pub use types::{DepthView, TargetView};
pub use types::TargetView;

use crate::graphics::{Color, Transformation};
use crate::{Error, Result};
Expand Down Expand Up @@ -122,16 +122,8 @@ impl Gpu {
&mut self,
font: &mut Font,
target: &TargetView,
_depth: &DepthView,
target_width: u32,
target_height: u32,
transformation: Transformation,
) {
font.draw(
&mut self.device,
&mut self.encoder,
target,
target_width,
target_height,
);
font.draw(&mut self.device, &mut self.encoder, target, transformation);
}
}
6 changes: 1 addition & 5 deletions src/graphics/backend_wgpu/surface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use super::{DepthView, Gpu, TargetView};
use super::{Gpu, TargetView};
pub use wgpu::winit;

pub struct Surface {
Expand Down Expand Up @@ -41,10 +41,6 @@ impl Surface {
&self.target
}

pub fn depth(&self) -> &DepthView {
&()
}

pub fn update_viewport(&mut self, gpu: &mut Gpu) {
let (swap_chain, extent, buffer, target) =
new_swap_chain(&gpu.device, &self.surface, &self.window);
Expand Down
2 changes: 0 additions & 2 deletions src/graphics/backend_wgpu/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

pub type TargetView = Rc<wgpu::TextureView>;

pub type DepthView = ();
12 changes: 5 additions & 7 deletions src/graphics/font.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::graphics::gpu;
use crate::graphics::{Frame, Gpu, Text};
use crate::graphics::{Gpu, Target, Text};
use crate::load::Task;
use crate::Result;

Expand All @@ -26,13 +26,11 @@ impl Font {
self.0.add(text)
}

/// Render and flush all the text added to this font.
///
/// As of now, [`Font`] can only draw on-screen. This limitation should be
/// easy to tackle in the near future.
/// Render and flush all the text added to this [`Font`].
///
/// [`Font`]: struct.Font.html
pub fn draw(&mut self, frame: &mut Frame) {
frame.draw_font(&mut self.0)
#[inline]
pub fn draw(&mut self, target: &mut Target) {
target.draw_font(&mut self.0)
}
}
6 changes: 5 additions & 1 deletion src/graphics/target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::graphics::gpu::{Gpu, Instance, TargetView, Texture};
use crate::graphics::gpu::{Font, Gpu, Instance, TargetView, Texture};
use crate::graphics::{Color, Transformation};

/// A rendering target.
Expand Down Expand Up @@ -104,6 +104,10 @@ impl<'a> Target<'a> {
&self.transformation,
);
}

pub(in crate::graphics) fn draw_font(&mut self, font: &mut Font) {
self.gpu.draw_font(font, &self.view, self.transformation);
}
}

impl<'a> std::fmt::Debug for Target<'a> {
Expand Down
14 changes: 14 additions & 0 deletions src/graphics/text.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32;

use crate::graphics::{Color, Point};

/// A section of text.
Expand All @@ -18,3 +20,15 @@ pub struct Text {
/// Text color.
pub color: Color,
}

impl Default for Text {
fn default() -> Text {
Text {
content: String::from(""),
position: Point::new(0.0, 0.0),
bounds: (f32::INFINITY, f32::INFINITY),
size: 16.0,
color: Color::BLACK,
}
}
}
12 changes: 1 addition & 11 deletions src/graphics/window/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Window;

use crate::graphics::{gpu, Color, Target};
use crate::graphics::{Color, Target};

/// The next frame of your game.
///
Expand Down Expand Up @@ -52,14 +52,4 @@ impl<'a> Frame<'a> {
pub fn clear(&mut self, color: Color) {
self.as_target().clear(color);
}

pub(in crate::graphics) fn draw_font(&mut self, font: &mut gpu::Font) {
self.window.gpu.draw_font(
font,
&self.window.surface.target(),
&self.window.surface.depth(),
self.width().round() as u32,
self.height().round() as u32,
);
}
}
2 changes: 1 addition & 1 deletion src/load/loading_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,6 @@ impl LoadingScreen for ProgressBar {
color: graphics::Color::WHITE,
});

self.font.draw(&mut frame);
self.font.draw(&mut frame.as_target());
}
}