-
Notifications
You must be signed in to change notification settings - Fork 2
/
screenfade.monkey2
188 lines (162 loc) · 4.32 KB
/
screenfade.monkey2
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
Namespace diddy2.screenfade
Class ScreenFade
Private
Field _fadeColor:Color = New Color(0, 0, 0)
Field _currentColor:Color = New Color(1, 1, 1)
Field _width:Int
Field _height:Int
Field _active:Bool
Field _ratio:Float = 0
Field _fadeType:Int
Field _fadeTime:Float = 1
Field _counter:Float
Field _fadeMusic:Bool
Field _fadeSound:Bool
Field _startTime:Int
Field _stopMusicOnFadeIn:Bool
Field _debug:Bool = False
Public
Const FADE_IN:Int = 0
Const FADE_OUT:Int = 1
Const EFFECT_FADE:Int = 0
Const EFFECT_PIXEL_FILL:Int = 1
Global Effect:Int = EFFECT_FADE
Global DefaultFadeTimeMs:Float = 500
Field pixelCount:Int
Field pixels:Int[,]
Property Active:Bool()
Return _active
End
Property FadeColor:Color()
Return _fadeColor
Setter(col:Color)
_fadeColor = col
End
Method New(width:Int, height:Int)
Self._width = width
Self._height = height
Self.pixels = New Int[Self._width, Self._height]
End
Method Start(fadeType:Int = FADE_IN, fadeTimeMs:Float = DefaultFadeTimeMs, fadeSound:Bool = True, fadeMusic:Bool = True, stopMusicOnFadeIn:Bool = False)
If _active Then Return
_active = True
SetFadeTime(fadeTimeMs)
_fadeType = fadeType
_fadeMusic = fadeMusic
_fadeSound = fadeSound
_stopMusicOnFadeIn = stopMusicOnFadeIn
If _fadeType = FADE_OUT
For Local y:Int = 0 Until _height
For Local x:Int = 0 Until _width
pixels[x, y] = 0
Next
Next
_ratio = 1
If Not DiddyApp.GetInstance().Window.NextScreen
Print "NextScreen is NULL, set NextScreen before calling Start with FADE_OUT"
App.Terminate()
End
Else
For Local y:Int = 0 Until _height
For Local x:Int = 0 Until _width
pixels[x, y] = 1
Next
Next
_ratio = 0
End
pixelCount = 0
_counter = 0
_startTime = Millisecs()
End
Method SetFadeTime(ms:Float)
Local gt:Float = DiddyApp.GetInstance().Window.GameTime.CalcFrameTime(ms)
_fadeTime = gt
End
Method Update(fixedRate:Float)
If Not _active Return
_counter += 1 + fixedRate
CalcRatio()
If Effect = EFFECT_PIXEL_FILL
Local completeRatioAmount:Bool = False
Local screenPixelCount:Float = _width * _height - 0.1
While Not completeRatioAmount
For Local i:Int = 0 To 1200
Local x:Int = Rnd(0, _width)
Local y:Int = Rnd(0, _height)
If _fadeType = FADE_OUT
If pixels[x, y] = 0
pixels[x, y] = 1
pixelCount +=1
End
Else
If pixels[x, y] = 1
pixels[x, y] = 0
pixelCount +=1
End
End
End
If pixelCount > screenPixelCount Then pixelCount = screenPixelCount
Local r:Float = pixelCount / screenPixelCount
r = Clamp(r, 0.0, 1.0)
If _fadeType = FADE_OUT
r = 1 - r
If r <= _ratio Or FloatEqual(r, _ratio, 0.01) Then completeRatioAmount = True
Else
If r >= _ratio Or FloatEqual(r, _ratio, 0.01) Then completeRatioAmount = True
End
End
End
If _fadeSound
For Local i:Int = 0 Until ChannelManager.MAX_CHANNELS
DiddyApp.GetInstance().ChannelManager.SetChannelVolume(_ratio * DiddyApp.GetInstance().SoundVolume, i)
Next
End
If _fadeMusic
DiddyApp.GetInstance().ChannelManager.SetMusicVolume(_ratio * DiddyApp.GetInstance().MusicVolume)
End
If _stopMusicOnFadeIn And _fadeType = FADE_IN
DiddyApp.GetInstance().ChannelManager.StopMusic()
End
If _counter > _fadeTime
_active = False
If _debug
Local totalTime := Millisecs() - _startTime
Print "Screen Fade TotalTime = " + totalTime
End
If _fadeType = FADE_OUT
DiddyApp.GetInstance().GetCurrentScreen().PostFadeOut(_fadeSound, _fadeMusic,_stopMusicOnFadeIn)
Else
DiddyApp.GetInstance().GetCurrentScreen().PostFadeIn()
End
End
End
Method CalcRatio()
_ratio = _counter / _fadeTime
_ratio = Clamp(_ratio, 0.0, 1.0)
If _fadeType = FADE_OUT
_ratio = 1 - _ratio
End
End
Method Render(canvas:Canvas)
If Not _active Return
Select Effect
Case EFFECT_FADE
canvas.Color = _fadeColor
canvas.Alpha = 1 - _ratio
canvas.DrawRect(0, 0, _width, _height)
canvas.Alpha = 1
canvas.Color = _currentColor
Case EFFECT_PIXEL_FILL
canvas.Color = _fadeColor
canvas.Alpha = 1
For Local y:Int = 0 Until _height
For Local x:Int = 0 Until _width
If pixels[x, y] = 1 Then
canvas.DrawRect(x, y, 2, 2)
End
Next
Next
canvas.Color = _currentColor
End
End
End