Add tools/ and manual/, move sources to src/
[dpadhero2.git] / src / common / fade.asm
blob3eca519265b7d7536ff44012d62f5591dbd104d3
2 ; Copyright (C) 2004, 2005 Kent Hansen.
4 ; This file is part of Neotoxin.
6 ; Neotoxin is free software; you can redistribute it and/or modify
7 ; it under the terms of the GNU General Public License as published by
8 ; the Free Software Foundation; either version 2 of the License, or
9 ; (at your option) any later version.
11 ; Neotoxin is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
16 ; You should have received a copy of the GNU General Public License
17 ; along with this program; if not, write to the Free Software
18 ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ; Description:
22 ; Routines for fading in/out the palette, from/to black/white.
23 ; Recipe for use:
24 ; 1. Load the palette: see load_palette in palette.asm.
25 ; 2. Set the range of palette entries to fade (set_fade_range),
26 ; set the delay (set_fade_delay).
27 ; 3. call one of the start_fade_* functions.
29 .include "tablecall.h"
31 .dataseg
33 ; private variables
34 temp_palette .byte[32]
35 counter .byte
36 fade_type .byte
37 pal_index_lo .byte
38 pal_index_hi .byte
39 frames_per_step .byte
41 .codeseg
43 ; exported API
44 .public set_fade_range
45 .public set_fade_delay
46 .public start_fade_from_black
47 .public start_fade_from_white
48 .public start_fade_to_black
49 .public start_fade_to_white
50 .public fade_out_step
51 .public fade_in_step
52 .public palette_to_temp_palette
53 .public palette_fade_in_progress
55 ; external symbols
56 .extrn palette:byte
57 .extrn ppu_buffer:byte
58 .extrn start_palette_ppu_string:proc
59 .extrn end_ppu_string:proc
60 .extrn table_call:proc
61 .extrn start_timer:proc
62 .extrn set_timer_callback:proc
64 .proc palette_fade_in_progress
65 lda counter
66 rts
67 .endp
69 ; Sets the range of palette entries affected by fade routines.
70 ; Params: A, Y = range
71 .proc set_fade_range
72 sta pal_index_lo
73 sty pal_index_hi
74 rts
75 .endp
77 ; Sets the # of frames per step during fading.
78 ; Params: A = # of frames per step
79 .proc set_fade_delay
80 sta frames_per_step
81 rts
82 .endp
84 .proc fade_out_step
85 jsr fade_to_black_step
86 jsr temp_palette_to_ppu_buffer
87 rts
88 .endp
90 .proc fade_in_step
91 jsr fade_from_black_step
92 jsr temp_palette_to_ppu_buffer
93 rts
94 .endp
96 ;-----------------------------------------------------------------------------
98 ; Does custom initialization for the selected fade type.
99 ; Params: None
100 fade_init:
101 jsr palette_to_temp_palette
102 lda fade_type
103 jsr table_call
104 TC_SLOT fade_from_black_init
105 TC_SLOT fade_from_white_init
106 TC_SLOT fade_to_black_init
107 TC_SLOT fade_to_white_init
109 fade_from_black_init:
110 ldy pal_index_hi
111 - lda temp_palette,y
112 and #$0F ; color intensity = 0
113 sta temp_palette,y
115 bmi +
116 cpy pal_index_lo
117 bcs -
118 + rts
120 fade_from_white_init:
121 ldy pal_index_hi
122 - lda temp_palette,y
123 ora #$30 ; color intensity = full
124 cmp #$3F
125 bne +
126 lda #$30
127 + sta temp_palette,y
129 cpy pal_index_lo
130 bpl -
133 fade_to_black_init:
136 fade_to_white_init:
137 ldy pal_index_hi
138 - lda palette,y
139 cmp #$0F
140 bne +
141 lda #$F0
142 + sta temp_palette,y
144 cpy pal_index_lo
145 bpl -
148 ;-----------------------------------------------------------------------------
150 ; Executes one step of fading for the selected fade type.
151 fade_step:
152 lda counter
153 beq +
154 jsr go_fade_handler
155 jsr temp_palette_to_ppu_buffer
156 dec counter
157 + rts
159 go_fade_handler:
160 lda fade_type
161 jsr table_call
162 TC_SLOT fade_from_black_step
163 TC_SLOT fade_from_white_step
164 TC_SLOT fade_to_black_step
165 TC_SLOT fade_to_white_step
167 fade_from_black_step:
168 ldy pal_index_hi
169 - lda temp_palette,y
170 cmp palette,y ; has color reached full intensity?
171 bcs + ; if yes, don't modify it
172 adc #$10 ; increase color intensity by 1
173 sta temp_palette,y
174 + dey
175 bmi +
176 cpy pal_index_lo
177 bcs -
178 + rts
180 fade_from_white_step:
181 ldy pal_index_hi
182 - lda temp_palette,y
183 cmp palette,y ; has color reached full intensity?
184 beq ++ ; if yes, don't modify it
185 cmp #$00
186 bne +
187 lda #$0F
188 bne ++
189 + sec
190 sbc #$10 ; decrease color intensity by 1
191 sta temp_palette,y
192 ++ dey
193 cpy pal_index_lo
194 bpl -
197 fade_to_black_step:
198 ldy pal_index_hi
199 - lda temp_palette,y
201 sbc #$10 ; decrease color intensity by 1
202 bcs + ; if result is < 0...
203 lda #$0F ; ... color = black
204 + sta temp_palette,y
206 cpy pal_index_lo
207 bpl -
210 fade_to_white_step:
211 ldy pal_index_hi
212 - lda temp_palette,y
214 adc #$10 ; increase color intensity by 1
215 cmp #$40
216 bcc + ; if result is >= 64...
217 lda #$30 ; ... color = white
218 + sta temp_palette,y
220 cpy pal_index_lo
221 bpl -
224 ;-----------------------------------------------------------------------------
226 fade_timer_callback:
227 jsr fade_step
228 bne start_fade_timer
229 ; fade done
232 start_fade_timer:
233 lda #1
234 ldy frames_per_step
235 jsr start_timer
236 lda #<fade_timer_callback
237 ldy #>fade_timer_callback
238 jmp set_timer_callback
240 ;-----------------------------------------------------------------------------
242 start_fade_from_black:
243 lda #0
244 beq start_fade
246 start_fade_from_white:
247 lda #1
248 bne start_fade
250 start_fade_to_black:
251 lda #2
252 bne start_fade
254 start_fade_to_white:
255 lda #3
257 start_fade:
258 sta fade_type
259 jsr fade_init
260 jsr temp_palette_to_ppu_buffer
261 lda #4
262 sta counter
263 jmp start_fade_timer
265 ;-----------------------------------------------------------------------------
267 ; Writes the intermediate palette to PPU buffer.
268 temp_palette_to_ppu_buffer:
269 lda pal_index_hi
271 sbc pal_index_lo
272 adc #0
274 lda pal_index_lo
275 jsr start_palette_ppu_string
276 ldy pal_index_lo
277 - lda temp_palette,y
279 sta ppu_buffer,x
281 cpy pal_index_hi
282 bcc -
283 beq -
284 jmp end_ppu_string
286 ; Copies palette to temp palette.
287 palette_to_temp_palette:
288 ldy pal_index_hi
289 - lda palette,y
290 sta temp_palette,y
292 bmi +
293 cpy pal_index_lo
294 bcs -
295 + rts
297 .end