Skip to content

Commit 8f13dea

Browse files
fINISH rENderer, add docs, fmt
1 parent b07dc62 commit 8f13dea

File tree

3 files changed

+177
-44
lines changed

3 files changed

+177
-44
lines changed

rlbot/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rlbot_flat::planus::{self, ReadAsRoot};
88
use thiserror::Error;
99

1010
pub mod agents;
11+
pub mod render;
1112
pub mod state_builder;
1213
pub mod util;
13-
pub mod render;
1414

1515
#[cfg(feature = "glam")]
1616
pub use rlbot_flat::glam;

rlbot/src/render.rs

Lines changed: 175 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,208 @@
1-
use rlbot_flat::flat::{Color, Line3D, PolyLine3D, Rect2D, Rect3D, RenderAnchor, RenderGroup, RenderMessage, String2D, String3D, TextHAlign, TextVAlign, Vector3};
1+
use rlbot_flat::flat::{
2+
Color, Line3D, PolyLine3D, Rect2D, Rect3D, RenderAnchor, RenderGroup, RenderMessage, String2D,
3+
String3D, TextHAlign, TextVAlign, Vector3,
4+
};
25

3-
pub const TRANSPARENT: Color = Color { r: 0, g: 0, b: 0, a: 0 };
4-
pub const BLACK: Color = Color { r: 0, g: 0, b: 0, a: 255 };
5-
pub const WHITE: Color = Color { r: 255, g: 255, b: 255, a: 255 };
6-
pub const RED: Color = Color { r: 255, g: 0, b: 0, a: 255 };
7-
pub const GREEN: Color = Color { r: 0, g: 128, b: 0, a: 255 };
8-
pub const BLUE: Color = Color { r: 0, g: 0, b: 255, a: 255 };
9-
pub const LIME: Color = Color { r: 0, g: 255, b: 0, a: 255 };
10-
pub const YELLOW: Color = Color { r: 255, g: 255, b: 0, a: 255 };
11-
pub const ORANGE: Color = Color { r: 255, g: 128, b: 0, a: 255 };
12-
pub const CYAN: Color = Color { r: 0, g: 255, b: 255, a: 255 };
13-
pub const PINK: Color = Color { r: 255, g: 0, b: 255, a: 255 };
14-
pub const PURPLE: Color = Color { r: 128, g: 0, b: 128, a: 255 };
15-
pub const TEAL: Color = Color { r: 0, g: 128, b: 128, a: 255 };
6+
#[rustfmt::skip]
7+
mod colors {
8+
use rlbot_flat::flat::Color;
9+
pub const TRANSPARENT: Color = Color { r: 0, g: 0, b: 0, a: 0 };
10+
pub const BLACK: Color = Color { r: 0, g: 0, b: 0, a: 255 };
11+
pub const WHITE: Color = Color { r: 255, g: 255, b: 255, a: 255 };
12+
pub const RED: Color = Color { r: 255, g: 0, b: 0, a: 255 };
13+
pub const GREEN: Color = Color { r: 0, g: 128, b: 0, a: 255 };
14+
pub const BLUE: Color = Color { r: 0, g: 0, b: 255, a: 255 };
15+
pub const LIME: Color = Color { r: 0, g: 255, b: 0, a: 255 };
16+
pub const YELLOW: Color = Color { r: 255, g: 255, b: 0, a: 255 };
17+
pub const ORANGE: Color = Color { r: 255, g: 128, b: 0, a: 255 };
18+
pub const CYAN: Color = Color { r: 0, g: 255, b: 255, a: 255 };
19+
pub const PINK: Color = Color { r: 255, g: 0, b: 255, a: 255 };
20+
pub const PURPLE: Color = Color { r: 128, g: 0, b: 128, a: 255 };
21+
pub const TEAL: Color = Color { r: 0, g: 128, b: 128, a: 255 };
22+
}
23+
pub use colors::*;
1624

