pub struct GraphicsContext {
pub camera: Camera,
pub screen_size: Vec2,
pub dpi_scale: f32,
/* private fields */
}Expand description
Graphics context that collects draw commands.
In a full implementation, this wraps the miniquad rendering pipeline with batched rendering, GPU resource management, and shader programs.
Fields§
§camera: CameraCurrent camera.
screen_size: Vec2Screen size in pixels.
dpi_scale: f32DPI scale factor.
Implementations§
Source§impl GraphicsContext
impl GraphicsContext
Sourcepub fn clear(&mut self, color: Color)
pub fn clear(&mut self, color: Color)
Clear the screen with a color.
Examples found in repository?
examples/scene_demo.rs (line 60)
59 fn render(&mut self, ctx: &mut Context) {
60 ctx.graphics.clear(Color::from_hex("#16213E").unwrap());
61
62 // Title
63 ctx.graphics.draw_text("game-gem Scene Demo", 250.0, 150.0, 36.0, Color::GOLD);
64
65 // Menu options
66 for (i, option) in self.options.iter().enumerate() {
67 let color = if i == self.selected {
68 Color::GOLD
69 } else {
70 Color::LIGHT_GRAY
71 };
72 let prefix = if i == self.selected { "> " } else { " " };
73 ctx.graphics.draw_text(
74 &format!("{}{}", prefix, option),
75 320.0, 250.0 + i as f32 * 50.0,
76 24.0, color,
77 );
78 }
79
80 ctx.graphics.draw_text("Arrow Keys / WASD to navigate, Enter to select", 200.0, 500.0, 14.0, Color::GRAY);
81 }More examples
examples/particles.rs (line 73)
72 fn render(&mut self, ctx: &mut Context) {
73 ctx.graphics.clear(Color::from_hex("#0A0A1A").unwrap());
74
75 // Draw particles
76 for emitter in &self.emitters {
77 for particle in emitter.particles() {
78 if !particle.alive {
79 continue;
80 }
81 ctx.graphics.draw_circle(
82 particle.position.x,
83 particle.position.y,
84 particle.size,
85 particle.color,
86 );
87 }
88 }
89
90 // HUD
91 ctx.graphics.draw_text(
92 &format!("Particles: {}", self.emitters.iter().map(|e| e.alive_count()).sum::<usize>()),
93 10.0, 10.0, 18.0, Color::WHITE,
94 );
95 ctx.graphics.draw_text(
96 "Move mouse = fire | Click = sparkles | Esc = quit",
97 10.0, 35.0, 14.0, Color::LIGHT_GRAY,
98 );
99 }examples/basics.rs (line 111)
110 fn render(&mut self, ctx: &mut Context) {
111 ctx.graphics.clear(ctx.window.clear_color);
112
113 // Draw trail
114 for &(pos, alpha) in &self.ball.trail {
115 ctx.graphics.draw_circle(
116 pos.x, pos.y,
117 self.ball.radius * 0.5 * alpha,
118 self.ball.color.with_alpha(alpha * 0.5),
119 );
120 }
121
122 // Draw ball
123 ctx.graphics.draw_circle(
124 self.ball.position.x,
125 self.ball.position.y,
126 self.ball.radius,
127 self.ball.color,
128 );
129
130 // Draw HUD
131 ctx.graphics.reset_camera();
132 ctx.graphics.draw_text(
133 &format!("FPS: {:.0}", ctx.time.fps()),
134 10.0, 10.0, 18.0, Color::WHITE,
135 );
136 ctx.graphics.draw_text(
137 &format!("Clicks: {}", self.click_count),
138 10.0, 35.0, 18.0, Color::WHITE,
139 );
140 ctx.graphics.draw_text(
141 "Space = shake | Click = change color | Scroll = zoom | Esc = quit",
142 10.0, 60.0, 14.0, Color::LIGHT_GRAY,
143 );
144 }Sourcepub fn draw_circle(&mut self, x: f32, y: f32, radius: f32, color: Color)
pub fn draw_circle(&mut self, x: f32, y: f32, radius: f32, color: Color)
Draw a filled circle.
Examples found in repository?
examples/particles.rs (lines 81-86)
72 fn render(&mut self, ctx: &mut Context) {
73 ctx.graphics.clear(Color::from_hex("#0A0A1A").unwrap());
74
75 // Draw particles
76 for emitter in &self.emitters {
77 for particle in emitter.particles() {
78 if !particle.alive {
79 continue;
80 }
81 ctx.graphics.draw_circle(
82 particle.position.x,
83 particle.position.y,
84 particle.size,
85 particle.color,
86 );
87 }
88 }
89
90 // HUD
91 ctx.graphics.draw_text(
92 &format!("Particles: {}", self.emitters.iter().map(|e| e.alive_count()).sum::<usize>()),
93 10.0, 10.0, 18.0, Color::WHITE,
94 );
95 ctx.graphics.draw_text(
96 "Move mouse = fire | Click = sparkles | Esc = quit",
97 10.0, 35.0, 14.0, Color::LIGHT_GRAY,
98 );
99 }More examples
examples/basics.rs (lines 115-119)
110 fn render(&mut self, ctx: &mut Context) {
111 ctx.graphics.clear(ctx.window.clear_color);
112
113 // Draw trail
114 for &(pos, alpha) in &self.ball.trail {
115 ctx.graphics.draw_circle(
116 pos.x, pos.y,
117 self.ball.radius * 0.5 * alpha,
118 self.ball.color.with_alpha(alpha * 0.5),
119 );
120 }
121
122 // Draw ball
123 ctx.graphics.draw_circle(
124 self.ball.position.x,
125 self.ball.position.y,
126 self.ball.radius,
127 self.ball.color,
128 );
129
130 // Draw HUD
131 ctx.graphics.reset_camera();
132 ctx.graphics.draw_text(
133 &format!("FPS: {:.0}", ctx.time.fps()),
134 10.0, 10.0, 18.0, Color::WHITE,
135 );
136 ctx.graphics.draw_text(
137 &format!("Clicks: {}", self.click_count),
138 10.0, 35.0, 18.0, Color::WHITE,
139 );
140 ctx.graphics.draw_text(
141 "Space = shake | Click = change color | Scroll = zoom | Esc = quit",
142 10.0, 60.0, 14.0, Color::LIGHT_GRAY,
143 );
144 }Sourcepub fn draw_rect(&mut self, x: f32, y: f32, w: f32, h: f32, color: Color)
pub fn draw_rect(&mut self, x: f32, y: f32, w: f32, h: f32, color: Color)
Draw a filled rectangle.
Sourcepub fn draw_line(
&mut self,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
thickness: f32,
color: Color,
)
pub fn draw_line( &mut self, x1: f32, y1: f32, x2: f32, y2: f32, thickness: f32, color: Color, )
Draw a line segment.
Sourcepub fn draw_text(&mut self, text: &str, x: f32, y: f32, size: f32, color: Color)
pub fn draw_text(&mut self, text: &str, x: f32, y: f32, size: f32, color: Color)
Draw text (placeholder — real impl uses font rendering).
Examples found in repository?
examples/scene_demo.rs (line 63)
59 fn render(&mut self, ctx: &mut Context) {
60 ctx.graphics.clear(Color::from_hex("#16213E").unwrap());
61
62 // Title
63 ctx.graphics.draw_text("game-gem Scene Demo", 250.0, 150.0, 36.0, Color::GOLD);
64
65 // Menu options
66 for (i, option) in self.options.iter().enumerate() {
67 let color = if i == self.selected {
68 Color::GOLD
69 } else {
70 Color::LIGHT_GRAY
71 };
72 let prefix = if i == self.selected { "> " } else { " " };
73 ctx.graphics.draw_text(
74 &format!("{}{}", prefix, option),
75 320.0, 250.0 + i as f32 * 50.0,
76 24.0, color,
77 );
78 }
79
80 ctx.graphics.draw_text("Arrow Keys / WASD to navigate, Enter to select", 200.0, 500.0, 14.0, Color::GRAY);
81 }More examples
examples/particles.rs (lines 91-94)
72 fn render(&mut self, ctx: &mut Context) {
73 ctx.graphics.clear(Color::from_hex("#0A0A1A").unwrap());
74
75 // Draw particles
76 for emitter in &self.emitters {
77 for particle in emitter.particles() {
78 if !particle.alive {
79 continue;
80 }
81 ctx.graphics.draw_circle(
82 particle.position.x,
83 particle.position.y,
84 particle.size,
85 particle.color,
86 );
87 }
88 }
89
90 // HUD
91 ctx.graphics.draw_text(
92 &format!("Particles: {}", self.emitters.iter().map(|e| e.alive_count()).sum::<usize>()),
93 10.0, 10.0, 18.0, Color::WHITE,
94 );
95 ctx.graphics.draw_text(
96 "Move mouse = fire | Click = sparkles | Esc = quit",
97 10.0, 35.0, 14.0, Color::LIGHT_GRAY,
98 );
99 }examples/basics.rs (lines 132-135)
110 fn render(&mut self, ctx: &mut Context) {
111 ctx.graphics.clear(ctx.window.clear_color);
112
113 // Draw trail
114 for &(pos, alpha) in &self.ball.trail {
115 ctx.graphics.draw_circle(
116 pos.x, pos.y,
117 self.ball.radius * 0.5 * alpha,
118 self.ball.color.with_alpha(alpha * 0.5),
119 );
120 }
121
122 // Draw ball
123 ctx.graphics.draw_circle(
124 self.ball.position.x,
125 self.ball.position.y,
126 self.ball.radius,
127 self.ball.color,
128 );
129
130 // Draw HUD
131 ctx.graphics.reset_camera();
132 ctx.graphics.draw_text(
133 &format!("FPS: {:.0}", ctx.time.fps()),
134 10.0, 10.0, 18.0, Color::WHITE,
135 );
136 ctx.graphics.draw_text(
137 &format!("Clicks: {}", self.click_count),
138 10.0, 35.0, 18.0, Color::WHITE,
139 );
140 ctx.graphics.draw_text(
141 "Space = shake | Click = change color | Scroll = zoom | Esc = quit",
142 10.0, 60.0, 14.0, Color::LIGHT_GRAY,
143 );
144 }Sourcepub fn draw_triangle(
&mut self,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
x3: f32,
y3: f32,
color: Color,
)
pub fn draw_triangle( &mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: Color, )
Draw a filled triangle.
Sourcepub fn draw_ellipse(&mut self, x: f32, y: f32, rx: f32, ry: f32, color: Color)
pub fn draw_ellipse(&mut self, x: f32, y: f32, rx: f32, ry: f32, color: Color)
Draw a filled ellipse.
Sourcepub fn draw_ring(
&mut self,
x: f32,
y: f32,
inner_radius: f32,
outer_radius: f32,
color: Color,
)
pub fn draw_ring( &mut self, x: f32, y: f32, inner_radius: f32, outer_radius: f32, color: Color, )
Draw a ring (annulus).
Sourcepub fn draw_arc(
&mut self,
x: f32,
y: f32,
radius: f32,
start_angle: f32,
end_angle: f32,
color: Color,
)
pub fn draw_arc( &mut self, x: f32, y: f32, radius: f32, start_angle: f32, end_angle: f32, color: Color, )
Draw an arc.
Sourcepub fn set_camera(&mut self, camera: Camera)
pub fn set_camera(&mut self, camera: Camera)
Set the active camera for subsequent draw calls.
Sourcepub fn reset_camera(&mut self)
pub fn reset_camera(&mut self)
Reset to the default camera.
Examples found in repository?
examples/basics.rs (line 131)
110 fn render(&mut self, ctx: &mut Context) {
111 ctx.graphics.clear(ctx.window.clear_color);
112
113 // Draw trail
114 for &(pos, alpha) in &self.ball.trail {
115 ctx.graphics.draw_circle(
116 pos.x, pos.y,
117 self.ball.radius * 0.5 * alpha,
118 self.ball.color.with_alpha(alpha * 0.5),
119 );
120 }
121
122 // Draw ball
123 ctx.graphics.draw_circle(
124 self.ball.position.x,
125 self.ball.position.y,
126 self.ball.radius,
127 self.ball.color,
128 );
129
130 // Draw HUD
131 ctx.graphics.reset_camera();
132 ctx.graphics.draw_text(
133 &format!("FPS: {:.0}", ctx.time.fps()),
134 10.0, 10.0, 18.0, Color::WHITE,
135 );
136 ctx.graphics.draw_text(
137 &format!("Clicks: {}", self.click_count),
138 10.0, 35.0, 18.0, Color::WHITE,
139 );
140 ctx.graphics.draw_text(
141 "Space = shake | Click = change color | Scroll = zoom | Esc = quit",
142 10.0, 60.0, 14.0, Color::LIGHT_GRAY,
143 );
144 }Auto Trait Implementations§
impl Freeze for GraphicsContext
impl RefUnwindSafe for GraphicsContext
impl Send for GraphicsContext
impl Sync for GraphicsContext
impl Unpin for GraphicsContext
impl UnsafeUnpin for GraphicsContext
impl UnwindSafe for GraphicsContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more