Selected Wgpu as rendering API
* adjusted API to better fit Wgpu's
This commit is contained in:
parent
313d311be8
commit
091454922b
@ -7,7 +7,8 @@ use crate::{
|
||||
io::{Key, Scroll},
|
||||
sprite::{Sprite, TextureSprite, TextSprite, ShapeSprite},
|
||||
texture::Texture,
|
||||
utils::{Size, Pixel, Position},
|
||||
utils::{Size, Pixel, Position, Color},
|
||||
renderer::Renderer,
|
||||
};
|
||||
|
||||
use std::{
|
||||
@ -24,13 +25,20 @@ pub trait Application {
|
||||
}
|
||||
|
||||
//--Canvas struct-----------------------------------------------------------------------------------
|
||||
pub struct Canvas {}
|
||||
pub struct Canvas {
|
||||
renderer: Renderer,
|
||||
clear_color: Pixel,
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
|
||||
pub fn create<W: HasRawWindowHandle>(_window: &W) -> Result<Canvas, &'static str> {
|
||||
pub fn create<W: HasRawWindowHandle>(window: &W) -> Result<Canvas, &'static str> {
|
||||
|
||||
let renderer = Renderer::create(window)?;
|
||||
|
||||
Ok(Self {
|
||||
renderer,
|
||||
clear_color: Color::WHITE,
|
||||
})
|
||||
}
|
||||
|
||||
@ -63,16 +71,17 @@ impl Canvas {
|
||||
}
|
||||
|
||||
//--Output functions--
|
||||
pub fn draw<S: Sprite>(&mut self, _sprite: S) {
|
||||
unimplemented!();
|
||||
pub fn draw<S: Sprite>(&mut self, sprite: &S) {
|
||||
//update texture
|
||||
self.renderer.draw(sprite);
|
||||
}
|
||||
|
||||
pub fn set_clear_color(&mut self, _color: Pixel) {
|
||||
unimplemented!();
|
||||
pub fn set_clear_color(&mut self, color: Pixel) {
|
||||
self.clear_color = color;
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
unimplemented!();
|
||||
self.renderer.clear(&self.clear_color);
|
||||
}
|
||||
|
||||
//--Input functions--
|
||||
@ -105,7 +114,7 @@ impl Canvas {
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
//unimplemented!();
|
||||
self.renderer.finish_frame();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,15 +9,15 @@ pub mod shape;
|
||||
pub mod sprite;
|
||||
pub mod texture;
|
||||
pub mod utils;
|
||||
mod renderer;
|
||||
|
||||
use utils::Size;
|
||||
|
||||
pub fn run_vk_canvas<A: 'static + Application>(title: &'static str, size: Size, mut app: A) -> ! {
|
||||
pub fn run_canvas<A: 'static + Application>(title: &'static str, size: Size, mut app: A) -> ! {
|
||||
use winit::{
|
||||
event_loop::EventLoop,
|
||||
window::WindowBuilder,
|
||||
};
|
||||
|
||||
// construct window
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new()
|
||||
@ -109,10 +109,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vk_create() {
|
||||
fn run_no_app() {
|
||||
use crate::utils::Size;
|
||||
|
||||
crate::run_vk_canvas("vk_create", Size {w: 1280, h: 720}, TestApp {});
|
||||
crate::run_canvas("vk_create", Size {w: 1280, h: 720}, TestApp {});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
112
src/main.rs
112
src/main.rs
@ -42,69 +42,69 @@ impl Application for ExampleApp {
|
||||
shape::{Shape, Rectangle},
|
||||
};
|
||||
|
||||
//// inputs
|
||||
//if canvas.key_pressed(Key::A) {
|
||||
// unimplemented!();
|
||||
//}
|
||||
//
|
||||
//if canvas.key_released(Key::A) {
|
||||
// unimplemented!();
|
||||
//}
|
||||
//
|
||||
//match canvas.get_key_presses() {
|
||||
// Key::A => unimplemented!(),
|
||||
// _ => (),
|
||||
//}
|
||||
//
|
||||
//match canvas.get_key_releases() {
|
||||
// Key::A => unimplemented!(),
|
||||
// _ => (),
|
||||
//}
|
||||
// inputs
|
||||
if canvas.key_pressed(Key::A) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
//let _mouse = canvas.get_mouse_position();
|
||||
if canvas.key_released(Key::A) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
//// outputs
|
||||
//canvas.set_clear_color(Color::BLACK);
|
||||
//canvas.clear();
|
||||
match canvas.get_key_presses() {
|
||||
Key::A => unimplemented!(),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
//// white rectangle inset by one in the sprite
|
||||
//let mut bas_sprite = canvas.create_shape_sprite();
|
||||
//bas_sprite.set_shape(Shape::Rectangle (Rectangle {size: Size {w: 38, h: 38}}));
|
||||
//bas_sprite.set_color(Color::WHITE);
|
||||
//canvas.draw(bas_sprite);
|
||||
match canvas.get_key_releases() {
|
||||
Key::A => unimplemented!(),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
//// 20 x 20 sprite of a picture
|
||||
//let texture = canvas.create_texture_from_file(
|
||||
// Position {x: 0, y: 0},
|
||||
// "texture.png",
|
||||
// Some(Color::WHITE)
|
||||
//);
|
||||
//let mut pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
|
||||
//pix_sprite.set_texture(texture, Some(Position {x: 0, y: 0}));
|
||||
//canvas.draw(pix_sprite);
|
||||
let _mouse = canvas.get_mouse_position();
|
||||
|
||||
//// scaled sprite of a manually drawed texture
|
||||
//let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::WHITE));
|
||||
//texture.borrow_mut().set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
|
||||
//let _ = texture.borrow_mut().iter_mut(); //iterate on whole texture
|
||||
//pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 2.0);
|
||||
//pix_sprite.set_texture(texture, None);
|
||||
//let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
|
||||
//canvas.draw(pix_sprite);
|
||||
// outputs
|
||||
canvas.set_clear_color(Color::BLACK);
|
||||
canvas.clear();
|
||||
|
||||
//// floating text
|
||||
//let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
|
||||
//txt_sprite.set_text("text");
|
||||
//txt_sprite.set_color(Color::BLACK);
|
||||
// white rectangle inset by one in the sprite
|
||||
let mut bas_sprite = canvas.create_shape_sprite();
|
||||
bas_sprite.set_shape(Shape::Rectangle (Rectangle {size: Size {w: 38, h: 38}}));
|
||||
bas_sprite.set_color(Color::WHITE);
|
||||
canvas.draw(&bas_sprite);
|
||||
|
||||
//// generic operations
|
||||
//txt_sprite.set_position(Position {x: 0, y: 0});
|
||||
//txt_sprite.set_rotation(50.0);
|
||||
//txt_sprite.set_alpha(255);
|
||||
//txt_sprite.set_scale(0.5);
|
||||
//canvas.draw(txt_sprite);
|
||||
// 20 x 20 sprite of a picture
|
||||
let texture = canvas.create_texture_from_file(
|
||||
Position {x: 0, y: 0},
|
||||
"texture.png",
|
||||
Some(Color::WHITE)
|
||||
);
|
||||
let mut pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
|
||||
pix_sprite.set_texture(texture, Some(Position {x: 0, y: 0}));
|
||||
canvas.draw(&pix_sprite);
|
||||
|
||||
//canvas.update();
|
||||
// scaled sprite of a manually drawed texture
|
||||
let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::WHITE));
|
||||
texture.borrow_mut().set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
|
||||
let _ = texture.borrow_mut().iter_mut(); //iterate on whole texture
|
||||
pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 2.0);
|
||||
pix_sprite.set_texture(texture, None);
|
||||
let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
|
||||
canvas.draw(&pix_sprite);
|
||||
|
||||
// floating text
|
||||
let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
|
||||
txt_sprite.set_text("text");
|
||||
txt_sprite.set_color(Color::BLACK);
|
||||
|
||||
// generic operations
|
||||
txt_sprite.set_position(Position {x: 0, y: 0});
|
||||
txt_sprite.set_rotation(50.0);
|
||||
txt_sprite.set_alpha(255);
|
||||
txt_sprite.set_scale(0.5);
|
||||
canvas.draw(&txt_sprite);
|
||||
|
||||
canvas.update();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -115,6 +115,6 @@ fn main() -> Result<(), &'static str> {
|
||||
|
||||
setup_logger()
|
||||
.map_err(|_| "Failed to setup logger")?;
|
||||
canvas::run_vk_canvas("vk_example", Size {w: 1280, h: 720}, ExampleApp {});
|
||||
canvas::run_canvas("vk_example", Size {w: 1280, h: 720}, ExampleApp {});
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user