1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::rc::Rc;
use crate::glm::{self, Mat4};
use crate::graphics::opengl::{GLFramebuffer, GLTexture, TextureFormat};
use crate::graphics::{DrawParams, Drawable, Texture};
use crate::Context;
#[derive(Debug, Clone, PartialEq)]
pub struct Canvas {
pub(crate) texture: Texture,
pub(crate) framebuffer: Rc<GLFramebuffer>,
pub(crate) projection: Mat4,
}
impl Canvas {
pub fn new(ctx: &mut Context, width: i32, height: i32) -> Canvas {
let texture = ctx.gl.new_texture(width, height, TextureFormat::Rgba);
let framebuffer = ctx.gl.new_framebuffer();
ctx.gl
.attach_texture_to_framebuffer(&framebuffer, &texture, true);
Canvas::from_handle(texture, framebuffer)
}
pub(crate) fn from_handle(texture: GLTexture, framebuffer: GLFramebuffer) -> Canvas {
let width = texture.width();
let height = texture.height();
Canvas {
texture: Texture::from_handle(texture),
framebuffer: Rc::new(framebuffer),
projection: glm::ortho(0.0, width as f32, 0.0, height as f32, -1.0, 1.0),
}
}
pub fn width(&self) -> i32 {
self.texture.width()
}
pub fn height(&self) -> i32 {
self.texture.height()
}
pub fn texture(&self) -> &Texture {
&self.texture
}
}
impl Drawable for Canvas {
fn draw<P>(&self, ctx: &mut Context, params: P)
where
P: Into<DrawParams>,
{
self.texture.draw(ctx, params)
}
}