1 module prova.graphics.sprites.gridspritesheet;
2 
3 import prova.graphics;
4 
5 /// Uses a grid of sprites
6 class GridSpriteSheet : SpriteSheet
7 {
8   /**
9    * Params:
10    *   path = Path of the image file
11    *   defaultDuration = Default duration for SpriteFrames
12    *   cols = Columns
13    *   rows = Rows
14    */
15   this(string path, int cols, int rows, float defaultDuration)
16   {
17     texture = Texture.fetch(path);
18 
19     float clipWidth = texture.width / cols;
20     float clipHeight = texture.height / rows;
21 
22     frames.reserve(cols * rows);
23 
24     foreach(int y; 0 .. rows) {
25       foreach(int x; 0 .. cols) {
26         SpriteFrame frame;
27         frame.clip.left = clipWidth * x;
28         frame.clip.top = clipHeight * y;
29         frame.clip.width = clipWidth;
30         frame.clip.height = clipHeight;
31         frame.duration = defaultDuration;
32 
33         frames ~= frame;
34       }
35     }
36   }
37 
38   ///
39   void createAnimation(string name, int start, int count)
40   {
41     SpriteFrame[] frames = this.frames[start .. start + count];
42 
43     animations[name] = new SpriteAnimation(name, frames);
44   }
45 }