-
Notifications
You must be signed in to change notification settings - Fork 1
/
scene_plasma.z80
205 lines (169 loc) · 3.56 KB
/
scene_plasma.z80
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
INCLUDE "Hardware.inc"
INCLUDE "rgbgrafx/rgbgrafx.inc"
TIME EQU $90
Section "Plasma scene",CODE
scene_plasma::
; Set all palette colors to black for the duration of scene setup
RGBG_WaitForVRAM
ld A, $FF
ld [rBGP], A
; Set time offset to 0
ldh [TIME], A
; Set LCDC register initial values
ld A, (LCDCF_ON | LCDCF_WINOFF | LCDCF_BG8000 | LCDCF_BG9800 | LCDCF_BGON)
ld [rLCDC], A
di
; Clear interrupt handlers
call initialize_interrupts
; Enable VBlank and LCDC interrupts
ld A, (IEF_LCDC | IEF_VBLANK)
ld [rIE], A
ei
; Reset background scroll registers
xor A
ld [rSCX], A
ld [rSCY], A
; Load tiles into VRAM
call load_tiles
; Draw one screen before changing palette
call update_plasma_buffer
RGBG_WaitVBL
ld HL, $9800
call write_plasma_to_vram
; Reset color palette to default
RGBG_WaitForVRAM
ld A, %11100100
ld [rBGP], A
ld [rOBP0], A
ld [rOBP1], A
.loop:
; Update plasma buffer contents
call update_plasma_buffer
RGBG_WaitVBL
ld HL, $9800
call write_plasma_to_vram
; Increment time offset
ldh A, [TIME]
inc A
ldh [TIME], A
cp 255 ; Start fadeout after 256 frames.
jr nz, .loop
.fade1_loop:
; Update plasma buffer contents
call update_plasma_buffer
RGBG_WaitVBL
ld HL, $9800
call write_plasma_to_vram
ldh A, [TIME]
inc A
ldh [TIME], A
cp 5
jr nz, .fade1_loop
call fade_to_black
.fade2_loop:
; Update plasma buffer contents
call update_plasma_buffer
RGBG_WaitVBL
ld HL, $9800
call write_plasma_to_vram
ldh A, [TIME]
inc A
ldh [TIME], A
cp 10
jr nz, .fade2_loop
call fade_to_black
.fade3_loop:
call update_plasma_buffer
RGBG_WaitVBL
ld HL, $9800
call write_plasma_to_vram
ldh A, [TIME]
inc A
ldh [TIME], A
cp 15
jr nz, .fade3_loop
call fade_to_black
ret
update_plasma_buffer:
ld HL, plasma_buffer
ld D, 18 ; Row counter
.row_loop:
ld B, 20 ; Column counter
.tile_loop:
push HL ; Store address to current place in plasma buffer
; Point HL to sine lut
ld H, $31
; The magic happens here!
ldh A, [TIME]
ld L, A
ld A, [HL]
add B
add 10
ld L, A
ld A, [HL] ; sin(x + time + 10)
ld E, A
ldh A, [TIME]
add D
add 100
ld L, A
ld A, [HL]
add E ; sin(x + time + 10) + sin(y + time + 100)
ld E, A
ldh A, [TIME]
add B
sub D
ld L, A
ld A, [HL]
add E ; sin(x + time + 10) + sin(y + time + 100) + sin(x - y + time)
; The magic ends here!
and 63 ; Keep result in range 0-63
; Store result A in plasma buffer and increment HL
pop HL
ldi [HL], A
dec B
jr nz, .tile_loop
dec D
jr nz, .row_loop
ret
write_plasma_to_vram:
; Code writes 18 lines (tiles 0-19) to [HL] from plasma_data
ld DE, plasma_buffer
ld B, 18 ; Row counter
.row_loop:
push BC
ld B, 10 ; Run 10 times, since inner loop does two iterations (=20 tiles)
.tile_loop:
RGBG_WaitForVRAM ; Wait for VRAM to become available
ld A, [DE]
ldi [HL], A
inc DE
ld A, [DE]
ldi [HL], A
inc DE
dec B ; Decrement column counter
jr nz, .tile_loop
ld BC, $000C ; Skip $C non-visible tiles on row
add HL, BC
pop BC
dec B ; Decrement row counter
jr nz, .row_loop
ret
load_tiles:
RGBG_WaitForVRAM
ld A, $80
ld [RGBG_tileset], A
ld BC, plasma_tiles ; Plasma tile data address
ld D, 64 ; Number of tiles
ld E, 0 ; Offset required by RGBG
call RGBG_LoadTiles
ret
INCLUDE "imagedata/plasma_tiles.inc"
Section "Plasma sine LUT",HOME[$3100]
plasma_sine_lut:
ANGLE=0.0
REPT 256
DB (MUL(32.0, SIN(ANGLE))+32.0)>>16
ANGLE=ANGLE+1024.0
ENDR
Section "Plasma variables",BSS
plasma_buffer: DS 360