Skip to main content

GraphicsContext

Struct GraphicsContext 

Source
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: Camera

Current camera.

§screen_size: Vec2

Screen size in pixels.

§dpi_scale: f32

DPI scale factor.

Implementations§

Source§

impl GraphicsContext

Source

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
Hide additional 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    }
Source

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
Hide additional 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    }
Source

pub fn draw_rect(&mut self, x: f32, y: f32, w: f32, h: f32, color: Color)

Draw a filled rectangle.

Source

pub fn draw_line( &mut self, x1: f32, y1: f32, x2: f32, y2: f32, thickness: f32, color: Color, )

Draw a line segment.

Source

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
Hide additional 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    }
Source

pub fn draw_triangle( &mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: Color, )

Draw a filled triangle.

Source

pub fn draw_poly(&mut self, points: Vec<Vec2>, color: Color)

Draw a filled polygon.

Source

pub fn draw_ellipse(&mut self, x: f32, y: f32, rx: f32, ry: f32, color: Color)

Draw a filled ellipse.

Source

pub fn draw_ring( &mut self, x: f32, y: f32, inner_radius: f32, outer_radius: f32, color: Color, )

Draw a ring (annulus).

Source

pub fn draw_arc( &mut self, x: f32, y: f32, radius: f32, start_angle: f32, end_angle: f32, color: Color, )

Draw an arc.

Source

pub fn set_camera(&mut self, camera: Camera)

Set the active camera for subsequent draw calls.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.