Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanisaacg committed Aug 2, 2020
1 parent de1d2e8 commit ba09c7b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/08_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn app(window: Window, mut gfx: Graphics, mut input: Input) -> Result<()>
Color::BLUE,
);
// Paint a red square at the mouse position
let mouse = gfx.screen_to_world(&window, input.mouse().location());
let mouse = gfx.screen_to_camera(&window, input.mouse().location());
gfx.fill_circle(&Circle::new(mouse, 32.0), Color::RED);
gfx.present(&window)?;
}
Expand Down
47 changes: 9 additions & 38 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ impl Graphics {
);
}

/// Draw to a Surface
pub fn flush_surface(&mut self, surface: &Surface) -> Result<(), QuicksilverError> {
if let (Some(width), Some(height)) = (surface.0.width(), surface.0.height()) {
self.ctx.set_viewport(0, 0, width, height);
Expand All @@ -493,11 +494,7 @@ impl Graphics {
Ok(())
}

fn calculate_viewport(&self, window: &Window) -> Rectangle {
let size = self.resize.content_size(window.size());
Rectangle::new((window.size() - size) / 2.0, size)
}

/// Draw to the Window, without writing those changes to the screen
pub fn flush_window(&mut self, window: &Window) -> Result<(), QuicksilverError> {
self.projection = Transform::orthographic(Rectangle::new_sized(self.world_size));
let viewport = self.calculate_viewport(window);
Expand All @@ -516,18 +513,17 @@ impl Graphics {
Ok(())
}

fn calculate_viewport(&self, window: &Window) -> Rectangle {
let size = self.resize.content_size(window.size());
Rectangle::new((window.size() - size) / 2.0, size)
}

/// Send the accumulated draw data to the GPU
///
/// Except when rendering to a [`Surface`], this should almost never be necessary for a user
/// to call directly. Use [`Graphics::present`] to draw to the window instead. When rendering
/// to a [`Surface`], remember to set the viewport via [`Graphics::set_viewport`]
fn flush_gpu(&mut self) -> Result<(), QuicksilverError> {
// Either bind a surface or draw directly to the default framebuffer, depending on the
// argument
/*match surface {
Some(surface) => surface.0.bind(),
None => golem::Surface::unbind(&self.ctx),
}*/
const TEX_BIND_POINT: u32 = 1;
let max_index = (self.vertex_data.len() / VERTEX_SIZE) as u32;
for index in self.index_data.iter() {
Expand Down Expand Up @@ -624,39 +620,14 @@ impl Graphics {

/// Send the draw data to the GPU and paint it to the Window
///
/// This will also re-set the viewport to fit the window before drawing to the screen.
/// If you don't want to use a custom viewport (the vast majority of cases), this takes care of
/// setting your viewport during resizes and similar situations.
/// If you would like to render to a custom viewport size (again, you probably don't), set your
/// viewport, flush, then present.
/// On desktop, this will block until drawing has completed. If vsync is enabled, it will block
/// until the frame completes. **Call this at the end of your frame.**
pub fn present(&mut self, win: &Window) -> Result<(), QuicksilverError> {
self.flush_window(win)?;
win.present();

Ok(())
}

/// Set the viewport to cover the given Surface
///
/// Will return an error if the surface has no image attachment, because then it has no size
pub fn fit_to_surface(&self, surface: &Surface) -> Result<(), QuicksilverError> {
if let (Some(width), Some(height)) = (surface.0.width(), surface.0.height()) {
self.ctx.set_viewport(0, 0, width, height);

Ok(())
} else {
Err(QuicksilverError::NoSurfaceImageBound)
}
}

/// Set the viewport to cover the window, taking into account DPI
pub fn fit_to_window(&self, window: &Window) {
let size = window.size();
let scale = window.scale_factor();
let width = size.x * scale;
let height = size.y * scale;
self.ctx.set_viewport(0, 0, width as u32, height as u32);
}
}

fn insert_if_changed<T: Clone>(
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/resize_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::geom::{Transform, Vector};
use crate::geom::Vector;

#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
Expand Down

0 comments on commit ba09c7b

Please sign in to comment.