-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
123 lines (109 loc) · 2.64 KB
/
Copy pathcache.go
File metadata and controls
123 lines (109 loc) · 2.64 KB
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
package main
import (
"runtime"
"sync/atomic"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/remeh/sizedwaitgroup"
)
func clearCaches() {
imageMu.Lock()
imageCache = make(map[imageKey]*ebiten.Image)
sheetCache = make(map[sheetKey]*ebiten.Image)
mobileCache = make(map[mobileKey]*ebiten.Image)
mobileBlendCache = make(map[mobileBlendKey]*ebiten.Image)
pictBlendCache = make(map[pictBlendKey]*ebiten.Image)
scaledImageCache = make(map[scaledImageKey]*ebiten.Image)
scaledMobileCache = make(map[scaledMobileKey]*ebiten.Image)
imageMu.Unlock()
pixelCountMu.Lock()
pixelCountCache = make(map[uint16]int)
pixelCountMu.Unlock()
soundMu.Lock()
pcmCache = make(map[uint16][]byte)
soundMu.Unlock()
if clImages != nil {
clImages.ClearCache()
}
if clSounds != nil {
clSounds.ClearCache()
}
}
var assetsPrecached = false
var precacheProgress func(done, total int)
func precacheAssets() {
for {
if (gs.precacheImages && clImages == nil) || (gs.precacheSounds && clSounds == nil) {
time.Sleep(time.Millisecond * 100)
} else {
break
}
}
var preloadMsg string
switch {
case gs.precacheImages && gs.precacheSounds:
preloadMsg = "Precaching game sounds and images..."
case gs.precacheImages:
preloadMsg = "Precaching game images..."
case gs.precacheSounds:
preloadMsg = "Precaching game sounds..."
}
if preloadMsg != "" {
consoleMessage(preloadMsg)
}
var total int
if gs.precacheImages && clImages != nil {
total += len(clImages.IDs())
}
if gs.precacheSounds && clSounds != nil {
total += len(clSounds.IDs())
}
if precacheProgress != nil {
precacheProgress(0, total)
}
var done int32
wg := sizedwaitgroup.New(runtime.NumCPU())
if gs.precacheImages && clImages != nil {
for _, id := range clImages.IDs() {
wg.Add()
go func(id uint32) {
loadSheet(uint16(id), nil, false)
if precacheProgress != nil {
n := int(atomic.AddInt32(&done, 1))
precacheProgress(n, total)
}
wg.Done()
}(id)
}
}
if gs.precacheSounds && clSounds != nil {
for _, id := range clSounds.IDs() {
wg.Add()
go func(id uint32) {
loadSound(uint16(id))
if precacheProgress != nil {
n := int(atomic.AddInt32(&done, 1))
precacheProgress(n, total)
}
wg.Done()
}(id)
}
}
wg.Wait()
if precacheProgress != nil {
precacheProgress(total, total)
}
assetsPrecached = true
var doneMsg string
switch {
case gs.precacheImages && gs.precacheSounds:
doneMsg = "All images and sounds have been loaded."
case gs.precacheImages:
doneMsg = "All images have been loaded."
case gs.precacheSounds:
doneMsg = "All sounds have been loaded."
}
if doneMsg != "" {
consoleMessage(doneMsg)
}
}