Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with a progress bar while loading #585

Merged
merged 4 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Work In Progress
- Addition and subtraction of Tranforms supported to help with easing function calculations
- Added an example with loading progress bar

## v0.4.0-alpha0.3
- Update `golem` to `v0.1.1` to fix non-power-of-2 textures
Expand Down
81 changes: 81 additions & 0 deletions examples/06_loading_screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Example 6: draw a progress bar while loading the application
//
// This (pretends to) load some data when starting and in between each step (eg. each texture or
// model or whatever), it updates a progress bar.
//
// Alternatively, one could also do loading in a background thread or threads and do only the
// redrawing in the main one, to separate it.
use std::thread::sleep;
use std::time::Duration;

use quicksilver::{
geom::{Rectangle, Vector},
graphics::{Color, Graphics},
lifecycle::{run, EventStream, Settings, Window},
Result,
};

fn main() {
run(
Settings {
size: Vector::new(800.0, 600.0).into(),
title: "Square Example",
..Settings::default()
},
app,
)
}

const STEPS: usize = 10;

fn load_something() {
/*
* Here would be some actual loading of something, like textures.
* In this example we simply call sleep as a placeholder.
*
* TODO: Something better than sleep here would be nice.
* Also, there's a problem with sleeps:
* https://github.com/ryanisaacg/quicksilver/issues/580
*/
sleep(Duration::from_secs(1));
}

fn draw_loader(window: &Window, gfx: &mut Graphics, progress: usize, total: usize) -> Result<()> {
gfx.clear(Color::BLACK);
gfx.fill_rect(
&Rectangle::new(Vector::new(50.0, 500.0), Vector::new(700.0, 25.0)),
Color::YELLOW,
);

let width = 700.0 * progress as f32 / total as f32;
gfx.fill_rect(
&Rectangle::new(Vector::new(50.0, 500.0), Vector::new(width, 25.0)),
Color::BLUE,
);

/*
* In real game, this might be a good place to make the loading screen nicer, possibly by
* adding an image. We stick with just the progress bar in the example for simplicity.
*/

gfx.present(&window)?;

Ok(())
}

async fn app(window: Window, mut gfx: Graphics, mut events: EventStream) -> Result<()> {
for i in 0..STEPS {
draw_loader(&window, &mut gfx, i, STEPS)?;
load_something();
}

// Now we have everything loaded. The rest is not interesting for this example, so it just
// fills the whole window with green.

gfx.clear(Color::GREEN);
gfx.present(&window)?;

loop {
while let Some(_) = events.next_event().await {}
}
}