@@ -19,16 +19,16 @@ type Ebiten struct {
19
19
reverseY bool
20
20
texts []* ebitenText
21
21
batches []* ebitenBatch
22
+ buffer * ebiten.Image
22
23
}
23
24
24
- func (e Ebiten ) Clear () {
25
- e .texts = make ([]* ebitenText , 0 )
26
- e .batches = make ([]* ebitenBatch , 0 )
25
+ func (e * Ebiten ) initBuffer (width int , height int ) {
26
+ e .buffer = ebiten .NewImage (width , height )
27
27
}
28
28
29
- func (e * Ebiten ) NewText (face font.Face , x int , y int , width int , height int , scale float64 , alignment alignment ) Text {
29
+ func (e * Ebiten ) NewText (face font.Face , x int , y int , width int , height int , _ float64 , alignment alignment ) Text {
30
30
buf := bytes.Buffer {}
31
- return & ebitenText {
31
+ txt := & ebitenText {
32
32
x : x ,
33
33
y : y ,
34
34
width : width ,
@@ -38,9 +38,11 @@ func (e *Ebiten) NewText(face font.Face, x int, y int, width int, height int, sc
38
38
graphics : e ,
39
39
alignment : alignment ,
40
40
}
41
+ e .texts = append (e .texts , txt )
42
+ return txt
41
43
}
42
44
43
- func (e * Ebiten ) RenderTexts (screen * ebiten.Image ) {
45
+ func (e * Ebiten ) RenderTexts (target * ebiten.Image ) {
44
46
for _ , t := range e .texts {
45
47
bound := text .BoundString (t .fontFace , t .textContent )
46
48
width := float64 (bound .Max .X - bound .Min .X )
@@ -49,17 +51,19 @@ func (e *Ebiten) RenderTexts(screen *ebiten.Image) {
49
51
x := float64 (t .x ) + float64 (t .width )/ 2 - width / 2
50
52
y := float64 (t .y ) + float64 (t .height )/ 2 - height / 2
51
53
52
- adjustedY := adjustY (e .reverseY , screen , int (y ), 0 )
54
+ adjustedY := adjustY (e .reverseY , target , int (y ), 0 )
53
55
54
56
switch t .alignment {
55
57
case AlignCenter :
56
- text .Draw (screen , t .textContent , t .fontFace , int (x ), adjustedY , color .White )
58
+ text .Draw (target , t .textContent , t .fontFace , int (x ), adjustedY , color .White )
57
59
}
58
60
}
59
61
}
60
62
61
63
func (e * Ebiten ) StartNewBatch (spriteSheet image.Image ) Batch {
62
- return newEbitenBatch (ebiten .NewImageFromImage (spriteSheet ), e )
64
+ batch := newEbitenBatch (ebiten .NewImageFromImage (spriteSheet ), e )
65
+ e .batches = append (e .batches , batch )
66
+ return batch
63
67
}
64
68
65
69
func (e Ebiten ) SetCursorVisible (isVisible bool ) {
@@ -72,9 +76,10 @@ func (e Ebiten) SetCursorVisible(isVisible bool) {
72
76
73
77
func (e * Ebiten ) Render (screen * ebiten.Image ) {
74
78
for _ , batch := range e .batches {
75
- batch .renderBatch (screen )
79
+ batch .renderBatch (e . buffer )
76
80
}
77
- e .RenderTexts (screen )
81
+ e .RenderTexts (e .buffer )
82
+ screen .DrawImage (e .buffer , nil )
78
83
}
79
84
80
85
func NewEbiten (reverseY bool ) Ebiten {
@@ -102,11 +107,9 @@ func (t *ebitenText) Write(p []byte) (int, error) {
102
107
}
103
108
104
109
func (t * ebitenText ) Draw () {
105
- t .buf .Flush ()
110
+ _ = t .buf .Flush ()
106
111
buf , _ := ioutil .ReadAll (t .buf )
107
112
t .textContent = string (buf )
108
-
109
- t .graphics .texts = append (t .graphics .texts , t )
110
113
}
111
114
112
115
var _ Batch = (* ebitenBatch )(nil )
@@ -117,11 +120,7 @@ type ebitenBatch struct {
117
120
spritesDrawn []* spriteDrawn
118
121
}
119
122
120
- func (e * ebitenBatch ) RenderBatch () {
121
- e .ebiten .batches = append (e .ebiten .batches , e )
122
- }
123
-
124
- func (e * ebitenBatch ) renderBatch (screen * ebiten.Image ) {
123
+ func (e * ebitenBatch ) renderBatch (target * ebiten.Image ) {
125
124
sort .SliceStable (e .spritesDrawn , func (i , j int ) bool {
126
125
if e .ebiten .reverseY {
127
126
return e .spritesDrawn [i ].z > e .spritesDrawn [j ].z
@@ -151,13 +150,13 @@ func (e *ebitenBatch) renderBatch(screen *ebiten.Image) {
151
150
op .GeoM .Scale (spriteDrawn .scale , spriteDrawn .scale )
152
151
153
152
scaledHeight := float64 (spriteDrawn .imageBound .Height ) * spriteDrawn .scale
154
- adjustedY := adjustY (e .ebiten .reverseY , screen , spriteDrawn .y , scaledHeight )
153
+ adjustedY := adjustY (e .ebiten .reverseY , target , spriteDrawn .y , scaledHeight )
155
154
op .GeoM .Translate (
156
155
float64 (spriteDrawn .x ),
157
156
float64 (adjustedY ),
158
157
)
159
158
160
- screen .DrawImage (e .spriteSheet .SubImage (bound ).(* ebiten.Image ), op )
159
+ target .DrawImage (e .spriteSheet .SubImage (bound ).(* ebiten.Image ), op )
161
160
}
162
161
e .spritesDrawn = make ([]* spriteDrawn , 0 )
163
162
}
0 commit comments