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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::math::{Color, Rect, Scalar, Vec2};
use core::{assets::database::AssetsDatabase, error::*};
use std::{borrow::Cow, ops::Range};
#[derive(Debug, Copy, Clone)]
pub enum TextAlign {
Left,
Center,
Right,
}
impl Default for TextAlign {
fn default() -> Self {
TextAlign::Left
}
}
#[derive(Debug, Default, Clone)]
pub struct Rectangle {
pub color: Color,
pub rect: Rect,
}
#[derive(Debug, Default, Clone)]
pub struct Text<'a> {
pub color: Color,
pub font: Cow<'a, str>,
pub align: TextAlign,
pub text: Cow<'a, str>,
pub position: Vec2,
pub size: Scalar,
}
#[derive(Debug, Clone)]
pub enum PathElement {
MoveTo(Vec2),
LineTo(Vec2),
BezierCurveTo(Vec2, Vec2, Vec2),
QuadraticCurveTo(Vec2, Vec2),
Arc(Vec2, Scalar, Range<Scalar>),
Ellipse(Vec2, Vec2, Scalar, Range<Scalar>),
Rectangle(Rect),
}
#[derive(Debug, Default, Clone)]
pub struct Path {
pub color: Color,
pub elements: Vec<PathElement>,
}
#[derive(Debug, Default, Clone)]
pub struct Image<'a> {
pub image: Cow<'a, str>,
pub source: Option<Rect>,
pub destination: Option<Rect>,
}
impl<'a> Image<'a> {
pub fn new(image: &'a str) -> Self {
Self {
image: image.into(),
source: None,
destination: None,
}
}
}
#[derive(Debug, Clone)]
pub enum Renderable<'a> {
Rectangle(Rectangle),
Text(Text<'a>),
Path(Path),
Image(Image<'a>),
}
impl<'a> From<Rectangle> for Renderable<'a> {
fn from(rect: Rectangle) -> Self {
Renderable::Rectangle(rect)
}
}
impl<'a> From<Text<'a>> for Renderable<'a> {
fn from(text: Text<'a>) -> Self {
Renderable::Text(text)
}
}
impl<'a> From<Path> for Renderable<'a> {
fn from(path: Path) -> Self {
Renderable::Path(path)
}
}
impl<'a> From<Image<'a>> for Renderable<'a> {
fn from(image: Image<'a>) -> Self {
Renderable::Image(image)
}
}
#[derive(Debug, Clone)]
pub enum Transformation {
Translate(Vec2),
Rotate(Scalar),
Scale(Vec2),
Transform(Scalar, Scalar, Scalar, Scalar, Scalar, Scalar),
}
#[derive(Debug, Clone)]
pub enum Command<'a> {
None,
Draw(Renderable<'a>),
Stroke(Scalar, Renderable<'a>),
Transform(Transformation),
Store,
Restore,
}
#[derive(Debug, Default, Clone)]
pub struct Stats {
pub render_ops: usize,
pub renderables: usize,
pub fps: f64,
pub delta_time: f64,
}
#[derive(Debug, Clone)]
pub struct RenderState {
pub clear_color: Option<Color>,
stats: Stats,
}
impl Default for RenderState {
fn default() -> Self {
Self {
clear_color: Some(Color::black()),
stats: Stats::default(),
}
}
}
impl RenderState {
pub fn new(clear_color: Option<Color>) -> Self {
Self {
clear_color,
stats: Stats::default(),
}
}
pub fn stats(&self) -> &Stats {
&self.stats
}
pub fn set_stats(&mut self, stats: Stats) {
self.stats = stats;
}
}
pub trait CompositeRenderer {
fn execute<'a, I>(&mut self, commands: I) -> Result<(usize, usize)>
where
I: IntoIterator<Item = Command<'a>>;
fn state(&self) -> &RenderState;
fn state_mut(&mut self) -> &mut RenderState;
fn viewport(&self) -> Vec2;
fn update_state(&mut self) {}
fn update_cache(&mut self, _assets: &AssetsDatabase) {}
}