canvas/src/canvas.rs
Steins7 8fb74a0381 Started implementing Wgpu renderer
+ implemented basic setup code
+ implemented basic resize code
! renderer will default to GLES backend due to NV prime (Wgpu bug)
2022-07-02 23:28:30 +02:00

126 lines
2.9 KiB
Rust

#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use raw_window_handle::HasRawWindowHandle;
use crate::{
io::{Key, Scroll},
sprite::{Sprite, TextureSprite, TextSprite, ShapeSprite},
texture::Texture,
utils::{Size, Pixel, Position, Color},
renderer::WgpuRenderer,
};
use std::{
rc::Rc,
cell::RefCell,
};
//--Application trait-------------------------------------------------------------------------------
pub trait Application {
fn init(&mut self, canvas: &mut Canvas) -> Result<(), &'static str>;
fn tick(&mut self, canvas: &mut Canvas) -> Result<(), &'static str>;
}
//--Canvas struct-----------------------------------------------------------------------------------
pub struct Canvas {
renderer: WgpuRenderer,
clear_color: Pixel,
}
impl Canvas {
pub async fn create<W: HasRawWindowHandle>(window: &W, size: Size)
-> Result<Canvas, &'static str>
{
let renderer = WgpuRenderer::create(window, size).await?;
Ok(Self {
renderer,
clear_color: Color::WHITE,
})
}
//--Create functions--
pub fn create_texture_sprite(&mut self, _size: Size, _scale: f32) -> TextureSprite {
unimplemented!();
}
pub fn create_text_sprite(&mut self, _size: Size, _scale: f32) -> TextSprite {
unimplemented!();
}
pub fn create_shape_sprite(&mut self) -> ShapeSprite {
unimplemented!();
}
pub fn create_texture(&mut self, _size: Size, _background: Option<Pixel>)
-> Rc<RefCell<Texture>>
{
unimplemented!();
}
pub fn create_texture_from_file(&mut self,
_pos: Position,
_file: &'static str,
_background: Option<Pixel>)
-> Rc<RefCell<Texture>>
{
unimplemented!();
}
//--Output functions--
pub fn draw<S: Sprite>(&mut self, sprite: &S) {
//update texture
self.renderer.render(sprite);
}
pub fn set_clear_color(&mut self, color: Pixel) {
self.clear_color = color;
}
pub fn set_size(&mut self, size: Size) {
self.renderer.resize(size);
}
pub fn clear(&mut self) {
self.renderer.clear(&self.clear_color);
}
//--Input functions--
pub fn close_requested(&self) -> bool {
unimplemented!();
}
pub fn key_pressed(&self, _key: Key) -> bool {
unimplemented!();
}
pub fn key_released(&self, _key: Key) -> bool {
unimplemented!();
}
pub fn get_key_presses(&self) -> Key {
unimplemented!();
}
pub fn get_key_releases(&self) -> Key {
unimplemented!();
}
pub fn get_mouse_position(&self) -> Position {
unimplemented!();
}
pub fn get_mouse_scroll(&self) -> Scroll {
unimplemented!();
}
pub fn update(&mut self) {
self.renderer.present();
}
}