-
Notifications
You must be signed in to change notification settings - Fork 8
/
sprite.go
134 lines (112 loc) · 3.3 KB
/
sprite.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
/*
Copyright (C) 2021 Alexander Lunsford
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"image"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/thetophatdemon/feta-feles-rebirth/vmath"
)
type Sprite struct {
subImg *ebiten.Image
matrix *ebiten.GeoM
}
func NewSprite(src image.Rectangle, ofs *vmath.Vec2f, flipH, flipV bool, orient int) *Sprite {
subImg := GetGraphics().SubImage(src).(*ebiten.Image)
return NewSpriteFromSubImg(subImg, ofs, flipH, flipV, orient)
}
func NewSpriteFromSubImg(subImg *ebiten.Image, ofs *vmath.Vec2f, flipH, flipV bool, orient int) *Sprite {
matrix := new(ebiten.GeoM)
//Perform rotation and scaling with respect to the center
hw := float64(subImg.Bounds().Dx()) / 2.0
hh := float64(subImg.Bounds().Dy()) / 2.0
matrix.Translate(-hw, -hh)
scx := 1.0
if flipH {
scx = -1.0
}
scy := 1.0
if flipV {
scy = -1.0
}
matrix.Scale(scx, scy)
matrix.Rotate(float64(orient) * math.Pi / 2.0)
matrix.Translate(hw, hh)
matrix.Translate(ofs.X, ofs.Y)
return &Sprite{subImg, matrix}
}
func CloneSprite(org *Sprite) *Sprite {
return &Sprite{
org.subImg,
org.matrix,
}
}
func CloneSprites(orgs []*Sprite) []*Sprite {
out := make([]*Sprite, len(orgs))
for i, spr := range orgs {
out[i] = CloneSprite(spr)
}
return out
}
func (spr *Sprite) Draw(target *ebiten.Image, pt *ebiten.GeoM) {
op := &ebiten.DrawImageOptions{}
op.GeoM = *spr.matrix
if pt != nil {
op.GeoM.Concat(*pt)
}
target.DrawImage(spr.subImg, op)
}
func NewSprites(ofs *vmath.Vec2f, rects ...image.Rectangle) []*Sprite {
sprites := make([]*Sprite, len(rects))
for i, rect := range rects {
sprites[i] = NewSprite(rect, ofs, false, false, 0)
}
return sprites
}
func (spr *Sprite) Flip(horz, vert bool) *Sprite {
matrix := new(ebiten.GeoM)
//Perform rotation and scaling with respect to the center
hw := float64(spr.subImg.Bounds().Dx()) / 2.0
hh := float64(spr.subImg.Bounds().Dy()) / 2.0
matrix.Translate(-hw, -hh)
scx, scy := 1.0, 1.0
if horz {
scx = -1.0
}
if vert {
scy = -1.0
}
matrix.Scale(scx, scy)
matrix.Translate(hw, hh)
matrix.Concat(*spr.matrix)
spr.matrix = matrix
return spr
}
//Creates a sprite that fills a given rectangle on the screen
func SpriteFromScaledImg(subImg *ebiten.Image, dest image.Rectangle, orient int) *Sprite {
matrix := new(ebiten.GeoM)
//Perform rotation
hw := float64(subImg.Bounds().Dx()) / 2.0
hh := float64(subImg.Bounds().Dy()) / 2.0
matrix.Translate(-hw, -hh)
matrix.Rotate(float64(orient) * math.Pi / 2.0)
matrix.Translate(hw, hh)
//Perform scaling
matrix.Scale(
float64(dest.Dx())/float64(subImg.Bounds().Dx()),
float64(dest.Dy())/float64(subImg.Bounds().Dy()))
//Offset
matrix.Translate(float64(dest.Min.X), float64(dest.Min.Y))
return &Sprite{subImg, matrix}
}