-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.s
271 lines (249 loc) · 4.77 KB
/
render.s
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
.include "global.inc"
.export render
.export render_escape
.export render_win
.segment "ZEROPAGE"
startx: .res 1
endx: .res 1
endy: .res 1
.segment "CODE"
; render the dungeon level on the current screen
.proc render
lda nmis
wait_nmi:
; wait for nmi
cmp nmis
beq wait_nmi
generate_ppu:
; turn off rendering
lda #%00000000 ; note: need second bit in order to show background on left side of screen
sta $2001
; initialize defaults for scrolling functionality
jsr init_buffer
; initialize scroll offsets
jsr update_screen_offsets
; update y-scroll to player Y offset
ldy #0
scroll_loop:
cpy #vertical_bound
bcc skip_scroll
beq skip_scroll
cpy #(max_height*2) - (screen_height-vertical_bound)
beq update_scroll
bcs skip_scroll
update_scroll:
jsr scroll_down ; scroll down 8 pixels
jsr scroll_down ; scroll down 8 pixels, 16 pixels total (1 metatile)
jsr iny_ppu ; increase top of PPUADDR 8 pixels
jsr iny_ppu ; increase top of PPUADDR 8 pixels, 16 pixels total (1 metatile)
skip_scroll:
iny
iny
tya
lsr ; divide by 2 to compare to player y-pos
cmp mobs + Mob::coords + Coord::ycoord ; check that we're at the mob ycoord
; end loop once y is *greater* than ycoord
bcc scroll_loop
beq scroll_loop
; clear previous nametable
bit $2002
lda #$20
sta $2006
lda #$00
sta $2006
lda #$00
tax
tay
clear_page:
sta $2007
inx
cpx #$FF
bne clear_page
sta $2007
iny
cpy #16 ; 16 total pages in NT
bne clear_page
; render dungeon
jsr buffer_seen
jsr render_buffer
; render statusbar
jsr buffer_status_text
jsr buffer_status
jsr buffer_status_end
jsr render_buffer
render_done:
jsr update_sprites
lda nmis
render_wait:
cmp nmis
beq render_wait
; tell PPU to render BG & sprites
lda #%00011010 ; note: need second bit in order to show background on left side of screen
sta $2001
rts
.endproc
.proc render_death
; offset PPUADDR
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
lda #<txt_gameover
sta str_pointer
lda #>txt_gameover
sta str_pointer+1
; start buffer
lda #9 ; todo strlen func
sta draw_length
lda base_nt
sta ppu_ctrl
jsr start_buffer
jsr append_str
; finish buffer
lda #$00
sta draw_buffer, y
rts
.endproc
.proc render_escape
; offset PPUADDR
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
lda #<txt_escape
sta str_pointer
lda #>txt_escape
sta str_pointer+1
; start buffer
lda #12 ; todo strlen func
sta draw_length
lda base_nt
sta ppu_ctrl
jsr start_buffer
jsr append_str
; finish buffer
lda #$00
sta draw_buffer, y
rts
.endproc
.proc render_win
; offset PPUADDR
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr iny_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
jsr inx_ppu
lda #<txt_win
sta str_pointer
lda #>txt_win
sta str_pointer+1
; start buffer
lda #8 ; todo strlen func
sta draw_length
lda base_nt
sta ppu_ctrl
jsr start_buffer
jsr append_str
; finish buffer
lda #$00
sta draw_buffer, y
rts
.endproc
; render padded num 0-99
; in: number
; clobbers: x and y
.proc render_num
cmp #10
bcc render_padded
; first, render tens place for number
ldx #0
tens_loop:
cmp #10
bcc render_tens
sec
sbc #10
inx
jmp tens_loop
render_tens:
tay
txa
jsr get_num_tile
sta $2007
; now, render ones place
tya
render_ones:
jsr get_num_tile
sta $2007
rts
render_padded:
tax
lda #$00
sta $2007
txa
jmp render_ones
.endproc
; render string constant to screen
; in: address to start of str
; clobbers: y
.proc render_str
ldy #0
loop:
lda (str_pointer), y
beq done
jsr get_str_tile
sta $2007
iny
jmp loop
done:
rts
.endproc
.segment "RODATA"
; end messages
txt_win: .asciiz "You win!"
txt_escape: .asciiz "You escaped!"
txt_death: .asciiz "You died!"
txt_gameover: .asciiz "Game Over"