forked from KorokEngine/Korok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
144 lines (114 loc) · 2.4 KB
/
data.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gfx
import (
"korok.io/korok/gfx/bk"
"unsafe"
"image/color"
)
type Color color.RGBA
var (
White = Color{0xFF, 0xFF, 0xFF, 0xFF}
Black = Color{0,0,0,0xFF}
LTGray = Color{0xCC,0xCC,0xCC, 0xFF}
Gray = Color{0x88, 0x88, 0x88, 0xFF}
DKGray = Color{0x44, 0x44, 0x44, 0xFF}
Red = Color{0xFF,0,0, 0xFF}
Green = Color{0,0xFF,0, 0xFF}
Blue = Color{0,0,0xFF, 0xFF}
Cyan = Color{0, 0xFF, 0xFF, 0xFF}
Magenta = Color{0xFF, 00, 0xFF, 0xFF}
Yellow = Color{0xFF, 0xFF, 0x00, 0xFF}
Transparent = Color{}
Opaque = Color{0xFF, 0xFF, 0xFF, 0xFF}
)
func (c Color) U32() uint32 {
return *(*uint32)(unsafe.Pointer(&c))
}
// Color value should follow byte-order: 0xAABBGGRR
func U32Color(v uint32) Color {
return *(*Color)(unsafe.Pointer(&v))
}
// PMAColor returns a pre-multiplied alpha Color.
func PMAColor(r, g, b, a uint8) Color {
f := float32(a)/255
return Color{
R:uint8(float32(r)*f),
G:uint8(float32(g)*f),
B:uint8(float32(b)*f),
A:a,
}
}
func PMAColorf(r, g, b, a float32) Color{
return Color{
R:uint8(r*a*255),
G:uint8(g*a*255),
B:uint8(b*a*255),
A:uint8(a*255),
}
}
func (c Color) RGBA() (r, g, b, a uint32) {
r = uint32(c.R)
r |= r << 8
g = uint32(c.G)
g |= g << 8
b = uint32(c.B)
b |= b << 8
a = uint32(c.A)
a |= a << 8
return
}
type CompRef struct {
Type int32
*Transform
*SpriteComp
}
type AABB struct {
x, y float32
width, height float32
}
func OverlapAB(a, b *AABB) bool {
if a.x < b.x+b.width && a.x+a.width>b.x && a.y < b.y+b.height && a.y+a.height > b.y {
return true
}
return false
}
type zOrder struct {
value int16
}
func (zo *zOrder) SetZOrder(z int16) {
zo.value = z
}
func (zo *zOrder) Z() int16 {
return zo.value
}
type batchId struct {
value uint16
}
func (b *batchId) SetBatchId(id uint16) {
b.value = id
}
func (b *batchId) BatchId() uint16 {
return b.value
}
func PackSortId(z int16, b uint16) (sid uint32) {
sid = uint32(int32(z) + 0xFFFF>>1)
sid = (sid << 16) + uint32(b)
return
}
func UnpackSortId(sortId uint32) (z int16, b uint16) {
b = uint16(sortId & 0xFFFF)
z = int16(int32(sortId>>16)-0xFFFF>>1)
return
}
// format <x,y,u,v rgba>
var P4C4 = []bk.VertexComp{
{4, bk.AttrFloat, 0, 0},
{4, bk.AttrUInt8, 16, 1},
}
// vertex struct
type PosTexColorVertex struct {
X, Y, U, V float32
RGBA uint32
}
//
var PosTexColorVertexSize = unsafe.Sizeof(PosTexColorVertex{})
var UInt16Size = unsafe.Sizeof(uint16(0))