소스 검색

add background

Bastien Sevajol 3 년 전
부모
커밋
c3a2cd7127
1개의 변경된 파일21개의 추가작업 그리고 7개의 파일을 삭제
  1. 21 7
      src/main.rs

+ 21 - 7
src/main.rs 파일 보기

@@ -141,15 +141,18 @@ enum MetaEvent {
141 141
 
142 142
 struct MainState {
143 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 146
     scene_items: Vec<SceneItem>,
146 147
     physics_events: Vec<PhysicEvent>,
147 148
 }
148 149
 
149 150
 impl MainState {
150 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 157
         let mut scene_items = vec![];
155 158
         for x in 0..1 {
@@ -169,7 +172,8 @@ impl MainState {
169 172
 
170 173
         let s = MainState {
171 174
             frame_i: 0,
172
-            scene_items_sprite_batch: batch,
175
+            sprite_sheet_batch,
176
+            map_batch,
173 177
             scene_items,
174 178
             physics_events: vec![],
175 179
         };
@@ -294,7 +298,7 @@ impl event::EventHandler for MainState {
294 298
         let mut mesh_builder = MeshBuilder::new();
295 299
 
296 300
         for scene_item in self.scene_items.iter() {
297
-            self.scene_items_sprite_batch.add(
301
+            self.sprite_sheet_batch.add(
298 302
                 scene_item
299 303
                     .as_draw_param(scene_item.current_frame as f32)
300 304
                     .dest(scene_item.position.clone()),
@@ -307,11 +311,21 @@ impl event::EventHandler for MainState {
307 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 320
         let mesh = mesh_builder.build(ctx)?;
312 321
         graphics::draw(
313 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 329
             graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
316 330
         )?;
317 331
         graphics::draw(
@@ -320,7 +334,7 @@ impl event::EventHandler for MainState {
320 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 338
         graphics::present(ctx)?;
325 339
 
326 340
         println!("FPS: {}", ggez::timer::fps(ctx));