Redone sprite loading

This commit is contained in:
Rendo 2025-11-20 00:31:37 +05:00
commit dfc25d0b77
9 changed files with 189 additions and 117 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

View file

@ -2,9 +2,9 @@
<!-- Created with Inkscape (http://www.inkscape.org/) --> <!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg <svg
width="1920" width="258.83063"
height="1080" height="363.80368"
viewBox="0 0 1920 1080" viewBox="0 0 258.83063 363.80368"
version="1.1" version="1.1"
id="svg1" id="svg1"
inkscape:version="1.4.2 (ebf0e940d0, 2025-05-08)" inkscape:version="1.4.2 (ebf0e940d0, 2025-05-08)"
@ -14,6 +14,7 @@
inkscape:export-ydpi="96" inkscape:export-ydpi="96"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"> xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview <sodipodi:namedview
@ -26,21 +27,84 @@
inkscape:pagecheckerboard="1" inkscape:pagecheckerboard="1"
inkscape:deskcolor="#d1d1d1" inkscape:deskcolor="#d1d1d1"
inkscape:document-units="px" inkscape:document-units="px"
inkscape:zoom="0.5" inkscape:zoom="1"
inkscape:cx="818" inkscape:cx="-81.5"
inkscape:cy="503" inkscape:cy="418.5"
inkscape:window-width="1890" inkscape:window-width="1890"
inkscape:window-height="1012" inkscape:window-height="1012"
inkscape:window-x="0" inkscape:window-x="0"
inkscape:window-y="0" inkscape:window-y="0"
inkscape:window-maximized="1" inkscape:window-maximized="1"
inkscape:current-layer="layer1" /> inkscape:current-layer="g50">
<inkscape:page
x="0"
y="0"
width="258.83063"
height="363.80368"
id="page2"
margin="0"
bleed="0"
inkscape:label="CardFront" />
<inkscape:page
x="258.83061"
y="3.3378601e-06"
width="166.27165"
height="166.27165"
id="page3"
inkscape:label="HandGUI" />
<inkscape:page
x="2.1457672e-06"
y="363.80369"
width="258.83031"
height="363.80368"
id="page50"
inkscape:label="CardBack" />
</sodipodi:namedview>
<defs <defs
id="defs1" /> id="defs1">
<pattern
inkscape:collect="always"
xlink:href="#Wave 2"
preserveAspectRatio="xMidYMid"
id="pattern50"
patternTransform="matrix(1.2144712,0,0,2.1737454,331.65722,1107.2246)"
x="0"
y="0" />
<pattern
patternUnits="userSpaceOnUse"
width="30"
height="9"
patternTransform="translate(435,777)"
preserveAspectRatio="xMidYMid"
style="fill:#8c6e58"
id="Wave 2"
inkscape:collect="always"
inkscape:isstock="true"
inkscape:label="Wave 2">
<path
id="path37"
style="stroke:none;stroke-linecap:butt;stroke-linejoin:bevel;paint-order:stroke markers fill;stop-color:#000000"
d="M 8,9 15,5.5 22,9 Z M 0,9 V 4 L 8,0 h 14 l 8,4 V 9 L 15,1 Z"
sodipodi:nodetypes="cccccccccccc" />
</pattern>
<clipPath
clipPathUnits="userSpaceOnUse"
id="clipPath24">
<rect
style="fill:none;stroke:#000000;stroke-width:0.999999;stroke-linecap:butt;stroke-linejoin:bevel;paint-order:stroke markers fill;stop-color:#000000"
id="rect24"
width="44.999748"
height="77.937294"
x="251.53429"
y="222.73541" />
</clipPath>
</defs>
<g <g
inkscape:label="Layer 1" inkscape:label="Layer 1"
inkscape:groupmode="layer" inkscape:groupmode="layer"
id="layer1"> id="layer1">
<g
id="g1">
<g <g
id="g7" id="g7"
transform="matrix(5.6603117,0,0,5.6603117,-524.28637,-261.14501)"> transform="matrix(5.6603117,0,0,5.6603117,-524.28637,-261.14501)">
@ -85,13 +149,33 @@
cy="23.113636" cy="23.113636"
r="4.4490924" /> r="4.4490924" />
</g> </g>
</g>
<g
id="g50">
<rect
style="mix-blend-mode:normal;fill:#ba8071;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:5.66031;stroke-opacity:1"
id="rect1-0-0"
width="253.1703"
height="358.14337"
x="2.8301558"
y="366.63385"
ry="26.072042" />
<rect
style="mix-blend-mode:normal;fill:url(#pattern50);fill-opacity:1;fill-rule:nonzero;stroke:#8b6f57;stroke-width:5.66031;stroke-opacity:1"
id="rect1-0"
width="253.17"
height="358.14337"
x="2.830157"
y="366.63385"
ry="26.072042" />
</g>
<rect <rect
style="fill:#6e4b37;fill-opacity:1;stroke:#50351e;stroke-width:5.66031;stroke-opacity:1" style="fill:#6e4b37;fill-opacity:1;stroke:#50351e;stroke-width:5.66031;stroke-opacity:1"
id="rect8" id="rect8"
width="160.61134" width="160.61134"
height="160.61134" height="160.61134"
x="261.66077" x="261.66077"
y="2.8301561" y="2.8301582"
ry="0" /> ry="0" />
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

Before After
Before After

BIN
assets/atlas_CardBack.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

BIN
assets/atlas_CardFront.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

BIN
assets/atlas_HandGUI.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 663 B

View file

@ -1,13 +1,23 @@
use crate::lofpara_atlas::*;
use bevy::prelude::*; use bevy::prelude::*;
use crate::sprite_loading::SpriteRegistry;
pub mod hand; pub mod hand;
pub struct CardsPlugin; pub struct CardsPlugin;
impl Plugin for CardsPlugin { impl Plugin for CardsPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_observer(on_create_card); app.add_observer(on_create_card)
.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Camera2d);
for i in 0..4 {
commands.trigger(CreateCard {
parent: None,
transform: Transform::from_xyz(100. * (i as f32), 0., 0.),
});
}
});
} }
} }
@ -16,26 +26,21 @@ pub struct Card;
#[derive(Event)] #[derive(Event)]
pub struct CreateCard { pub struct CreateCard {
parent: Option<Entity>, pub parent: Option<Entity>,
pub transform: Transform,
} }
pub fn on_create_card( pub fn on_create_card(event: On<CreateCard>, mut commands: Commands, sprites: Res<SpriteRegistry>) {
event: On<CreateCard>, let Some(sprite) = sprites.0.get("card_front") else {
mut commands: Commands, return;
atlas_layout: Res<LofparaAtlasLayout>, };
atlas_sprite: Res<LofparaAtlasImage>,
) {
if let Some(parent) = event.parent { if let Some(parent) = event.parent {
commands.spawn(( commands.spawn((
Card, Card,
Transform::from_xyz(0., 0., 0.), event.transform,
Sprite { Sprite {
image: atlas_sprite.get(), image: sprite.clone(),
image_mode: SpriteImageMode::Auto, image_mode: SpriteImageMode::Auto,
texture_atlas: Some(TextureAtlas {
layout: atlas_layout.get(),
index: 0,
}),
..default() ..default()
}, },
ChildOf(parent), ChildOf(parent),
@ -43,14 +48,10 @@ pub fn on_create_card(
} else { } else {
commands.spawn(( commands.spawn((
Card, Card,
Transform::from_xyz(0., 0., 0.), event.transform,
Sprite { Sprite {
image: atlas_sprite.get(), image: sprite.clone(),
image_mode: SpriteImageMode::Auto, image_mode: SpriteImageMode::Auto,
texture_atlas: Some(TextureAtlas {
layout: atlas_layout.get(),
index: 0,
}),
..default() ..default()
}, },
)); ));

View file

@ -1,43 +0,0 @@
use bevy::prelude::*;
#[derive(Resource)]
pub struct LofparaAtlasLayout(Handle<TextureAtlasLayout>);
impl LofparaAtlasLayout {
pub fn get(&self) -> Handle<TextureAtlasLayout> {
return self.0.clone();
}
}
#[derive(Resource)]
pub struct LofparaAtlasImage(Handle<Image>);
impl LofparaAtlasImage {
pub fn get(&self) -> Handle<Image> {
return self.0.clone();
}
}
pub struct LofparaAtlasPlugin;
impl Plugin for LofparaAtlasPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, startup_image);
}
}
fn startup_image(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
commands.insert_resource(LofparaAtlasImage(asset_server.load("atlas.png")));
let mut atlas_layout = TextureAtlasLayout::new_empty(UVec2::new(1920, 1080));
//Card, index = 0
atlas_layout.add_texture(URect::new(0, 0, 258, 363));
//Cardholder, index = 1
atlas_layout.add_texture(URect::new(259, 0, 166, 166));
commands.insert_resource(LofparaAtlasLayout(atlas_layouts.add(atlas_layout)));
}

View file

@ -1,11 +1,12 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::cards::CardsPlugin; use crate::{cards::CardsPlugin, sprite_loading::SpriteLoadingPlugin};
use crate::lofpara_atlas::LofparaAtlasPlugin;
mod cards; mod cards;
mod lofpara_atlas; mod sprite_loading;
fn main() { fn main() {
App::new().add_plugins((DefaultPlugins, LofparaAtlasPlugin, CardsPlugin)); App::new()
.add_plugins((DefaultPlugins, CardsPlugin, SpriteLoadingPlugin))
.run();
} }

29
src/sprite_loading.rs Normal file
View file

@ -0,0 +1,29 @@
use bevy::prelude::*;
use std::collections::HashMap;
#[derive(Resource)]
pub struct SpriteRegistry(pub HashMap<String, Handle<Image>>);
pub struct SpriteLoadingPlugin;
impl Plugin for SpriteLoadingPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(SpriteRegistry(HashMap::new()))
.add_systems(Startup, setup_sprite_registry);
}
}
pub fn setup_sprite_registry(mut sprites: ResMut<SpriteRegistry>, asset_server: Res<AssetServer>) {
sprites.0.insert(
"card_back".to_string(),
asset_server.load("atlas_CardBack.png"),
);
sprites.0.insert(
"card_front".to_string(),
asset_server.load("atlas_CardFront.png"),
);
sprites.0.insert(
"hand_gui".to_string(),
asset_server.load("atlas_HandGUI.png"),
);
}