-
Notifications
You must be signed in to change notification settings - Fork 107
/
StrictLoads.bb
350 lines (287 loc) · 9.31 KB
/
StrictLoads.bb
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
; ID: 2975
; Author: RifRaf, further modified by MonocleBios
; Date: 2012-09-11 11:44:22
; Title: Safe Loads (b3d) ;strict loads sounds more appropriate IMO
; Description: Get the missing filename reported
;safe loads for mav trapping media issues
;basic wrapper functions that check to make sure that the file exists before attempting to load it, raises an RTE if it doesn't
;more informative alternative to MAVs outside of debug mode, makes it immiediately obvious whether or not someone is loading resources
;likely to cause more crashes than 'clean' CB, as this prevents anyone from loading any assets that don't exist, regardless if they are ever used
;added zero checks since blitz load functions return zero sometimes even if the filetype exists
Function LoadImage_Strict(file$)
If FileType(file$)<>1 Then RuntimeError "Image " + Chr(34) + file$ + Chr(34) + " missing. "
tmp = LoadImage(file$)
Return tmp
;attempt to load the image again
If tmp = 0 Then tmp2 = LoadImage(file)
DebugLog "Attempting to load again: "+file
Return tmp2
End Function
Type Sound
Field internalHandle%
Field name$
Field channels%[32]
Field releaseTime%
End Type
Function AutoReleaseSounds()
Local snd.Sound
For snd.Sound = Each Sound
Local tryRelease% = True
For i = 0 To 31
If snd\channels[i] <> 0 Then
If ChannelPlaying(snd\channels[i]) Then
tryRelease = False
snd\releaseTime = MilliSecs2()+5000
Exit
EndIf
EndIf
Next
If tryRelease Then
If snd\releaseTime < MilliSecs2() Then
If snd\internalHandle <> 0 Then
FreeSound snd\internalHandle
snd\internalHandle = 0
EndIf
EndIf
EndIf
Next
End Function
Function PlaySound_Strict%(sndHandle%)
Local snd.Sound = Object.Sound(sndHandle)
If snd <> Null Then
Local shouldPlay% = True
For i = 0 To 31
If snd\channels[i] <> 0 Then
If Not ChannelPlaying(snd\channels[i]) Then
If snd\internalHandle = 0 Then
If FileType(snd\name) <> 1 Then
CreateConsoleMsg("Sound " + Chr(34) + snd\name + Chr(34) + " not found.")
If ConsoleOpening
ConsoleOpen = True
EndIf
Else
If EnableSFXRelease Then snd\internalHandle = LoadSound(snd\name)
EndIf
If snd\internalHandle = 0 Then
CreateConsoleMsg("Failed to load Sound: " + Chr(34) + snd\name + Chr(34))
If ConsoleOpening
ConsoleOpen = True
EndIf
EndIf
EndIf
If ConsoleFlush Then
snd\channels[i] = PlaySound(ConsoleFlushSnd)
Else
snd\channels[i] = PlaySound(snd\internalHandle)
EndIf
ChannelVolume snd\channels[i],SFXVolume#
snd\releaseTime = MilliSecs2()+5000 ;release after 5 seconds
Return snd\channels[i]
EndIf
Else
If snd\internalHandle = 0 Then
If FileType(snd\name) <> 1 Then
CreateConsoleMsg("Sound " + Chr(34) + snd\name + Chr(34) + " not found.")
If ConsoleOpening
ConsoleOpen = True
EndIf
Else
If EnableSFXRelease Then snd\internalHandle = LoadSound(snd\name)
EndIf
If snd\internalHandle = 0 Then
CreateConsoleMsg("Failed to load Sound: " + Chr(34) + snd\name + Chr(34))
If ConsoleOpening
ConsoleOpen = True
EndIf
EndIf
EndIf
If ConsoleFlushSnd Then
snd\channels[i] = PlaySound(ConsoleFlushSnd)
Else
snd\channels[i] = PlaySound(snd\internalHandle)
EndIf
ChannelVolume snd\channels[i],SFXVolume#
snd\releaseTime = MilliSecs2()+5000 ;release after 5 seconds
Return snd\channels[i]
EndIf
Next
EndIf
Return 0
End Function
Function LoadSound_Strict(file$)
Local snd.Sound = New Sound
snd\name = file
snd\internalHandle = 0
snd\releaseTime = 0
If (Not EnableSFXRelease) Then
If snd\internalHandle = 0 Then
snd\internalHandle = LoadSound(snd\name)
EndIf
EndIf
Return Handle(snd)
End Function
Function FreeSound_Strict(sndHandle%)
Local snd.Sound = Object.Sound(sndHandle)
If snd <> Null Then
If snd\internalHandle <> 0 Then
FreeSound snd\internalHandle
snd\internalHandle = 0
EndIf
Delete snd
EndIf
End Function
Type Stream
Field sfx%
Field chn%
End Type
Function StreamSound_Strict(file$,volume#=1.0,custommode=Mode)
If FileType(file$)<>1
CreateConsoleMsg("Sound " + Chr(34) + file$ + Chr(34) + " not found.")
If ConsoleOpening
ConsoleOpen = True
EndIf
Return 0
EndIf
Local st.Stream = New Stream
st\sfx = FSOUND_Stream_Open(file$,custommode,0)
If st\sfx = 0
CreateConsoleMsg("Failed to stream Sound (returned 0): " + Chr(34) + file$ + Chr(34))
If ConsoleOpening
ConsoleOpen = True
EndIf
Return 0
EndIf
st\chn = FSOUND_Stream_Play(FreeChannel,st\sfx)
If st\chn = -1
CreateConsoleMsg("Failed to stream Sound (returned -1): " + Chr(34) + file$ + Chr(34))
If ConsoleOpening
ConsoleOpen = True
EndIf
Return -1
EndIf
FSOUND_SetVolume(st\chn,volume*255)
FSOUND_SetPaused(st\chn,False)
Return Handle(st)
End Function
Function StopStream_Strict(streamHandle%)
Local st.Stream = Object.Stream(streamHandle)
If st = Null
CreateConsoleMsg("Failed to stop stream Sound: Unknown Stream")
Return
EndIf
If st\chn=0 Or st\chn=-1
CreateConsoleMsg("Failed to stop stream Sound: Return value "+st\chn)
Return
EndIf
FSOUND_StopSound(st\chn)
FSOUND_Stream_Stop(st\sfx)
FSOUND_Stream_Close(st\sfx)
Delete st
End Function
Function SetStreamVolume_Strict(streamHandle%,volume#)
Local st.Stream = Object.Stream(streamHandle)
If st = Null
CreateConsoleMsg("Failed to set stream Sound volume: Unknown Stream")
Return
EndIf
If st\chn=0 Or st\chn=-1
CreateConsoleMsg("Failed to set stream Sound volume: Return value "+st\chn)
Return
EndIf
FSOUND_SetVolume(st\chn,volume*255.0)
FSOUND_SetPaused(st\chn,False)
End Function
Function SetStreamPaused_Strict(streamHandle%,paused%)
Local st.Stream = Object.Stream(streamHandle)
If st = Null
CreateConsoleMsg("Failed to pause/unpause stream Sound: Unknown Stream")
Return
EndIf
If st\chn=0 Or st\chn=-1
CreateConsoleMsg("Failed to pause/unpause stream Sound: Return value "+st\chn)
Return
EndIf
FSOUND_SetPaused(st\chn,paused)
End Function
Function IsStreamPlaying_Strict(streamHandle%)
Local st.Stream = Object.Stream(streamHandle)
If st = Null
CreateConsoleMsg("Failed to find stream Sound: Unknown Stream")
Return
EndIf
If st\chn=0 Or st\chn=-1
CreateConsoleMsg("Failed to find stream Sound: Return value "+st\chn)
Return
EndIf
Return FSOUND_IsPlaying(st\chn)
End Function
Function SetStreamPan_Strict(streamHandle%,pan#)
Local st.Stream = Object.Stream(streamHandle)
If st = Null
CreateConsoleMsg("Failed to find stream Sound: Unknown Stream")
Return
EndIf
If st\chn=0 Or st\chn=-1
CreateConsoleMsg("Failed to find stream Sound: Return value "+st\chn)
Return
EndIf
;-1 = Left = 0
;0 = Middle = 127.5 (127)
;1 = Right = 255
Local fmod_pan% = 0
fmod_pan% = Int((255.0/2.0)+((255.0/2.0)*pan#))
FSOUND_SetPan(st\chn,fmod_pan%)
End Function
Function UpdateStreamSoundOrigin(streamHandle%,cam%,entity%,range#=10,volume#=1.0)
;Local st.Stream = Object.Stream(streamHandle)
range# = Max(range,1.0)
If volume>0 Then
Local dist# = EntityDistance(cam, entity) / range#
If 1 - dist# > 0 And 1 - dist# < 1 Then
Local panvalue# = Sin(-DeltaYaw(cam,entity))
SetStreamVolume_Strict(streamHandle,volume#*(1-dist#)*SFXVolume#)
SetStreamPan_Strict(streamHandle,panvalue)
Else
SetStreamVolume_Strict(streamHandle,0.0)
EndIf
Else
If streamHandle <> 0 Then
SetStreamVolume_Strict(streamHandle,0.0)
EndIf
EndIf
End Function
Function LoadMesh_Strict(File$,parent=0)
If FileType(File$) <> 1 Then RuntimeError "3D Mesh " + File$ + " not found."
tmp = LoadMesh(File$, parent)
If tmp = 0 Then RuntimeError "Failed to load 3D Mesh: " + File$
Return tmp
End Function
Function LoadAnimMesh_Strict(File$,parent=0)
DebugLog File
If FileType(File$) <> 1 Then RuntimeError "3D Animated Mesh " + File$ + " not found."
tmp = LoadAnimMesh(File$, parent)
If tmp = 0 Then RuntimeError "Failed to load 3D Animated Mesh: " + File$
Return tmp
End Function
;don't use in LoadRMesh, as Reg does this manually there. If you wanna fuck around with the logic in that function, be my guest
Function LoadTexture_Strict(File$,flags=1)
If FileType(File$) <> 1 Then RuntimeError "Texture " + File$ + " not found."
tmp = LoadTexture(File$, flags+(256*(EnableVRam=True)))
If tmp = 0 Then RuntimeError "Failed to load Texture: " + File$
Return tmp
End Function
Function LoadBrush_Strict(file$,flags,u#=1.0,v#=1.0)
If FileType(file$)<>1 Then RuntimeError "Brush Texture " + file$ + "not found."
tmp = LoadBrush(file$, flags, u, v)
If tmp = 0 Then RuntimeError "Failed to load Brush: " + file$
Return tmp
End Function
Function LoadFont_Strict(file$="Tahoma", height=13, bold=0, italic=0, underline=0)
If FileType(file$)<>1 Then RuntimeError "Font " + file$ + " not found."
tmp = LoadFont(file, height, bold, italic, underline)
If tmp = 0 Then RuntimeError "Failed to load Font: " + file$
Return tmp
End Function
;~IDEal Editor Parameters:
;~F#F#34#3B
;~C#Blitz3D