浏览代码

sprite sheet

Bastien Sevajol 3 年前
父节点
当前提交
6dc7b6e9ae
共有 5 个文件被更改,包括 2643 次插入0 次删除
  1. 2 0
      .gitignore
  2. 2489 0
      Cargo.lock
  3. 10 0
      Cargo.toml
  4. 二进制
      resources/test.png
  5. 142 0
      src/main.rs

+ 2 - 0
.gitignore 查看文件

@@ -0,0 +1,2 @@
1
+/target
2
+.idea

文件差异内容过多而无法显示
+ 2489 - 0
Cargo.lock


+ 10 - 0
Cargo.toml 查看文件

@@ -0,0 +1,10 @@
1
+[package]
2
+name = "ggez_tests"
3
+version = "0.1.0"
4
+authors = ["Sevajol Bastien <contact@bux.fr>"]
5
+edition = "2018"
6
+
7
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+[dependencies]
10
+ggez = "0.5.1"

二进制
resources/test.png 查看文件


+ 142 - 0
src/main.rs 查看文件

@@ -0,0 +1,142 @@
1
+use ggez;
2
+use ggez::event;
3
+use ggez::graphics;
4
+use ggez::nalgebra as na;
5
+use ggez::timer::check_update_time;
6
+use ggez::{Context, GameResult};
7
+use std::env;
8
+use std::path;
9
+
10
+struct SpriteInfo {
11
+    source: graphics::Rect,
12
+    tile_count: u16,
13
+    relative_tile_width: f32,
14
+    relative_tile_height: f32,
15
+}
16
+
17
+impl SpriteInfo {
18
+    pub fn new(source: graphics::Rect, tile_count: u16) -> Self {
19
+        let relative_tile_width: f32 = 1.0 / tile_count as f32;
20
+        Self {
21
+            source,
22
+            tile_count,
23
+            relative_tile_width,
24
+            relative_tile_height: 1.0,
25
+        }
26
+    }
27
+
28
+    pub fn from_type(type_: &SpriteType) -> Self {
29
+        match type_ {
30
+            SpriteType::WalkingSoldier => Self::new(graphics::Rect::new(0.0, 0.0, 128.0, 24.0), 7),
31
+        }
32
+    }
33
+}
34
+
35
+enum SpriteType {
36
+    WalkingSoldier,
37
+}
38
+
39
+fn sprite_batch_part_from_sprite_info(
40
+    sprite_info: &SpriteInfo,
41
+    frame_i: u32,
42
+) -> graphics::DrawParam {
43
+    let src = graphics::Rect::new(
44
+        frame_i as f32 * sprite_info.relative_tile_width,
45
+        0.0,
46
+        sprite_info.relative_tile_width,
47
+        sprite_info.relative_tile_height,
48
+    );
49
+    graphics::DrawParam::new().src(src)
50
+}
51
+
52
+struct SceneItem {
53
+    current_sprite_type: SpriteType,
54
+    position: na::Point2<f32>,
55
+}
56
+
57
+impl SceneItem {
58
+    pub fn new(current_sprite_type: SpriteType, position: na::Point2<f32>) -> Self {
59
+        Self {
60
+            current_sprite_type,
61
+            position,
62
+        }
63
+    }
64
+}
65
+
66
+struct MainState {
67
+    scene_items_sprite_batch: graphics::spritebatch::SpriteBatch,
68
+    scene_items: Vec<SceneItem>,
69
+    i: u32,
70
+}
71
+
72
+impl MainState {
73
+    fn new(ctx: &mut Context) -> GameResult<MainState> {
74
+        let image = graphics::Image::new(ctx, "/test.png").unwrap();
75
+        let batch = graphics::spritebatch::SpriteBatch::new(image);
76
+
77
+        let mut scene_items = vec![];
78
+        for x in 0..10 {
79
+            for y in 0..10 {
80
+                scene_items.push(SceneItem::new(
81
+                    SpriteType::WalkingSoldier,
82
+                    na::Point2::new(x as f32 * 24.0, y as f32 * 24.0),
83
+                ));
84
+            }
85
+        }
86
+
87
+        let s = MainState {
88
+            scene_items_sprite_batch: batch,
89
+            scene_items,
90
+            i: 0,
91
+        };
92
+        Ok(s)
93
+    }
94
+}
95
+impl event::EventHandler for MainState {
96
+    fn update(&mut self, ctx: &mut Context) -> GameResult {
97
+        if check_update_time(ctx, 5) {
98
+            self.i += 1;
99
+        }
100
+        if self.i > 6 {
101
+            self.i = 0;
102
+        }
103
+        Ok(())
104
+    }
105
+
106
+    fn draw(&mut self, ctx: &mut Context) -> GameResult {
107
+        graphics::clear(ctx, graphics::BLACK);
108
+
109
+        for scene_item in self.scene_items.iter() {
110
+            let sprite_info = SpriteInfo::from_type(&scene_item.current_sprite_type);
111
+            let sprite_batch_part = sprite_batch_part_from_sprite_info(&sprite_info, self.i).dest(scene_item.position.clone());
112
+            self.scene_items_sprite_batch.add(sprite_batch_part);
113
+        }
114
+        graphics::draw(
115
+            ctx,
116
+            &self.scene_items_sprite_batch,
117
+            graphics::DrawParam::new().dest(na::Point2::new(0.0, 0.0)),
118
+        );
119
+
120
+        self.scene_items_sprite_batch.clear();
121
+        graphics::present(ctx);
122
+
123
+        println!("FPS: {}", ggez::timer::fps(ctx));
124
+        Ok(())
125
+    }
126
+}
127
+
128
+pub fn main() -> GameResult {
129
+    let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
130
+        let mut path = path::PathBuf::from(manifest_dir);
131
+        path.push("resources");
132
+        path
133
+    } else {
134
+        path::PathBuf::from("./resources")
135
+    };
136
+
137
+    let cb = ggez::ContextBuilder::new("oc", "bux").add_resource_path(resource_dir);
138
+    let (ctx, event_loop) = &mut cb.build()?;
139
+
140
+    let state = &mut MainState::new(ctx)?;
141
+    event::run(ctx, event_loop, state)
142
+}