-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpritesheet.cs
More file actions
86 lines (72 loc) · 2.17 KB
/
Spritesheet.cs
File metadata and controls
86 lines (72 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Chroma.Graphics;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using Color = Chroma.Graphics.Color;
namespace Chromastein
{
public class Spritesheet
{
private int _cellWidth;
private int _cellHeight;
private List<Rectangle> Cells { get; }
internal Texture Texture { get; }
public int CellWidth
{
get => _cellWidth;
set
{
_cellWidth = value;
GenerateSourceRectangles();
}
}
public int CellHeight
{
get => _cellHeight;
set
{
_cellHeight = value;
GenerateSourceRectangles();
}
}
public int CellCount => Cells.Count;
public Spritesheet(string filePath)
{
Cells = new List<Rectangle>();
Texture = new Texture(filePath);
CellWidth = 0;
CellHeight = 0;
}
public Vector2 GetGranularXY(int cellIndex)
=> new Vector2(
Cells[cellIndex].Left / _cellWidth,
Cells[cellIndex].Top / _cellHeight
);
internal void Draw(RenderContext context, int cellIndex, Vector2 position, Vector2 scale, Vector2 origin, int rotation)
{
context.DrawTexture(Texture, position, scale, origin, rotation, Cells[cellIndex]);
}
private void GenerateSourceRectangles()
{
if (_cellWidth == 0 || _cellHeight == 0)
return;
Cells.Clear();
var totalCellsX = Texture.Width / _cellWidth;
var totalCellsY = Texture.Height / _cellHeight;
for (int y = 0; y < totalCellsY; y++)
{
for (int x = 0; x < totalCellsX; x++)
{
Cells.Add(
new Rectangle(
x * CellWidth,
y * CellHeight,
CellWidth,
CellHeight
)
);
}
}
}
}
}