애셋
그림, 글, JSON, 바이너리, 글꼴, 소리를 받아 엔진에서 쓰는 방법을 다룹니다. 애셋 하나를 받는 애셋 구조체(ImageAsset, TextAsset, JsonAsset 같은 것), 여러 애셋을 함께 받으며 진행률과 실패 목록을 주는 AssetLoader, 로딩 막대를 부드럽게 움직이는 LoadingProgress, 묶은 그림을 이름으로 꺼내는 SpriteAtlas 차례로 봅니다.
씬의 create 에서 불러오기
애셋은 씬이 멤버로 듭니다. 중앙 리소스 매니저는 없습니다. 씬의 load 에서 load(engine, 경로) 로 읽기를 요청하고 scene_base.track_asset 으로 씬에 맡기면, 맡긴 것이 다 끝난 뒤 엔진이 initialize 를 부릅니다. 애셋을 쓰는 노드는 initialize 에서 만듭니다.
use vanilla::{Engine, EngineConfiguration, Graphic, ImageAsset, Scene, SceneBase, Sprite, Vector2};
struct AssetScene {
scene_base: SceneBase,
icon_asset: ImageAsset,
}
impl Scene for AssetScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.icon_asset.load(engine, "examples/assets/atlas/icons.png");
self.scene_base.track_asset(&self.icon_asset);
}
fn initialize(&mut self, _engine: &Engine) {
let icon_image = match self.icon_asset.get_image() {
Some(icon_image) => {
icon_image
}
None => {
return;
}
};
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let icon_id = node_tree.create_child(root_id, "icon");
let icon_node = &mut node_tree[icon_id];
icon_node.set_local_position(Vector2::create(640.0, 360.0));
icon_node.set_content_size(Vector2::create(256.0, 256.0));
icon_node.add_component(Sprite::create_with_image(icon_image));
}
fn pre_draw(&mut self, graphic: &mut Graphic) {
self.scene_base.draw_scene_background(graphic);
}
}
fn main() {
let mut engine_configuration = EngineConfiguration::new();
engine_configuration.reference_resolution_size = Vector2::create(1280.0, 720.0);
engine_configuration.auto_resize_on_window_resize = true;
let engine = Engine::new(engine_configuration);
let asset_scene = AssetScene {
scene_base: SceneBase::new(),
icon_asset: ImageAsset::new(),
};
if let Err(error) = engine.run(Box::new(asset_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- 데스크탑은
load를 부른 자리에서 파일을 읽어 끝냅니다. 웹은fetch가 끝난 뒤 다음 프레임들 사이에 끝납니다. 그래서 받은 것을 읽는 코드는load가 아니라initialize에 둡니다. - 맡긴 것이 다 끝날 때까지 엔진은 씬의
tick,draw대신draw_on_load를 매 프레임 부릅니다. 다 끝나면 0.3 초를 더 기다린 뒤initialize를 부릅니다. 맡긴 것이 없으면load뒤 0.3 초에 불립니다. - 받기에 실패한 애셋도 끝난 것으로 칩니다. 씬은 멈추지 않고, 실패한 애셋은 읽어도
None입니다. - 이미 요청한 애셋에
load를 다시 부르면 다시 읽지 않고 처음 손잡이를 돌려줍니다. 실패한 애셋도 같습니다. 다시 받으려면unload뒤에 부르거나 새 애셋을 만듭니다. - 씬에 맡기는 함수는 넷입니다.
track_asset(&애셋),track_load_handle(&읽기 손잡이),track_audio_asset(&Rc<AudioAsset>),track_task(&작업 손잡이).get_tracked_asset_progress()는 맡긴 것 가운데 끝난 비율(0 ~ 1, 실패 포함, 맡긴 것이 없으면 1)입니다.
await 로 불러오기
원본의 async load(engine) { await asset.load(경로); } 는 load 에서 씬 작업을 맡기고 track_task 로 씬에 맡기는 꼴로 옮깁니다. 작업은 맡긴 자리에서 첫 await 까지 돌고, 씬과 엔진은 scene_context.with 의 클로저 안에서만 빌립니다. load 가 돌려준 읽기 손잡이(AssetLoadHandle)는 애셋을 빌리지 않으므로 클로저 밖에서 .await 합니다.
use vanilla::{wait, Engine, EngineConfiguration, JsonAsset, Scene, SceneBase};
struct StageScene {
scene_base: SceneBase,
stage_asset: JsonAsset,
is_stage_ready: bool,
}
impl Scene for StageScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
let load_task = engine.spawn_scene_task(self, |mut scene_context| async move {
let load_result = scene_context.with(|scene, engine| scene.stage_asset.load(engine, "examples/assets/scenedocument.scene.json")).await;
if let Err(error_message) = load_result {
eprintln!("{}", error_message);
}
wait::seconds(0.5).await;
scene_context.with(|scene, _engine| {
scene.is_stage_ready = true;
});
});
self.scene_base.track_task(&load_task);
}
fn initialize(&mut self, _engine: &Engine) {
if let Some(stage_data) = self.stage_asset.get_data() {
println!("{} {}", stage_data["version"], self.is_stage_ready);
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let stage_scene = StageScene {
scene_base: SceneBase::new(),
stage_asset: JsonAsset::new(),
is_stage_ready: false,
};
if let Err(error) = engine.run(Box::new(stage_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- 읽기 손잡이를
await하면Result<Rc<Vec<u8>>, String>이 나옵니다. 받은 바이트이거나 실패 까닭입니다. (원본의 거부 자리) ImageAsset의 손잡이는 원본img.onload처럼 그림을 풀기까지 끝나야 끝나고, 풀지 못하면 실패로 끝납니다.JsonAsset의 손잡이는 원본처럼 받기만 끝나면 끝나고, 풀지 못한 JSON 은get_data()가None입니다.- 이미
load를 부른 애셋은wait_for_load()가 돌려준 퓨처를 나중에await해도 같습니다. (Asset트레이트) - async/await 의 자세한 쓰임은 "async/await" 장에서 봅니다.
애셋 경로
- 데스크탑은
EngineConfiguration.asset_directory_paths의 폴더 후보에서 앞에서부터 파일을 찾습니다. 어디에도 없으면 첫 후보 기준 경로로 읽다가 실패하고, 후보가 비었으면(기본) 경로를 실행한 폴더 기준 그대로 씁니다.engine.resolve_asset_path(경로)가 고른 경로를 돌려줍니다. - 웹은
asset_base_url + 경로를fetch합니다.asset_base_url이 비었으면(기본) 페이지 기준 상대 주소입니다. "data:"주소는 두 곳 모두 그대로 읽힙니다. 데스크탑은 엔진이 그 자리에서 풀고(퍼센트 꼴과;base64), 웹은 기준 주소를 붙이지 않고 브라우저fetch에 맡깁니다.- 게임 코드는 같고, 플랫폼마다 위 설정만 다릅니다.
use std::path::PathBuf;
use vanilla::{Engine, EngineConfiguration, TextAsset};
let mut engine_configuration = EngineConfiguration::new();
engine_configuration.asset_directory_paths = vec![PathBuf::from("examples/assets")];
engine_configuration.asset_base_url = String::from("assets/");
let engine = Engine::new(engine_configuration);
let mut text_asset = TextAsset::new();
let load_handle = text_asset.load(&engine, "data:,hello%20vanilla");
assert!(load_handle.is_loaded());
assert_eq!(text_asset.get_text(), Some("hello vanilla"));
let mut missing_asset = TextAsset::new();
let missing_handle = missing_asset.load(&engine, "missing.txt");
assert!(missing_handle.is_failed());
assert!(missing_handle.get_error_message().is_some());
assert_eq!(missing_asset.get_text(), None);애셋 종류
| 구조체 | 받는 것 | 받은 뒤 읽는 곳 |
|---|---|---|
ImageAsset |
그림 (png, jpeg, webp, gif 첫 장, bmp, ico, svg, avif) | get_image() (Option<Rc<Image>>), get_image_size() (픽셀, 없으면 0) |
TextAsset |
글 파일 | get_text() (UTF-8 로 풂) |
JsonAsset |
JSON 파일 | get_data() (serde_json::Value), get_text() |
BlobAsset |
바이너리 | get_blob() (Rc<Vec<u8>>) |
BunchAsset |
바이너리 (애셋 타입만 Bunch) |
get_blob() |
FontAsset |
글꼴 (ttf, otf, woff, woff2) | get_family(), get_descriptors(), is_bold() |
AudioAsset |
소리 (wav, mp3, ogg 와 webm 의 Opus 와 Vorbis, flac, m4a 와 aac 의 AAC) | get_audio_buffer(), get_duration() (초) |
VisualAsset |
받지 않습니다 (원본처럼 경로만 적음) | set_visual, get_visual |
AudioAsset을 뺀 애셋은Asset트레이트를 구현합니다.is_loaded(),get_asset_path()(요청 전이면 빈 글),wait_for_load(),get_asset_base()가 있고, 애셋 타입은get_asset_base().get_asset_type()으로 읽습니다.unload()는 애셋마다 있습니다.- 받은 바이트는 처음 읽을 때 풉니다. 그래서
get_image()와get_text()는&mut self를 받습니다. 그림을 풀지 못하면 표준 에러에 까닭을 찍고 다시 풀지 않습니다. JsonAsset은 풀지 못한 JSON 을 읽히지 않은 것으로 칩니다. (is_loaded()가 거짓,get_data()가None,get_text()는 남음)TextAsset과JsonAsset은set_no_cache(true)를load전에 주면 웹fetch에cache: "no-cache"를 붙입니다. 배포한 뒤에도 브라우저가 옛 데이터 표를 쓰는 일을 막습니다. 데스크탑은 보지 않습니다.get_data()와SpriteAtlas::load_data가 쓰는serde_json::Value는 엔진이 다시 내보내지 않으므로, 게임의Cargo.toml에serde_json을 더해 씁니다.
use vanilla::{Asset, AssetType, BlobAsset, Engine, EngineConfiguration, JsonAsset, TextAsset};
let engine = Engine::new(EngineConfiguration::new());
let mut json_asset = JsonAsset::new();
json_asset.set_no_cache(true);
json_asset.load_from_bytes("stage.json", br#"{ "name": "forest", "enemies": [3, 5] }"#.to_vec());
assert!(json_asset.is_loaded());
let stage_data = json_asset.get_data().expect("json data");
assert_eq!(stage_data["name"], "forest");
assert_eq!(stage_data["enemies"][1], 5);
let mut text_asset = TextAsset::new();
text_asset.load(&engine, "data:,line%20one");
assert_eq!(text_asset.get_text(), Some("line one"));
assert_eq!(text_asset.get_asset_path(), "data:,line%20one");
let mut blob_asset = BlobAsset::new();
blob_asset.load(&engine, "data:application/octet-stream;base64,AAEC");
assert_eq!(blob_asset.get_blob().expect("bytes").as_slice(), &[0, 1, 2]);
assert_eq!(blob_asset.get_asset_base().get_asset_type(), AssetType::Blob);
blob_asset.unload();
assert!(!blob_asset.is_loaded());글꼴
FontAsset 은 load 가 아니라 load_font(engine, 글꼴 이름, 경로) 로 받습니다. 바이트가 오면 엔진이 다음 프레임 시작에 글자 캐시의 글꼴 목록에 그 이름으로 올리고, 그 뒤로 Text.set_font_family(글꼴 이름) 이나 폰트 문자열("32px 글꼴 이름")에서 그 이름을 씁니다. load_font 들은 &Engine 을 받으므로 load 에서 부릅니다.
| 메서드 | 내용 |
|---|---|
load_font(engine, family, path) |
굵기 400 얼굴로 올립니다 |
load_font_with_bold(engine, family, path, is_bold) |
true 면 굵기 700 얼굴 |
load_font_with_descriptors(engine, family, path, descriptors) |
FontFaceDescriptors(굵기 범위, 기울기, 글자 범위)대로 올립니다 |
load_font_from_bytes(engine, family, bytes) |
이미 가진 바이트로 |
같은 이름에 굵기나 기울기가 다른 파일을 여러 개 올리면 글자 캐시가 CSS 처럼 골라 그립니다. 서술자는 FontFaceDescriptors::create_from_css(weight, style, unicode_range) 로 원본 서술자 글("700", "italic", "U+AC00-D7A3")에서 짓습니다. 아래 예제의 글꼴 파일은 저장소에 없으니 게임이 가진 파일 경로로 바꿉니다.
use vanilla::{Engine, EngineConfiguration, FontAsset, FontFaceDescriptors, Graphic, Scene, SceneBase, Text, Vector2};
struct FontScene {
scene_base: SceneBase,
regular_font_asset: FontAsset,
bold_font_asset: FontAsset,
}
impl Scene for FontScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.regular_font_asset.load_font(engine, "GameFont", "fonts/GameFont-Regular.ttf");
let bold_descriptors = FontFaceDescriptors::create_from_css("700", "normal", "");
self.bold_font_asset.load_font_with_descriptors(engine, "GameFont", "fonts/GameFont-Bold.ttf", bold_descriptors);
self.scene_base.track_asset(&self.regular_font_asset);
self.scene_base.track_asset(&self.bold_font_asset);
}
fn initialize(&mut self, _engine: &Engine) {
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let title_id = node_tree.create_child(root_id, "title");
let title_node = &mut node_tree[title_id];
title_node.set_local_position(Vector2::create(640.0, 360.0));
title_node.set_content_size(Vector2::create(800.0, 80.0));
let title_text = title_node.add_component(Text::create_with_text("Hello, Vanilla Engine", 48.0));
title_text.set_font_family("GameFont");
title_text.set_bold(true);
}
fn pre_draw(&mut self, graphic: &mut Graphic) {
self.scene_base.draw_scene_background(graphic);
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let font_scene = FontScene {
scene_base: SceneBase::new(),
regular_font_asset: FontAsset::new(),
bold_font_asset: FontAsset::new(),
};
if let Err(error) = engine.run(Box::new(font_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}load_font들은FontLoadHandle을 돌려줍니다.await하면 글꼴 목록에 올린 뒤(또는 받기나 올리기에 실패하면 까닭으로) 끝납니다.track_asset은 바이트 받기만 기다리지만, 올리기는 바로 다음 프레임 시작이라 0.3 초 뒤의initialize에서는 이미 올라 있습니다.- 이미 요청한
FontAsset에load_font를 다시 부르면 그 손잡이를 돌려줍니다.unload(engine)는 원본처럼 그 얼굴을 글꼴 목록에서 뺍니다. - 엔진이 처음 쓰는 기본 글꼴(
default_font_paths,default_font_bytes,default_font_url)과 여러 언어 글꼴은 "언어 묶음" 장에서 다룹니다.
소리
AudioAsset 은 다른 애셋과 달리 Asset 트레이트가 없고, 플레이어들이 함께 들 수 있게 Rc<AudioAsset> 로 씁니다. 애셋 경로로 읽을 때는 engine.load_audio_asset(경로) 를 부르고 track_audio_asset 으로 맡깁니다.
use std::rc::Rc;
use vanilla::{AudioAsset, Engine, EngineConfiguration, Scene, SceneBase};
struct SoundScene {
scene_base: SceneBase,
hit_sound: Option<Rc<AudioAsset>>,
}
impl Scene for SoundScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
match engine.load_audio_asset("tests/assets/audio/wav_s16_stereo.wav") {
Ok(audio_asset) => {
self.scene_base.track_audio_asset(&audio_asset);
self.hit_sound = Some(audio_asset);
}
Err(error_message) => {
eprintln!("{}", error_message);
}
}
}
fn initialize(&mut self, _engine: &Engine) {
if let Some(hit_sound) = &self.hit_sound {
println!("{}", hit_sound.get_duration());
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let sound_scene = SoundScene {
scene_base: SceneBase::new(),
hit_sound: None,
};
if let Err(error) = engine.run(Box::new(sound_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- 데스크탑은
load_audio_asset이 그 자리에서 파일을 읽고 풀어 돌아오고, 실패하면Err(까닭)입니다. 소리는 원본의 크롬decodeAudioData와 같은 길이와 표본으로 풀고, 표본율은 44100 으로 바꿉니다. 꼴은 확장자가 아니라 내용으로 가립니다. - 웹은
fetch와 풀기를 걸어 두고 바로 애셋을 돌려줍니다. 끝나면is_loaded()가 참이 되고, 실패하면get_load_error()에 까닭이 남습니다. 그 자리에서 알 수 있는 실패만Err입니다. Rc<AudioAsset>에 바로load_from_asset(engine, 경로),load(파일 경로),load_from_bytes(바이트)를 불러도 됩니다. 모두await할 수 있는AudioLoadHandle을 돌려주고, 이미 읽힌 애셋이면 아무것도 하지 않습니다.- 받은 소리를 내는 방법은 "소리" 장에서 다룹니다.
use std::rc::Rc;
use vanilla::AudioAsset;
let audio_asset = Rc::new(AudioAsset::new());
let audio_load_handle = audio_asset.load_from_bytes(vec![0, 1, 2, 3]);
assert!(audio_load_handle.is_finished());
assert!(audio_asset.get_load_error().is_some());
assert!(!audio_asset.is_loaded());
assert_eq!(audio_asset.get_duration(), 0.0);AssetLoader 로 함께 받기
AssetLoader 는 목록에 넣은 것을 동시에 받는 수만큼씩 함께 받습니다.
add_image(path),add_json(path),add_text(path),add_blob(path)는 아직 비어 있는 애셋을 만들어 목록에 넣고 그 애셋을 돌려줍니다. 다 받은 뒤 그 애셋에서 바로 읽습니다.add_audio(path)는Rc<AudioAsset>을,add_font(engine, family, path, descriptors)는 글꼴 바이트의 읽기 손잡이를 돌려줍니다. (원본은FontAsset을 돌려줍니다)- 원본
addTask자리는add_load_handle(&읽기 손잡이)입니다. 직접 요청했거나 직접 끝낼 손잡이(AssetLoadHandle::create_pending(이름)으로 만든 것)를 넣으면 진행률에 같이 셉니다. 넣은 때부터 받는 중으로 치고, 끝나기 전까지 동시 수의 한 자리를 씁니다. set_concurrency(count)로 동시에 받는 수를 정합니다. 기본은 6 이고, 1 보다 작으면 1 입니다.load(engine)가 받기를 시작합니다. 데스크탑은 여기서 다 끝납니다. 웹은 그 뒤로 프레임마다update(engine)를 불러야 앞의 것이 끝난 만큼 다음 것을 요청합니다.load가 돌려준 손잡이를 엔진 작업 안에서await하면 기다리는 동안update를 불러 주고 실패 목록(Vec<AssetLoadFailure>)으로 끝납니다.- 실패한 것도 끝난 것으로 쳐서 멈추지 않습니다. 실패마다 표준 에러에 까닭을 찍고
get_failed_list()에AssetLoadFailure { path, error_message }로 남깁니다. 그림은 다 받았을 때 풀어 보아 깨진 것을, JSON 은 풀지 못한 것을, 소리는 풀지 못한 것을 실패로 칩니다. - 진행률은 끝난 수(실패 포함) 나누기 전체 수입니다.
get_progress()(0 ~ 1, 넣은 것이 없으면 1),get_finished_count(),get_loaded_count()(실패 뺀 수),get_total_count(),get_in_flight_count(),is_finished()로 읽습니다. set_progress_handler(Some(Box::new(|progress, finished_count, total_count| { ... })))는 진행이 바뀔 때마다,set_complete_handler(Some(Box::new(|failed_list| { ... })))는 다 끝났을 때 불립니다.AssetLoader는 목록을 나눠 드는 값싼 복제 꼴입니다. 복제하면 같은 목록을 가리킵니다.
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{Asset, AssetLoader, Engine, EngineConfiguration};
let engine = Engine::new(EngineConfiguration::new());
let asset_loader = AssetLoader::new();
asset_loader.set_concurrency(4);
let mut text_asset = asset_loader.add_text("data:,hello");
let json_asset = asset_loader.add_json("data:application/json,{\"level\":3}");
let blob_asset = asset_loader.add_blob("data:,abc");
let missing_asset = asset_loader.add_image("missing.png");
let last_progress = Rc::new(Cell::new(0.0));
let progress_reader = last_progress.clone();
asset_loader.set_progress_handler(Some(Box::new(move |progress: f64, _finished_count: usize, _total_count: usize| {
progress_reader.set(progress);
})));
asset_loader.load(&engine);
assert!(asset_loader.is_finished());
assert_eq!(asset_loader.get_total_count(), 4);
assert_eq!(asset_loader.get_loaded_count(), 3);
assert_eq!(last_progress.get(), 1.0);
let failed_list = asset_loader.get_failed_list();
assert_eq!(failed_list.len(), 1);
assert_eq!(failed_list[0].path, "missing.png");
assert_eq!(text_asset.get_text(), Some("hello"));
assert_eq!(json_asset.get_data().expect("json data")["level"], 3);
assert_eq!(blob_asset.get_blob().expect("bytes").len(), 3);
assert!(!missing_asset.is_loaded());로딩 막대
씬에 맡길 때는 asset_loader.get_completion_handle() 을 track_load_handle 로 맡깁니다. 이 손잡이는 다 받으면 끝나므로 엔진이 그때까지 initialize 를 미룹니다. 그동안 draw_on_load 에서 update 를 부르고 로딩 막대를 그립니다.

LoadingProgress 는 알려 준 정도(set_progress, 0 ~ 1 로 자르고 줄어들지 않음)를 보이는 정도(get_shown_progress)가 부드럽게 따라가게 합니다. 매 프레임 update(time_delta) 를 부르고, 따라가는 빠르기는 set_follow_rate(초당 남은 거리의 비율, 기본 6)로 줍니다. 받기가 몰려 진행률이 한 번에 뛰어도 막대가 미끄러집니다.
use vanilla::{AssetLoader, Colors, Engine, EngineConfiguration, Graphic, ImageAsset, JsonAsset, LoadingProgress, Rect, Scene, SceneBase};
struct LoadingScene {
scene_base: SceneBase,
asset_loader: AssetLoader,
loading_progress: LoadingProgress,
icon_asset: Option<ImageAsset>,
atlas_json_asset: Option<JsonAsset>,
}
impl Scene for LoadingScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.asset_loader.set_concurrency(4);
self.icon_asset = Some(self.asset_loader.add_image("examples/assets/atlas/icons.png"));
self.atlas_json_asset = Some(self.asset_loader.add_json("examples/assets/atlas/icons.json"));
let completion_handle = self.asset_loader.get_completion_handle();
self.scene_base.track_load_handle(&completion_handle);
self.asset_loader.load(engine);
}
fn draw_on_load(&mut self, graphic: &mut Graphic) {
let engine = Engine::get_engine().unwrap();
self.asset_loader.update(engine);
self.loading_progress.set_progress(self.asset_loader.get_progress());
let time_delta = engine.get_time_manager().get_unscale_delta_time();
self.loading_progress.update(time_delta);
let shown_progress = self.loading_progress.get_shown_progress();
self.scene_base.draw_scene_background(graphic);
let view_size = engine.get_view_manager().get_view_size();
let bar_width = view_size.x * 0.6;
let bar_left = (view_size.x - bar_width) * 0.5;
let bar_top = view_size.y * 0.8;
graphic.set_fill_color("#404040");
graphic.draw_rect(Rect::create(bar_left, bar_top, bar_width, 12.0));
graphic.set_fill_color(Colors::WHITE);
graphic.draw_rect(Rect::create(bar_left, bar_top, bar_width * shown_progress, 12.0));
}
fn initialize(&mut self, _engine: &Engine) {
for failure in self.asset_loader.get_failed_list() {
eprintln!("{} {}", failure.path, failure.error_message);
}
if let Some(icon_asset) = self.icon_asset.as_mut() {
println!("{}", icon_asset.get_image_size().x);
}
if let Some(atlas_json_asset) = &self.atlas_json_asset {
println!("{}", atlas_json_asset.get_data().is_some());
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let loading_scene = LoadingScene {
scene_base: SceneBase::new(),
asset_loader: AssetLoader::new(),
loading_progress: LoadingProgress::new(),
icon_asset: None,
atlas_json_asset: None,
};
if let Err(error) = engine.run(Box::new(loading_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}GameSceneBase 를 쓰는 씬은 track_asset_loader(&asset_loader) 한 줄로 같은 일을 합니다. 기본 로딩 화면의 draw_on_load 가 프레임마다 update 를 부르고 진행률을 막대에 넘깁니다.
이미 가진 바이트로 읽기
압축 파일에서 꺼낸 것처럼 바이트를 이미 들고 있으면 받지 않고 바로 읽힙니다. 이미 요청한 애셋이어도 새 바이트로 바꿉니다. (FontAsset 과 AudioAsset 은 이미 읽기를 걸었으면 아무것도 하지 않습니다)
| 구조체 | 메서드 | 돌려줌 |
|---|---|---|
ImageAsset |
load_from_bytes(asset_path, bytes) |
읽기 손잡이 (풀지 못하면 실패로 끝남) |
TextAsset, JsonAsset, BlobAsset, BunchAsset |
load_from_bytes(asset_path, bytes) |
없음 |
FontAsset |
load_font_from_bytes(engine, family, bytes), load_font_from_bytes_with_descriptors(engine, family, bytes, descriptors) |
FontLoadHandle |
AudioAsset |
load_from_bytes(bytes) (경로 인자 없음, Rc<AudioAsset> 에 부름) |
AudioLoadHandle |
asset_path 는 알림 글과 get_asset_path() 에 쓰는 이름입니다. 바이트는 Vec<u8> 로 넘겨 애셋이 가집니다. 직접 만든 애셋 구조체는 AssetBase 를 필드로 들고 Asset 트레이트의 get_asset_base, get_asset_base_mut 만 채우면 track_asset 에 맡길 수 있습니다.
use vanilla::{Asset, AssetBase, AssetType, ImageAsset};
struct NoteAsset {
asset_base: AssetBase,
}
impl Asset for NoteAsset {
fn get_asset_base(&self) -> &AssetBase {
&self.asset_base
}
fn get_asset_base_mut(&mut self) -> &mut AssetBase {
&mut self.asset_base
}
}
let mut note_asset = NoteAsset {
asset_base: AssetBase::new(AssetType::Text),
};
note_asset.get_asset_base_mut().load_from_bytes("note.txt", b"hello".to_vec());
assert!(note_asset.is_loaded());
assert_eq!(note_asset.get_asset_path(), "note.txt");
let mut image_asset = ImageAsset::new();
let load_handle = image_asset.load_from_bytes("broken.png", vec![0, 1, 2, 3]);
assert!(load_handle.is_loaded());
assert!(image_asset.get_image().is_none());
assert_eq!(image_asset.get_image_size().x, 0.0);SpriteAtlas
여러 그림을 쪽 그림 몇 장에 모아 두고 이름으로 꺼냅니다. 같은 쪽 그림을 쓰는 스프라이트는 엔진이 묶어 한 번에 그리므로 그리기 호출이 줄어듭니다.
읽는 JSON 은 원본과 같은 두 꼴입니다. 하나는 원본 vanilla.js 저장소(https://github.com/ddukbaek2/vanilla.js)의 tools/atlas.cjs 가 만드는 꼴이고, 이 저장소에는 그 도구가 없습니다. 저장소의 examples/assets/atlas/icons.json 이 그 꼴입니다.
{
"images": {
"coin.png": { "x": 84, "y": 55, "width": 30, "height": 30, "page": 0 }
},
"meta": {
"imageFileName": "icons.png",
"pages": [{ "imageFileName": "icons.png", "width": 128, "height": 128 }]
}
}page가 없으면 0 쪽이고,meta.pages가 없으면meta.imageFileName한 장입니다.- 다른 하나는 쪽 경로 목록과
[쪽, x, y, 가로, 세로]표입니다. ({ "pagePaths": [...], "frames": { "이름": [...] } }) - 쪽 그림 경로에 빗금(/)이 없으면 JSON 이 있는 폴더(
load_data는base_directory)를 앞에 붙이고, 있으면 적힌 그대로 읽습니다."./"로 시작하면 그것을 떼고 애셋 경로로 읽습니다. - 그림 이름은 파일 이름으로도, 확장자를 뗀 이름으로도 찾습니다. (
"coin.png"는"coin"으로도 찾음)
JSON 파일은 load(engine, json_path) 로 읽습니다. 데스크탑은 이 자리에서 JSON 과 쪽 그림이 다 읽힙니다. 웹은 JSON 이 온 뒤 쪽 그림을 청해야 하므로, load 가 돌려준 손잡이를 엔진 작업 안에서 await 합니다. (기다리는 동안 프레임마다 update 를 불러 줍니다)
use vanilla::{Engine, EngineConfiguration, Scene, SceneBase, Sprite, SpriteAtlas, Vector2};
struct AtlasScene {
scene_base: SceneBase,
sprite_atlas: SpriteAtlas,
}
impl Scene for AtlasScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
let load_task = engine.spawn_scene_task(self, |mut scene_context| async move {
scene_context.with(|scene, engine| scene.sprite_atlas.load(engine, "examples/assets/atlas/icons.json")).await;
});
self.scene_base.track_task(&load_task);
}
fn initialize(&mut self, _engine: &Engine) {
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let frame_names = ["star", "heart", "leaf", "gem", "coin"];
for (frame_index, frame_name) in frame_names.iter().enumerate() {
let icon_id = node_tree.create_child(root_id, frame_name);
let icon_node = &mut node_tree[icon_id];
icon_node.set_local_position(Vector2::create(240.0 + frame_index as f64 * 200.0, 360.0));
icon_node.set_content_size(Vector2::create(96.0, 96.0));
let sprite = icon_node.add_component(Sprite::new());
self.sprite_atlas.apply_frame(sprite, frame_name);
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let atlas_scene = AtlasScene {
scene_base: SceneBase::new(),
sprite_atlas: SpriteAtlas::new(),
};
if let Err(error) = engine.run(Box::new(atlas_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}코드에 둔 표는 load_data(engine, &atlas_data, base_directory) 로 읽습니다. 아래 예제는 쪽 그림 자리에 64 x 32 SVG 를 "data:" 주소로 두고 32 x 32 조각 둘로 나눕니다.
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{Engine, EngineConfiguration, Rect, Sprite, SpriteAtlas};
let engine = Engine::new(EngineConfiguration::new());
let atlas_data = serde_json::json!({
"pagePaths": ["data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='64' height='32'/>"],
"frames": {
"left": [0, 0, 0, 32, 32],
"right": [0, 32, 0, 32, 32]
}
});
let mut sprite_atlas = SpriteAtlas::new();
sprite_atlas.load_data(&engine, &atlas_data, "");
assert!(sprite_atlas.is_loaded());
assert_eq!(sprite_atlas.get_page_count(), 1);
let right_frame = sprite_atlas.read_frame("right").expect("right frame");
assert_eq!(right_frame.get_image_rect(), Rect::create(32.0, 0.0, 32.0, 32.0));
assert_eq!(right_frame.get_image().get_width(), 64);
let mut sprite = Sprite::new();
assert!(sprite_atlas.apply_frame(&mut sprite, "left"));
assert!(!sprite_atlas.apply_frame(&mut sprite, "missing"));
assert!(sprite.get_image().is_none());
let shared_atlas = Rc::new(RefCell::new(sprite_atlas));
SpriteAtlas::add_search_atlas(&shared_atlas);
assert!(SpriteAtlas::apply_search_frame(&mut sprite, "right"));
assert_eq!(sprite.get_image_rect(), Rect::create(32.0, 0.0, 32.0, 32.0));
SpriteAtlas::clear_search_atlases();
assert!(SpriteAtlas::find_frame("right").is_none());read_frame(name)은Option<&Frame>(그림과 그 안의 영역)을 돌려줍니다.has_frame(name),get_frame_names(),get_page_images(),get_page_count(),is_loaded()도 있습니다.apply_frame(&mut sprite, name)은 스프라이트에 조각을 겁니다. 없는 이름이면 스프라이트 그림을 비우고false를 돌려줍니다.- 찾을 아틀라스 목록은
Rc<RefCell<SpriteAtlas>>로 넣습니다.SpriteAtlas::add_search_atlas로 넣어 두면SpriteAtlas::find_frame(name)(조각의 복사,Option<Frame>),SpriteAtlas::apply_search_frame(&mut sprite, name)이 넣은 차례로 찾습니다(앞의 것이 이깁니다).remove_search_atlas,clear_search_atlases로 뺍니다. 목록은 스레드 하나에 하나입니다. 전역 목록을 쓰지 않으려면SpriteAtlas::find_frame_in_order(&[&atlas], name),SpriteAtlas::apply_frame_in_order(&[&atlas], &mut sprite, name)을 씁니다. await없이 쓰려면load에서load한 뒤track_json_asset(&mut scene_base)로 JSON 을 씬에 맡기고,is_loaded()가 참이 될 때까지draw_on_load나tick에서update(engine)를 부릅니다. 쪽 그림은 JSON 이 온 뒤update가 청합니다.- 다시
load하는 동안은 원본처럼 앞서 다 읽힌 조각을 그대로 씁니다.
주의할 점
- 원본의
load는 실패하면 예외를 냅니다. 러스트 판은 예외가 없습니다. 읽기 손잡이가 실패(is_failed(),get_error_message())로 끝나고,await하면Err(까닭)입니다. 씬은 실패한 애셋도 끝난 것으로 칩니다. - 그림 손잡이는 바이트가 다 오면
is_loaded()가 참입니다. 깨진 그림인지는get_image()가None인지로 압니다. (손잡이를await했거나AssetLoader로 받았으면 그때 실패로 끝납니다) - 같은 애셋에
load를 두 번 부르면 두 번째는 첫 요청의 손잡이를 돌려줍니다. 다른 경로로 바꿔 받으려면unload뒤에 부르거나load_from_bytes로 바꿉니다. AudioAsset은Asset이 아니어서track_asset대신track_audio_asset으로 맡깁니다.SpriteAtlas도 애셋이 아니어서track_json_asset이나 작업 맡기기(track_task)로 기다립니다.SpriteAtlas는 JSON 을 읽지 못하거나 꼴을 모르면 표준 에러에 알리고 조각 없이 끝납니다. 손잡이의await도 끝나므로is_loaded()로 확인합니다.AssetLoader의add_font는 원본과 달리FontAsset이 아니라 읽기 손잡이를 돌려줍니다.draw_on_load는 맡긴 것이 끝나기 전까지만 불립니다. 이때 그리는 좌표계는 직접 맞춥니다. (draw_scene_background나engine.get_view_manager().apply_view_rect(graphic))
관련 타입
Asset, AssetBase, AssetType, AssetLoadHandle, AssetLoadFuture, ImageAsset, TextAsset, JsonAsset, BlobAsset, BunchAsset, FontAsset, FontLoadHandle, FontFaceDescriptors, AudioAsset, AudioLoadHandle, VisualAsset, AssetLoader, AssetLoaderLoadHandle, AssetLoadFailure, LoadingProgress, SpriteAtlas, SpriteAtlasLoadHandle, Frame, Image, Sprite, SceneBase, EngineConfiguration
Assets
This chapter covers loading images, text, JSON, binary data, fonts, and sounds for use in the engine. It goes through the asset structs that load one asset each (ImageAsset, TextAsset, JsonAsset, and so on), AssetLoader, which loads many assets together and reports progress and a failure list, LoadingProgress, which moves a loading bar smoothly, and SpriteAtlas, which picks packed images by name.
Loading in the scene's create
Assets are held by the scene as members. There is no central resource manager. Request a load with load(engine, path) in the scene's load and hand the asset to the scene with scene_base.track_asset; once everything tracked has finished, the engine calls initialize. Build the nodes that use the assets in initialize.
use vanilla::{Engine, EngineConfiguration, Graphic, ImageAsset, Scene, SceneBase, Sprite, Vector2};
struct AssetScene {
scene_base: SceneBase,
icon_asset: ImageAsset,
}
impl Scene for AssetScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.icon_asset.load(engine, "examples/assets/atlas/icons.png");
self.scene_base.track_asset(&self.icon_asset);
}
fn initialize(&mut self, _engine: &Engine) {
let icon_image = match self.icon_asset.get_image() {
Some(icon_image) => {
icon_image
}
None => {
return;
}
};
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let icon_id = node_tree.create_child(root_id, "icon");
let icon_node = &mut node_tree[icon_id];
icon_node.set_local_position(Vector2::create(640.0, 360.0));
icon_node.set_content_size(Vector2::create(256.0, 256.0));
icon_node.add_component(Sprite::create_with_image(icon_image));
}
fn pre_draw(&mut self, graphic: &mut Graphic) {
self.scene_base.draw_scene_background(graphic);
}
}
fn main() {
let mut engine_configuration = EngineConfiguration::new();
engine_configuration.reference_resolution_size = Vector2::create(1280.0, 720.0);
engine_configuration.auto_resize_on_window_resize = true;
let engine = Engine::new(engine_configuration);
let asset_scene = AssetScene {
scene_base: SceneBase::new(),
icon_asset: ImageAsset::new(),
};
if let Err(error) = engine.run(Box::new(asset_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- On desktop,
loadreads the file right where it is called. On the web, the load finishes some frames later, afterfetchcompletes. That is why code that reads the loaded data goes ininitialize, not inload. - Until everything tracked has finished, the engine calls
draw_on_loadevery frame instead of the scene'stickanddraw. When everything has finished, it waits 0.3 seconds more and then callsinitialize. With nothing tracked, it is called 0.3 seconds afterload. - An asset that failed to load also counts as finished. The scene does not get stuck, and reading a failed asset gives
None. - Calling
loadagain on an asset that already has a request does not read again; it returns the first handle. This holds for failed assets too. To load again, callunloadfirst or create a new asset. - There are four tracking functions:
track_asset(&asset),track_load_handle(&load_handle),track_audio_asset(&Rc<AudioAsset>), andtrack_task(&task_handle).get_tracked_asset_progress()is the finished fraction of what was tracked (0 to 1, failures included, 1 when nothing is tracked).
Loading with await
The original async load(engine) { await asset.load(path); } becomes a scene task spawned in load and handed to the scene with track_task. The task runs right where it is spawned up to its first await, and it borrows the scene and the engine only inside the closure of scene_context.with. The load handle (AssetLoadHandle) returned by load does not borrow the asset, so it is awaited outside the closure with .await.
use vanilla::{wait, Engine, EngineConfiguration, JsonAsset, Scene, SceneBase};
struct StageScene {
scene_base: SceneBase,
stage_asset: JsonAsset,
is_stage_ready: bool,
}
impl Scene for StageScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
let load_task = engine.spawn_scene_task(self, |mut scene_context| async move {
let load_result = scene_context.with(|scene, engine| scene.stage_asset.load(engine, "examples/assets/scenedocument.scene.json")).await;
if let Err(error_message) = load_result {
eprintln!("{}", error_message);
}
wait::seconds(0.5).await;
scene_context.with(|scene, _engine| {
scene.is_stage_ready = true;
});
});
self.scene_base.track_task(&load_task);
}
fn initialize(&mut self, _engine: &Engine) {
if let Some(stage_data) = self.stage_asset.get_data() {
println!("{} {}", stage_data["version"], self.is_stage_ready);
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let stage_scene = StageScene {
scene_base: SceneBase::new(),
stage_asset: JsonAsset::new(),
is_stage_ready: false,
};
if let Err(error) = engine.run(Box::new(stage_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- Awaiting a load handle gives
Result<Rc<Vec<u8>>, String>: the loaded bytes or the reason for the failure. (Where the original rejects) - An
ImageAssethandle finishes only after the image is decoded, like the originalimg.onload, and finishes as a failure if it cannot be decoded. AJsonAssethandle finishes as soon as the download finishes, like the original, and JSON that cannot be parsed givesNonefromget_data(). - For an asset whose
loadwas already called, awaiting the future fromwait_for_load()later does the same. (Assettrait) - See the "async/await" chapter for more on async/await.
Asset paths
- On desktop, files are looked up in the candidate folders of
EngineConfiguration.asset_directory_paths, from the front. If the file is in none of them, the path under the first candidate is read and the load fails; if the list is empty (the default), the path is used as is, relative to the folder the game was started from.engine.resolve_asset_path(path)returns the chosen path. - On the web,
asset_base_url + pathis fetched. Ifasset_base_urlis empty (the default), it is a URL relative to the page. "data:"URLs load as they are on both. On desktop the engine decodes them on the spot (percent escapes and;base64); on the web they go to the browser'sfetchwithout the base URL.- The game code is the same; only the settings above differ per platform.
use std::path::PathBuf;
use vanilla::{Engine, EngineConfiguration, TextAsset};
let mut engine_configuration = EngineConfiguration::new();
engine_configuration.asset_directory_paths = vec![PathBuf::from("examples/assets")];
engine_configuration.asset_base_url = String::from("assets/");
let engine = Engine::new(engine_configuration);
let mut text_asset = TextAsset::new();
let load_handle = text_asset.load(&engine, "data:,hello%20vanilla");
assert!(load_handle.is_loaded());
assert_eq!(text_asset.get_text(), Some("hello vanilla"));
let mut missing_asset = TextAsset::new();
let missing_handle = missing_asset.load(&engine, "missing.txt");
assert!(missing_handle.is_failed());
assert!(missing_handle.get_error_message().is_some());
assert_eq!(missing_asset.get_text(), None);Asset types
| Struct | Loads | Read after loading |
|---|---|---|
ImageAsset |
Images (png, jpeg, webp, first frame of gif, bmp, ico, svg, avif) | get_image() (Option<Rc<Image>>), get_image_size() (pixels, 0 when missing) |
TextAsset |
Text files | get_text() (decoded as UTF-8) |
JsonAsset |
JSON files | get_data() (serde_json::Value), get_text() |
BlobAsset |
Binary data | get_blob() (Rc<Vec<u8>>) |
BunchAsset |
Binary data (only the asset type is Bunch) |
get_blob() |
FontAsset |
Fonts (ttf, otf, woff, woff2) | get_family(), get_descriptors(), is_bold() |
AudioAsset |
Sounds (wav, mp3, Opus and Vorbis in ogg and webm, flac, AAC in m4a and aac) | get_audio_buffer(), get_duration() (seconds) |
VisualAsset |
Nothing (it only records the path, like the original) | set_visual, get_visual |
- Every asset except
AudioAssetimplements theAssettrait. It hasis_loaded(),get_asset_path()(empty before a request),wait_for_load(), andget_asset_base(); the asset type is read withget_asset_base().get_asset_type(). Each asset has its ownunload(). - Loaded bytes are decoded the first time they are read, so
get_image()andget_text()take&mut self. If an image cannot be decoded, the reason is printed to standard error and decoding is not tried again. JsonAssettreats JSON that cannot be parsed as not loaded. (is_loaded()is false,get_data()isNone,get_text()remains)TextAssetandJsonAssetaddcache: "no-cache"to the webfetchwhenset_no_cache(true)is given beforeload. This keeps the browser from using an old data table after a deployment. Desktop ignores it.- The engine does not re-export the
serde_json::Valueused byget_data()andSpriteAtlas::load_data, so addserde_jsonto the game'sCargo.toml.
use vanilla::{Asset, AssetType, BlobAsset, Engine, EngineConfiguration, JsonAsset, TextAsset};
let engine = Engine::new(EngineConfiguration::new());
let mut json_asset = JsonAsset::new();
json_asset.set_no_cache(true);
json_asset.load_from_bytes("stage.json", br#"{ "name": "forest", "enemies": [3, 5] }"#.to_vec());
assert!(json_asset.is_loaded());
let stage_data = json_asset.get_data().expect("json data");
assert_eq!(stage_data["name"], "forest");
assert_eq!(stage_data["enemies"][1], 5);
let mut text_asset = TextAsset::new();
text_asset.load(&engine, "data:,line%20one");
assert_eq!(text_asset.get_text(), Some("line one"));
assert_eq!(text_asset.get_asset_path(), "data:,line%20one");
let mut blob_asset = BlobAsset::new();
blob_asset.load(&engine, "data:application/octet-stream;base64,AAEC");
assert_eq!(blob_asset.get_blob().expect("bytes").as_slice(), &[0, 1, 2]);
assert_eq!(blob_asset.get_asset_base().get_asset_type(), AssetType::Blob);
blob_asset.unload();
assert!(!blob_asset.is_loaded());Fonts
FontAsset loads with load_font(engine, family, path), not with load. When the bytes arrive, the engine adds the font to the glyph cache's font list under that name at the start of the next frame; from then on the name works in Text.set_font_family(family) and in font strings ("32px family"). The load_font functions take &Engine, so call them in load.
| Method | Description |
|---|---|
load_font(engine, family, path) |
Adds a weight 400 face |
load_font_with_bold(engine, family, path, is_bold) |
A weight 700 face when true |
load_font_with_descriptors(engine, family, path, descriptors) |
Adds the face as described by FontFaceDescriptors (weight range, italic, character range) |
load_font_from_bytes(engine, family, bytes) |
From bytes you already have |
When several files with different weights or styles are added under one name, the glyph cache picks among them the way CSS does. FontFaceDescriptors::create_from_css(weight, style, unicode_range) builds descriptors from the original descriptor strings ("700", "italic", "U+AC00-D7A3"). The font files in the example below are not in the repository; replace them with paths to files the game has.
use vanilla::{Engine, EngineConfiguration, FontAsset, FontFaceDescriptors, Graphic, Scene, SceneBase, Text, Vector2};
struct FontScene {
scene_base: SceneBase,
regular_font_asset: FontAsset,
bold_font_asset: FontAsset,
}
impl Scene for FontScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.regular_font_asset.load_font(engine, "GameFont", "fonts/GameFont-Regular.ttf");
let bold_descriptors = FontFaceDescriptors::create_from_css("700", "normal", "");
self.bold_font_asset.load_font_with_descriptors(engine, "GameFont", "fonts/GameFont-Bold.ttf", bold_descriptors);
self.scene_base.track_asset(&self.regular_font_asset);
self.scene_base.track_asset(&self.bold_font_asset);
}
fn initialize(&mut self, _engine: &Engine) {
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let title_id = node_tree.create_child(root_id, "title");
let title_node = &mut node_tree[title_id];
title_node.set_local_position(Vector2::create(640.0, 360.0));
title_node.set_content_size(Vector2::create(800.0, 80.0));
let title_text = title_node.add_component(Text::create_with_text("Hello, Vanilla Engine", 48.0));
title_text.set_font_family("GameFont");
title_text.set_bold(true);
}
fn pre_draw(&mut self, graphic: &mut Graphic) {
self.scene_base.draw_scene_background(graphic);
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let font_scene = FontScene {
scene_base: SceneBase::new(),
regular_font_asset: FontAsset::new(),
bold_font_asset: FontAsset::new(),
};
if let Err(error) = engine.run(Box::new(font_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- The
load_fontfunctions return aFontLoadHandle. Awaiting it finishes once the font is in the font list (or with the reason, if loading or adding failed).track_assetwaits only for the bytes, but the font is added at the start of the very next frame, so it is already there ininitialize0.3 seconds later. - Calling
load_fontagain on aFontAssetthat already has a request returns that handle.unload(engine)removes the face from the font list, like the original. - The default fonts the engine starts with (
default_font_paths,default_font_bytes,default_font_url) and fonts for several languages are covered in the "Language Packs" chapter.
Sounds
Unlike the other assets, AudioAsset has no Asset trait, and it is used as Rc<AudioAsset> so several players can share it. To load from an asset path, call engine.load_audio_asset(path) and track it with track_audio_asset.
use std::rc::Rc;
use vanilla::{AudioAsset, Engine, EngineConfiguration, Scene, SceneBase};
struct SoundScene {
scene_base: SceneBase,
hit_sound: Option<Rc<AudioAsset>>,
}
impl Scene for SoundScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
match engine.load_audio_asset("tests/assets/audio/wav_s16_stereo.wav") {
Ok(audio_asset) => {
self.scene_base.track_audio_asset(&audio_asset);
self.hit_sound = Some(audio_asset);
}
Err(error_message) => {
eprintln!("{}", error_message);
}
}
}
fn initialize(&mut self, _engine: &Engine) {
if let Some(hit_sound) = &self.hit_sound {
println!("{}", hit_sound.get_duration());
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let sound_scene = SoundScene {
scene_base: SceneBase::new(),
hit_sound: None,
};
if let Err(error) = engine.run(Box::new(sound_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}- On desktop,
load_audio_assetreads and decodes the file right there before returning, and givesErr(reason)on failure. Sounds are decoded to the same length and samples as Chrome'sdecodeAudioDatain the original, resampled to 44100. The format is detected from the content, not the extension. - On the web, the fetch and decode are started and the asset is returned at once. When they finish,
is_loaded()becomes true; on failure the reason is kept inget_load_error(). Only failures known on the spot areErr. - You can also call
load_from_asset(engine, path),load(file_path), orload_from_bytes(bytes)on anRc<AudioAsset>directly. All of them return an awaitableAudioLoadHandle, and they do nothing if the asset is already loaded. - Playing loaded sounds is covered in the "Sound" chapter.
use std::rc::Rc;
use vanilla::AudioAsset;
let audio_asset = Rc::new(AudioAsset::new());
let audio_load_handle = audio_asset.load_from_bytes(vec![0, 1, 2, 3]);
assert!(audio_load_handle.is_finished());
assert!(audio_asset.get_load_error().is_some());
assert!(!audio_asset.is_loaded());
assert_eq!(audio_asset.get_duration(), 0.0);Loading together with AssetLoader
AssetLoader loads everything in its list together, up to a set number at a time.
add_image(path),add_json(path),add_text(path), andadd_blob(path)create an empty asset, put it in the list, and return it. Read from that asset once loading is done.add_audio(path)returns anRc<AudioAsset>, andadd_font(engine, family, path, descriptors)returns the load handle of the font bytes. (The original returns aFontAsset)- The original
addTaskbecomesadd_load_handle(&load_handle). A handle you requested yourself or will finish yourself (made withAssetLoadHandle::create_pending(name)) is counted in the progress. It counts as in flight from the moment it is added and takes one concurrency slot until it finishes. set_concurrency(count)sets how many load at once. The default is 6, and values below 1 become 1.load(engine)starts loading. On desktop everything finishes here. On the web, callupdate(engine)every frame after that so the next items are requested as earlier ones finish. Awaiting the handle returned byloadinside an engine task callsupdatewhile waiting and finishes with the failure list (Vec<AssetLoadFailure>).- Failures also count as finished, so loading never gets stuck. Each failure prints its reason to standard error and is kept in
get_failed_list()asAssetLoadFailure { path, error_message }. Images are test decoded when downloaded and broken ones fail, JSON that cannot be parsed fails, and sounds that cannot be decoded fail. - Progress is the finished count (failures included) divided by the total. Read it with
get_progress()(0 to 1, 1 when the list is empty),get_finished_count(),get_loaded_count()(excluding failures),get_total_count(),get_in_flight_count(), andis_finished(). set_progress_handler(Some(Box::new(|progress, finished_count, total_count| { ... })))is called whenever progress changes, andset_complete_handler(Some(Box::new(|failed_list| { ... })))when everything is done.AssetLoaderis a cheap clone that shares its list. A clone points to the same list.
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{Asset, AssetLoader, Engine, EngineConfiguration};
let engine = Engine::new(EngineConfiguration::new());
let asset_loader = AssetLoader::new();
asset_loader.set_concurrency(4);
let mut text_asset = asset_loader.add_text("data:,hello");
let json_asset = asset_loader.add_json("data:application/json,{\"level\":3}");
let blob_asset = asset_loader.add_blob("data:,abc");
let missing_asset = asset_loader.add_image("missing.png");
let last_progress = Rc::new(Cell::new(0.0));
let progress_reader = last_progress.clone();
asset_loader.set_progress_handler(Some(Box::new(move |progress: f64, _finished_count: usize, _total_count: usize| {
progress_reader.set(progress);
})));
asset_loader.load(&engine);
assert!(asset_loader.is_finished());
assert_eq!(asset_loader.get_total_count(), 4);
assert_eq!(asset_loader.get_loaded_count(), 3);
assert_eq!(last_progress.get(), 1.0);
let failed_list = asset_loader.get_failed_list();
assert_eq!(failed_list.len(), 1);
assert_eq!(failed_list[0].path, "missing.png");
assert_eq!(text_asset.get_text(), Some("hello"));
assert_eq!(json_asset.get_data().expect("json data")["level"], 3);
assert_eq!(blob_asset.get_blob().expect("bytes").len(), 3);
assert!(!missing_asset.is_loaded());Loading bar
To have the scene wait, track asset_loader.get_completion_handle() with track_load_handle. This handle finishes when everything is loaded, so the engine holds back initialize until then. Meanwhile, call update and draw a loading bar in draw_on_load.

LoadingProgress makes the shown amount (get_shown_progress) follow the reported amount (set_progress, clamped to 0 to 1 and never decreasing) smoothly. Call update(time_delta) every frame; the follow speed is set with set_follow_rate (the fraction of the remaining distance per second, default 6). Even when progress jumps because many loads finish at once, the bar glides.
use vanilla::{AssetLoader, Colors, Engine, EngineConfiguration, Graphic, ImageAsset, JsonAsset, LoadingProgress, Rect, Scene, SceneBase};
struct LoadingScene {
scene_base: SceneBase,
asset_loader: AssetLoader,
loading_progress: LoadingProgress,
icon_asset: Option<ImageAsset>,
atlas_json_asset: Option<JsonAsset>,
}
impl Scene for LoadingScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.asset_loader.set_concurrency(4);
self.icon_asset = Some(self.asset_loader.add_image("examples/assets/atlas/icons.png"));
self.atlas_json_asset = Some(self.asset_loader.add_json("examples/assets/atlas/icons.json"));
let completion_handle = self.asset_loader.get_completion_handle();
self.scene_base.track_load_handle(&completion_handle);
self.asset_loader.load(engine);
}
fn draw_on_load(&mut self, graphic: &mut Graphic) {
let engine = Engine::get_engine().unwrap();
self.asset_loader.update(engine);
self.loading_progress.set_progress(self.asset_loader.get_progress());
let time_delta = engine.get_time_manager().get_unscale_delta_time();
self.loading_progress.update(time_delta);
let shown_progress = self.loading_progress.get_shown_progress();
self.scene_base.draw_scene_background(graphic);
let view_size = engine.get_view_manager().get_view_size();
let bar_width = view_size.x * 0.6;
let bar_left = (view_size.x - bar_width) * 0.5;
let bar_top = view_size.y * 0.8;
graphic.set_fill_color("#404040");
graphic.draw_rect(Rect::create(bar_left, bar_top, bar_width, 12.0));
graphic.set_fill_color(Colors::WHITE);
graphic.draw_rect(Rect::create(bar_left, bar_top, bar_width * shown_progress, 12.0));
}
fn initialize(&mut self, _engine: &Engine) {
for failure in self.asset_loader.get_failed_list() {
eprintln!("{} {}", failure.path, failure.error_message);
}
if let Some(icon_asset) = self.icon_asset.as_mut() {
println!("{}", icon_asset.get_image_size().x);
}
if let Some(atlas_json_asset) = &self.atlas_json_asset {
println!("{}", atlas_json_asset.get_data().is_some());
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let loading_scene = LoadingScene {
scene_base: SceneBase::new(),
asset_loader: AssetLoader::new(),
loading_progress: LoadingProgress::new(),
icon_asset: None,
atlas_json_asset: None,
};
if let Err(error) = engine.run(Box::new(loading_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}A scene using GameSceneBase does the same with one line, track_asset_loader(&asset_loader). The default loading screen's draw_on_load calls update every frame and passes the progress to the bar.
Loading from bytes you already have
If you already hold the bytes, for example taken out of an archive, they load right away without a download. The new bytes replace an earlier request. (FontAsset and AudioAsset do nothing if a load was already started)
| Struct | Method | Returns |
|---|---|---|
ImageAsset |
load_from_bytes(asset_path, bytes) |
Load handle (finishes as a failure if it cannot be decoded) |
TextAsset, JsonAsset, BlobAsset, BunchAsset |
load_from_bytes(asset_path, bytes) |
Nothing |
FontAsset |
load_font_from_bytes(engine, family, bytes), load_font_from_bytes_with_descriptors(engine, family, bytes, descriptors) |
FontLoadHandle |
AudioAsset |
load_from_bytes(bytes) (no path argument, called on Rc<AudioAsset>) |
AudioLoadHandle |
asset_path is the name used in messages and by get_asset_path(). The bytes are passed as a Vec<u8> and owned by the asset. A custom asset struct can be tracked with track_asset if it holds an AssetBase field and fills in only get_asset_base and get_asset_base_mut of the Asset trait.
use vanilla::{Asset, AssetBase, AssetType, ImageAsset};
struct NoteAsset {
asset_base: AssetBase,
}
impl Asset for NoteAsset {
fn get_asset_base(&self) -> &AssetBase {
&self.asset_base
}
fn get_asset_base_mut(&mut self) -> &mut AssetBase {
&mut self.asset_base
}
}
let mut note_asset = NoteAsset {
asset_base: AssetBase::new(AssetType::Text),
};
note_asset.get_asset_base_mut().load_from_bytes("note.txt", b"hello".to_vec());
assert!(note_asset.is_loaded());
assert_eq!(note_asset.get_asset_path(), "note.txt");
let mut image_asset = ImageAsset::new();
let load_handle = image_asset.load_from_bytes("broken.png", vec![0, 1, 2, 3]);
assert!(load_handle.is_loaded());
assert!(image_asset.get_image().is_none());
assert_eq!(image_asset.get_image_size().x, 0.0);SpriteAtlas
Several images are packed into a few page images and picked by name. The engine batches sprites that use the same page image into one draw, so there are fewer draw calls.
It reads the same two JSON formats as the original. One is the format produced by tools/atlas.cjs in the original vanilla.js repository (https://github.com/ddukbaek2/vanilla.js); this repository does not include that tool. examples/assets/atlas/icons.json in the repository uses this format.
{
"images": {
"coin.png": { "x": 84, "y": 55, "width": 30, "height": 30, "page": 0 }
},
"meta": {
"imageFileName": "icons.png",
"pages": [{ "imageFileName": "icons.png", "width": 128, "height": 128 }]
}
}- Without
page, the page is 0; withoutmeta.pages, there is one page,meta.imageFileName. - The other format is a list of page paths and a table of
[page, x, y, width, height]. ({ "pagePaths": [...], "frames": { "name": [...] } }) - A page image path without a slash (/) gets the JSON file's folder (
base_directoryforload_data) put in front; a path with one is read as written. A leading"./"is removed and the rest is read as an asset path. - Images are found by file name and also by the name without its extension. (
"coin.png"is also found as"coin")
A JSON file is read with load(engine, json_path). On desktop, the JSON and the page images are all read right there. On the web, the page images can only be requested after the JSON arrives, so await the handle returned by load inside an engine task. (It calls update every frame while waiting)
use vanilla::{Engine, EngineConfiguration, Scene, SceneBase, Sprite, SpriteAtlas, Vector2};
struct AtlasScene {
scene_base: SceneBase,
sprite_atlas: SpriteAtlas,
}
impl Scene for AtlasScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
let load_task = engine.spawn_scene_task(self, |mut scene_context| async move {
scene_context.with(|scene, engine| scene.sprite_atlas.load(engine, "examples/assets/atlas/icons.json")).await;
});
self.scene_base.track_task(&load_task);
}
fn initialize(&mut self, _engine: &Engine) {
let root_id = self.scene_base.get_root_id();
let node_tree = self.scene_base.get_node_tree_mut();
let frame_names = ["star", "heart", "leaf", "gem", "coin"];
for (frame_index, frame_name) in frame_names.iter().enumerate() {
let icon_id = node_tree.create_child(root_id, frame_name);
let icon_node = &mut node_tree[icon_id];
icon_node.set_local_position(Vector2::create(240.0 + frame_index as f64 * 200.0, 360.0));
icon_node.set_content_size(Vector2::create(96.0, 96.0));
let sprite = icon_node.add_component(Sprite::new());
self.sprite_atlas.apply_frame(sprite, frame_name);
}
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let atlas_scene = AtlasScene {
scene_base: SceneBase::new(),
sprite_atlas: SpriteAtlas::new(),
};
if let Err(error) = engine.run(Box::new(atlas_scene)) {
eprintln!("{}", error);
std::process::exit(1);
}
}A table in code is read with load_data(engine, &atlas_data, base_directory). The example below puts a 64 x 32 SVG in the page slot as a "data:" URL and splits it into two 32 x 32 pieces.
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{Engine, EngineConfiguration, Rect, Sprite, SpriteAtlas};
let engine = Engine::new(EngineConfiguration::new());
let atlas_data = serde_json::json!({
"pagePaths": ["data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='64' height='32'/>"],
"frames": {
"left": [0, 0, 0, 32, 32],
"right": [0, 32, 0, 32, 32]
}
});
let mut sprite_atlas = SpriteAtlas::new();
sprite_atlas.load_data(&engine, &atlas_data, "");
assert!(sprite_atlas.is_loaded());
assert_eq!(sprite_atlas.get_page_count(), 1);
let right_frame = sprite_atlas.read_frame("right").expect("right frame");
assert_eq!(right_frame.get_image_rect(), Rect::create(32.0, 0.0, 32.0, 32.0));
assert_eq!(right_frame.get_image().get_width(), 64);
let mut sprite = Sprite::new();
assert!(sprite_atlas.apply_frame(&mut sprite, "left"));
assert!(!sprite_atlas.apply_frame(&mut sprite, "missing"));
assert!(sprite.get_image().is_none());
let shared_atlas = Rc::new(RefCell::new(sprite_atlas));
SpriteAtlas::add_search_atlas(&shared_atlas);
assert!(SpriteAtlas::apply_search_frame(&mut sprite, "right"));
assert_eq!(sprite.get_image_rect(), Rect::create(32.0, 0.0, 32.0, 32.0));
SpriteAtlas::clear_search_atlases();
assert!(SpriteAtlas::find_frame("right").is_none());read_frame(name)returnsOption<&Frame>(an image and an area in it). There are alsohas_frame(name),get_frame_names(),get_page_images(),get_page_count(), andis_loaded().apply_frame(&mut sprite, name)sets the piece on a sprite. For an unknown name it clears the sprite's image and returnsfalse.- The search list holds atlases as
Rc<RefCell<SpriteAtlas>>. After adding them withSpriteAtlas::add_search_atlas,SpriteAtlas::find_frame(name)(a copy of the piece,Option<Frame>) andSpriteAtlas::apply_search_frame(&mut sprite, name)search in the order they were added (earlier wins). Remove them withremove_search_atlasandclear_search_atlases. There is one list per thread. To avoid the global list, useSpriteAtlas::find_frame_in_order(&[&atlas], name)andSpriteAtlas::apply_frame_in_order(&[&atlas], &mut sprite, name). - Without await, call
loadinload, hand the JSON to the scene withtrack_json_asset(&mut scene_base), and callupdate(engine)indraw_on_loadortickuntilis_loaded()is true. The page images are requested byupdateafter the JSON arrives. - While loading again, the previously loaded pieces stay in use, like the original.
Things to watch
- The original
loadthrows on failure. The Rust version has no exceptions: the load handle finishes as a failure (is_failed(),get_error_message()), and awaiting it givesErr(reason). The scene counts failed assets as finished. - An image handle reports
is_loaded()as true once all bytes arrive. Whether the image is broken shows inget_image()returningNone. (If the handle was awaited or loaded throughAssetLoader, it finishes as a failure at that point) - Calling
loadtwice on the same asset returns the first request's handle the second time. To load a different path, callunloadfirst or switch withload_from_bytes. AudioAssetis not anAsset, so track it withtrack_audio_assetinstead oftrack_asset.SpriteAtlasis not an asset either; wait for it withtrack_json_assetor by tracking a task (track_task).- If
SpriteAtlascannot read the JSON or does not know its format, it reports to standard error and finishes without pieces. Awaiting its handle also finishes, so checkis_loaded(). - Unlike the original,
AssetLoader'sadd_fontreturns a load handle, not aFontAsset. draw_on_loadis called only until what was tracked has finished. Set up the drawing coordinates yourself there. (draw_scene_backgroundorengine.get_view_manager().apply_view_rect(graphic))
Related types
Asset, AssetBase, AssetType, AssetLoadHandle, AssetLoadFuture, ImageAsset, TextAsset, JsonAsset, BlobAsset, BunchAsset, FontAsset, FontLoadHandle, FontFaceDescriptors, AudioAsset, AudioLoadHandle, VisualAsset, AssetLoader, AssetLoaderLoadHandle, AssetLoadFailure, LoadingProgress, SpriteAtlas, SpriteAtlasLoadHandle, Frame, Image, Sprite, SceneBase, EngineConfiguration