25+
/// The Renderer allows of easy construction of [RenderGroup]s for in-game debug rendering.
26+
/// When done, call [build] and queue the resulting [RenderGroup] in the packet queue.
27+
///
28+
/// Example:
29+
/// ```ignore
30+
/// use rlbot::render::WHITE;
31+
/// let mut draw = Renderer::new(0);
32+
/// draw.line_3d(car.pos, car.pos + car.forward() * 120., RED);
33+
/// draw.line_3d(car.pos, car.pos + car.rightward() * 120., GREEN);
34+
/// draw.line_3d(car.pos, car.pos + car.upward() * 120., BLUE);
35+
/// packet_queue.push(draw.build());
36+
/// ```
1737
pub struct Renderer {
1838
pub group: RenderGroup,
19-
pub default_color: Color,
2039
}
2140

2241
impl Renderer {
23-
pub fn new(group: i32, default_color: Color) -> Self {
24-
Self { group: RenderGroup { render_messages: vec![], id: group }, default_color }
42+
/// Create a new Renderer.
43+
/// Each render group must have a unique id.
44+
/// Re-using an id will result in overwriting (watch out when using hiveminds).
45+
pub fn new(group_id: i32) -> Self {
46+
Self {
47+
group: RenderGroup {
48+
render_messages: vec![],
49+
id: group_id,
50+
},
51+
}
2552
}
2653

54+
/// Get the resulting [RenderGroup].
2755
pub fn build(self) -> RenderGroup {
2856
self.group
2957
}
3058

59+
/// Add a [RenderMessage] to this group.
3160
pub fn push(&mut self, message: impl Into<RenderMessage>) {
3261
self.group.render_messages.push(message.into());
3362
}
3463

35-
pub fn line_3d(&mut self, start: impl Into<RenderAnchor>, end: impl Into<RenderAnchor>, color: Option<Color>) {
64+
/// Draws a line between two anchors in 3d space.
65+
pub fn line_3d(
66+
&mut self,
67+
start: impl Into<RenderAnchor>,
68+
end: impl Into<RenderAnchor>,
69+
color: Color,
70+
) {
3671
self.group.render_messages.push(
3772
Line3D {
3873
start: Box::new(start.into()),
3974
end: Box::new(end.into()),
40-
color: color.unwrap_or(self.default_color),
41-
}.into()
75+
color,
76+
}
77+
.into(),
4278
);
4379
}
4480

45-
pub fn polyline_3d(&mut self, points: impl IntoIterator<Item = impl Into<Vector3>>, color: Option<Color>) {
46-
self.group.render_messages.push(PolyLine3D {
47-
points: points.into_iter().map(|p| p.into()).collect(),
48-
color: color.unwrap_or(self.default_color),
49-
}.into());
81+
/// Draws a line going through each of the provided points.
82+
pub fn polyline_3d(
83+
&mut self,
84+
points: impl IntoIterator<Item = impl Into<Vector3>>,
85+
color: Color,
86+
) {
87+
self.group.render_messages.push(
88+
PolyLine3D {
89+
points: points.into_iter().map(|p| p.into()).collect(),
90+
color,
91+
}
92+
.into(),
93+
);
5094
}
5195

52-
pub fn string_2d(&mut self, str: String2D) {
53-
self.group.render_messages.push(str.into());
96+
/// Draws text in 2d space.
97+
/// X and y uses screen-space coordinates, i.e. 0.1 is 10% of the screen width/height.
98+
/// Use `set_resolution` to change to pixel coordinates.
99+
/// Characters of the font are 20 pixels tall and 10 pixels wide when `scale == 1.0`.
100+
/// Consider using [push] and `..default()` when using multiple default values.
101+
pub fn string_2d(
102+
&mut self,
103+
text: impl Into<String>,
104+
x: f32,
105+
y: f32,
106+
scale: f32,
107+
foreground: Color,
108+
background: Color,
109+
h_align: TextHAlign,
110+
v_align: TextVAlign,
111+
) {
112+
self.group.render_messages.push(
113+
String2D {
114+
text: text.into(),
115+
x,
116+
y,
117+
scale,
118+
foreground,
119+
background,
120+
h_align,
121+
v_align,
122+
}
123+
.into(),
124+
);
54125
}
55126

56-
pub fn string_3d(&mut self, str: String3D) {
57-
self.group.render_messages.push(str.into());
127+
/// Draws text anchored in 3d space.
128+
/// Characters of the font are 20 pixels tall and 10 pixels wide when `scale == 1.0`.
129+
/// Consider using [push] and `..default()` when using multiple default values.
130+
pub fn string_3d(
131+
&mut self,
132+
text: impl Into<String>,
133+
anchor: impl Into<RenderAnchor>,
134+
scale: f32,
135+
foreground: Color,
136+
background: Color,
137+
h_align: TextHAlign,
138+
v_align: TextVAlign,
139+
) {
140+
self.group.render_messages.push(
141+
String3D {
142+
text: text.into(),
143+
anchor: Box::new(anchor.into()),
144+
scale,
145+
foreground,
146+
background,
147+
h_align,
148+
v_align,
149+
}
150+
.into(),
151+
);
58152
}
59153

60-
pub fn rect_2d(&mut self, rect: Rect2D) {
61-
self.group.render_messages.push(rect.into());
154+
/// Draws a rectangle anchored in 2d space.
155+
/// X, y, width, and height uses screen-space coordinates, i.e. 0.1 is 10% of the screen width/height.
156+
/// Use `set_resolution` to change to pixel coordinates.
157+
/// Consider using [push] and `..default()` when using multiple default values.
158+
pub fn rect_2d(
159+
&mut self,
160+
x: f32,
161+
y: f32,
162+
width: f32,
163+
height: f32,
164+
color: Color,
165+
h_align: TextHAlign,
166+
v_align: TextVAlign,
167+
) {
168+
self.group.render_messages.push(
169+
Rect2D {
170+
x,
171+
y,
172+
width,
173+
height,
174+
color,
175+
h_align,
176+
v_align,
177+
}
178+
.into(),
179+
);
62180
}
63181

64-
pub fn rect_3d(&mut self, rect: Rect3D) {
65-
self.group.render_messages.push(rect.into());
182+
/// Draws a rectangle anchored in 3d space.
183+
/// Width and height are screen-space sizes, i.e. 0.1 is 10% of the screen width/height.
184+
/// Use `set_resolution` to change to pixel coordinates.
185+
/// The size does not change based on distance to the camera.
186+
/// Consider using [push] and `..default()` when using multiple default values.
187+
pub fn rect_3d(
188+
&mut self,
189+
anchor: impl Into<RenderAnchor>,
190+
width: f32,
191+
height: f32,
192+
color: Color,
193+
h_align: TextHAlign,
194+
v_align: TextVAlign,
195+
) {
196+
self.group.render_messages.push(
197+
Rect3D {
198+
anchor: Box::new(anchor.into()),
199+
width,
200+
height,
201+
color,
202+
h_align,
203+
v_align,
204+
}
205+
.into(),
206+
);
66207
}
67-
}
208+
}

rlbot_flat/src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ macro_rules! from_render_message {
7171
};
7272
}
7373

74-
from_render_message!(
75-
Line3D,
76-
PolyLine3D,
77-
String2D,
78-
String3D,
79-
Rect2D,
80-
Rect3D
81-
);
74+
from_render_message!(Line3D, PolyLine3D, String2D, String3D, Rect2D, Rect3D);
8275

8376
impl From<flat::Vector3> for flat::RenderAnchor {
8477
fn from(value: flat::Vector3) -> Self {
@@ -109,4 +102,3 @@ impl From<flat::BallAnchor> for flat::RelativeAnchor {
109102
flat::RelativeAnchor::BallAnchor(Box::new(value))
110103
}
111104
}
112-

0 commit comments

Comments
 (0)