Skip to content

Commit

Permalink
load textures only once
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmut committed Nov 11, 2023
1 parent a65c076 commit a760979
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/external/backends.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::common::cli::{CliArgs, GIT_VERSION};
use crate::external::assets_macroquad::load_tileset;
use crate::external::drawer_egui_macroquad::DrawerEguiMacroquad;
use crate::external::drawer_macroquad::DrawerMacroquad;
use crate::external::input_macroquad::InputMacroquad;
Expand Down Expand Up @@ -34,10 +33,9 @@ impl FromStr for UiBackend {
}
}

pub async fn factory(args: &CliArgs) -> Box<Option<SceneState>> {
pub async fn factory(args: &CliArgs, textures: Vec<Texture2D>) -> Box<Option<SceneState>> {
println!("Running Bioengineer version {}", GIT_VERSION);
let tileset = load_tileset("assets/image/tileset.png");
let drawer = drawer_factory(args.ui, tileset.await);
let drawer = drawer_factory(args.ui, textures);
let input_source = Box::new(InputSource::new());
let world = World::new_with_options(args.profile, args.fluids);
Box::new(Some(SceneState::Main(MainScene {
Expand Down
4 changes: 4 additions & 0 deletions src/external/drawer_egui_macroquad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl<'a> DrawerTrait for DrawerEguiMacroquad<'a> {
self.inner.as_mut().unwrap().set_textures(textures);
}

fn take_textures(self: Box<Self>) -> Vec<Texture2D> {
self.inner.unwrap().textures
}

fn screen_width(&self) -> f32 {
// self.inner.borrow().screen_width()
self.inner.as_ref().unwrap().screen_width()
Expand Down
4 changes: 4 additions & 0 deletions src/external/drawer_macroquad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl DrawerTrait for DrawerMacroquad {
self.textures = textures;
}

fn take_textures(self: Box<Self>) -> Vec<Texture2D> {
self.textures
}

// fn draw(&self, game_state: &GameState) {
// self.debug_draw_all_textures();
// }
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ pub enum SceneState {
Main(MainScene),
}

impl SceneState {
pub fn take_textures(self) -> Vec<Texture2D> {
match self {
SceneState::Introduction(state) => state.take_textures(),
SceneState::Main(state) => state.screen.drawer.take_textures(),
}
}
}

pub fn frame(scene_wrapper: &mut Box<Option<SceneState>>) -> State {
let wrapper = scene_wrapper.take().unwrap();
match wrapper {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() {
}
next_frame().await;

let mut scene = factory(&args).await;
let mut scene = factory(&args, scene.unwrap().take_textures()).await;
while frame(&mut scene) == State::ShouldContinue {
next_frame().await
}
Expand Down
13 changes: 8 additions & 5 deletions src/scene/introduction_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use juquad::widgets::button::{Button, InteractionStyle, Style};
use std::f32::consts::PI;

use crate::external::assets_macroquad::split_tileset;
use macroquad::prelude::{Color, Image, KeyCode, MouseButton, DARKGRAY, get_fps};
use macroquad::prelude::{get_fps, Color, Image, KeyCode, MouseButton, Texture2D, DARKGRAY};

use crate::scene::introduction_scene::fire_particles::Particle;
use crate::scene::{Scene, State};
Expand Down Expand Up @@ -41,7 +41,6 @@ const STYLE: Style = Style {
},
};


pub struct IntroductionSceneState {
pub drawer: Option<Box<dyn DrawerTrait>>,
pub input: Option<Box<dyn InputTrait>>,
Expand All @@ -59,7 +58,6 @@ pub struct IntroductionScene {
pub state: IntroductionSceneState,
}


impl IntroductionSceneState {
pub fn new(
drawer: Box<dyn DrawerTrait>,
Expand Down Expand Up @@ -133,9 +131,11 @@ impl IntroductionSceneState {
}
}
}
pub fn take_textures(self) -> Vec<Texture2D> {
self.drawer.unwrap().take_textures()
}
}


impl Scene for IntroductionScene {
fn frame(&mut self) -> State {
self.state.frame = (self.state.frame + 1) % 100000000;
Expand Down Expand Up @@ -199,7 +199,10 @@ impl IntroductionScene {
self.state.ship_pos.x -= 2.0;
}
}
(buttons, new_game_clicked || self.input().is_key_pressed(KeyCode::Escape))
(
buttons,
new_game_clicked || self.input().is_key_pressed(KeyCode::Escape),
)
}

fn render(&mut self, height: f32, width: f32, buttons: &Vec<Button>) {
Expand Down
2 changes: 1 addition & 1 deletion src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod main_scene_input;
pub const GREY: Color = Color::new(0.5, 0.5, 0.5, 1.0);

pub struct Screen {
drawer: Box<dyn DrawerTrait>,
pub drawer: Box<dyn DrawerTrait>,
input_source: Box<dyn MainSceneInputTrait>,
gui: Gui,
drawing_state: DrawingState,
Expand Down
1 change: 1 addition & 0 deletions src/screen/drawer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub trait DrawerTrait {
Self: Sized;

fn set_textures(&mut self, textures: Vec<Texture2D>);
fn take_textures(self: Box<Self>) -> Vec<Texture2D>;
fn screen_width(&self) -> f32;
fn screen_height(&self) -> f32;
fn clear_background(&self, color: Color);
Expand Down

0 comments on commit a760979

Please sign in to comment.