Browse Source

add background

Bastien Sevajol 3 years ago
parent
commit
c3a2cd7127
1 changed files with 21 additions and 7 deletions
  1. 21 7
      src/main.rs

+ 21 - 7
src/main.rs View File

141
 
141
 
142
 struct MainState {
142
 struct MainState {
143
     frame_i: u32,
143
     frame_i: u32,
144
-    scene_items_sprite_batch: graphics::spritebatch::SpriteBatch,
144
+    sprite_sheet_batch: graphics::spritebatch::SpriteBatch,
145
+    map_batch: graphics::spritebatch::SpriteBatch,
145
     scene_items: Vec<SceneItem>,
146
     scene_items: Vec<SceneItem>,
146
     physics_events: Vec<PhysicEvent>,
147
     physics_events: Vec<PhysicEvent>,
147
 }
148
 }
148
 
149
 
149
 impl MainState {
150
 impl MainState {
150
     fn new(ctx: &mut Context) -> GameResult<MainState> {
151
     fn new(ctx: &mut Context) -> GameResult<MainState> {
151
-        let image = graphics::Image::new(ctx, "/sprite_sheet.png").unwrap();
152
-        let batch = graphics::spritebatch::SpriteBatch::new(image);
152
+        let sprite_sheet = graphics::Image::new(ctx, "/sprite_sheet.png").unwrap();
153
+        let sprite_sheet_batch = graphics::spritebatch::SpriteBatch::new(sprite_sheet);
154
+        let map = graphics::Image::new(ctx, "/map1bg.png").unwrap();
155
+        let map_batch = graphics::spritebatch::SpriteBatch::new(map);
153
 
156
 
154
         let mut scene_items = vec![];
157
         let mut scene_items = vec![];
155
         for x in 0..1 {
158
         for x in 0..1 {
169
 
172
 
170
         let s = MainState {
173
         let s = MainState {
171
             frame_i: 0,
174
             frame_i: 0,
172
-            scene_items_sprite_batch: batch,
175
+            sprite_sheet_batch,
176
+            map_batch,
173
             scene_items,
177
             scene_items,
174
             physics_events: vec![],
178
             physics_events: vec![],
175
         };
179
         };
294
         let mut mesh_builder = MeshBuilder::new();
298
         let mut mesh_builder = MeshBuilder::new();
295
 
299
 
296
         for scene_item in self.scene_items.iter() {
300
         for scene_item in self.scene_items.iter() {
297
-            self.scene_items_sprite_batch.add(
301
+            self.sprite_sheet_batch.add(
298
                 scene_item
302
                 scene_item
299
                     .as_draw_param(scene_item.current_frame as f32)
303
                     .as_draw_param(scene_item.current_frame as f32)
300
                     .dest(scene_item.position.clone()),
304
                     .dest(scene_item.position.clone()),
307
                 graphics::WHITE,
311
                 graphics::WHITE,
308
             );
312
             );
309
         }
313
         }
314
+        self.map_batch.add(
315
+            graphics::DrawParam::new()
316
+            .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
317
+            .dest(na::Point2::new(0.0, 0.0))
318
+        );
310
 
319
 
311
         let mesh = mesh_builder.build(ctx)?;
320
         let mesh = mesh_builder.build(ctx)?;
312
         graphics::draw(
321
         graphics::draw(
313
             ctx,
322
             ctx,
314
-            &self.scene_items_sprite_batch,
323
+            &self.map_batch,
324
+            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
325
+        )?;
326
+        graphics::draw(
327
+            ctx,
328
+            &self.sprite_sheet_batch,
315
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
329
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
316
         )?;
330
         )?;
317
         graphics::draw(
331
         graphics::draw(
320
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
334
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
321
         )?;
335
         )?;
322
 
336
 
323
-        self.scene_items_sprite_batch.clear();
337
+        self.sprite_sheet_batch.clear();
324
         graphics::present(ctx)?;
338
         graphics::present(ctx)?;
325
 
339
 
326
         println!("FPS: {}", ggez::timer::fps(ctx));
340
         println!("FPS: {}", ggez::timer::fps(ctx));