-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassets.go
368 lines (278 loc) · 8.36 KB
/
assets.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package fnf
import (
"bytes"
"embed"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
rl "github.com/gen2brain/raylib-go/raylib"
)
//go:embed assets
var EmebededAssets embed.FS
var (
ArrowsSprite Sprite
ArrowsGlowSprite Sprite
)
var UIarrowsSprite Sprite
const (
UIarrowLeftStroke = iota
UIarrowRightStroke
UIarrowLeftFill
UIarrowRightFill
UIarrowsSpriteCount
)
var SustainTex rl.Texture2D
var (
CheckBoxMark Sprite
CheckBoxBox rl.Texture2D
)
var (
GameScreenBg rl.Texture2D
MenuScreenBg rl.Texture2D
MenuScreenSimpleBg rl.Texture2D
)
var (
SongLoadingScreen rl.Texture2D
DirSelectScreen rl.Texture2D
)
var (
SplashFillSprite [2]Sprite
SplashStrokeSprite [2]Sprite
)
var HitRatingTexs [HitRatingSize]rl.Texture2D
var (
BookMarkBigTex rl.Texture2D
BookMarkSmallTex rl.Texture2D
)
var PopupBg rl.Texture2D
var (
BlackPixel rl.Texture2D
WhitePixel rl.Texture2D
)
var HitSoundAudio []byte
var DancingNoteSprite Sprite
var (
texsToUnload []rl.Texture2D
)
func LoadAssets() {
loadAssets(false)
}
func ReloadAssets() {
loadAssets(true)
}
func UnloadAssets() {
unloadAssets(false)
}
func loadAssets(isReload bool) {
unloadAssets(isReload)
loadData := func(pathStr string) []byte {
var byteArray []byte
var err error
if *FlagHotReloading {
byteArray, err = os.ReadFile(filepath.Clean(pathStr))
} else {
byteArray, err = fs.ReadFile(EmebededAssets, path.Clean(pathStr))
}
if err != nil {
ErrorLogger.Fatal(err)
}
return byteArray
}
loadTexture := func(path string, premultiply bool) rl.Texture2D {
byteArray := loadData(path)
img := rl.LoadImageFromMemory(filepath.Ext(path), byteArray, int32(len(byteArray)))
if !rl.IsImageReady(img) {
ErrorLogger.Fatalf("failed to load img : %v", path)
}
if premultiply {
rl.ImageAlphaPremultiply(img)
}
tex := rl.LoadTextureFromImage(img)
rl.GenTextureMipmaps(&tex)
rl.SetTextureFilter(tex, rl.FilterTrilinear)
if tex.ID == 0 {
ErrorLogger.Fatalf("failed to load texture from img : %v", path)
}
texsToUnload = append(texsToUnload, tex)
rl.UnloadImage(img)
return tex
}
loadSprite := func(jsonPath string, imgPath string, premultiply bool) Sprite {
jsonBytes := loadData(jsonPath)
buffer := bytes.NewBuffer(jsonBytes)
var err error
var sprite Sprite
if sprite, err = ParseSpriteJsonMetadata(buffer); err != nil {
ErrorLogger.Fatal(err)
}
sprite.Texture = loadTexture(imgPath, premultiply)
return sprite
}
// =============================
// load reloadable assets
// =============================
// load fnf arrows texture
ArrowsSprite = loadSprite("assets/arrows.json", "assets/arrows.png", true)
// load fnf arrows glow texture
{
glowTex := loadTexture("assets/arrows-glow.png", true)
ArrowsGlowSprite.Texture = glowTex
ArrowsGlowSprite.Count = int(NoteDirSize)
ArrowsGlowSprite.Height = f32(glowTex.Height)
// NOTE : same goes for glow arrows
ArrowsGlowSprite.Width = f32(glowTex.Width) / f32(NoteDirSize)
}
// load ui arrows texture
{
uiArrowsTex := loadTexture("assets/ui-arrows.png", true)
UIarrowsSprite.Texture = uiArrowsTex
UIarrowsSprite.Count = UIarrowsSpriteCount
UIarrowsSprite.Height = f32(uiArrowsTex.Height)
// NOTE : same also goes for ui arrows
UIarrowsSprite.Width = f32(uiArrowsTex.Width) / UIarrowsSpriteCount
}
SustainTex = loadTexture("assets/sustain-bar.png", true)
if SustainTex.Width > SustainTex.Height {
ErrorLogger.Printf("SustainTex width(%v) is bigger than height(%v)",
SustainTex.Width, SustainTex.Height)
}
// load checkbox sprite
{
CheckBoxBox = loadTexture("assets/checkbox-box.png", true)
CheckBoxMark = loadSprite("assets/checkbox-sprites.json", "assets/checkbox-sprites.png", true)
}
// load splash fill sprite
for i := range 2 {
SplashFillSprite[i] = loadSprite(
fmt.Sprintf("assets/splash-fill%d.json", i+1),
fmt.Sprintf("assets/splash-fill%d.png", i+1), true,
)
}
// load splash stroke sprite
for i := range 2 {
SplashStrokeSprite[i] = loadSprite(
fmt.Sprintf("assets/splash-stroke%d.json", i+1),
fmt.Sprintf("assets/splash-stroke%d.png", i+1), true,
)
}
// check if splash fill and stroke have the same size and sprite count
for a := range 2 {
for b := range 2 {
if SplashFillSprite[a].Count != SplashStrokeSprite[b].Count {
ErrorLogger.Fatal(
fmt.Errorf("SplashFillSprite%d and SplashStrokeSprite%d have different sprite count", a, b))
}
if SplashFillSprite[a].Width != SplashStrokeSprite[b].Width {
ErrorLogger.Fatal(
fmt.Errorf("SplashFillSprite%d and SplashStrokeSprite%d have different width", a, b))
}
if SplashFillSprite[a].Height != SplashStrokeSprite[b].Height {
ErrorLogger.Fatal(
fmt.Errorf("SplashFillSprite%d and SplashStrokeSprite%d have different height", a, b))
}
}
}
BookMarkBigTex = loadTexture("assets/bookmark-big.png", true)
BookMarkSmallTex = loadTexture("assets/bookmark-small.png", true)
GameScreenBg = loadTexture("assets/game-background.png", true)
MenuScreenBg = loadTexture("assets/menu-background.png", true)
MenuScreenSimpleBg = loadTexture("assets/menu-background-simple.png", true)
SongLoadingScreen = loadTexture("assets/song-loading-screen.png", true)
DirSelectScreen = loadTexture("assets/directory-select-screen.png", true)
ratingImgPaths := [HitRatingSize]string{
"assets/bad.png",
"assets/good.png",
"assets/sick.png",
}
for r := FnfHitRating(0); r < HitRatingSize; r++ {
HitRatingTexs[r] = loadTexture(ratingImgPaths[r], true)
}
PopupBg = loadTexture("assets/popup-bg.png", true)
// load dancing notes
DancingNoteSprite = loadSprite("assets/dancing-note.json", "assets/dancing-note.png", true)
// create black pixel
blackPixelImg := rl.GenImageColor(2, 2, ToRlColor(FnfColor{0, 0, 0, 255}))
BlackPixel = rl.LoadTextureFromImage(blackPixelImg)
texsToUnload = append(texsToUnload, BlackPixel)
// create white pixel
whitePixelImg := rl.GenImageColor(2, 2, ToRlColor(FnfColor{255, 255, 255, 255}))
WhitePixel = rl.LoadTextureFromImage(whitePixelImg)
texsToUnload = append(texsToUnload, WhitePixel)
// =============================
// load unreloadable assets
// =============================
if !isReload {
// load hit sound
loadCustomHitSound := func() bool {
var err error
var dirPath string
if dirPath, err = RelativePath("./"); err != nil {
ErrorLogger.Printf("failed to load custom hitsound %v", err)
return false
}
var dirEntries []os.DirEntry
if dirEntries, err = os.ReadDir(dirPath); err != nil {
ErrorLogger.Printf("failed to load custom hitsound %v", err)
return false
}
var foundHitSoundCandidate bool = false
for _, entry := range dirEntries {
if mode := entry.Type(); !(mode.IsRegular() && !mode.IsDir()) {
continue
}
nameLow := strings.ToLower(entry.Name())
if nameLow == "hit-sound.ogg" ||
nameLow == "hit-sound.mp3" ||
nameLow == "hit-sound.wav" {
foundHitSoundCandidate = true
var audioErr error
var audioFile []byte
if audioFile, audioErr = os.ReadFile(filepath.Join(dirPath, entry.Name())); audioErr != nil {
ErrorLogger.Printf("failed to decode custom hitsound %v: %v", entry.Name(), audioErr)
continue
}
var decoder AudioDecoder
if decoder, audioErr = NewAudioDeocoder(audioFile, filepath.Ext(nameLow)); audioErr != nil {
ErrorLogger.Printf("failed to decode custom hitsound %v: %v", entry.Name(), audioErr)
continue
}
var audio []byte
if audio, audioErr = io.ReadAll(decoder); audioErr != nil {
ErrorLogger.Printf("failed to decode custom hitsound %v: %v", entry.Name(), audioErr)
continue
}
HitSoundAudio = audio
return true
}
}
if foundHitSoundCandidate {
DisplayAlert("failed to load custom hitsound")
}
return false
}
if !loadCustomHitSound() {
const hitSoundPath = "assets/hit-sound.ogg"
audioFile := loadData(hitSoundPath)
var err error
var decoder AudioDecoder
if decoder, err = NewAudioDeocoder(audioFile, filepath.Ext(hitSoundPath)); err != nil {
ErrorLogger.Fatalf("failed to create decoder %v", err)
}
if HitSoundAudio, err = io.ReadAll(decoder); err != nil {
ErrorLogger.Fatalf("failed to decode audio %v", err)
}
}
}
}
func unloadAssets(isReload bool) {
for _, tex := range texsToUnload {
if tex.ID > 0 {
rl.UnloadTexture(tex)
}
}
texsToUnload = texsToUnload[:0]
}