aboutsummaryrefslogtreecommitdiffstats
path: root/common/buffer/put.go
blob: 8a5064ed7b31f89ba5dfb0f46ba4de82d58c0bf4 (plain) (blame)
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
87
package buffer

import (
	"encoding/binary"
	"math"
)

func (b *Buffer) PutByte(v byte) {
	b.off += 1
	b.buf = append(b.buf, v)
}

func (b *Buffer) PutBytes(v []byte) {
	b.off += len(v)
	b.buf = append(b.buf, v...)
}

func (b *Buffer) PutInt8(v int8) {
	b.PutUint8(uint8(v))
}

func (b *Buffer) PutUint8(v uint8) {
	b.off++
	b.buf = append(b.buf, v)
}

func (b *Buffer) PutInt16(v int16) {
	b.PutUint16(uint16(v))
}

func (b *Buffer) PutUint16(v uint16) {
	b.off += 2

	tmp := make([]byte, 2)
	binary.LittleEndian.PutUint16(tmp, v)
	b.buf = append(b.buf, tmp...)
}

func (b *Buffer) PutInt32(v int32) {
	b.PutUint32(uint32(v))
}

func (b *Buffer) PutUint32(v uint32) {
	b.off += 4

	tmp := make([]byte, 4)
	binary.LittleEndian.PutUint32(tmp, v)
	b.buf = append(b.buf, tmp...)
}

func (b *Buffer) PutFloat32(v float32) {
	b.off += 4

	tmp := make([]byte, 4)
	binary.LittleEndian.PutUint32(tmp, math.Float32bits(v))
	b.buf = append(b.buf, tmp...)
}

func (b *Buffer) PutString(v string) {
	b.off += len(v) + 1

	for i := 0; i < len(v); i++ {
		b.PutByte(byte(v[i]))
	}

	b.PutByte(0)
}

func (b *Buffer) PutCoord16(v float32) {
	b.PutUint16(uint16(v * 8.0))
}

func (b *Buffer) PutCoord32(v float32) {
	b.PutFloat32(v)
}

func (b *Buffer) PutAngle8(v float32) {
	b.PutByte(byte(v / (360.0 / 256)))
}

func (b *Buffer) PutAngle16(v float32) {
	b.PutUint16(uint16(v / (360.0 / 65536)))
}

func (b *Buffer) PutAngle32(v float32) {
	b.PutFloat32(v)
}