-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPGEng.Buffer.cs
66 lines (58 loc) · 1.66 KB
/
CPGEng.Buffer.cs
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
/*
* Crispycat PixelGraphic Engine
* CPGEng.Buffer.cs; Buffer objects and functions
* (C) 2020 crispycat; https://github.com/crispycat0/CPGEng/LICENSE
* 2020/04/01
*/
namespace CPGEng {
public class Buffer {
private byte[] data;
public readonly uint Length;
/// <summary>Creates a new Buffer.</summary>
/// <param name="l">Length</param>
/// <param name="v">Default value</param>
public Buffer(uint l = 256, byte v = 0) {
Length = l;
data = new byte[l];
if (v > 0) for (int b = 0; b < l; b++) data[b] = v;
}
/// <summary>Create a Buffer from a byte[]</summary>
/// <param name="d">Byte[] data</param>
public Buffer(byte[] d) {
data = d;
Length = (uint)d.Length;
}
/// <summary>Create a Buffer from another Buffer</summary>
/// <param name="b">Buffer buffer</param>
public Buffer(Buffer b) {
data = (byte[])b.data.Clone();
Length = b.Length;
}
/// <summary>Get byte</summary>
/// <param name="n">Index</param>
/// <returns>byte</returns>
public byte Get(uint n = 0) {
if (n >= Length) return 0;
return data[n];
}
/// <summary>Set byte</summary>
/// <param name="n">Index</param>
/// <param name="b">Value</param>
public void Set(uint n, byte b) {
if (n >= Length) return;
data[n] = b;
}
/// <summary>Set byte</summary>
/// <param name="n">Index</param>
/// <param name="b">Value</param>
public void Set(uint n, uint b) {
if (n >= Length) return;
data[n] = (byte)((b < 256) ? b : b % 256);
}
/// <summary>Get data</summary>
/// <returns>byte[]</returns>
public byte[] Data() {
return (byte[])data.Clone();
}
}
}