MCPcopy Create free account
hub / github.com/Logicalshift/flo_draw / main

Function main

draw/examples/texture_sprites.rs:62–130  ·  view source on GitHub ↗

Bouncing ball example that renders using textured sprites

()

Source from the content-addressed store, hash-verified

60/// Bouncing ball example that renders using textured sprites
61///
62pub fn main() {
63 // 'with_2d_graphics' is used to support operating systems that can't run event loops anywhere other than the main thread
64 with_2d_graphics(|| {
65 // Load a png file
66 let flo_bytes: &[u8] = include_bytes!["flo_and_carrot.png"];
67
68 // Create a window with a canvas to draw on
69 let canvas = create_drawing_window("Bouncing sprites");
70
71 // Clear the canvas to set a background colour
72 let mut flo_w = 0;
73 let mut flo_h = 0;
74 canvas.draw(|gc| {
75 gc.clear_canvas(Color::Rgba(0.6, 0.7, 0.8, 1.0));
76
77 // Set up the texture
78 gc.set_texture_fill_alpha(TextureId(0), 0.75);
79 let (w, h) = gc.load_texture(TextureId(0), io::Cursor::new(flo_bytes)).unwrap();
80 flo_w = w;
81 flo_h = h;
82 });
83
84 // Declare a sprite with our PNG file in it
85 canvas.draw(|gc| {
86 gc.sprite(SpriteId(0));
87 gc.clear_sprite();
88
89 let height = (flo_h as f32) / (flo_w as f32) * 128.0;
90
91 gc.new_path();
92 gc.circle(0.0, 0.0, height/2.0);
93 gc.fill_texture(TextureId(0), -64.0, height/2.0, 64.0, -height/2.0);
94 gc.fill();
95
96 gc.line_width(0.25);
97 gc.stroke_color(Color::Rgba(0.0, 0.0, 0.0, 1.0));
98 gc.stroke();
99 });
100
101 // Generate some random balls
102 let mut balls = (0..256).into_iter().map(|_| Ball::random(SpriteId(0))).collect::<Vec<_>>();
103
104 // Animate them
105 loop {
106 // Update the balls for this frame
107 for ball in balls.iter_mut() {
108 ball.update();
109 }
110
111 // Render the frame on layer 0
112 canvas.draw(|gc| {
113 gc.layer(LayerId(0));
114 gc.clear_layer();
115 gc.canvas_height(1000.0);
116 gc.center_region(0.0, 0.0, 1000.0, 1000.0);
117
118 for ball in balls.iter() {
119 // Render the ball's sprite at its location

Callers

nothing calls this directly

Calls 15

with_2d_graphicsFunction · 0.85
create_drawing_windowFunction · 0.85
SpriteIdClass · 0.85
LayerIdClass · 0.85
clear_canvasMethod · 0.80
load_textureMethod · 0.80
spriteMethod · 0.80
clear_spriteMethod · 0.80
new_pathMethod · 0.80
circleMethod · 0.80
fill_textureMethod · 0.80

Tested by

no test coverage detected