screenspace coords was much easier than expected

master
mitchellhansen 4 years ago
parent 67b7d98f99
commit 0619b79d34

@ -11,7 +11,7 @@ use winit::event::Event;
/// Trait which may be inherited by objects that wish to be drawn to the screen
pub trait Drawable {
fn get(&self) -> Vec<VertexType>;
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType>;
}
/// Trait which may be inherited by objects that wish to receive events
@ -19,17 +19,29 @@ pub trait Eventable<T> {
fn notify(&mut self, event: &Event<T>) -> ();
}
/// Trait which may be inherited by objects that wish to be updated
pub trait Updatable {
fn update(&mut self, delta_time: f32) -> ();
}
/// Accumulator for Vectors of VertexTypes
#[derive(Default)]
pub struct CanvasFrame {
pub map: Vec<VertexType>,
window_size: (u32, u32),
}
impl CanvasFrame {
pub fn new(window_size: (u32, u32)) -> CanvasFrame {
CanvasFrame {
map: vec![],
window_size: window_size,
}
}
/// Push this drawable onto the back of the accumulator
pub fn draw(&mut self, drawable: &dyn Drawable) {
for i in drawable.get() {
for i in drawable.get(self.window_size) {
self.map.push(i);
}
}

@ -22,14 +22,17 @@ pub struct CompuFrame {
Arc<CompuBufferHandle>,
Arc<CompuBufferHandle>,
Arc<CompuKernelHandle>)>,
window_size: (u32, u32),
}
impl CompuFrame {
pub fn new() -> CompuFrame {
pub fn new(window_size: (u32, u32)) -> CompuFrame {
CompuFrame {
pure_compute: vec![],
swapped_to_image: vec![],
swapped_to_buffer: vec![],
window_size: window_size,
}
}
@ -54,7 +57,7 @@ impl CompuFrame {
kernel: Arc<CompuKernelHandle>,
sprite: &CompuSprite) {
let compu_sprites = sprite.get();
let compu_sprites = sprite.get(self.window_size);
if compu_sprites.len() == 1 {
if let VertexType::ImageType(a, b) = compu_sprites.first().unwrap() {

@ -53,7 +53,7 @@ impl CompuSprite {
}
impl Drawable for CompuSprite {
fn get(&self) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
vec![self.verts.clone()]
}
}

@ -61,7 +61,7 @@ impl Polygon {
}
}
impl Drawable for Polygon {
fn get(&self) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
vec![self.verts.clone()]
}

@ -50,8 +50,9 @@ impl Rect {
}
}
}
impl Drawable for Rect {
fn get(&self) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
vec![self.verts.clone()]
}

@ -1,15 +1,13 @@
use std::sync::Arc;
use crate::canvas::*;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle};
use crate::canvas::canvas_frame::{Drawable, Eventable};
use crate::canvas::canvas_frame::{Drawable, Eventable, Updatable};
use crate::util::vertex::{VertexType, TextureVertex3D, Vertex3D};
use winit::event::{DeviceEvent, MouseButton, ElementState, Event, WindowEvent};
///
#[derive(Debug, Clone)]
pub struct Sprite {
verts: VertexType,
pub position: (f32, f32),
pub size: (f32, f32),
depth: f32,
@ -18,30 +16,41 @@ pub struct Sprite {
/// Container class which implements drawable.
impl Sprite {
fn generate_verts(position: (f32, f32), size: (f32, f32), depth: f32) -> Vec<TextureVertex3D> {
fn generate_verts(window_size: (u32, u32), position: (f32, f32), size: (f32, f32), depth: f32) -> Vec<TextureVertex3D> {
let ss_position = (
position.0 / window_size.0 as f32 - 1.0,
position.1 / window_size.1 as f32 - 1.0
);
let ss_size = (
size.0 / window_size.0 as f32,
size.1 / window_size.1 as f32
);
vec![
TextureVertex3D {
v_position: [position.0, position.1, depth], // top left
v_position: [ss_position.0, ss_position.1, depth], // top left
ti_position: [-0.0, -0.0],
},
TextureVertex3D {
v_position: [position.0, position.1 + size.1, depth], // bottom left
v_position: [ss_position.0, ss_position.1 + ss_size.1, depth], // bottom left
ti_position: [-0.0, 1.0],
},
TextureVertex3D {
v_position: [position.0 + size.0, position.1 + size.1, depth], // bottom right
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
ti_position: [1.0, 1.0],
},
TextureVertex3D {
v_position: [position.0, position.1, depth], // top left
v_position: [ss_position.0, ss_position.1, depth], // top left
ti_position: [-0.0, -0.0],
},
TextureVertex3D {
v_position: [position.0 + size.0, position.1 + size.1, depth], // bottom right
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
ti_position: [1.0, 1.0],
},
TextureVertex3D {
v_position: [position.0 + size.0, position.1, depth], // top right
v_position: [ss_position.0 + ss_size.0, ss_position.1, depth], // top right
ti_position: [1.0, -0.0],
},
]
@ -55,7 +64,6 @@ impl Sprite {
let normalized_depth = (depth as f32 / 255.0);
Sprite {
verts: VertexType::TextureType(Sprite::generate_verts(position, size, normalized_depth), texture_handle.clone()),
position: position,
size: size,
depth: normalized_depth,
@ -65,10 +73,10 @@ impl Sprite {
}
impl Drawable for Sprite {
fn get(&self) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
vec![
VertexType::TextureType(
Sprite::generate_verts(self.position, self.size, self.depth),
Sprite::generate_verts(window_size, self.position, self.size, self.depth),
self.texture_handle.clone())
]
}
@ -92,4 +100,8 @@ impl<T> Eventable<T> for Sprite {
}
}
impl Updatable for Sprite {
fn update(&mut self, delta_time: f32) -> () {
}
}

@ -137,7 +137,7 @@ impl Text {
}
impl Drawable for Text {
fn get(&self) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
vec![self.verts.clone()]
}
}

@ -26,7 +26,7 @@ use winit::platform::unix::WindowBuilderExtUnix;
use winit::window::WindowBuilder;
use crate::button_m::Slider;
use crate::canvas::canvas_frame::{CanvasFrame, Drawable, Eventable};
use crate::canvas::canvas_frame::{CanvasFrame, Drawable, Eventable, Updatable};
use crate::canvas::canvas_state::CanvasState;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasTextureHandle, Handle};
use crate::compute::compu_frame::CompuFrame;
@ -71,7 +71,6 @@ pub mod button_m {
pub fn new(size: (f32, f32), position: (f32, f32), value: u16) -> Slider {
// render the guide first
let red = (1.0, 0.0, 0.0, 0.0);
let green = (0.0, 1.0, 0.0, 0.0);
let blue = (0.0, 1.0, 1.0, 0.0);
@ -84,7 +83,6 @@ pub mod button_m {
let scale = value as f32 / u16::max_value() as f32;
let handle = Rect::new((position.0 + (size.0 * scale) as f32, position.1 as f32), (0.03, size.1), 1, rg);
Slider {
handle: handle,
guide: vec![left_guide_bar, right_guide_bar, line],
@ -97,7 +95,7 @@ pub mod button_m {
}
impl Drawable for Slider {
fn get(&self) -> Vec<VertexType> {
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
let mut vertices = vec![self.handle.verts.clone()];
vertices.extend_from_slice(self.guide.iter()
.map(|x| x.clone().verts).collect::<Vec<VertexType>>().as_slice()
@ -114,12 +112,6 @@ pub mod button_m {
}
pub fn main() {
hprof::start_frame();
@ -158,8 +150,9 @@ pub fn main() {
Box::new(sync::now(processor.device.clone())) as Box<dyn GpuFuture>;
let step_size: f32 = 0.005;
let mut elapsed_time: f32;
let mut delta_time: f32;
let mut elapsed_time: f32 = timer.elap_time();
;
let mut delta_time: f32 = 0.0;
let mut accumulator_time: f32 = 0.0;
let mut current_time: f32 = timer.elap_time();
@ -167,7 +160,7 @@ pub fn main() {
let image_dimensions_f: (f32, f32) = ((image_data.1).clone().0 as f32, (image_data.1).clone().1 as f32);
let image_dimensions_u: (u32, u32) = image_data.1;
let compu_sprite1: CompuSprite =
CompuSprite::new((0.0, -0.5), (1.0, 1.0), 0, image_dimensions_f,
CompuSprite::new((-1.0, -1.0), (1.0, 1.0), 0, image_dimensions_f,
// Swap image to render the result to. Must match dimensions
processor.new_swap_image(image_dimensions_u));
@ -190,17 +183,15 @@ pub fn main() {
//let font_handle : Arc<CanvasFontHandle> =
// processor.get_font_handle(String::from("sansation.ttf")).unwrap();
let mut funky_sprite = Sprite::new((0.0, 0.5), (0.5, 0.5), 0, funky_handle.clone());
let mut funky_sprite = Sprite::new(
(200.0, 200.0),
(100.0, 150.0), 10, funky_handle.clone());
let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
let slider = button_m::Slider::new((0.3, 0.04), (-0.5, -0.3), 30000);
// how do i register a sprite to get events...
// explicit is much easieri.lo
//let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
// let text_sprite = Text::new((-0.1, -0.1), (10.0, 10.0), 1);
//let text_sprite = Text::new((-0.1, -0.1), (10.0, 10.0), 1);
//let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0));
drop(q2);
@ -242,9 +233,13 @@ pub fn main() {
}
});
let mut window_size: (u32, u32) = (0, 0);
let mut canvas_frame = CanvasFrame::new(window_size);
let mut compu_frame = CompuFrame::new(window_size);
let mut canvas_frame = CanvasFrame::default();
let mut compu_frame = CompuFrame::new();
let mut container = Box::new(Slider::new((0.1, 0.1), (0.9, 0.9), 5000));
//container.push(Sprite::new((0.1)));
// Events loop is borrowed from the surface
@ -259,21 +254,29 @@ pub fn main() {
canvas_frame.draw(&funky_sprite);
canvas_frame.draw(&compu_sprite1);
canvas_frame.draw(&slider);
window_size = surface.window().inner_size().into();
}
elapsed_time = timer.elap_time();
delta_time = elapsed_time - current_time;
current_time = elapsed_time;
if delta_time > 0.02 {
delta_time = 0.02;
}
accumulator_time += delta_time;
}
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
*control_flow = ControlFlow::Exit
}
Event::WindowEvent { event: WindowEvent::Resized(_), .. } => {
Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => {
processor.swapchain_recreate_needed = true;
let size = (new_size.width, new_size.height);
}
Event::WindowEvent {
event: WindowEvent::MouseInput {
device_id, state, button, modifiers
}, ..
} => {
}
} => {}
Event::UserEvent(TrEvent::KeyHeldEvent {}) => {}
Event::UserEvent(TrEvent::MouseHeldEvent {}) => {}
Event::UserEvent(TrEvent::GamepadEvent { gil_event }) => {}
@ -282,13 +285,10 @@ pub fn main() {
DeviceEvent::Key(keyboard_input) => {
match keyboard_input.virtual_keycode.unwrap() {
VirtualKeyCode::A => {
if keyboard_input.state == ElementState::Pressed {
}
if keyboard_input.state == ElementState::Pressed {}
}
VirtualKeyCode::S => {
if keyboard_input.state == ElementState::Pressed {
}
if keyboard_input.state == ElementState::Pressed {}
}
VirtualKeyCode::P => {
if keyboard_input.state == ElementState::Pressed {
@ -304,14 +304,16 @@ pub fn main() {
}
Event::MainEventsCleared => {
canvas_frame = CanvasFrame::default();
funky_sprite.update(delta_time);
canvas_frame = CanvasFrame::new(window_size);
canvas_frame.draw(&funky_sprite);
canvas_frame.draw(&compu_sprite1);
// canvas_frame.draw(&compu_sprite1);
canvas_frame.draw(&slider);
compu_frame = CompuFrame::new();
compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
compu_frame = CompuFrame::new(window_size);
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
{
let g = hprof::enter("Run");
@ -319,6 +321,10 @@ pub fn main() {
&canvas_frame,
&compu_frame);
}
// while (accumulator_time - step_size) >= step_size {
// accumulator_time -= step_size;
// }
}
_ => ()
}
@ -356,22 +362,8 @@ pub fn click_test(event_loop_proxy: EventLoopProxy<TrEvent>, canvas_state: &Canv
}
// while let true = processor.is_open() {
//
// // Take care of our timing
// {
// elapsed_time = timer.elap_time();
// delta_time = elapsed_time - current_time;
// current_time = elapsed_time;
// if delta_time > 0.02 {
// delta_time = 0.02;
// }
// accumulator_time += delta_time;
// }
//
// while (accumulator_time - step_size) >= step_size {
// accumulator_time -= step_size;
// }

Loading…
Cancel
Save